2018-06-10 22:50:03 +08:00
|
|
|
package constant
|
|
|
|
|
|
|
|
import (
|
2019-07-02 19:18:03 +08:00
|
|
|
"context"
|
2019-08-09 01:28:37 +08:00
|
|
|
"fmt"
|
2018-06-14 01:00:58 +08:00
|
|
|
"net"
|
2019-03-17 14:52:39 +08:00
|
|
|
"time"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
// Adapter Type
|
|
|
|
const (
|
|
|
|
Direct AdapterType = iota
|
2018-09-26 00:34:15 +08:00
|
|
|
Fallback
|
2018-07-12 23:28:38 +08:00
|
|
|
Reject
|
|
|
|
Selector
|
|
|
|
Shadowsocks
|
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-07-12 23:28:38 +08:00
|
|
|
URLTest
|
2018-09-06 10:53:29 +08:00
|
|
|
Vmess
|
2019-02-15 14:25:20 +08:00
|
|
|
LoadBalance
|
2018-07-12 23:28:38 +08:00
|
|
|
)
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
type ServerAdapter interface {
|
2019-04-23 23:29:36 +08:00
|
|
|
net.Conn
|
2018-09-30 12:25:52 +08:00
|
|
|
Metadata() *Metadata
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08: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
|
|
|
|
}
|
|
|
|
|
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
|
2019-10-12 23:55:39 +08:00
|
|
|
DialContext(ctx context.Context, metadata *Metadata) (Conn, error)
|
2019-08-09 01:28:37 +08:00
|
|
|
DialUDP(metadata *Metadata) (PacketConn, net.Addr, error)
|
2019-04-23 23:29:36 +08:00
|
|
|
SupportUDP() bool
|
2018-11-21 13:47:46 +08:00
|
|
|
MarshalJSON() ([]byte, error)
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2018-07-12 23:28:38 +08:00
|
|
|
|
2019-03-17 14:52:39 +08:00
|
|
|
type DelayHistory struct {
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Delay uint16 `json:"delay"`
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:43:16 +08:00
|
|
|
type Proxy interface {
|
|
|
|
ProxyAdapter
|
|
|
|
Alive() bool
|
2019-03-17 14:52:39 +08:00
|
|
|
DelayHistory() []DelayHistory
|
2019-10-12 23:55:39 +08:00
|
|
|
Dial(metadata *Metadata) (Conn, error)
|
2019-03-17 14:52:39 +08:00
|
|
|
LastDelay() uint16
|
2019-07-02 19:18:03 +08:00
|
|
|
URLTest(ctx context.Context, url string) (uint16, 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"
|
2018-09-26 00:34:15 +08:00
|
|
|
case Fallback:
|
|
|
|
return "Fallback"
|
2018-07-12 23:28:38 +08:00
|
|
|
case Reject:
|
|
|
|
return "Reject"
|
|
|
|
case Selector:
|
|
|
|
return "Selector"
|
|
|
|
case Shadowsocks:
|
|
|
|
return "Shadowsocks"
|
2019-10-09 18:46:23 +08:00
|
|
|
case Snell:
|
|
|
|
return "Snell"
|
2018-08-12 13:50:54 +08:00
|
|
|
case Socks5:
|
|
|
|
return "Socks5"
|
2018-12-03 23:27:00 +08:00
|
|
|
case Http:
|
|
|
|
return "Http"
|
2018-07-12 23:28:38 +08:00
|
|
|
case URLTest:
|
|
|
|
return "URLTest"
|
2018-09-06 10:53:29 +08:00
|
|
|
case Vmess:
|
|
|
|
return "Vmess"
|
2019-02-15 14:25:20 +08:00
|
|
|
case LoadBalance:
|
|
|
|
return "LoadBalance"
|
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
|
|
|
}
|
|
|
|
}
|