Skip to content

Commit e1d9843

Browse files
committed
chore: add new cors response
1 parent 6372629 commit e1d9843

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

experimental/clashapi/server.go

+10
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func NewServer(ctx context.Context, router adapter.Router, logFactory log.Observ
9595
AllowedHeaders: []string{"Content-Type", "Authorization"},
9696
MaxAge: 300,
9797
})
98+
chiRouter.Use(setPrivateNetworkAccess)
9899
chiRouter.Use(cors.Handler)
99100
chiRouter.Group(func(r chi.Router) {
100101
r.Use(authentication(options.Secret))
@@ -270,6 +271,15 @@ func castMetadata(metadata adapter.InboundContext) trafficontrol.Metadata {
270271
}
271272
}
272273

274+
func setPrivateNetworkAccess(next http.Handler) http.Handler {
275+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
276+
if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" {
277+
w.Header().Add("Access-Control-Allow-Private-Network", "true")
278+
}
279+
next.ServeHTTP(w, r)
280+
})
281+
}
282+
273283
func authentication(serverSecret string) func(next http.Handler) http.Handler {
274284
return func(next http.Handler) http.Handler {
275285
fn := func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)