Add groups command client

This commit is contained in:
problematicconsumer
2023-08-29 19:10:14 +03:30
parent 219ca7ced9
commit 2506403aec
4 changed files with 75 additions and 32 deletions

View File

@@ -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"`
}

View File

@@ -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
}

View File

@@ -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() {}

View File

@@ -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 (