fix(core): panic to error

This commit is contained in:
arm64v8a 2023-04-06 08:12:31 +09:00
parent 9601549d83
commit b2883b9f5e
5 changed files with 19 additions and 20 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"libcore/device"
"log" "log"
"net/http" "net/http"
"reflect" "reflect"
@ -82,11 +83,7 @@ type BoxInstance struct {
} }
func NewSingBoxInstance(config string) (b *BoxInstance, err error) { func NewSingBoxInstance(config string) (b *BoxInstance, err error) {
defer func() { defer device.DeferPanicToError("NewSingBoxInstance", func(err_ error) { err = err_ })
if v := recover(); v != nil {
err = fmt.Errorf("panic: %v", v)
}
}()
// parse options // parse options
var options option.Options var options option.Options
@ -127,10 +124,13 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) {
return b, nil 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 != "" { if outdated != "" {
return errors.New(outdated) return errors.New(outdated)
} }
if b.state == 0 { if b.state == 0 {
b.state = 1 b.state = 1
return b.Box.Start() return b.Box.Start()
@ -138,7 +138,9 @@ func (b *BoxInstance) Start() error {
return errors.New("already started") 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 // no double close
if b.state == 2 { if b.state == 2 {
return nil return nil
@ -209,7 +211,8 @@ func (b *BoxInstance) SelectOutbound(tag string) bool {
return false 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 { if i == nil {
// test current // test current
return speedtest.UrlTest(neko_common.GetProxyHttpClient(), link, timeout) return speedtest.UrlTest(neko_common.GetProxyHttpClient(), link, timeout)

View File

@ -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 { if r := recover(); r != nil {
s := fmt.Sprintln(name+" panic", r, string(debug.Stack())) s := fmt.Errorf("%s panic: %s\n%s", name, r, string(debug.Stack()))
log(s) err(s)
} }
} }

View File

@ -5,7 +5,7 @@ go 1.18
require ( require (
github.com/codeclysm/extract v2.2.0+incompatible github.com/codeclysm/extract v2.2.0+incompatible
github.com/matsuridayo/libneko v0.0.0-20230315005352-9d7e3f3a79d1 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/miekg/dns v1.1.53
github.com/sagernet/sing v0.2.2-0.20230402035613-6d63c1a7dca5 github.com/sagernet/sing v0.2.2-0.20230402035613-6d63c1a7dca5
github.com/sagernet/sing-box v1.2.3-0.20230402040603-f8be48401998 github.com/sagernet/sing-box v1.2.3-0.20230402040603-f8be48401998

View File

@ -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/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 h1:+FflyEuq2hn++MENFuT1/qFHz0KITKK/F6ZHxs23mrg=
github.com/matsuridayo/libneko v0.0.0-20230315005352-9d7e3f3a79d1/go.mod h1:IRO07Queptz/rGFvEW+3Hmwpx7MCup6WiDs4p5jMt4g= 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-20230405052125-4308a4d39f94 h1:gCsJ1fmhpR0S8F2eJSWum+D8qVtvlo7m2kUNOijyH/s=
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/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 h1:BqIt3G/NXyTLao96ouiK6XIvzt0U7gAi2u6FeN88b0c=
github.com/matsuridayo/sing-dns v0.0.0-20230402050810-781b80b9110f/go.mod h1:69PNSHyEmXdjf6C+bXBOdr2GQnPeEyWjIzo/MV8gmz8= 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= github.com/mholt/acmez v1.1.0 h1:IQ9CGHKOHokorxnffsqDvmmE30mDenO1lptYZ1AYkHY=

View File

@ -34,15 +34,11 @@ func SetLocalResolver(lr LocalResolver) {
underlyingResolver.localResolver = lr underlyingResolver.localResolver = lr
} }
func initCoreDefer() {
device.AllDefer("InitCore", func(s string) { log.Println(s) })
}
func InitCore(process, cachePath, internalAssets, externalAssets string, func InitCore(process, cachePath, internalAssets, externalAssets string,
maxLogSizeKb int32, logEnable bool, maxLogSizeKb int32, logEnable bool,
iif NB4AInterface, iif NB4AInterface,
) { ) {
defer initCoreDefer() defer device.DeferPanicToError("InitCore", func(err error) { log.Println(err) })
isBgProcess := strings.HasSuffix(process, ":bg") isBgProcess := strings.HasSuffix(process, ":bg")
neko_common.RunMode = neko_common.RunMode_NekoBoxForAndroid neko_common.RunMode = neko_common.RunMode_NekoBoxForAndroid
@ -66,7 +62,7 @@ func InitCore(process, cachePath, internalAssets, externalAssets string,
// Set up some component // Set up some component
go func() { go func() {
defer initCoreDefer() defer device.DeferPanicToError("InitCore-go", func(err error) { log.Println(err) })
device.GoDebug(process) device.GoDebug(process)
externalAssetsPath = externalAssets externalAssetsPath = externalAssets