Add: cors for external controller

This commit is contained in:
Dreamacro 2018-07-19 09:31:53 +08:00
parent d540a0d29f
commit 7357d2d0c2
3 changed files with 26 additions and 6 deletions

16
Gopkg.lock generated
View File

@ -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

View File

@ -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

View File

@ -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())