Skip to content

Commit

Permalink
chore: add new cors response
Browse files Browse the repository at this point in the history
  • Loading branch information
PuerNya committed Dec 30, 2023
1 parent 1f56df4 commit 6917369
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions experimental/clashapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6917369

Please sign in to comment.