Clash.Meta/constant/adapters.go

205 lines
4.1 KiB
Go
Raw Normal View History

2018-06-10 22:50:03 +08:00
package constant
import (
2019-07-02 19:18:03 +08:00
"context"
"fmt"
2018-06-14 01:00:58 +08:00
"net"
"time"
"github.com/Dreamacro/clash/component/dialer"
2018-06-10 22:50:03 +08:00
)
// Adapter Type
const (
Direct AdapterType = iota
Reject
Compatible
Pass
Relay
Selector
Fallback
URLTest
LoadBalance
Shadowsocks
ShadowsocksR
2019-10-09 18:46:23 +08:00
Snell
2018-08-12 13:50:54 +08:00
Socks5
Http
2018-09-06 10:53:29 +08:00
Vmess
2021-11-17 16:03:47 +08:00
Vless
2020-03-19 20:26:53 +08:00
Trojan
2022-06-07 13:38:45 +08:00
Hysteria
2022-11-09 18:44:06 +08:00
WireGuard
)
2021-06-10 14:05:56 +08:00
const (
DefaultTCPTimeout = 5 * time.Second
DefaultUDPTimeout = DefaultTCPTimeout
DefaultTLSTimeout = DefaultTCPTimeout
2021-06-10 14:05:56 +08:00
)
type Connection interface {
Chains() Chain
AppendToChains(adapter ProxyAdapter)
2022-05-26 23:41:09 +08:00
RemoteDestination() string
}
type Chain []string
func (c Chain) String() string {
switch len(c) {
case 0:
return ""
case 1:
return c[0]
default:
return fmt.Sprintf("%s[%s]", c[len(c)-1], c[0])
}
}
func (c Chain) Last() string {
switch len(c) {
case 0:
return ""
default:
return c[0]
}
}
type Conn interface {
net.Conn
Connection
}
type PacketConn interface {
net.PacketConn
Connection
// Deprecate WriteWithMetadata because of remote resolve DNS cause TURN failed
// WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error)
}
type ProxyAdapter interface {
2018-06-16 21:34:13 +08:00
Name() string
Type() AdapterType
Addr() string
SupportUDP() bool
MarshalJSON() ([]byte, error)
// StreamConn wraps a protocol around net.Conn with Metadata.
//
// Examples:
// conn, _ := net.DialContext(context.Background(), "tcp", "host:port")
// conn, _ = adapter.StreamConn(conn, metadata)
//
// It returns a C.Conn with protocol which start with
// a new session (if any)
StreamConn(c net.Conn, metadata *Metadata) (net.Conn, error)
// DialContext return a C.Conn with protocol which
// contains multiplexing-related reuse logic (if any)
DialContext(ctx context.Context, metadata *Metadata, opts ...dialer.Option) (Conn, error)
ListenPacketContext(ctx context.Context, metadata *Metadata, opts ...dialer.Option) (PacketConn, error)
// SupportUOT return UDP over TCP support
SupportUOT() bool
ListenPacketOnStreamConn(c net.Conn, metadata *Metadata) (PacketConn, error)
// Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract.
2022-10-30 23:08:18 +08:00
Unwrap(metadata *Metadata, touch bool) Proxy
2018-06-10 22:50:03 +08:00
}
2022-05-30 21:55:09 +08:00
type Group interface {
URLTest(ctx context.Context, url string) (mp map[string]uint16, err error)
GetProxies(touch bool) []Proxy
Touch()
2022-05-30 21:55:09 +08:00
}
type DelayHistory struct {
Time time.Time `json:"time"`
Delay uint16 `json:"delay"`
}
type Proxy interface {
ProxyAdapter
Alive() bool
DelayHistory() []DelayHistory
LastDelay() uint16
2019-07-02 19:18:03 +08:00
URLTest(ctx context.Context, url string) (uint16, error)
// Deprecated: use DialContext instead.
Dial(metadata *Metadata) (Conn, error)
// Deprecated: use DialPacketConn instead.
DialUDP(metadata *Metadata) (PacketConn, error)
}
// AdapterType is enum of adapter type
type AdapterType int
func (at AdapterType) String() string {
switch at {
case Direct:
return "Direct"
case Reject:
return "Reject"
case Compatible:
return "Compatible"
case Pass:
return "Pass"
case Shadowsocks:
return "Shadowsocks"
case ShadowsocksR:
return "ShadowsocksR"
2019-10-09 18:46:23 +08:00
case Snell:
return "Snell"
2018-08-12 13:50:54 +08:00
case Socks5:
return "Socks5"
case Http:
return "Http"
2018-09-06 10:53:29 +08:00
case Vmess:
return "Vmess"
2021-11-17 16:03:47 +08:00
case Vless:
return "Vless"
2020-03-19 20:26:53 +08:00
case Trojan:
return "Trojan"
2022-06-07 13:38:45 +08:00
case Hysteria:
return "Hysteria"
2022-11-09 18:44:06 +08:00
case WireGuard:
return "WireGuard"
2020-03-19 20:26:53 +08:00
case Relay:
return "Relay"
2020-03-19 20:26:53 +08:00
case Selector:
return "Selector"
case Fallback:
return "Fallback"
case URLTest:
return "URLTest"
2019-02-15 14:25:20 +08:00
case LoadBalance:
return "LoadBalance"
2020-03-19 20:26:53 +08:00
default:
2019-08-26 12:26:14 +08:00
return "Unknown"
}
}
// UDPPacket contains the data of UDP packet, and offers control/info of UDP packet's source
type UDPPacket interface {
// Data get the payload of UDP Packet
Data() []byte
// WriteBack writes the payload with source IP/Port equals addr
// - variable source IP/Port is important to STUN
2020-10-14 19:56:02 +08:00
// - if addr is not provided, WriteBack will write out UDP packet with SourceIP/Port equals to original Target,
// this is important when using Fake-IP.
WriteBack(b []byte, addr net.Addr) (n int, err error)
// Drop call after packet is used, could recycle buffer in this function.
Drop()
// LocalAddr returns the source IP/Port of packet
LocalAddr() net.Addr
}