clash/constant/adapters.go

310 lines
6.7 KiB
Go
Raw Normal View History

2018-06-10 14:50:03 +00:00
package constant
import (
2019-07-02 11:18:03 +00:00
"context"
"errors"
"fmt"
2018-06-13 17:00:58 +00:00
"net"
2022-12-19 13:34:07 +00:00
"net/netip"
"sync"
"time"
2023-11-03 13:01:45 +00:00
N "github.com/metacubex/mihomo/common/net"
"github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/component/dialer"
2018-06-10 14:50:03 +00:00
)
// Adapter Type
const (
Direct AdapterType = iota
Reject
2023-11-18 05:17:15 +00:00
RejectDrop
Compatible
Pass
Dns
Relay
Selector
Fallback
URLTest
LoadBalance
Shadowsocks
ShadowsocksR
2019-10-09 10:46:23 +00:00
Snell
2018-08-12 05:50:54 +00:00
Socks5
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
2023-09-21 02:28:28 +00:00
Hysteria2
2022-11-09 10:44:06 +00:00
WireGuard
2022-11-25 00:08:14 +00:00
Tuic
Ssh
)
2021-06-10 06:05:56 +00:00
const (
2024-01-24 09:52:45 +00:00
DefaultTCPTimeout = dialer.DefaultTCPTimeout
DefaultUDPTimeout = dialer.DefaultUDPTimeout
DefaultDropTime = 12 * DefaultTCPTimeout
DefaultTLSTimeout = DefaultTCPTimeout
2023-12-27 03:58:39 +00:00
DefaultTestURL = "https://www.gstatic.com/generate_204"
2021-06-10 06:05:56 +00:00
)
var ErrNotSupport = errors.New("no support")
type Connection interface {
Chains() Chain
AppendToChains(adapter ProxyAdapter)
2022-05-26 15:41:09 +00: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 {
N.ExtendedConn
Connection
}
type PacketConn interface {
N.EnhancePacketConn
Connection
// Deprecate WriteWithMetadata because of remote resolve DNS cause TURN failed
// WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error)
}
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)
}
type ProxyAdapter interface {
2018-06-16 13:34:13 +00:00
Name() string
Type() AdapterType
Addr() string
SupportUDP() bool
SupportXUDP() bool
SupportTFO() bool
MarshalJSON() ([]byte, error)
2022-12-19 13:34:07 +00:00
// Deprecated: use DialContextWithDialer and ListenPacketWithDialer instead.
// StreamConn wraps a protocol around net.Conn with Metadata.
//
// Examples:
// conn, _ := net.DialContext(context.Background(), "tcp", "host:port")
// conn, _ = adapter.StreamConnContext(context.Background(), conn, metadata)
//
// It returns a C.Conn with protocol which start with
// a new session (if any)
StreamConnContext(ctx context.Context, 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
SupportWithDialer() NetWork
2022-12-19 13:34:07 +00:00
DialContextWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (Conn, error)
ListenPacketWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (PacketConn, error)
// IsL3Protocol return ProxyAdapter working in L3 (tell dns module not pass the domain to avoid loopback)
IsL3Protocol(metadata *Metadata) bool
// 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
}
2022-05-30 13:55:09 +00:00
type Group interface {
URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (mp map[string]uint16, err error)
2022-05-30 13:55:09 +00:00
GetProxies(touch bool) []Proxy
Touch()
2022-05-30 13:55:09 +00:00
}
type DelayHistory struct {
Time time.Time `json:"time"`
Delay uint16 `json:"delay"`
}
type ProxyState struct {
Alive bool `json:"alive"`
History []DelayHistory `json:"history"`
}
type DelayHistoryStoreType int
type Proxy interface {
ProxyAdapter
AliveForTestUrl(url string) bool
DelayHistory() []DelayHistory
ExtraDelayHistories() map[string]ProxyState
LastDelayForTestUrl(url string) uint16
URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (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"
2023-11-18 05:17:15 +00:00
case RejectDrop:
return "RejectDrop"
case Compatible:
return "Compatible"
case Pass:
return "Pass"
2024-03-04 12:02:09 +00:00
case Dns:
return "Dns"
case Shadowsocks:
return "Shadowsocks"
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"
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"
2023-09-21 02:28:28 +00:00
case Hysteria2:
return "Hysteria2"
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
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"
case Ssh:
return "Ssh"
default:
2019-08-26 04:26:14 +00: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 11:56:02 +00: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
// Drop call after packet is used, could recycle buffer in this function.
Drop()
// 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
}
// PacketAdapter is a UDP Packet adapter for socks/redir/tun
type PacketAdapter interface {
UDPPacket
Metadata() *Metadata
}
2023-10-11 02:55:12 +00:00
type packetAdapter struct {
UDPPacket
metadata *Metadata
}
// Metadata returns destination metadata
func (s *packetAdapter) Metadata() *Metadata {
return s.metadata
}
func NewPacketAdapter(packet UDPPacket, metadata *Metadata) PacketAdapter {
return &packetAdapter{
packet,
metadata,
}
}
type WriteBack interface {
WriteBack(b []byte, addr net.Addr) (n int, err error)
}
type WriteBackProxy interface {
WriteBack
UpdateWriteBack(wb WriteBack)
}
type NatTable interface {
Set(key string, e PacketConn, w WriteBackProxy)
Get(key string) (PacketConn, WriteBackProxy)
GetOrCreateLock(key string) (*sync.Cond, bool)
Delete(key string)
DeleteLock(key string)
GetForLocalConn(lAddr, rAddr string) *net.UDPConn
AddForLocalConn(lAddr, rAddr string, conn *net.UDPConn) bool
RangeForLocalConn(lAddr string, f func(key string, value *net.UDPConn) bool)
GetOrCreateLockForLocalConn(lAddr string, key string) (*sync.Cond, bool)
DeleteForLocalConn(lAddr, key string)
DeleteLockForLocalConn(lAddr, key string)
}