From e1d984351184fe46456a078b34fb368d7e16fe50 Mon Sep 17 00:00:00 2001 From: PuerNya Date: Wed, 27 Dec 2023 16:19:55 +0800 Subject: [PATCH] chore: add new cors response --- experimental/clashapi/server.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/experimental/clashapi/server.go b/experimental/clashapi/server.go index 1eec8448af..a62e67e169 100644 --- a/experimental/clashapi/server.go +++ b/experimental/clashapi/server.go @@ -95,6 +95,7 @@ func NewServer(ctx context.Context, router adapter.Router, logFactory log.Observ AllowedHeaders: []string{"Content-Type", "Authorization"}, MaxAge: 300, }) + chiRouter.Use(setPrivateNetworkAccess) chiRouter.Use(cors.Handler) chiRouter.Group(func(r chi.Router) { r.Use(authentication(options.Secret)) @@ -270,6 +271,15 @@ func castMetadata(metadata adapter.InboundContext) trafficontrol.Metadata { } } +func setPrivateNetworkAccess(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" { + w.Header().Add("Access-Control-Allow-Private-Network", "true") + } + next.ServeHTTP(w, r) + }) +} + func authentication(serverSecret string) func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) {