Skip to content

Commit

Permalink
fix(core): panic to error
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Apr 5, 2023
1 parent 9601549 commit b2883b9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
19 changes: 11 additions & 8 deletions libcore/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"libcore/device"
"log"
"net/http"
"reflect"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -127,18 +124,23 @@ 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()
}
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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions libcore/device/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion libcore/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libcore/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 2 additions & 6 deletions libcore/nb4a.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b2883b9

Please sign in to comment.