From b2883b9f5e33908fa664a1f75c8a6d377e854120 Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Thu, 6 Apr 2023 08:12:31 +0900 Subject: [PATCH] fix(core): panic to error --- libcore/box.go | 19 +++++++++++-------- libcore/device/debug.go | 6 +++--- libcore/go.mod | 2 +- libcore/go.sum | 4 ++-- libcore/nb4a.go | 8 ++------ 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/libcore/box.go b/libcore/box.go index 1e77465..34f64d6 100644 --- a/libcore/box.go +++ b/libcore/box.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "libcore/device" "log" "net/http" "reflect" @@ -82,11 +83,7 @@ type BoxInstance struct { } func NewSingBoxInstance(config string) (b *BoxInstance, err error) { - defer func() { - if v := recover(); v != nil { - err = fmt.Errorf("panic: %v", v) - } - }() + defer device.DeferPanicToError("NewSingBoxInstance", func(err_ error) { err = err_ }) // parse options var options option.Options @@ -127,10 +124,13 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) { return b, nil } -func (b *BoxInstance) Start() error { +func (b *BoxInstance) Start() (err error) { + defer device.DeferPanicToError("box.Start", func(err_ error) { err = err_ }) + if outdated != "" { return errors.New(outdated) } + if b.state == 0 { b.state = 1 return b.Box.Start() @@ -138,7 +138,9 @@ func (b *BoxInstance) Start() error { return errors.New("already started") } -func (b *BoxInstance) Close() error { +func (b *BoxInstance) Close() (err error) { + defer device.DeferPanicToError("box.Close", func(err_ error) { err = err_ }) + // no double close if b.state == 2 { return nil @@ -209,7 +211,8 @@ func (b *BoxInstance) SelectOutbound(tag string) bool { return false } -func UrlTest(i *BoxInstance, link string, timeout int32) (int32, error) { +func UrlTest(i *BoxInstance, link string, timeout int32) (latency int32, err error) { + defer device.DeferPanicToError("box.UrlTest", func(err_ error) { err = err_ }) if i == nil { // test current return speedtest.UrlTest(neko_common.GetProxyHttpClient(), link, timeout) diff --git a/libcore/device/debug.go b/libcore/device/debug.go index 889442c..016c17f 100644 --- a/libcore/device/debug.go +++ b/libcore/device/debug.go @@ -13,9 +13,9 @@ func GoDebug(any interface{}) { } } -func AllDefer(name string, log func(string)) { +func DeferPanicToError(name string, err func(error)) { if r := recover(); r != nil { - s := fmt.Sprintln(name+" panic", r, string(debug.Stack())) - log(s) + s := fmt.Errorf("%s panic: %s\n%s", name, r, string(debug.Stack())) + err(s) } } diff --git a/libcore/go.mod b/libcore/go.mod index df84f6f..ede5374 100644 --- a/libcore/go.mod +++ b/libcore/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/codeclysm/extract v2.2.0+incompatible github.com/matsuridayo/libneko v0.0.0-20230315005352-9d7e3f3a79d1 - github.com/matsuridayo/sing-box-extra v0.0.0-20230404135215-f6737f68d71c + github.com/matsuridayo/sing-box-extra v0.0.0-20230405052125-4308a4d39f94 github.com/miekg/dns v1.1.53 github.com/sagernet/sing v0.2.2-0.20230402035613-6d63c1a7dca5 github.com/sagernet/sing-box v1.2.3-0.20230402040603-f8be48401998 diff --git a/libcore/go.sum b/libcore/go.sum index 10d3ff3..44e9891 100644 --- a/libcore/go.sum +++ b/libcore/go.sum @@ -73,8 +73,8 @@ github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczG github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/matsuridayo/libneko v0.0.0-20230315005352-9d7e3f3a79d1 h1:+FflyEuq2hn++MENFuT1/qFHz0KITKK/F6ZHxs23mrg= github.com/matsuridayo/libneko v0.0.0-20230315005352-9d7e3f3a79d1/go.mod h1:IRO07Queptz/rGFvEW+3Hmwpx7MCup6WiDs4p5jMt4g= -github.com/matsuridayo/sing-box-extra v0.0.0-20230404135215-f6737f68d71c h1:dLXFFqBQXNCXAkLH9KURnSe4rwkCe3h/5BXsmwGKmXI= -github.com/matsuridayo/sing-box-extra v0.0.0-20230404135215-f6737f68d71c/go.mod h1:MJNdOWGkzlQilH83i2lG5aSZx1IA8Y6Ywnn8Je9y5lY= +github.com/matsuridayo/sing-box-extra v0.0.0-20230405052125-4308a4d39f94 h1:gCsJ1fmhpR0S8F2eJSWum+D8qVtvlo7m2kUNOijyH/s= +github.com/matsuridayo/sing-box-extra v0.0.0-20230405052125-4308a4d39f94/go.mod h1:MJNdOWGkzlQilH83i2lG5aSZx1IA8Y6Ywnn8Je9y5lY= github.com/matsuridayo/sing-dns v0.0.0-20230402050810-781b80b9110f h1:BqIt3G/NXyTLao96ouiK6XIvzt0U7gAi2u6FeN88b0c= github.com/matsuridayo/sing-dns v0.0.0-20230402050810-781b80b9110f/go.mod h1:69PNSHyEmXdjf6C+bXBOdr2GQnPeEyWjIzo/MV8gmz8= github.com/mholt/acmez v1.1.0 h1:IQ9CGHKOHokorxnffsqDvmmE30mDenO1lptYZ1AYkHY= diff --git a/libcore/nb4a.go b/libcore/nb4a.go index 0923cae..2123742 100644 --- a/libcore/nb4a.go +++ b/libcore/nb4a.go @@ -34,15 +34,11 @@ func SetLocalResolver(lr LocalResolver) { underlyingResolver.localResolver = lr } -func initCoreDefer() { - device.AllDefer("InitCore", func(s string) { log.Println(s) }) -} - func InitCore(process, cachePath, internalAssets, externalAssets string, maxLogSizeKb int32, logEnable bool, iif NB4AInterface, ) { - defer initCoreDefer() + defer device.DeferPanicToError("InitCore", func(err error) { log.Println(err) }) isBgProcess := strings.HasSuffix(process, ":bg") neko_common.RunMode = neko_common.RunMode_NekoBoxForAndroid @@ -66,7 +62,7 @@ func InitCore(process, cachePath, internalAssets, externalAssets string, // Set up some component go func() { - defer initCoreDefer() + defer device.DeferPanicToError("InitCore-go", func(err error) { log.Println(err) }) device.GoDebug(process) externalAssetsPath = externalAssets