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