github.com/chwjbn/xclash@v0.2.0/constant/adapters.go (about) 1 package constant 2 3 import ( 4 "context" 5 "fmt" 6 "net" 7 "time" 8 9 "github.com/chwjbn/xclash/component/dialer" 10 ) 11 12 // Adapter Type 13 const ( 14 Direct AdapterType = iota 15 Reject 16 17 Shadowsocks 18 ShadowsocksR 19 Snell 20 Socks5 21 Http 22 Vmess 23 Trojan 24 25 Relay 26 Selector 27 Fallback 28 URLTest 29 LoadBalance 30 ) 31 32 const ( 33 DefaultTCPTimeout = 5 * time.Second 34 DefaultUDPTimeout = DefaultTCPTimeout 35 ) 36 37 type Connection interface { 38 Chains() Chain 39 AppendToChains(adapter ProxyAdapter) 40 } 41 42 type Chain []string 43 44 func (c Chain) String() string { 45 switch len(c) { 46 case 0: 47 return "" 48 case 1: 49 return c[0] 50 default: 51 return fmt.Sprintf("%s[%s]", c[len(c)-1], c[0]) 52 } 53 } 54 55 func (c Chain) Last() string { 56 switch len(c) { 57 case 0: 58 return "" 59 default: 60 return c[0] 61 } 62 } 63 64 type Conn interface { 65 net.Conn 66 Connection 67 } 68 69 type PacketConn interface { 70 net.PacketConn 71 Connection 72 // Deprecate WriteWithMetadata because of remote resolve DNS cause TURN failed 73 // WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error) 74 } 75 76 type ProxyAdapter interface { 77 Name() string 78 Type() AdapterType 79 Addr() string 80 SupportUDP() bool 81 MarshalJSON() ([]byte, error) 82 83 // StreamConn wraps a protocol around net.Conn with Metadata. 84 // 85 // Examples: 86 // conn, _ := net.DialContext(context.Background(), "tcp", "host:port") 87 // conn, _ = adapter.StreamConn(conn, metadata) 88 // 89 // It returns a C.Conn with protocol which start with 90 // a new session (if any) 91 StreamConn(c net.Conn, metadata *Metadata) (net.Conn, error) 92 93 // DialContext return a C.Conn with protocol which 94 // contains multiplexing-related reuse logic (if any) 95 DialContext(ctx context.Context, metadata *Metadata, opts ...dialer.Option) (Conn, error) 96 ListenPacketContext(ctx context.Context, metadata *Metadata, opts ...dialer.Option) (PacketConn, error) 97 98 // Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract. 99 Unwrap(metadata *Metadata) Proxy 100 } 101 102 type DelayHistory struct { 103 Time time.Time `json:"time"` 104 Delay uint16 `json:"delay"` 105 } 106 107 type Proxy interface { 108 ProxyAdapter 109 Alive() bool 110 DelayHistory() []DelayHistory 111 LastDelay() uint16 112 URLTest(ctx context.Context, url string) (uint16, error) 113 114 // Deprecated: use DialContext instead. 115 Dial(metadata *Metadata) (Conn, error) 116 117 // Deprecated: use DialPacketConn instead. 118 DialUDP(metadata *Metadata) (PacketConn, error) 119 } 120 121 // AdapterType is enum of adapter type 122 type AdapterType int 123 124 func (at AdapterType) String() string { 125 switch at { 126 case Direct: 127 return "Direct" 128 case Reject: 129 return "Reject" 130 131 case Shadowsocks: 132 return "Shadowsocks" 133 case ShadowsocksR: 134 return "ShadowsocksR" 135 case Snell: 136 return "Snell" 137 case Socks5: 138 return "Socks5" 139 case Http: 140 return "Http" 141 case Vmess: 142 return "Vmess" 143 case Trojan: 144 return "Trojan" 145 146 case Relay: 147 return "Relay" 148 case Selector: 149 return "Selector" 150 case Fallback: 151 return "Fallback" 152 case URLTest: 153 return "URLTest" 154 case LoadBalance: 155 return "LoadBalance" 156 157 default: 158 return "Unknown" 159 } 160 } 161 162 // UDPPacket contains the data of UDP packet, and offers control/info of UDP packet's source 163 type UDPPacket interface { 164 // Data get the payload of UDP Packet 165 Data() []byte 166 167 // WriteBack writes the payload with source IP/Port equals addr 168 // - variable source IP/Port is important to STUN 169 // - if addr is not provided, WriteBack will write out UDP packet with SourceIP/Port equals to original Target, 170 // this is important when using Fake-IP. 171 WriteBack(b []byte, addr net.Addr) (n int, err error) 172 173 // Drop call after packet is used, could recycle buffer in this function. 174 Drop() 175 176 // LocalAddr returns the source IP/Port of packet 177 LocalAddr() net.Addr 178 }