Skip to content

Commit

Permalink
platform: Add group interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jul 2, 2023
1 parent 5ad0ea2 commit bf75427
Show file tree
Hide file tree
Showing 23 changed files with 549 additions and 81 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ lib:

lib_install:
go get -v -d
go install -v github.com/sagernet/gomobile/cmd/[email protected]20230413023804-244d7ff07035
go install -v github.com/sagernet/gomobile/cmd/[email protected]20230413023804-244d7ff07035
go install -v github.com/sagernet/gomobile/cmd/[email protected]20230701084532-493ee2e45182
go install -v github.com/sagernet/gomobile/cmd/[email protected]20230701084532-493ee2e45182

clean:
rm -rf bin dist sing-box
Expand Down
1 change: 1 addition & 0 deletions adapter/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Tracker interface {
}

type OutboundGroup interface {
Outbound
Now() string
All() []string
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/build_libbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func buildiOS() {
log.Fatal(err)
}

copyPath := filepath.Join("..", "sing-box-for-ios")
copyPath := filepath.Join("..", "sing-box-for-apple")
if rw.FileExists(copyPath) {
targetDir := filepath.Join(copyPath, "Libbox.xcframework")
targetDir, _ = filepath.Abs(targetDir)
Expand Down
28 changes: 26 additions & 2 deletions common/urltest/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/x/list"
)

type History struct {
Expand All @@ -20,6 +21,7 @@ type History struct {
type HistoryStorage struct {
access sync.RWMutex
delayHistory map[string]*History
callbacks list.List[func()]
}

func NewHistoryStorage() *HistoryStorage {
Expand All @@ -28,6 +30,18 @@ func NewHistoryStorage() *HistoryStorage {
}
}

func (s *HistoryStorage) AddListener(listener func()) *list.Element[func()] {
s.access.Lock()
defer s.access.Unlock()
return s.callbacks.PushBack(listener)
}

func (s *HistoryStorage) RemoveListener(element *list.Element[func()]) {
s.access.Lock()
defer s.access.Unlock()
s.callbacks.Remove(element)
}

func (s *HistoryStorage) LoadURLTestHistory(tag string) *History {
if s == nil {
return nil
Expand All @@ -39,14 +53,24 @@ func (s *HistoryStorage) LoadURLTestHistory(tag string) *History {

func (s *HistoryStorage) DeleteURLTestHistory(tag string) {
s.access.Lock()
defer s.access.Unlock()
delete(s.delayHistory, tag)
s.access.Unlock()
s.notifyUpdated()
}

func (s *HistoryStorage) StoreURLTestHistory(tag string, history *History) {
s.access.Lock()
defer s.access.Unlock()
s.delayHistory[tag] = history
s.access.Unlock()
s.notifyUpdated()
}

func (s *HistoryStorage) notifyUpdated() {
s.access.RLock()
defer s.access.RUnlock()
for element := s.callbacks.Front(); element != nil; element = element.Next() {
element.Value()
}
}

func URLTest(ctx context.Context, link string, detour N.Dialer) (t uint16, err error) {
Expand Down
6 changes: 5 additions & 1 deletion experimental/clashapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"
"github.com/sagernet/sing/service/filemanager"
"github.com/sagernet/websocket"

Expand Down Expand Up @@ -68,13 +69,16 @@ func NewServer(ctx context.Context, router adapter.Router, logFactory log.Observ
Handler: chiRouter,
},
trafficManager: trafficManager,
urlTestHistory: urltest.NewHistoryStorage(),
mode: strings.ToLower(options.DefaultMode),
storeSelected: options.StoreSelected,
storeFakeIP: options.StoreFakeIP,
externalUIDownloadURL: options.ExternalUIDownloadURL,
externalUIDownloadDetour: options.ExternalUIDownloadDetour,
}
server.urlTestHistory = service.PtrFromContext[urltest.HistoryStorage](ctx)
if server.urlTestHistory == nil {
server.urlTestHistory = urltest.NewHistoryStorage()
}
if server.mode == "" {
server.mode = "rule"
}
Expand Down
4 changes: 3 additions & 1 deletion experimental/libbox/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package libbox
const (
CommandLog int32 = iota
CommandStatus
CommandServiceStop
CommandServiceReload
CommandCloseConnections
CommandGroup
CommandSelectOutbound
CommandURLTest
)
20 changes: 17 additions & 3 deletions experimental/libbox/command_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ type CommandClientHandler interface {
Disconnected(message string)
WriteLog(message string)
WriteStatus(message *StatusMessage)
WriteGroups(message OutboundGroupIterator)
}

func NewStandaloneCommandClient(sharedDirectory string) *CommandClient {
return &CommandClient{
sharedDirectory: sharedDirectory,
}
}

func NewCommandClient(sharedDirectory string, handler CommandClientHandler, options *CommandClientOptions) *CommandClient {
Expand All @@ -36,16 +43,16 @@ func NewCommandClient(sharedDirectory string, handler CommandClientHandler, opti
}
}

func clientConnect(sharedDirectory string) (net.Conn, error) {
func (c *CommandClient) directConnect() (net.Conn, error) {
return net.DialUnix("unix", nil, &net.UnixAddr{
Name: filepath.Join(sharedDirectory, "command.sock"),
Name: filepath.Join(c.sharedDirectory, "command.sock"),
Net: "unix",
})
}

func (c *CommandClient) Connect() error {
common.Close(c.conn)
conn, err := clientConnect(c.sharedDirectory)
conn, err := c.directConnect()
if err != nil {
return err
}
Expand All @@ -65,6 +72,13 @@ func (c *CommandClient) Connect() error {
}
c.handler.Connected()
go c.handleStatusConn(conn)
case CommandGroup:
err = binary.Write(conn, binary.BigEndian, c.options.StatusInterval)
if err != nil {
return E.Cause(err, "write interval")
}
c.handler.Connected()
go c.handleGroupConn(conn)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions experimental/libbox/command_conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/sagernet/sing-box/common/dialer/conntrack"
)

func ClientCloseConnections(sharedDirectory string) error {
conn, err := clientConnect(sharedDirectory)
func (c *CommandClient) CloseConnections() error {
conn, err := c.directConnect()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit bf75427

Please sign in to comment.