2018-06-10 22:50:03 +08:00
|
|
|
package constant
|
|
|
|
|
|
|
|
import (
|
2019-07-02 19:18:03 +08:00
|
|
|
"context"
|
2023-04-11 12:51:24 +08:00
|
|
|
"errors"
|
2019-08-09 01:28:37 +08:00
|
|
|
"fmt"
|
2018-06-14 01:00:58 +08:00
|
|
|
"net"
|
2022-12-19 21:34:07 +08:00
|
|
|
"net/netip"
|
2023-02-17 16:31:15 +08:00
|
|
|
"sync"
|
2019-03-17 14:52:39 +08:00
|
|
|
"time"
|
2021-11-07 16:48:51 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
// Adapter Type
|
|
|
|
const (
|
|
|
|
Direct AdapterType = iota
|
|
|
|
Reject
|
2023-11-18 13:17:15 +08:00
|
|
|
RejectDrop
|
2022-01-18 21:09:36 +08:00
|
|
|
Compatible
|
2022-03-27 23:44:51 +08:00
|
|
|
Pass
|
2022-02-23 16:17:29 +08:00
|
|
|
|
|
|
|
Relay
|
|
|
|
Selector
|
|
|
|
Fallback
|
|
|
|
URLTest
|
|
|
|
LoadBalance
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
Shadowsocks
|
2020-07-22 23:02:15 +08:00
|
|
|
ShadowsocksR
|
2019-10-09 18:46:23 +08:00
|
|
|
Snell
|
2018-08-12 13:50:54 +08:00
|
|
|
Socks5
|
2018-12-03 23:27:00 +08:00
|
|
|
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
|
2023-09-21 10:28:28 +08:00
|
|
|
Hysteria2
|
2022-11-09 18:44:06 +08:00
|
|
|
WireGuard
|
2022-11-25 08:08:14 +08:00
|
|
|
Tuic
|
2018-07-12 23:28:38 +08:00
|
|
|
)
|
|
|
|
|
2021-06-10 14:05:56 +08:00
|
|
|
const (
|
2023-06-07 11:04:03 +08:00
|
|
|
DefaultTCPTimeout = 5 * time.Second
|
2023-11-18 13:17:15 +08:00
|
|
|
DefaultDropTime = 12 * DefaultTCPTimeout
|
2023-06-07 11:04:03 +08:00
|
|
|
DefaultUDPTimeout = DefaultTCPTimeout
|
|
|
|
DefaultTLSTimeout = DefaultTCPTimeout
|
|
|
|
DefaultMaxHealthCheckUrlNum = 16
|
2021-06-10 14:05:56 +08:00
|
|
|
)
|
|
|
|
|
2023-04-11 12:51:24 +08:00
|
|
|
var ErrNotSupport = errors.New("no support")
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
type Connection interface {
|
|
|
|
Chains() Chain
|
|
|
|
AppendToChains(adapter ProxyAdapter)
|
2022-05-26 23:41:09 +08:00
|
|
|
RemoteDestination() string
|
2019-08-09 01:28:37 +08: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 14:49:46 +08:00
|
|
|
func (c Chain) Last() string {
|
|
|
|
switch len(c) {
|
|
|
|
case 0:
|
|
|
|
return ""
|
|
|
|
default:
|
|
|
|
return c[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
type Conn interface {
|
2023-04-02 22:24:46 +08:00
|
|
|
N.ExtendedConn
|
2019-08-09 01:28:37 +08:00
|
|
|
Connection
|
|
|
|
}
|
|
|
|
|
|
|
|
type PacketConn interface {
|
2023-05-11 13:47:51 +08:00
|
|
|
N.EnhancePacketConn
|
2019-08-09 01:28:37 +08:00
|
|
|
Connection
|
2020-06-12 23:39:03 +08:00
|
|
|
// Deprecate WriteWithMetadata because of remote resolve DNS cause TURN failed
|
|
|
|
// WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error)
|
2019-08-09 01:28:37 +08:00
|
|
|
}
|
|
|
|
|
2022-12-19 21:34:07 +08: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-16 00:43:16 +08:00
|
|
|
type ProxyAdapter interface {
|
2018-06-16 21:34:13 +08:00
|
|
|
Name() string
|
2018-07-12 23:28:38 +08:00
|
|
|
Type() AdapterType
|
2021-10-15 21:44:53 +08:00
|
|
|
Addr() string
|
|
|
|
SupportUDP() bool
|
2023-01-28 00:07:20 +08:00
|
|
|
SupportXUDP() bool
|
2022-11-28 19:52:55 +08:00
|
|
|
SupportTFO() bool
|
2021-10-15 21:44:53 +08:00
|
|
|
MarshalJSON() ([]byte, error)
|
2021-03-22 23:26:20 +08:00
|
|
|
|
2022-12-19 21:34:07 +08:00
|
|
|
// Deprecated: use DialContextWithDialer and ListenPacketWithDialer instead.
|
2021-03-22 23:26:20 +08:00
|
|
|
// StreamConn wraps a protocol around net.Conn with Metadata.
|
|
|
|
//
|
|
|
|
// Examples:
|
2021-10-15 21:44:53 +08:00
|
|
|
// conn, _ := net.DialContext(context.Background(), "tcp", "host:port")
|
2023-05-18 13:15:08 +08:00
|
|
|
// conn, _ = adapter.StreamConnContext(context.Background(), conn, metadata)
|
2021-03-22 23:26:20 +08:00
|
|
|
//
|
|
|
|
// It returns a C.Conn with protocol which start with
|
|
|
|
// a new session (if any)
|
2023-05-18 13:15:08 +08:00
|
|
|
StreamConnContext(ctx context.Context, c net.Conn, metadata *Metadata) (net.Conn, error)
|
2021-03-22 23:26:20 +08:00
|
|
|
|
|
|
|
// DialContext return a C.Conn with protocol which
|
|
|
|
// contains multiplexing-related reuse logic (if any)
|
2021-11-07 16:48:51 +08: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 21:44:53 +08:00
|
|
|
|
2022-04-30 11:36:42 +08:00
|
|
|
// SupportUOT return UDP over TCP support
|
|
|
|
SupportUOT() bool
|
|
|
|
|
2023-04-11 12:51:24 +08:00
|
|
|
SupportWithDialer() NetWork
|
2022-12-19 21:34:07 +08:00
|
|
|
DialContextWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (Conn, error)
|
|
|
|
ListenPacketWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (PacketConn, error)
|
2022-12-19 17:02:04 +08:00
|
|
|
|
2023-04-11 14:10:57 +08:00
|
|
|
// IsL3Protocol return ProxyAdapter working in L3 (tell dns module not pass the domain to avoid loopback)
|
|
|
|
IsL3Protocol(metadata *Metadata) bool
|
|
|
|
|
2020-05-07 21:42:52 +08:00
|
|
|
// 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
|
|
|
}
|
2018-07-12 23:28:38 +08:00
|
|
|
|
2022-05-30 21:55:09 +08:00
|
|
|
type Group interface {
|
2023-06-04 11:51:30 +08:00
|
|
|
URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (mp map[string]uint16, err error)
|
2022-05-30 21:55:09 +08:00
|
|
|
GetProxies(touch bool) []Proxy
|
2022-10-31 16:45:14 +08:00
|
|
|
Touch()
|
2022-05-30 21:55:09 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 14:52:39 +08:00
|
|
|
type DelayHistory struct {
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Delay uint16 `json:"delay"`
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:51:30 +08:00
|
|
|
type DelayHistoryStoreType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
OriginalHistory DelayHistoryStoreType = iota
|
|
|
|
ExtraHistory
|
|
|
|
DropHistory
|
|
|
|
)
|
|
|
|
|
2019-03-16 00:43:16 +08:00
|
|
|
type Proxy interface {
|
|
|
|
ProxyAdapter
|
|
|
|
Alive() bool
|
2023-06-04 11:51:30 +08:00
|
|
|
AliveForTestUrl(url string) bool
|
2019-03-17 14:52:39 +08:00
|
|
|
DelayHistory() []DelayHistory
|
2023-06-04 11:51:30 +08:00
|
|
|
ExtraDelayHistory() map[string][]DelayHistory
|
2019-03-17 14:52:39 +08:00
|
|
|
LastDelay() uint16
|
2023-06-04 11:51:30 +08:00
|
|
|
LastDelayForTestUrl(url string) uint16
|
|
|
|
URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16], store DelayHistoryStoreType) (uint16, error)
|
2021-10-15 21:44:53 +08:00
|
|
|
|
|
|
|
// Deprecated: use DialContext instead.
|
|
|
|
Dial(metadata *Metadata) (Conn, error)
|
|
|
|
|
|
|
|
// Deprecated: use DialPacketConn instead.
|
|
|
|
DialUDP(metadata *Metadata) (PacketConn, error)
|
2019-03-16 00:43:16 +08:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:28:38 +08: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"
|
2023-11-18 13:17:15 +08:00
|
|
|
case RejectDrop:
|
|
|
|
return "RejectDrop"
|
2022-01-18 21:09:36 +08:00
|
|
|
case Compatible:
|
|
|
|
return "Compatible"
|
2022-03-27 23:44:51 +08:00
|
|
|
case Pass:
|
|
|
|
return "Pass"
|
2018-07-12 23:28:38 +08:00
|
|
|
case Shadowsocks:
|
|
|
|
return "Shadowsocks"
|
2020-07-22 23:02:15 +08:00
|
|
|
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:
|
2023-06-19 14:28:06 +08:00
|
|
|
return "Socks5"
|
2018-12-03 23:27:00 +08:00
|
|
|
case Http:
|
2023-06-19 14:28:06 +08:00
|
|
|
return "Http"
|
2018-09-06 10:53:29 +08:00
|
|
|
case Vmess:
|
2023-06-19 14:28:06 +08:00
|
|
|
return "Vmess"
|
2021-11-17 16:03:47 +08:00
|
|
|
case Vless:
|
2023-06-19 14:28:06 +08:00
|
|
|
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"
|
2023-09-21 10:28:28 +08:00
|
|
|
case Hysteria2:
|
|
|
|
return "Hysteria2"
|
2022-11-09 18:44:06 +08:00
|
|
|
case WireGuard:
|
|
|
|
return "WireGuard"
|
2022-11-25 08:08:14 +08:00
|
|
|
case Tuic:
|
2023-06-19 14:28:06 +08:00
|
|
|
return "Tuic"
|
2020-03-19 20:26:53 +08:00
|
|
|
|
2020-03-21 23:46:49 +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
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
default:
|
2019-08-26 12:26:14 +08:00
|
|
|
return "Unknown"
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-28 18:44:01 +08: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 19:56:02 +08:00
|
|
|
// - if addr is not provided, WriteBack will write out UDP packet with SourceIP/Port equals to original Target,
|
2019-12-28 18:44:01 +08:00
|
|
|
// this is important when using Fake-IP.
|
2023-06-03 21:40:09 +08:00
|
|
|
WriteBack
|
2019-12-28 18:44:01 +08:00
|
|
|
|
2020-04-16 18:19:36 +08:00
|
|
|
// Drop call after packet is used, could recycle buffer in this function.
|
|
|
|
Drop()
|
2019-12-28 18:44:01 +08:00
|
|
|
|
|
|
|
// LocalAddr returns the source IP/Port of packet
|
|
|
|
LocalAddr() net.Addr
|
|
|
|
}
|
2022-11-11 23:36:06 +08:00
|
|
|
|
|
|
|
type UDPPacketInAddr interface {
|
|
|
|
InAddr() net.Addr
|
|
|
|
}
|
2022-12-04 14:37:52 +08:00
|
|
|
|
|
|
|
// PacketAdapter is a UDP Packet adapter for socks/redir/tun
|
|
|
|
type PacketAdapter interface {
|
|
|
|
UDPPacket
|
|
|
|
Metadata() *Metadata
|
|
|
|
}
|
2023-02-17 16:31:15 +08:00
|
|
|
|
2023-10-11 10:55:12 +08: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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-03 21:40:09 +08:00
|
|
|
type WriteBack interface {
|
|
|
|
WriteBack(b []byte, addr net.Addr) (n int, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type WriteBackProxy interface {
|
|
|
|
WriteBack
|
|
|
|
UpdateWriteBack(wb WriteBack)
|
|
|
|
}
|
|
|
|
|
2023-02-17 16:31:15 +08:00
|
|
|
type NatTable interface {
|
2023-06-03 21:40:09 +08:00
|
|
|
Set(key string, e PacketConn, w WriteBackProxy)
|
2023-02-17 16:31:15 +08:00
|
|
|
|
2023-06-03 21:40:09 +08:00
|
|
|
Get(key string) (PacketConn, WriteBackProxy)
|
2023-02-17 16:31:15 +08:00
|
|
|
|
|
|
|
GetOrCreateLock(key string) (*sync.Cond, bool)
|
|
|
|
|
|
|
|
Delete(key string)
|
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
DeleteLock(key string)
|
2023-02-17 16:31:15 +08:00
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
GetForLocalConn(lAddr, rAddr string) *net.UDPConn
|
2023-02-17 16:31:15 +08:00
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
AddForLocalConn(lAddr, rAddr string, conn *net.UDPConn) bool
|
2023-02-17 16:31:15 +08:00
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
RangeForLocalConn(lAddr string, f func(key string, value *net.UDPConn) bool)
|
2023-02-17 16:31:15 +08:00
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
GetOrCreateLockForLocalConn(lAddr string, key string) (*sync.Cond, bool)
|
|
|
|
|
|
|
|
DeleteForLocalConn(lAddr, key string)
|
|
|
|
|
|
|
|
DeleteLockForLocalConn(lAddr, key string)
|
2023-02-17 16:31:15 +08:00
|
|
|
}
|