2018-06-10 14:50:03 +00:00
|
|
|
package constant
|
|
|
|
|
|
|
|
import (
|
2019-07-02 11:18:03 +00:00
|
|
|
"context"
|
2019-08-08 17:28:37 +00:00
|
|
|
"fmt"
|
2018-06-13 17:00:58 +00:00
|
|
|
"net"
|
2022-12-19 13:34:07 +00:00
|
|
|
"net/netip"
|
2023-02-17 08:31:15 +00:00
|
|
|
"sync"
|
2019-03-17 06:52:39 +00:00
|
|
|
"time"
|
2021-11-07 08:48:51 +00:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2018-06-10 14:50:03 +00:00
|
|
|
)
|
|
|
|
|
2018-07-12 15:28:38 +00:00
|
|
|
// Adapter Type
|
|
|
|
const (
|
|
|
|
Direct AdapterType = iota
|
|
|
|
Reject
|
2022-01-18 13:09:36 +00:00
|
|
|
Compatible
|
2022-03-27 15:44:51 +00:00
|
|
|
Pass
|
2022-02-23 08:17:29 +00:00
|
|
|
|
|
|
|
Relay
|
|
|
|
Selector
|
|
|
|
Fallback
|
|
|
|
URLTest
|
|
|
|
LoadBalance
|
|
|
|
|
2018-07-12 15:28:38 +00:00
|
|
|
Shadowsocks
|
2020-07-22 15:02:15 +00:00
|
|
|
ShadowsocksR
|
2019-10-09 10:46:23 +00:00
|
|
|
Snell
|
2018-08-12 05:50:54 +00:00
|
|
|
Socks5
|
2018-12-03 15:27:00 +00:00
|
|
|
Http
|
2018-09-06 02:53:29 +00:00
|
|
|
Vmess
|
2021-11-17 08:03:47 +00:00
|
|
|
Vless
|
2020-03-19 12:26:53 +00:00
|
|
|
Trojan
|
2022-06-07 05:38:45 +00:00
|
|
|
Hysteria
|
2022-11-09 10:44:06 +00:00
|
|
|
WireGuard
|
2022-11-25 00:08:14 +00:00
|
|
|
Tuic
|
2018-07-12 15:28:38 +00:00
|
|
|
)
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
const (
|
|
|
|
DefaultTCPTimeout = 5 * time.Second
|
2021-10-15 13:44:53 +00:00
|
|
|
DefaultUDPTimeout = DefaultTCPTimeout
|
2022-01-15 11:33:21 +00:00
|
|
|
DefaultTLSTimeout = DefaultTCPTimeout
|
2021-06-10 06:05:56 +00:00
|
|
|
)
|
|
|
|
|
2019-08-08 17:28:37 +00:00
|
|
|
type Connection interface {
|
|
|
|
Chains() Chain
|
|
|
|
AppendToChains(adapter ProxyAdapter)
|
2022-05-26 15:41:09 +00:00
|
|
|
RemoteDestination() string
|
2019-08-08 17:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 06:49:46 +00:00
|
|
|
func (c Chain) Last() string {
|
|
|
|
switch len(c) {
|
|
|
|
case 0:
|
|
|
|
return ""
|
|
|
|
default:
|
|
|
|
return c[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:28:37 +00:00
|
|
|
type Conn interface {
|
|
|
|
net.Conn
|
|
|
|
Connection
|
|
|
|
}
|
|
|
|
|
|
|
|
type PacketConn interface {
|
|
|
|
net.PacketConn
|
|
|
|
Connection
|
2020-06-12 15:39:03 +00:00
|
|
|
// Deprecate WriteWithMetadata because of remote resolve DNS cause TURN failed
|
|
|
|
// WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error)
|
2019-08-08 17:28:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 13:34:07 +00:00
|
|
|
type Dialer interface {
|
|
|
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
|
|
|
ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error)
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:43:16 +00:00
|
|
|
type ProxyAdapter interface {
|
2018-06-16 13:34:13 +00:00
|
|
|
Name() string
|
2018-07-12 15:28:38 +00:00
|
|
|
Type() AdapterType
|
2021-10-15 13:44:53 +00:00
|
|
|
Addr() string
|
|
|
|
SupportUDP() bool
|
2023-01-27 16:07:20 +00:00
|
|
|
SupportXUDP() bool
|
2022-11-28 11:52:55 +00:00
|
|
|
SupportTFO() bool
|
2021-10-15 13:44:53 +00:00
|
|
|
MarshalJSON() ([]byte, error)
|
2021-03-22 15:26:20 +00:00
|
|
|
|
2022-12-19 13:34:07 +00:00
|
|
|
// Deprecated: use DialContextWithDialer and ListenPacketWithDialer instead.
|
2021-03-22 15:26:20 +00:00
|
|
|
// StreamConn wraps a protocol around net.Conn with Metadata.
|
|
|
|
//
|
|
|
|
// Examples:
|
2021-10-15 13:44:53 +00:00
|
|
|
// conn, _ := net.DialContext(context.Background(), "tcp", "host:port")
|
2021-03-22 15:26:20 +00:00
|
|
|
// conn, _ = adapter.StreamConn(conn, metadata)
|
|
|
|
//
|
|
|
|
// It returns a C.Conn with protocol which start with
|
|
|
|
// a new session (if any)
|
2020-03-21 15:46:49 +00:00
|
|
|
StreamConn(c net.Conn, metadata *Metadata) (net.Conn, error)
|
2021-03-22 15:26:20 +00:00
|
|
|
|
|
|
|
// DialContext return a C.Conn with protocol which
|
|
|
|
// contains multiplexing-related reuse logic (if any)
|
2021-11-07 08:48:51 +00:00
|
|
|
DialContext(ctx context.Context, metadata *Metadata, opts ...dialer.Option) (Conn, error)
|
|
|
|
ListenPacketContext(ctx context.Context, metadata *Metadata, opts ...dialer.Option) (PacketConn, error)
|
2021-10-15 13:44:53 +00:00
|
|
|
|
2022-04-30 03:36:42 +00:00
|
|
|
// SupportUOT return UDP over TCP support
|
|
|
|
SupportUOT() bool
|
|
|
|
|
2022-12-19 13:34:07 +00:00
|
|
|
SupportWithDialer() bool
|
|
|
|
DialContextWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (Conn, error)
|
|
|
|
ListenPacketWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (PacketConn, error)
|
2022-12-19 09:02:04 +00:00
|
|
|
|
2020-05-07 13:42:52 +00:00
|
|
|
// Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract.
|
2022-10-30 15:08:18 +00:00
|
|
|
Unwrap(metadata *Metadata, touch bool) Proxy
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2018-07-12 15:28:38 +00:00
|
|
|
|
2022-05-30 13:55:09 +00:00
|
|
|
type Group interface {
|
|
|
|
URLTest(ctx context.Context, url string) (mp map[string]uint16, err error)
|
|
|
|
GetProxies(touch bool) []Proxy
|
2022-10-31 08:45:14 +00:00
|
|
|
Touch()
|
2022-05-30 13:55:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 06:52:39 +00:00
|
|
|
type DelayHistory struct {
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Delay uint16 `json:"delay"`
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:43:16 +00:00
|
|
|
type Proxy interface {
|
|
|
|
ProxyAdapter
|
|
|
|
Alive() bool
|
2019-03-17 06:52:39 +00:00
|
|
|
DelayHistory() []DelayHistory
|
|
|
|
LastDelay() uint16
|
2019-07-02 11:18:03 +00:00
|
|
|
URLTest(ctx context.Context, url string) (uint16, error)
|
2021-10-15 13:44:53 +00:00
|
|
|
|
|
|
|
// Deprecated: use DialContext instead.
|
|
|
|
Dial(metadata *Metadata) (Conn, error)
|
|
|
|
|
|
|
|
// Deprecated: use DialPacketConn instead.
|
|
|
|
DialUDP(metadata *Metadata) (PacketConn, error)
|
2019-03-15 16:43:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 15:28:38 +00:00
|
|
|
// AdapterType is enum of adapter type
|
|
|
|
type AdapterType int
|
|
|
|
|
|
|
|
func (at AdapterType) String() string {
|
|
|
|
switch at {
|
|
|
|
case Direct:
|
|
|
|
return "Direct"
|
|
|
|
case Reject:
|
|
|
|
return "Reject"
|
2022-01-18 13:09:36 +00:00
|
|
|
case Compatible:
|
|
|
|
return "Compatible"
|
2022-03-27 15:44:51 +00:00
|
|
|
case Pass:
|
|
|
|
return "Pass"
|
2018-07-12 15:28:38 +00:00
|
|
|
case Shadowsocks:
|
|
|
|
return "Shadowsocks"
|
2020-07-22 15:02:15 +00:00
|
|
|
case ShadowsocksR:
|
|
|
|
return "ShadowsocksR"
|
2019-10-09 10:46:23 +00:00
|
|
|
case Snell:
|
|
|
|
return "Snell"
|
2018-08-12 05:50:54 +00:00
|
|
|
case Socks5:
|
|
|
|
return "Socks5"
|
2018-12-03 15:27:00 +00:00
|
|
|
case Http:
|
|
|
|
return "Http"
|
2018-09-06 02:53:29 +00:00
|
|
|
case Vmess:
|
|
|
|
return "Vmess"
|
2021-11-17 08:03:47 +00:00
|
|
|
case Vless:
|
|
|
|
return "Vless"
|
2020-03-19 12:26:53 +00:00
|
|
|
case Trojan:
|
|
|
|
return "Trojan"
|
2022-06-07 05:38:45 +00:00
|
|
|
case Hysteria:
|
|
|
|
return "Hysteria"
|
2022-11-09 10:44:06 +00:00
|
|
|
case WireGuard:
|
|
|
|
return "WireGuard"
|
2022-11-25 00:08:14 +00:00
|
|
|
case Tuic:
|
|
|
|
return "Tuic"
|
2020-03-19 12:26:53 +00:00
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
case Relay:
|
|
|
|
return "Relay"
|
2020-03-19 12:26:53 +00:00
|
|
|
case Selector:
|
|
|
|
return "Selector"
|
|
|
|
case Fallback:
|
|
|
|
return "Fallback"
|
|
|
|
case URLTest:
|
|
|
|
return "URLTest"
|
2019-02-15 06:25:20 +00:00
|
|
|
case LoadBalance:
|
|
|
|
return "LoadBalance"
|
2020-03-19 12:26:53 +00:00
|
|
|
|
2018-07-12 15:28:38 +00:00
|
|
|
default:
|
2019-08-26 04:26:14 +00:00
|
|
|
return "Unknown"
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-28 10:44:01 +00:00
|
|
|
|
|
|
|
// 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 11:56:02 +00:00
|
|
|
// - if addr is not provided, WriteBack will write out UDP packet with SourceIP/Port equals to original Target,
|
2019-12-28 10:44:01 +00:00
|
|
|
// this is important when using Fake-IP.
|
|
|
|
WriteBack(b []byte, addr net.Addr) (n int, err error)
|
|
|
|
|
2020-04-16 10:19:36 +00:00
|
|
|
// Drop call after packet is used, could recycle buffer in this function.
|
|
|
|
Drop()
|
2019-12-28 10:44:01 +00:00
|
|
|
|
|
|
|
// LocalAddr returns the source IP/Port of packet
|
|
|
|
LocalAddr() net.Addr
|
|
|
|
}
|
2022-11-11 15:36:06 +00:00
|
|
|
|
|
|
|
type UDPPacketInAddr interface {
|
|
|
|
InAddr() net.Addr
|
|
|
|
}
|
2022-12-04 06:37:52 +00:00
|
|
|
|
|
|
|
// PacketAdapter is a UDP Packet adapter for socks/redir/tun
|
|
|
|
type PacketAdapter interface {
|
|
|
|
UDPPacket
|
|
|
|
Metadata() *Metadata
|
|
|
|
}
|
2023-02-17 08:31:15 +00:00
|
|
|
|
|
|
|
type NatTable interface {
|
|
|
|
Set(key string, e PacketConn)
|
|
|
|
|
|
|
|
Get(key string) PacketConn
|
|
|
|
|
|
|
|
GetOrCreateLock(key string) (*sync.Cond, bool)
|
|
|
|
|
|
|
|
Delete(key string)
|
|
|
|
|
|
|
|
GetLocalConn(lAddr, rAddr string) *net.UDPConn
|
|
|
|
|
|
|
|
AddLocalConn(lAddr, rAddr string, conn *net.UDPConn) bool
|
|
|
|
|
|
|
|
RangeLocalConn(lAddr string, f func(key, value any) bool)
|
|
|
|
|
|
|
|
GetOrCreateLockForLocalConn(lAddr, key string) (*sync.Cond, bool)
|
|
|
|
|
|
|
|
DeleteLocalConnMap(lAddr, key string)
|
|
|
|
}
|