@@ -7,39 +7,25 @@ import (
77 "io"
88 "libcore/device"
99 "log"
10- "net/http"
11- "reflect"
1210 "runtime"
1311 "runtime/debug"
1412 "strings"
1513 "time"
16- "unsafe"
1714
1815 "github.com/matsuridayo/sing-box-extra/boxbox"
1916 _ "github.com/matsuridayo/sing-box-extra/distro/all"
2017
21- "github.com/matsuridayo/libneko/neko_common"
2218 "github.com/matsuridayo/libneko/protect_server"
2319 "github.com/matsuridayo/libneko/speedtest"
2420 "github.com/matsuridayo/sing-box-extra/boxapi"
2521
2622 "github.com/sagernet/sing-box/common/dialer/conntrack"
27- sblog "github.com/sagernet/sing-box/log"
2823 "github.com/sagernet/sing-box/option"
2924 "github.com/sagernet/sing-box/outbound"
3025)
3126
3227var mainInstance * BoxInstance
3328
34- func init () {
35- neko_common .GetProxyHttpClient = func () * http.Client {
36- if mainInstance == nil {
37- return nil
38- }
39- return boxapi .GetProxyHttpClient (mainInstance .Box )
40- }
41- }
42-
4329func VersionBox () string {
4430 version := []string {
4531 "sing-box-extra: " + boxbox .Version (),
@@ -110,13 +96,7 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) {
11096 }
11197
11298 // fuck your sing-box platformFormatter
113- logFactory_ := reflect .Indirect (reflect .ValueOf (instance )).FieldByName ("logFactory" )
114- logFactory_ = reflect .NewAt (logFactory_ .Type (), unsafe .Pointer (logFactory_ .UnsafeAddr ())).Elem () // get unexported logFactory
115- logFactory_ = logFactory_ .Elem ().Elem () // get struct
116- platformFormatter_ := logFactory_ .FieldByName ("platformFormatter" )
117- platformFormatter_ = reflect .NewAt (platformFormatter_ .Type (), unsafe .Pointer (platformFormatter_ .UnsafeAddr ())) // get unexported Formatter
118- platformFormatter := platformFormatter_ .Interface ().(* sblog.Formatter )
119- platformFormatter .DisableColors = true
99+ instance .GetLogPlatformFormatter ().DisableColors = true
120100
121101 // selector
122102 if proxy , ok := b .Router ().Outbound ("proxy" ); ok {
@@ -158,25 +138,8 @@ func (b *BoxInstance) Close() (err error) {
158138 }
159139
160140 // close box
161- t := time .NewTimer (time .Second * 2 )
162- c := make (chan struct {}, 1 )
163- disableSingBoxLog = true
164-
165- go func (cancel context.CancelFunc , closer io.Closer ) {
166- cancel ()
167- closer .Close ()
168- c <- struct {}{}
169- close (c )
170- }(b .cancel , b .Box )
171-
172- select {
173- case <- t .C :
174- log .Println ("[Warning] sing-box close takes longer than expected." )
175- case <- c :
176- }
141+ b .CloseWithTimeout (b .cancel , time .Second * 2 , log .Println )
177142
178- disableSingBoxLog = false
179- t .Stop ()
180143 return nil
181144}
182145
@@ -215,9 +178,9 @@ func UrlTest(i *BoxInstance, link string, timeout int32) (latency int32, err err
215178 defer device .DeferPanicToError ("box.UrlTest" , func (err_ error ) { err = err_ })
216179 if i == nil {
217180 // test current
218- return speedtest .UrlTest (neko_common . GetProxyHttpClient ( ), link , timeout )
181+ return speedtest .UrlTest (boxapi . CreateProxyHttpClient ( mainInstance . Box ), link , timeout )
219182 }
220- return speedtest .UrlTest (boxapi .GetProxyHttpClient (i .Box ), link , timeout )
183+ return speedtest .UrlTest (boxapi .CreateProxyHttpClient (i .Box ), link , timeout )
221184}
222185
223186var protectCloser io.Closer
0 commit comments