diff --git a/custom/command_client.go b/custom/command_client.go index 89e3f42..fbdc8e9 100644 --- a/custom/command_client.go +++ b/custom/command_client.go @@ -42,9 +42,51 @@ func (cch *CommandClientHandler) WriteStatus(message *libbox.StatusMessage) { } } -func (cch *CommandClientHandler) WriteGroups(message libbox.OutboundGroupIterator) {} +func (cch *CommandClientHandler) WriteGroups(message libbox.OutboundGroupIterator) { + if message == nil { + return + } + groups := []*OutboundGroup{} + for message.HasNext() { + group := message.Next() + items := group.GetItems() + groupItems := []*OutboundGroupItem{} + for items.HasNext() { + item := items.Next() + groupItems = append(groupItems, + &OutboundGroupItem{ + Tag: item.Tag, + Type: item.Type, + URLTestTime: item.URLTestTime, + URLTestDelay: item.URLTestDelay, + }, + ) + } + groups = append(groups, &OutboundGroup{Tag: group.Tag, Type: group.Type, Selected: group.Selected, Items: groupItems}) + } + response, err := json.Marshal(groups) + if err != nil { + bridge.SendStringToPort(cch.port, fmt.Sprintf("error: %e", err)) + } else { + bridge.SendStringToPort(cch.port, string(response)) + } +} func (cch *CommandClientHandler) InitializeClashMode(modeList libbox.StringIterator, currentMode string) { } func (cch *CommandClientHandler) UpdateClashMode(newMode string) {} + +type OutboundGroup struct { + Tag string `json:"tag"` + Type string `json:"type"` + Selected string `json:"selected"` + Items []*OutboundGroupItem `json:"items"` +} + +type OutboundGroupItem struct { + Tag string `json:"tag"` + Type string `json:"type"` + URLTestTime int64 `json:"url-test-time"` + URLTestDelay int32 `json:"url-test-delay"` +} diff --git a/custom/commands.go b/custom/commands.go index 0f30ac5..c79c66d 100644 --- a/custom/commands.go +++ b/custom/commands.go @@ -4,6 +4,7 @@ import "github.com/sagernet/sing-box/experimental/libbox" var ( statusCommand *libbox.CommandClient + groupCommand *libbox.CommandClient ) func StartCommand(command int32, port int64) error { @@ -17,6 +18,15 @@ func StartCommand(command int32, port int64) error { }, ) return statusCommand.Connect() + case libbox.CommandGroup: + groupCommand = libbox.NewCommandClient( + &CommandClientHandler{port: port}, + &libbox.CommandClientOptions{ + Command: libbox.CommandGroup, + StatusInterval: 1000000000, + }, + ) + return groupCommand.Connect() } return nil } @@ -27,6 +37,10 @@ func StopCommand(command int32) error { err := statusCommand.Disconnect() statusCommand = nil return err + case libbox.CommandGroup: + err := groupCommand.Disconnect() + groupCommand = nil + return err } return nil } diff --git a/custom/custom.go b/custom/custom.go index c65588b..c535a89 100644 --- a/custom/custom.go +++ b/custom/custom.go @@ -131,4 +131,22 @@ func stopCommandClient(command C.int) *C.char { return C.CString("") } +//export selectOutbound +func selectOutbound(groupTag *C.char, outboundTag *C.char) *C.char { + err := libbox.NewStandaloneCommandClient().SelectOutbound(C.GoString(groupTag), C.GoString(outboundTag)) + if err != nil { + return C.CString(err.Error()) + } + return C.CString("") +} + +//export urlTest +func urlTest(groupTag *C.char) *C.char { + err := libbox.NewStandaloneCommandClient().URLTest(C.GoString(groupTag)) + if err != nil { + return C.CString(err.Error()) + } + return C.CString("") +} + func main() {} diff --git a/mobile/mobile.go b/mobile/mobile.go index 63b18a7..216ca6d 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -1,34 +1,3 @@ -// package mobile - -// import ( -// "encoding/json" -// "os" - -// "github.com/hiddify/libcore/shared" -// _ "github.com/sagernet/gomobile/event/key" -// "github.com/sagernet/sing-box/option" -// ) - -// func Parse(path string) error { -// return shared.ParseConfig(path) -// } - -// func ApplyOverrides(path string) (string, error) { -// fileContent, err := os.ReadFile(path) -// if err != nil { -// return "", err -// } -// var options option.Options -// err = options.UnmarshalJSON(fileContent) -// if err != nil { -// return "", err -// } -// overrides := shared.ConfigOverrides{ExcludeTunInbound: false, IncludeMixedInbound: false, IncludeLogOutput: false, LogLevel: "", IncludeLogTimestamp: false, ClashApiPort: 9090} -// options = shared.ApplyOverrides(options, overrides) -// config, err := json.Marshal(options) -// return string(config), err -// } - package mobile import (