Skip to content

Commit

Permalink
Add groups command client
Browse files Browse the repository at this point in the history
  • Loading branch information
problematicconsumer committed Aug 29, 2023
1 parent 219ca7c commit 2506403
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
44 changes: 43 additions & 1 deletion custom/command_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
14 changes: 14 additions & 0 deletions custom/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
18 changes: 18 additions & 0 deletions custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
31 changes: 0 additions & 31 deletions mobile/mobile.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down

0 comments on commit 2506403

Please sign in to comment.