From 8b644462744cdc855178ba33acf9863286af1e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 5 Apr 2023 04:38:56 +0800 Subject: [PATCH] platform: Fixes and improvements --- common/dialer/conntrack/track.go | 9 +++++++++ experimental/libbox/command.go | 2 -- experimental/libbox/command_client.go | 3 +-- experimental/libbox/command_conntrack.go | 2 -- experimental/libbox/command_log.go | 2 -- experimental/libbox/command_reload.go | 2 -- experimental/libbox/command_server.go | 8 +++++--- experimental/libbox/command_status.go | 2 -- experimental/libbox/command_stop.go | 2 -- experimental/libbox/config.go | 20 ++++++++++++++++++-- experimental/libbox/iterator.go | 2 -- experimental/libbox/platform.go | 2 -- experimental/libbox/pprof.go | 2 -- experimental/libbox/service.go | 2 -- experimental/libbox/setup.go | 2 -- experimental/libbox/tun.go | 2 -- 16 files changed, 33 insertions(+), 31 deletions(-) diff --git a/common/dialer/conntrack/track.go b/common/dialer/conntrack/track.go index 3de0b8eb..2c3e328b 100644 --- a/common/dialer/conntrack/track.go +++ b/common/dialer/conntrack/track.go @@ -14,10 +14,16 @@ var ( ) func Count() int { + if !Enabled { + return 0 + } return openConnection.Len() } func List() []io.Closer { + if !Enabled { + return nil + } connAccess.RLock() defer connAccess.RUnlock() connList := make([]io.Closer, 0, openConnection.Len()) @@ -28,6 +34,9 @@ func List() []io.Closer { } func Close() { + if !Enabled { + return + } connAccess.Lock() defer connAccess.Unlock() for element := openConnection.Front(); element != nil; element = element.Next() { diff --git a/experimental/libbox/command.go b/experimental/libbox/command.go index f42b302d..5f344d10 100644 --- a/experimental/libbox/command.go +++ b/experimental/libbox/command.go @@ -1,5 +1,3 @@ -//go:build darwin - package libbox const ( diff --git a/experimental/libbox/command_client.go b/experimental/libbox/command_client.go index 4a2915a0..f48df1af 100644 --- a/experimental/libbox/command_client.go +++ b/experimental/libbox/command_client.go @@ -1,5 +1,3 @@ -//go:build darwin - package libbox import ( @@ -46,6 +44,7 @@ func clientConnect(sharedDirectory string) (net.Conn, error) { } func (c *CommandClient) Connect() error { + common.Close(c.conn) conn, err := clientConnect(c.sharedDirectory) if err != nil { return err diff --git a/experimental/libbox/command_conntrack.go b/experimental/libbox/command_conntrack.go index a062a12f..c7f24199 100644 --- a/experimental/libbox/command_conntrack.go +++ b/experimental/libbox/command_conntrack.go @@ -1,5 +1,3 @@ -//go:build darwin - package libbox import ( diff --git a/experimental/libbox/command_log.go b/experimental/libbox/command_log.go index c8c7a3e2..b70e9884 100644 --- a/experimental/libbox/command_log.go +++ b/experimental/libbox/command_log.go @@ -1,5 +1,3 @@ -//go:build darwin - package libbox import ( diff --git a/experimental/libbox/command_reload.go b/experimental/libbox/command_reload.go index 81e00fe2..19ac0264 100644 --- a/experimental/libbox/command_reload.go +++ b/experimental/libbox/command_reload.go @@ -1,5 +1,3 @@ -//go:build darwin - package libbox import ( diff --git a/experimental/libbox/command_server.go b/experimental/libbox/command_server.go index a04be839..4ca1429c 100644 --- a/experimental/libbox/command_server.go +++ b/experimental/libbox/command_server.go @@ -1,5 +1,3 @@ -//go:build darwin - package libbox import ( @@ -10,6 +8,7 @@ import ( "sync" "github.com/sagernet/sing-box/log" + "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/observable" "github.com/sagernet/sing/common/x/list" @@ -57,7 +56,10 @@ func (s *CommandServer) Start() error { } func (s *CommandServer) Close() error { - return s.listener.Close() + return common.Close( + s.listener, + s.observer, + ) } func (s *CommandServer) loopConnection(listener net.Listener) { diff --git a/experimental/libbox/command_status.go b/experimental/libbox/command_status.go index 4d18063e..ac088a68 100644 --- a/experimental/libbox/command_status.go +++ b/experimental/libbox/command_status.go @@ -1,5 +1,3 @@ -//go:build darwin - package libbox import ( diff --git a/experimental/libbox/command_stop.go b/experimental/libbox/command_stop.go index 13048d05..8609b99a 100644 --- a/experimental/libbox/command_stop.go +++ b/experimental/libbox/command_stop.go @@ -1,5 +1,3 @@ -//go:build darwin - package libbox import ( diff --git a/experimental/libbox/config.go b/experimental/libbox/config.go index 3a2f1ad9..66651b60 100644 --- a/experimental/libbox/config.go +++ b/experimental/libbox/config.go @@ -1,9 +1,9 @@ -//go:build linux || darwin - package libbox import ( + "bytes" "context" + "encoding/json" "github.com/sagernet/sing-box" "github.com/sagernet/sing-box/option" @@ -35,3 +35,19 @@ func CheckConfig(configContent string) error { } return err } + +func FormatConfig(configContent string) (string, error) { + options, err := parseConfig(configContent) + if err != nil { + return "", err + } + var buffer bytes.Buffer + json.NewEncoder(&buffer) + encoder := json.NewEncoder(&buffer) + encoder.SetIndent("", " ") + err = encoder.Encode(options) + if err != nil { + return "", err + } + return buffer.String(), nil +} diff --git a/experimental/libbox/iterator.go b/experimental/libbox/iterator.go index a11133be..e23e2a7f 100644 --- a/experimental/libbox/iterator.go +++ b/experimental/libbox/iterator.go @@ -1,5 +1,3 @@ -//go:build linux || darwin - package libbox import "github.com/sagernet/sing/common" diff --git a/experimental/libbox/platform.go b/experimental/libbox/platform.go index 5c8bb0ec..09195525 100644 --- a/experimental/libbox/platform.go +++ b/experimental/libbox/platform.go @@ -1,5 +1,3 @@ -//go:build linux || darwin - package libbox import "github.com/sagernet/sing-box/option" diff --git a/experimental/libbox/pprof.go b/experimental/libbox/pprof.go index c451dce0..d6d07800 100644 --- a/experimental/libbox/pprof.go +++ b/experimental/libbox/pprof.go @@ -1,5 +1,3 @@ -//go:build linux || darwin - package libbox import ( diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index 1bc24446..33c459ac 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -1,5 +1,3 @@ -//go:build linux || darwin - package libbox import ( diff --git a/experimental/libbox/setup.go b/experimental/libbox/setup.go index 08855fd0..c0466471 100644 --- a/experimental/libbox/setup.go +++ b/experimental/libbox/setup.go @@ -1,5 +1,3 @@ -//go:build linux || darwin - package libbox import ( diff --git a/experimental/libbox/tun.go b/experimental/libbox/tun.go index bf8450c9..e7db249c 100644 --- a/experimental/libbox/tun.go +++ b/experimental/libbox/tun.go @@ -1,5 +1,3 @@ -//go:build linux || darwin - package libbox import (