github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/experimental/libbox/command_system_proxy.go (about) 1 package libbox 2 3 import ( 4 "encoding/binary" 5 "net" 6 ) 7 8 type SystemProxyStatus struct { 9 Available bool 10 Enabled bool 11 } 12 13 func (c *CommandClient) GetSystemProxyStatus() (*SystemProxyStatus, error) { 14 conn, err := c.directConnectWithRetry() 15 if err != nil { 16 return nil, err 17 } 18 defer conn.Close() 19 err = binary.Write(conn, binary.BigEndian, uint8(CommandGetSystemProxyStatus)) 20 if err != nil { 21 return nil, err 22 } 23 var status SystemProxyStatus 24 err = binary.Read(conn, binary.BigEndian, &status.Available) 25 if err != nil { 26 return nil, err 27 } 28 if status.Available { 29 err = binary.Read(conn, binary.BigEndian, &status.Enabled) 30 if err != nil { 31 return nil, err 32 } 33 } 34 return &status, nil 35 } 36 37 func (s *CommandServer) handleGetSystemProxyStatus(conn net.Conn) error { 38 defer conn.Close() 39 status := s.handler.GetSystemProxyStatus() 40 err := binary.Write(conn, binary.BigEndian, status.Available) 41 if err != nil { 42 return err 43 } 44 if status.Available { 45 err = binary.Write(conn, binary.BigEndian, status.Enabled) 46 if err != nil { 47 return err 48 } 49 } 50 return nil 51 } 52 53 func (c *CommandClient) SetSystemProxyEnabled(isEnabled bool) error { 54 conn, err := c.directConnect() 55 if err != nil { 56 return err 57 } 58 defer conn.Close() 59 err = binary.Write(conn, binary.BigEndian, uint8(CommandSetSystemProxyEnabled)) 60 if err != nil { 61 return err 62 } 63 err = binary.Write(conn, binary.BigEndian, isEnabled) 64 if err != nil { 65 return err 66 } 67 return readError(conn) 68 } 69 70 func (s *CommandServer) handleSetSystemProxyEnabled(conn net.Conn) error { 71 defer conn.Close() 72 var isEnabled bool 73 err := binary.Read(conn, binary.BigEndian, &isEnabled) 74 if err != nil { 75 return err 76 } 77 err = s.handler.SetSystemProxyEnabled(isEnabled) 78 if err != nil { 79 return writeError(conn, err) 80 } 81 return writeError(conn, nil) 82 }