mihomo/adapter/outbound/base.go

198 lines
4.1 KiB
Go
Raw Normal View History

2021-06-10 14:05:56 +08:00
package outbound
import (
"context"
2021-06-10 14:05:56 +08:00
"encoding/json"
"errors"
2022-05-17 16:47:21 +08:00
"github.com/gofrs/uuid"
2021-06-10 14:05:56 +08:00
"net"
"strings"
2021-06-10 14:05:56 +08:00
"github.com/Dreamacro/clash/component/dialer"
2021-06-10 14:05:56 +08:00
C "github.com/Dreamacro/clash/constant"
)
type Base struct {
name string
addr string
iface string
tp C.AdapterType
udp bool
2021-11-08 16:59:48 +08:00
rmark int
2022-05-17 16:47:21 +08:00
id string
2021-06-10 14:05:56 +08:00
}
// Name implements C.ProxyAdapter
func (b *Base) Name() string {
return b.name
}
2022-05-17 16:47:21 +08: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 14:05:56 +08: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")
}
// ListenPacketContext implements C.ProxyAdapter
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
2021-06-10 14:05:56 +08:00
return nil, errors.New("no support")
}
// 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 14:05:56 +08:00
// SupportUDP implements C.ProxyAdapter
func (b *Base) SupportUDP() bool {
return b.udp
}
// MarshalJSON implements C.ProxyAdapter
func (b *Base) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
"type": b.Type().String(),
2022-05-17 16:47:21 +08:00
"id": b.Id(),
2021-06-10 14:05:56 +08:00
})
}
// Addr implements C.ProxyAdapter
func (b *Base) Addr() string {
return b.addr
}
// Unwrap implements C.ProxyAdapter
func (b *Base) Unwrap(metadata *C.Metadata) C.Proxy {
return nil
}
// 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 16:59:48 +08:00
if b.rmark != 0 {
opts = append(opts, dialer.WithRoutingMark(b.rmark))
}
return opts
}
type BasicOption struct {
2021-11-08 16:59:48 +08:00
Interface string `proxy:"interface-name,omitempty" group:"interface-name,omitempty"`
RoutingMark int `proxy:"routing-mark,omitempty" group:"routing-mark,omitempty"`
}
type BaseOption struct {
2021-11-08 16:59:48 +08:00
Name string
Addr string
Type C.AdapterType
UDP bool
Interface string
RoutingMark int
}
func NewBase(opt BaseOption) *Base {
return &Base{
name: opt.Name,
addr: opt.Addr,
tp: opt.Type,
udp: opt.UDP,
iface: opt.Interface,
2021-11-08 16:59:48 +08:00
rmark: opt.RoutingMark,
}
2021-06-10 14:05:56 +08:00
}
type conn struct {
net.Conn
chain C.Chain
actualRemoteDestination string
2022-05-26 23:41:09 +08:00
}
func (c *conn) RemoteDestination() string {
return c.actualRemoteDestination
2021-06-10 14:05:56 +08: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 {
var remoteDestination string
2022-05-27 20:33:27 +08:00
if c != nil {
if tcpAddr, ok := c.RemoteAddr().(*net.TCPAddr); ok {
remoteDestination = tcpAddr.IP.String()
} else {
remoteDestination = parseRemoteDestination(a.Addr())
}
}
return &conn{c, []string{a.Name()}, remoteDestination}
2021-06-10 14:05:56 +08:00
}
type packetConn struct {
net.PacketConn
chain C.Chain
actualRemoteDestination string
2022-05-26 23:41:09 +08:00
}
func (c *packetConn) RemoteDestination() string {
return c.actualRemoteDestination
2021-06-10 14:05:56 +08: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 {
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 14:05:56 +08:00
}