github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/experimental/libbox/command_select.go (about) 1 package libbox 2 3 import ( 4 "encoding/binary" 5 "net" 6 7 "github.com/inazumav/sing-box/outbound" 8 E "github.com/sagernet/sing/common/exceptions" 9 "github.com/sagernet/sing/common/rw" 10 ) 11 12 func (c *CommandClient) SelectOutbound(groupTag string, outboundTag string) error { 13 conn, err := c.directConnect() 14 if err != nil { 15 return err 16 } 17 defer conn.Close() 18 err = binary.Write(conn, binary.BigEndian, uint8(CommandSelectOutbound)) 19 if err != nil { 20 return err 21 } 22 err = rw.WriteVString(conn, groupTag) 23 if err != nil { 24 return err 25 } 26 err = rw.WriteVString(conn, outboundTag) 27 if err != nil { 28 return err 29 } 30 return readError(conn) 31 } 32 33 func (s *CommandServer) handleSelectOutbound(conn net.Conn) error { 34 defer conn.Close() 35 groupTag, err := rw.ReadVString(conn) 36 if err != nil { 37 return err 38 } 39 outboundTag, err := rw.ReadVString(conn) 40 if err != nil { 41 return err 42 } 43 service := s.service 44 if service == nil { 45 return writeError(conn, E.New("service not ready")) 46 } 47 outboundGroup, isLoaded := service.instance.Router().Outbound(groupTag) 48 if !isLoaded { 49 return writeError(conn, E.New("selector not found: ", groupTag)) 50 } 51 selector, isSelector := outboundGroup.(*outbound.Selector) 52 if !isSelector { 53 return writeError(conn, E.New("outbound is not a selector: ", groupTag)) 54 } 55 if !selector.SelectOutbound(outboundTag) { 56 return writeError(conn, E.New("outbound not found in selector: ", outboundTag)) 57 } 58 return writeError(conn, nil) 59 }