2021-06-10 06:05:56 +00:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
2021-10-15 13:44:53 +00:00
|
|
|
"context"
|
2021-06-10 06:05:56 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-05-17 08:47:21 +00:00
|
|
|
"github.com/gofrs/uuid"
|
2021-06-10 06:05:56 +00:00
|
|
|
"net"
|
2022-05-27 00:58:36 +00:00
|
|
|
"strings"
|
2021-06-10 06:05:56 +00:00
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2021-06-10 06:05:56 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Base struct {
|
2022-08-28 05:41:19 +00:00
|
|
|
name string
|
|
|
|
addr string
|
|
|
|
iface string
|
|
|
|
tp C.AdapterType
|
|
|
|
udp bool
|
2022-11-28 11:52:55 +00:00
|
|
|
tfo bool
|
2022-08-28 05:41:19 +00:00
|
|
|
rmark int
|
|
|
|
id string
|
|
|
|
prefer C.DNSPrefer
|
2021-06-10 06:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements C.ProxyAdapter
|
|
|
|
func (b *Base) Name() string {
|
|
|
|
return b.name
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:47:21 +00:00
|
|
|
// Id implements C.ProxyAdapter
|
|
|
|
func (b *Base) Id() string {
|
|
|
|
if b.id == "" {
|
|
|
|
id, err := uuid.NewV6()
|
|
|
|
if err != nil {
|
|
|
|
b.id = b.name
|
|
|
|
} else {
|
|
|
|
b.id = id.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.id
|
|
|
|
}
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
// Type implements C.ProxyAdapter
|
|
|
|
func (b *Base) Type() C.AdapterType {
|
|
|
|
return b.tp
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamConn implements C.ProxyAdapter
|
|
|
|
func (b *Base) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|
|
|
return c, errors.New("no support")
|
|
|
|
}
|
|
|
|
|
2022-05-30 13:55:09 +00:00
|
|
|
func (b *Base) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
|
|
|
return nil, errors.New("no support")
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:44:53 +00:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
2021-06-10 06:05:56 +00:00
|
|
|
return nil, errors.New("no support")
|
|
|
|
}
|
|
|
|
|
2022-04-30 03:36:42 +00:00
|
|
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
|
|
|
func (b *Base) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
|
|
return nil, errors.New("no support")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SupportUOT implements C.ProxyAdapter
|
|
|
|
func (b *Base) SupportUOT() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
// SupportUDP implements C.ProxyAdapter
|
|
|
|
func (b *Base) SupportUDP() bool {
|
|
|
|
return b.udp
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:52:55 +00:00
|
|
|
// SupportTFO implements C.ProxyAdapter
|
|
|
|
func (b *Base) SupportTFO() bool {
|
|
|
|
return b.tfo
|
|
|
|
}
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
// MarshalJSON implements C.ProxyAdapter
|
|
|
|
func (b *Base) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]string{
|
|
|
|
"type": b.Type().String(),
|
2022-05-17 08:47:21 +00:00
|
|
|
"id": b.Id(),
|
2021-06-10 06:05:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr implements C.ProxyAdapter
|
|
|
|
func (b *Base) Addr() string {
|
|
|
|
return b.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap implements C.ProxyAdapter
|
2022-10-30 15:08:18 +00:00
|
|
|
func (b *Base) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
2021-06-10 06:05:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
// DialOptions return []dialer.Option from struct
|
|
|
|
func (b *Base) DialOptions(opts ...dialer.Option) []dialer.Option {
|
|
|
|
if b.iface != "" {
|
|
|
|
opts = append(opts, dialer.WithInterface(b.iface))
|
|
|
|
}
|
|
|
|
|
2021-11-08 08:59:48 +00:00
|
|
|
if b.rmark != 0 {
|
|
|
|
opts = append(opts, dialer.WithRoutingMark(b.rmark))
|
|
|
|
}
|
|
|
|
|
2022-08-28 05:41:19 +00:00
|
|
|
switch b.prefer {
|
|
|
|
case C.IPv4Only:
|
|
|
|
opts = append(opts, dialer.WithOnlySingleStack(true))
|
|
|
|
case C.IPv6Only:
|
|
|
|
opts = append(opts, dialer.WithOnlySingleStack(false))
|
|
|
|
case C.IPv4Prefer:
|
|
|
|
opts = append(opts, dialer.WithPreferIPv4())
|
|
|
|
case C.IPv6Prefer:
|
|
|
|
opts = append(opts, dialer.WithPreferIPv6())
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
type BasicOption struct {
|
2021-11-08 08:59:48 +00:00
|
|
|
Interface string `proxy:"interface-name,omitempty" group:"interface-name,omitempty"`
|
|
|
|
RoutingMark int `proxy:"routing-mark,omitempty" group:"routing-mark,omitempty"`
|
2022-08-28 05:41:19 +00:00
|
|
|
IPVersion string `proxy:"ip-version,omitempty" group:"ip-version,omitempty"`
|
2021-11-07 08:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BaseOption struct {
|
2021-11-08 08:59:48 +00:00
|
|
|
Name string
|
|
|
|
Addr string
|
|
|
|
Type C.AdapterType
|
|
|
|
UDP bool
|
2022-11-28 11:52:55 +00:00
|
|
|
TFO bool
|
2021-11-08 08:59:48 +00:00
|
|
|
Interface string
|
|
|
|
RoutingMark int
|
2022-08-28 05:41:19 +00:00
|
|
|
Prefer C.DNSPrefer
|
2021-11-07 08:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBase(opt BaseOption) *Base {
|
|
|
|
return &Base{
|
2022-08-28 05:41:19 +00:00
|
|
|
name: opt.Name,
|
|
|
|
addr: opt.Addr,
|
|
|
|
tp: opt.Type,
|
|
|
|
udp: opt.UDP,
|
2022-11-28 11:52:55 +00:00
|
|
|
tfo: opt.TFO,
|
2022-08-28 05:41:19 +00:00
|
|
|
iface: opt.Interface,
|
|
|
|
rmark: opt.RoutingMark,
|
|
|
|
prefer: opt.Prefer,
|
2021-11-07 08:48:51 +00:00
|
|
|
}
|
2021-06-10 06:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type conn struct {
|
|
|
|
net.Conn
|
2022-05-27 00:58:36 +00:00
|
|
|
chain C.Chain
|
|
|
|
actualRemoteDestination string
|
2022-05-26 15:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) RemoteDestination() string {
|
2022-05-27 00:58:36 +00:00
|
|
|
return c.actualRemoteDestination
|
2021-06-10 06:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chains implements C.Connection
|
|
|
|
func (c *conn) Chains() C.Chain {
|
|
|
|
return c.chain
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendToChains implements C.Connection
|
|
|
|
func (c *conn) AppendToChains(a C.ProxyAdapter) {
|
|
|
|
c.chain = append(c.chain, a.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn {
|
2022-05-28 12:00:02 +00:00
|
|
|
return &conn{c, []string{a.Name()}, parseRemoteDestination(a.Addr())}
|
2021-06-10 06:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type packetConn struct {
|
|
|
|
net.PacketConn
|
2022-05-27 00:58:36 +00:00
|
|
|
chain C.Chain
|
|
|
|
actualRemoteDestination string
|
2022-05-26 15:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *packetConn) RemoteDestination() string {
|
2022-05-27 00:58:36 +00:00
|
|
|
return c.actualRemoteDestination
|
2021-06-10 06:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chains implements C.Connection
|
|
|
|
func (c *packetConn) Chains() C.Chain {
|
|
|
|
return c.chain
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendToChains implements C.Connection
|
|
|
|
func (c *packetConn) AppendToChains(a C.ProxyAdapter) {
|
|
|
|
c.chain = append(c.chain, a.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn {
|
2022-05-27 00:58:36 +00:00
|
|
|
return &packetConn{pc, []string{a.Name()}, parseRemoteDestination(a.Addr())}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRemoteDestination(addr string) string {
|
|
|
|
if dst, _, err := net.SplitHostPort(addr); err == nil {
|
|
|
|
return dst
|
|
|
|
} else {
|
|
|
|
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
|
|
|
|
return dst
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 06:05:56 +00:00
|
|
|
}
|