platform: Fixes and improvements

This commit is contained in:
世界 2023-04-05 04:38:56 +08:00
parent 28aa4c4d1f
commit 8b64446274
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
16 changed files with 33 additions and 31 deletions

View file

@ -14,10 +14,16 @@ var (
) )
func Count() int { func Count() int {
if !Enabled {
return 0
}
return openConnection.Len() return openConnection.Len()
} }
func List() []io.Closer { func List() []io.Closer {
if !Enabled {
return nil
}
connAccess.RLock() connAccess.RLock()
defer connAccess.RUnlock() defer connAccess.RUnlock()
connList := make([]io.Closer, 0, openConnection.Len()) connList := make([]io.Closer, 0, openConnection.Len())
@ -28,6 +34,9 @@ func List() []io.Closer {
} }
func Close() { func Close() {
if !Enabled {
return
}
connAccess.Lock() connAccess.Lock()
defer connAccess.Unlock() defer connAccess.Unlock()
for element := openConnection.Front(); element != nil; element = element.Next() { for element := openConnection.Front(); element != nil; element = element.Next() {

View file

@ -1,5 +1,3 @@
//go:build darwin
package libbox package libbox
const ( const (

View file

@ -1,5 +1,3 @@
//go:build darwin
package libbox package libbox
import ( import (
@ -46,6 +44,7 @@ func clientConnect(sharedDirectory string) (net.Conn, error) {
} }
func (c *CommandClient) Connect() error { func (c *CommandClient) Connect() error {
common.Close(c.conn)
conn, err := clientConnect(c.sharedDirectory) conn, err := clientConnect(c.sharedDirectory)
if err != nil { if err != nil {
return err return err

View file

@ -1,5 +1,3 @@
//go:build darwin
package libbox package libbox
import ( import (

View file

@ -1,5 +1,3 @@
//go:build darwin
package libbox package libbox
import ( import (

View file

@ -1,5 +1,3 @@
//go:build darwin
package libbox package libbox
import ( import (

View file

@ -1,5 +1,3 @@
//go:build darwin
package libbox package libbox
import ( import (
@ -10,6 +8,7 @@ import (
"sync" "sync"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/observable" "github.com/sagernet/sing/common/observable"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
@ -57,7 +56,10 @@ func (s *CommandServer) Start() error {
} }
func (s *CommandServer) Close() error { func (s *CommandServer) Close() error {
return s.listener.Close() return common.Close(
s.listener,
s.observer,
)
} }
func (s *CommandServer) loopConnection(listener net.Listener) { func (s *CommandServer) loopConnection(listener net.Listener) {

View file

@ -1,5 +1,3 @@
//go:build darwin
package libbox package libbox
import ( import (

View file

@ -1,5 +1,3 @@
//go:build darwin
package libbox package libbox
import ( import (

View file

@ -1,9 +1,9 @@
//go:build linux || darwin
package libbox package libbox
import ( import (
"bytes"
"context" "context"
"encoding/json"
"github.com/sagernet/sing-box" "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
@ -35,3 +35,19 @@ func CheckConfig(configContent string) error {
} }
return err 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
}

View file

@ -1,5 +1,3 @@
//go:build linux || darwin
package libbox package libbox
import "github.com/sagernet/sing/common" import "github.com/sagernet/sing/common"

View file

@ -1,5 +1,3 @@
//go:build linux || darwin
package libbox package libbox
import "github.com/sagernet/sing-box/option" import "github.com/sagernet/sing-box/option"

View file

@ -1,5 +1,3 @@
//go:build linux || darwin
package libbox package libbox
import ( import (

View file

@ -1,5 +1,3 @@
//go:build linux || darwin
package libbox package libbox
import ( import (

View file

@ -1,5 +1,3 @@
//go:build linux || darwin
package libbox package libbox
import ( import (

View file

@ -1,5 +1,3 @@
//go:build linux || darwin
package libbox package libbox
import ( import (