sing-box/experimental/libbox/command_select.go

59 lines
1.5 KiB
Go
Raw Normal View History

2023-07-02 08:45:30 +00:00
package libbox
import (
"encoding/binary"
"net"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing-box/protocol/group"
2023-07-02 08:45:30 +00:00
E "github.com/sagernet/sing/common/exceptions"
2024-06-24 01:49:15 +00:00
"github.com/sagernet/sing/common/varbin"
2023-07-02 08:45:30 +00:00
)
func (c *CommandClient) SelectOutbound(groupTag string, outboundTag string) error {
conn, err := c.directConnect()
if err != nil {
return err
}
defer conn.Close()
err = binary.Write(conn, binary.BigEndian, uint8(CommandSelectOutbound))
if err != nil {
return err
}
2024-06-24 01:49:15 +00:00
err = varbin.Write(conn, binary.BigEndian, groupTag)
2023-07-02 08:45:30 +00:00
if err != nil {
return err
}
2024-06-24 01:49:15 +00:00
err = varbin.Write(conn, binary.BigEndian, outboundTag)
2023-07-02 08:45:30 +00:00
if err != nil {
return err
}
return readError(conn)
}
func (s *CommandServer) handleSelectOutbound(conn net.Conn) error {
2024-06-24 01:49:15 +00:00
groupTag, err := varbin.ReadValue[string](conn, binary.BigEndian)
2023-07-02 08:45:30 +00:00
if err != nil {
return err
}
2024-06-24 01:49:15 +00:00
outboundTag, err := varbin.ReadValue[string](conn, binary.BigEndian)
2023-07-02 08:45:30 +00:00
if err != nil {
return err
}
service := s.service
if service == nil {
return writeError(conn, E.New("service not ready"))
}
2024-11-10 04:11:21 +00:00
outboundGroup, isLoaded := service.instance.Outbound().Outbound(groupTag)
2023-07-02 08:45:30 +00:00
if !isLoaded {
return writeError(conn, E.New("selector not found: ", groupTag))
}
2024-11-01 16:39:02 +00:00
selector, isSelector := outboundGroup.(*group.Selector)
2023-07-02 08:45:30 +00:00
if !isSelector {
return writeError(conn, E.New("outbound is not a selector: ", groupTag))
}
if !selector.SelectOutbound(outboundTag) {
return writeError(conn, E.New("outbound not found in selector: ", outboundTag))
}
return writeError(conn, nil)
}