2019-12-08 12:17:24 +08:00
|
|
|
package outbound
|
2019-10-09 18:46:23 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-12 23:55:39 +08:00
|
|
|
"context"
|
2019-10-09 18:46:23 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/structure"
|
2020-02-15 21:42:46 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2019-10-09 18:46:23 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 22:18:49 +08:00
|
|
|
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
|
|
|
"github.com/Dreamacro/clash/transport/snell"
|
2019-10-09 18:46:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Snell struct {
|
|
|
|
*Base
|
|
|
|
psk []byte
|
2020-09-21 00:33:13 +08:00
|
|
|
pool *snell.Pool
|
2019-10-09 18:46:23 +08:00
|
|
|
obfsOption *simpleObfsOption
|
2020-09-21 00:33:13 +08:00
|
|
|
version int
|
2019-10-09 18:46:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type SnellOption struct {
|
2021-11-07 16:48:51 +08:00
|
|
|
BasicOption
|
2022-03-16 12:10:13 +08:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
Psk string `proxy:"psk"`
|
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
|
|
|
Version int `proxy:"version,omitempty"`
|
|
|
|
ObfsOpts map[string]any `proxy:"obfs-opts,omitempty"`
|
2019-10-09 18:46:23 +08:00
|
|
|
}
|
|
|
|
|
2020-09-21 00:33:13 +08:00
|
|
|
type streamOption struct {
|
|
|
|
psk []byte
|
|
|
|
version int
|
|
|
|
addr string
|
|
|
|
obfsOption *simpleObfsOption
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamConn(c net.Conn, option streamOption) *snell.Snell {
|
|
|
|
switch option.obfsOption.Mode {
|
2019-10-09 18:46:23 +08:00
|
|
|
case "tls":
|
2020-09-21 00:33:13 +08:00
|
|
|
c = obfs.NewTLSObfs(c, option.obfsOption.Host)
|
2019-10-09 18:46:23 +08:00
|
|
|
case "http":
|
2020-09-21 00:33:13 +08:00
|
|
|
_, port, _ := net.SplitHostPort(option.addr)
|
|
|
|
c = obfs.NewHTTPObfs(c, option.obfsOption.Host, port)
|
2019-10-09 18:46:23 +08:00
|
|
|
}
|
2020-09-21 00:33:13 +08:00
|
|
|
return snell.StreamConn(c, option.psk, option.version)
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// StreamConn implements C.ProxyAdapter
|
2020-09-21 00:33:13 +08:00
|
|
|
func (s *Snell) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|
|
|
c = streamConn(c, streamOption{s.psk, s.version, s.addr, s.obfsOption})
|
2022-04-30 22:26:38 +08:00
|
|
|
if metadata.NetWork == C.UDP {
|
|
|
|
err := snell.WriteUDPHeader(c, s.version)
|
|
|
|
return c, err
|
|
|
|
}
|
2022-01-02 01:09:29 +08:00
|
|
|
port, _ := strconv.ParseUint(metadata.DstPort, 10, 16)
|
2020-09-21 00:33:13 +08:00
|
|
|
err := snell.WriteHeader(c, metadata.String(), uint(port), s.version)
|
2020-03-21 23:46:49 +08:00
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
|
|
|
if s.version == snell.Version2 && len(opts) == 0 {
|
2020-09-21 00:33:13 +08:00
|
|
|
c, err := s.pool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:31:08 +08:00
|
|
|
port, _ := strconv.ParseUint(metadata.DstPort, 10, 16)
|
2021-03-22 23:26:20 +08:00
|
|
|
if err = snell.WriteHeader(c, metadata.String(), uint(port), s.version); err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-21 00:33:13 +08:00
|
|
|
return NewConn(c, s), err
|
|
|
|
}
|
|
|
|
|
2022-12-20 00:11:02 +08:00
|
|
|
return s.DialContextWithDialer(ctx, dialer.NewDialer(s.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContextWithDialer implements C.ProxyAdapter
|
|
|
|
func (s *Snell) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", s.addr)
|
2020-03-21 23:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", s.addr, err)
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
|
2022-12-16 22:15:44 +08:00
|
|
|
defer func(c net.Conn) {
|
2022-12-13 13:20:40 +08:00
|
|
|
safeConnClose(c, err)
|
2022-12-16 22:15:44 +08:00
|
|
|
}(c)
|
2021-03-22 23:26:20 +08:00
|
|
|
|
2020-03-21 23:46:49 +08:00
|
|
|
c, err = s.StreamConn(c, metadata)
|
|
|
|
return NewConn(c, s), err
|
2019-10-09 18:46:23 +08:00
|
|
|
}
|
|
|
|
|
2022-01-10 20:24:20 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
|
|
|
func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
2022-12-20 00:11:02 +08:00
|
|
|
return s.ListenPacketWithDialer(ctx, dialer.NewDialer(s.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
|
|
func (s *Snell) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (C.PacketConn, error) {
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", s.addr)
|
2022-01-10 20:24:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
c = streamConn(c, streamOption{s.psk, s.version, s.addr, s.obfsOption})
|
|
|
|
|
2022-05-02 06:27:45 +08:00
|
|
|
err = snell.WriteUDPHeader(c, s.version)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pc := snell.PacketConn(c)
|
|
|
|
return newPacketConn(pc, s), nil
|
2022-04-30 11:36:42 +08:00
|
|
|
}
|
|
|
|
|
2022-12-19 21:34:07 +08:00
|
|
|
// SupportWithDialer implements C.ProxyAdapter
|
|
|
|
func (s *Snell) SupportWithDialer() bool {
|
|
|
|
return true
|
2022-01-10 20:24:20 +08:00
|
|
|
}
|
|
|
|
|
2022-04-30 11:36:42 +08:00
|
|
|
// SupportUOT implements C.ProxyAdapter
|
|
|
|
func (s *Snell) SupportUOT() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-09 18:46:23 +08:00
|
|
|
func NewSnell(option SnellOption) (*Snell, error) {
|
2020-03-21 23:46:49 +08:00
|
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
2019-10-09 18:46:23 +08:00
|
|
|
psk := []byte(option.Psk)
|
|
|
|
|
|
|
|
decoder := structure.NewDecoder(structure.Option{TagName: "obfs", WeaklyTypedInput: true})
|
|
|
|
obfsOption := &simpleObfsOption{Host: "bing.com"}
|
|
|
|
if err := decoder.Decode(option.ObfsOpts, obfsOption); err != nil {
|
2020-03-21 23:46:49 +08:00
|
|
|
return nil, fmt.Errorf("snell %s initialize obfs error: %w", addr, err)
|
2019-10-09 18:46:23 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 13:47:56 +08:00
|
|
|
switch obfsOption.Mode {
|
|
|
|
case "tls", "http", "":
|
|
|
|
break
|
|
|
|
default:
|
2020-03-21 23:46:49 +08:00
|
|
|
return nil, fmt.Errorf("snell %s obfs mode error: %s", addr, obfsOption.Mode)
|
2019-10-09 18:46:23 +08:00
|
|
|
}
|
|
|
|
|
2020-09-21 00:33:13 +08:00
|
|
|
// backward compatible
|
|
|
|
if option.Version == 0 {
|
|
|
|
option.Version = snell.DefaultSnellVersion
|
|
|
|
}
|
2022-01-10 20:24:20 +08:00
|
|
|
switch option.Version {
|
|
|
|
case snell.Version1, snell.Version2:
|
|
|
|
if option.UDP {
|
|
|
|
return nil, fmt.Errorf("snell version %d not support UDP", option.Version)
|
|
|
|
}
|
|
|
|
case snell.Version3:
|
|
|
|
default:
|
2020-09-21 00:33:13 +08:00
|
|
|
return nil, fmt.Errorf("snell version error: %d", option.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Snell{
|
2019-10-09 18:46:23 +08:00
|
|
|
Base: &Base{
|
2022-08-28 13:41:19 +08:00
|
|
|
name: option.Name,
|
|
|
|
addr: addr,
|
|
|
|
tp: C.Snell,
|
|
|
|
udp: option.UDP,
|
|
|
|
iface: option.Interface,
|
|
|
|
rmark: option.RoutingMark,
|
|
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
2019-10-09 18:46:23 +08:00
|
|
|
},
|
|
|
|
psk: psk,
|
|
|
|
obfsOption: obfsOption,
|
2020-09-21 00:33:13 +08:00
|
|
|
version: option.Version,
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.Version == snell.Version2 {
|
|
|
|
s.pool = snell.NewPool(func(ctx context.Context) (*snell.Snell, error) {
|
2021-11-07 16:48:51 +08:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", addr, s.Base.DialOptions()...)
|
2020-09-21 00:33:13 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
return streamConn(c, streamOption{psk, option.Version, addr, obfsOption}), nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return s, nil
|
2019-10-09 18:46:23 +08:00
|
|
|
}
|