From bf079f1ae26eaa743ac84ab842a8ec9b96d7c934 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Sun, 15 Dec 2024 23:12:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E8=AF=81=E4=B9=A6=E9=93=BE?= =?UTF-8?q?=E4=B8=8D=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 5 ++++ api/frontend.html | 4 +-- api/index.go | 25 ++++++++++--------- api/provide.go | 29 ++++++++++++++++++++++ api/wire.go | 14 +++++++++++ api/wire_gen.go | 23 +++++++++++++++++ go.mod | 3 +++ go.sum | 57 +++++++++++++++++++++++++++++++++++++++++++ main.go | 5 +++- 9 files changed, 150 insertions(+), 15 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 api/provide.go create mode 100644 api/wire.go create mode 100644 api/wire_gen.go diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..42f5e3e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "gopls": { + "buildFlags":["-tags=wireinject"] + }, +} \ No newline at end of file diff --git a/api/frontend.html b/api/frontend.html index 382015e..e9cb364 100644 --- a/api/frontend.html +++ b/api/frontend.html @@ -233,7 +233,7 @@ } }); - function onChange(){ + function onChange() { outFields.value = "" if (configType.value === "0") { configurl.value = "config.json.template" @@ -243,7 +243,7 @@ outFields.value = "0" } else if (configType.value === "2") { configurl.value = "" - if (config.value == ""){ + if (config.value == "") { config.value = oldConfig } } else if (configType.value === "3") { diff --git a/api/index.go b/api/index.go index 474fde2..b414074 100644 --- a/api/index.go +++ b/api/index.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "runtime/debug" + "sync" "text/template" "time" @@ -26,14 +27,19 @@ var static embed.FS //go:embed frontend.html var FrontendByte []byte -func SetMux(h slog.Handler) *chi.Mux { - c := &http.Client{ - Timeout: 60 * time.Second, - } - l := NewSlog(h) +var handleOnce = sync.OnceValue[http.Handler](func() http.Handler { + level := &slog.LevelVar{} + level.Set(slog.Level(-4)) + h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ + Level: level, + }) + handle, _, err := InitializeServer(h) + lo.Must0(err) + return handle +}) +func SetMux(h slog.Handler, c *http.Client, l *slog.Logger) *chi.Mux { static := lo.Must(fs.Sub(static, "static")) - convert := service.NewConvert(c, l) subH := handle.NewHandle(convert, l, static) @@ -63,12 +69,7 @@ func SetMux(h slog.Handler) *chi.Mux { } func Handler(w http.ResponseWriter, r *http.Request) { - level := &slog.LevelVar{} - level.Set(slog.Level(-4)) - h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ - Level: level, - }) - SetMux(h).ServeHTTP(w, r) + handleOnce().ServeHTTP(w, r) } func NewStructuredLogger(Logger *slog.Logger) func(next http.Handler) http.Handler { diff --git a/api/provide.go b/api/provide.go new file mode 100644 index 0000000..2a598f3 --- /dev/null +++ b/api/provide.go @@ -0,0 +1,29 @@ +package handler + +import ( + "crypto/tls" + "net/http" + "time" + + "filippo.io/intermediates" + "github.com/go-chi/chi/v5" + "github.com/google/wire" +) + +var All = wire.NewSet(NewSlog, NewClient, SetMux, NewHttpServer) + +func NewClient() *http.Client { + tr := http.DefaultTransport.(*http.Transport).Clone() + tr.TLSClientConfig = &tls.Config{ + InsecureSkipVerify: true, + VerifyConnection: intermediates.VerifyConnection, + } + return &http.Client{ + Transport: tr, + Timeout: 60 * time.Second, + } +} + +func NewHttpServer(m *chi.Mux) http.Handler { + return m +} diff --git a/api/wire.go b/api/wire.go new file mode 100644 index 0000000..af4e045 --- /dev/null +++ b/api/wire.go @@ -0,0 +1,14 @@ +//go:build wireinject + +package handler + +import ( + "log/slog" + "net/http" + + "github.com/google/wire" +) + +func InitializeServer(h slog.Handler) (http.Handler, func(), error) { + panic(wire.Build(All)) +} diff --git a/api/wire_gen.go b/api/wire_gen.go new file mode 100644 index 0000000..f727296 --- /dev/null +++ b/api/wire_gen.go @@ -0,0 +1,23 @@ +// Code generated by Wire. DO NOT EDIT. + +//go:generate go run -mod=mod github.com/google/wire/cmd/wire +//go:build !wireinject +// +build !wireinject + +package handler + +import ( + "log/slog" + "net/http" +) + +// Injectors from wire.go: + +func InitializeServer(h slog.Handler) (http.Handler, func(), error) { + client := NewClient() + logger := NewSlog(h) + mux := SetMux(h, client, logger) + handler := NewHttpServer(mux) + return handler, func() { + }, nil +} diff --git a/go.mod b/go.mod index a779a29..15d3afa 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,10 @@ require ( github.com/xmdhs/clash2singbox v0.1.5-0.20241108153332-efa112d7d5a7 ) +require filippo.io/intermediates v0.0.0-20241213023635-cbf57e734ef1 // indirect + require ( + github.com/google/wire v0.6.0 github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect diff --git a/go.sum b/go.sum index 7b009b7..ba12450 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,14 @@ +filippo.io/intermediates v0.0.0-20241213023635-cbf57e734ef1 h1:/q0LprzzYX5LNhfPaFv25X12zSGXavZRB0XhgBV9UIk= +filippo.io/intermediates v0.0.0-20241213023635-cbf57e734ef1/go.mod h1:oFwJrtHxYeWR/Lhr/MC2TPSR+BsYpybldbpRKvSjggw= github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0= github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM= github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +github.com/google/wire v0.6.0 h1:HBkoIh4BdSxoyo9PveV8giw7ZsaBOvzWKfcg/6MrVwI= +github.com/google/wire v0.6.0/go.mod h1:F4QhpQ9EDIdJ1Mbop/NZBRB+5yrR6qg3BnctaoUk6NA= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc= github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= @@ -13,12 +20,62 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/xmdhs/clash2singbox v0.1.5-0.20241108153332-efa112d7d5a7 h1:ogrsjR20m6tXbjhlkC6rfucNdgodF4L+Mm+K//Tcagc= github.com/xmdhs/clash2singbox v0.1.5-0.20241108153332-efa112d7d5a7/go.mod h1:MIcdHIM8PHHjg7TEH5AJbH3zsf+R5cLhCD9lMmDJdko= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY= golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index e2f1486..dcf0c47 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "log/slog" + "github.com/samber/lo" handler "github.com/xmdhs/clash2sfa/api" ) @@ -31,12 +32,14 @@ func main() { Level: level, }) + handler, _ := lo.Must2(handler.InitializeServer(h)) + s := http.Server{ ReadTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second, ReadHeaderTimeout: 10 * time.Second, Addr: port, - Handler: handler.SetMux(h), + Handler: handler, } fmt.Println(s.ListenAndServe()) }