@@ -23,10 +23,10 @@ import (
23
23
24
24
"github.com/go-chi/chi/v5"
25
25
"github.com/go-chi/chi/v5/middleware"
26
- "github.com/go-chi/cors"
27
26
"github.com/go-chi/render"
28
27
"github.com/gobwas/ws"
29
28
"github.com/gobwas/ws/wsutil"
29
+ "github.com/sagernet/cors"
30
30
)
31
31
32
32
var (
@@ -58,6 +58,22 @@ type Config struct {
58
58
PrivateKey string
59
59
DohServer string
60
60
IsDebug bool
61
+ Cors Cors
62
+ }
63
+
64
+ type Cors struct {
65
+ AllowOrigins []string
66
+ AllowPrivateNetwork bool
67
+ }
68
+
69
+ func (c Cors ) Apply (r chi.Router ) {
70
+ r .Use (cors .New (cors.Options {
71
+ AllowedOrigins : c .AllowOrigins ,
72
+ AllowedMethods : []string {"GET" , "POST" , "PUT" , "PATCH" , "DELETE" },
73
+ AllowedHeaders : []string {"Content-Type" , "Authorization" },
74
+ AllowPrivateNetwork : c .AllowPrivateNetwork ,
75
+ MaxAge : 300 ,
76
+ }).Handler )
61
77
}
62
78
63
79
func ReCreateServer (cfg * Config ) {
@@ -73,16 +89,9 @@ func SetUIPath(path string) {
73
89
uiPath = C .Path .Resolve (path )
74
90
}
75
91
76
- func router (isDebug bool , secret string , dohServer string ) * chi.Mux {
92
+ func router (isDebug bool , secret string , dohServer string , cors Cors ) * chi.Mux {
77
93
r := chi .NewRouter ()
78
- corsM := cors .New (cors.Options {
79
- AllowedOrigins : []string {"*" },
80
- AllowedMethods : []string {"GET" , "POST" , "PUT" , "PATCH" , "DELETE" },
81
- AllowedHeaders : []string {"Content-Type" , "Authorization" },
82
- MaxAge : 300 ,
83
- })
84
- r .Use (setPrivateNetworkAccess )
85
- r .Use (corsM .Handler )
94
+ cors .Apply (r )
86
95
if isDebug {
87
96
r .Mount ("/debug" , func () http.Handler {
88
97
r := chi .NewRouter ()
@@ -151,7 +160,7 @@ func start(cfg *Config) {
151
160
log .Infoln ("RESTful API listening at: %s" , l .Addr ().String ())
152
161
153
162
server := & http.Server {
154
- Handler : router (cfg .IsDebug , cfg .Secret , cfg .DohServer ),
163
+ Handler : router (cfg .IsDebug , cfg .Secret , cfg .DohServer , cfg . Cors ),
155
164
}
156
165
httpServer = server
157
166
if err = server .Serve (l ); err != nil {
@@ -183,7 +192,7 @@ func startTLS(cfg *Config) {
183
192
184
193
log .Infoln ("RESTful API tls listening at: %s" , l .Addr ().String ())
185
194
server := & http.Server {
186
- Handler : router (cfg .IsDebug , cfg .Secret , cfg .DohServer ),
195
+ Handler : router (cfg .IsDebug , cfg .Secret , cfg .DohServer , cfg . Cors ),
187
196
TLSConfig : & tls.Config {
188
197
Certificates : []tls.Certificate {c },
189
198
},
@@ -232,7 +241,7 @@ func startUnix(cfg *Config) {
232
241
log .Infoln ("RESTful API unix listening at: %s" , l .Addr ().String ())
233
242
234
243
server := & http.Server {
235
- Handler : router (cfg .IsDebug , "" , cfg .DohServer ),
244
+ Handler : router (cfg .IsDebug , "" , cfg .DohServer , cfg . Cors ),
236
245
}
237
246
unixServer = server
238
247
if err = server .Serve (l ); err != nil {
@@ -263,7 +272,7 @@ func startPipe(cfg *Config) {
263
272
log .Infoln ("RESTful API pipe listening at: %s" , l .Addr ().String ())
264
273
265
274
server := & http.Server {
266
- Handler : router (cfg .IsDebug , "" , cfg .DohServer ),
275
+ Handler : router (cfg .IsDebug , "" , cfg .DohServer , cfg . Cors ),
267
276
}
268
277
pipeServer = server
269
278
if err = server .Serve (l ); err != nil {
@@ -272,15 +281,6 @@ func startPipe(cfg *Config) {
272
281
}
273
282
}
274
283
275
- func setPrivateNetworkAccess (next http.Handler ) http.Handler {
276
- return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
277
- if r .Method == http .MethodOptions && r .Header .Get ("Access-Control-Request-Method" ) != "" {
278
- w .Header ().Add ("Access-Control-Allow-Private-Network" , "true" )
279
- }
280
- next .ServeHTTP (w , r )
281
- })
282
- }
283
-
284
284
func safeEuqal (a , b string ) bool {
285
285
aBuf := utils .ImmutableBytesFromString (a )
286
286
bBuf := utils .ImmutableBytesFromString (b )
0 commit comments