From 18696e7715ab85eee3157b2a7c020ad94c424f6f Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 19 Jul 2018 09:31:53 +0800 Subject: [PATCH] Add: cors for external controller --- Gopkg.lock | 16 +++++++++++----- Gopkg.toml | 6 +++++- hub/server.go | 10 ++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index a879f708..4dab52be 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -19,6 +19,12 @@ revision = "e83ac2304db3c50cf03d96a2fcd39009d458bc35" version = "v3.3.2" +[[projects]] + name = "github.com/go-chi/cors" + packages = ["."] + revision = "dba6525398619dead495962a916728e7ee2ca322" + version = "v1.0.0" + [[projects]] name = "github.com/go-chi/render" packages = ["."] @@ -65,7 +71,7 @@ "poly1305", "ssh/terminal" ] - revision = "027cca12c2d63e3d62b670d901e8a2c95854feec" + revision = "a2144134853fc9a27a7b1e3eb4f19f1a76df13c9" [[projects]] branch = "master" @@ -75,7 +81,7 @@ "unix", "windows" ] - revision = "6c888cc515d3ed83fc103cf1d84468aad274b0a7" + revision = "ac767d655b305d4e9612f5f6e33120b9176c4ad4" [[projects]] name = "gopkg.in/eapache/channels.v1" @@ -86,12 +92,12 @@ [[projects]] name = "gopkg.in/ini.v1" packages = ["."] - revision = "06f5f3d67269ccec1fe5fe4134ba6e982984f7f5" - version = "v1.37.0" + revision = "358ee7663966325963d4e8b2e1fbd570c5195153" + version = "v1.38.1" [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "c3c901e4e393a2df9e421924d3a4d85ec73642e36dcbc1ddca5fc13159220e86" + inputs-digest = "0ccb06b3617c87e75bd650f92adc99e55b93070e0b2a0bc71634270226e125fc" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index ffa7005a..c381b4d9 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -29,6 +29,10 @@ name = "github.com/go-chi/chi" version = "3.3.2" +[[constraint]] + name = "github.com/go-chi/cors" + version = "1.0.0" + [[constraint]] name = "github.com/go-chi/render" version = "1.0.1" @@ -51,7 +55,7 @@ [[constraint]] name = "gopkg.in/ini.v1" - version = "1.37.0" + version = "1.38.1" [prune] go-tests = true diff --git a/hub/server.go b/hub/server.go index 328e5108..5a6e7276 100644 --- a/hub/server.go +++ b/hub/server.go @@ -8,6 +8,7 @@ import ( T "github.com/Dreamacro/clash/tunnel" "github.com/go-chi/chi" + "github.com/go-chi/cors" "github.com/go-chi/render" log "github.com/sirupsen/logrus" ) @@ -20,6 +21,15 @@ type Traffic struct { func NewHub(addr string) { r := chi.NewRouter() + cors := cors.New(cors.Options{ + AllowedOrigins: []string{"*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Content-Type"}, + MaxAge: 300, + }) + + r.Use(cors.Handler) + r.Get("/traffic", traffic) r.Get("/logs", getLogs) r.Mount("/configs", configRouter())