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"
|
2019-03-17 06:52:39 +00:00
|
|
|
"time"
|
2018-06-10 14:50:03 +00:00
|
|
|
)
|
|
|
|
|
2018-07-12 15:28:38 +00:00
|
|
|
// Adapter Type
|
|
|
|
const (
|
|
|
|
Direct AdapterType = iota
|
|
|
|
Reject
|
2020-03-19 12:26:53 +00:00
|
|
|
|
2018-07-12 15:28:38 +00:00
|
|
|
Shadowsocks
|
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
|
2020-03-19 12:26:53 +00:00
|
|
|
Trojan
|
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
Relay
|
2020-03-19 12:26:53 +00:00
|
|
|
Selector
|
|
|
|
Fallback
|
|
|
|
URLTest
|
2019-02-15 06:25:20 +00:00
|
|
|
LoadBalance
|
2018-07-12 15:28:38 +00:00
|
|
|
)
|
|
|
|
|
2018-06-10 14:50:03 +00:00
|
|
|
type ServerAdapter interface {
|
2019-04-23 15:29:36 +00:00
|
|
|
net.Conn
|
2018-09-30 04:25:52 +00:00
|
|
|
Metadata() *Metadata
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:28:37 +00:00
|
|
|
type Connection interface {
|
|
|
|
Chains() Chain
|
|
|
|
AppendToChains(adapter ProxyAdapter)
|
|
|
|
}
|
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
2020-03-21 15:46:49 +00:00
|
|
|
StreamConn(c net.Conn, metadata *Metadata) (net.Conn, error)
|
2019-10-12 15:55:39 +00:00
|
|
|
DialContext(ctx context.Context, metadata *Metadata) (Conn, error)
|
2020-01-31 06:43:54 +00:00
|
|
|
DialUDP(metadata *Metadata) (PacketConn, error)
|
2019-04-23 15:29:36 +00:00
|
|
|
SupportUDP() bool
|
2018-11-21 05:47:46 +00:00
|
|
|
MarshalJSON() ([]byte, error)
|
2020-03-21 15:46:49 +00:00
|
|
|
Addr() string
|
2020-05-07 13:42:52 +00:00
|
|
|
// Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract.
|
|
|
|
Unwrap(metadata *Metadata) Proxy
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2018-07-12 15:28:38 +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
|
2019-10-12 15:55:39 +00:00
|
|
|
Dial(metadata *Metadata) (Conn, error)
|
2019-03-17 06:52:39 +00:00
|
|
|
LastDelay() uint16
|
2019-07-02 11:18:03 +00:00
|
|
|
URLTest(ctx context.Context, url string) (uint16, 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"
|
2020-03-19 12:26:53 +00:00
|
|
|
|
2018-07-12 15:28:38 +00:00
|
|
|
case Shadowsocks:
|
|
|
|
return "Shadowsocks"
|
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"
|
2020-03-19 12:26:53 +00:00
|
|
|
case Trojan:
|
|
|
|
return "Trojan"
|
|
|
|
|
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
|
|
|
|
// - if addr is not provided, WriteBack will wirte out UDP packet with SourceIP/Prot equals to origional Target,
|
|
|
|
// 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
|
|
|
|
}
|