2019-12-08 04:17:24 +00:00
|
|
|
package outbound
|
2018-08-12 05:50:54 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-12 15:55:39 +00:00
|
|
|
"context"
|
2018-10-28 11:46:49 +00:00
|
|
|
"crypto/tls"
|
2020-03-02 15:47:23 +00:00
|
|
|
"errors"
|
2018-08-12 05:50:54 +00:00
|
|
|
"fmt"
|
2019-07-25 09:47:39 +00:00
|
|
|
"io"
|
2018-08-12 05:50:54 +00:00
|
|
|
"net"
|
2018-11-04 13:12:16 +00:00
|
|
|
"strconv"
|
2018-08-12 05:50:54 +00:00
|
|
|
|
2020-02-09 09:02:48 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2023-01-13 01:55:01 +00:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
2018-08-12 05:50:54 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2018-08-12 05:50:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Socks5 struct {
|
2018-12-22 15:56:42 +00:00
|
|
|
*Base
|
2018-11-09 09:36:30 +00:00
|
|
|
user string
|
|
|
|
pass string
|
2018-10-29 12:16:43 +00:00
|
|
|
tls bool
|
|
|
|
skipCertVerify bool
|
2018-11-01 03:54:45 +00:00
|
|
|
tlsConfig *tls.Config
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
2022-04-22 10:55:28 +00:00
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
type Socks5Option struct {
|
2021-11-08 08:59:48 +00:00
|
|
|
BasicOption
|
2018-10-29 12:16:43 +00:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
2018-11-09 09:36:30 +00:00
|
|
|
UserName string `proxy:"username,omitempty"`
|
|
|
|
Password string `proxy:"password,omitempty"`
|
2018-10-29 12:16:43 +00:00
|
|
|
TLS bool `proxy:"tls,omitempty"`
|
2019-04-23 15:29:36 +00:00
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
2018-10-29 12:16:43 +00:00
|
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
2022-07-11 05:42:28 +00:00
|
|
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
2018-10-02 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// StreamConn implements C.ProxyAdapter
|
2020-03-21 15:46:49 +00:00
|
|
|
func (ss *Socks5) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|
|
|
if ss.tls {
|
2018-11-01 03:54:45 +00:00
|
|
|
cc := tls.Client(c, ss.tlsConfig)
|
2022-08-13 04:35:39 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err := cc.HandshakeContext(ctx)
|
2018-11-01 03:54:45 +00:00
|
|
|
c = cc
|
2020-03-21 15:46:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
|
|
|
}
|
2018-10-28 11:46:49 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
var user *socks5.User
|
|
|
|
if ss.user != "" {
|
|
|
|
user = &socks5.User{
|
|
|
|
Username: ss.user,
|
|
|
|
Password: ss.pass,
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 09:47:39 +00:00
|
|
|
if _, err := socks5.ClientHandshake(c, serializesSocksAddr(metadata), socks5.CmdConnect, user); err != nil {
|
2018-08-12 05:50:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-21 15:46:49 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
2022-12-19 16:11:02 +00:00
|
|
|
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContextWithDialer implements C.ProxyAdapter
|
|
|
|
func (ss *Socks5) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", ss.addr)
|
2020-03-21 15:46:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
|
2022-12-16 14:15:44 +00:00
|
|
|
defer func(c net.Conn) {
|
2022-12-13 05:20:40 +00:00
|
|
|
safeConnClose(c, err)
|
2022-12-16 14:15:44 +00:00
|
|
|
}(c)
|
2021-03-22 15:26:20 +00:00
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
c, err = ss.StreamConn(c, metadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewConn(c, ss), nil
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 13:34:07 +00:00
|
|
|
// SupportWithDialer implements C.ProxyAdapter
|
|
|
|
func (ss *Socks5) SupportWithDialer() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:44:53 +00:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", ss.addr, ss.Base.DialOptions(opts...)...)
|
2019-07-25 09:47:39 +00:00
|
|
|
if err != nil {
|
2019-12-05 09:51:21 +00:00
|
|
|
err = fmt.Errorf("%s connect error: %w", ss.addr, err)
|
2019-07-25 09:47:39 +00:00
|
|
|
return
|
|
|
|
}
|
2018-08-12 05:50:54 +00:00
|
|
|
|
2019-07-25 09:47:39 +00:00
|
|
|
if ss.tls {
|
2019-04-25 05:48:47 +00:00
|
|
|
cc := tls.Client(c, ss.tlsConfig)
|
2022-08-13 04:35:39 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err = cc.HandshakeContext(ctx)
|
2019-04-25 05:48:47 +00:00
|
|
|
c = cc
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 14:15:44 +00:00
|
|
|
defer func(c net.Conn) {
|
2022-12-13 05:20:40 +00:00
|
|
|
safeConnClose(c, err)
|
2022-12-16 14:15:44 +00:00
|
|
|
}(c)
|
2019-07-25 09:47:39 +00:00
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
tcpKeepAlive(c)
|
|
|
|
var user *socks5.User
|
|
|
|
if ss.user != "" {
|
|
|
|
user = &socks5.User{
|
|
|
|
Username: ss.user,
|
|
|
|
Password: ss.pass,
|
2018-11-09 09:36:30 +00:00
|
|
|
}
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 09:47:39 +00:00
|
|
|
bindAddr, err := socks5.ClientHandshake(c, serializesSocksAddr(metadata), socks5.CmdUDPAssociate, user)
|
|
|
|
if err != nil {
|
2019-12-05 09:51:21 +00:00
|
|
|
err = fmt.Errorf("client hanshake error: %w", err)
|
2019-07-25 09:47:39 +00:00
|
|
|
return
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
2019-07-25 09:47:39 +00:00
|
|
|
|
2020-12-31 10:58:03 +00:00
|
|
|
// Support unspecified UDP bind address.
|
|
|
|
bindUDPAddr := bindAddr.UDPAddr()
|
|
|
|
if bindUDPAddr == nil {
|
|
|
|
err = errors.New("invalid UDP bind address")
|
|
|
|
return
|
|
|
|
} else if bindUDPAddr.IP.IsUnspecified() {
|
2022-11-12 05:18:36 +00:00
|
|
|
serverAddr, err := resolveUDPAddr(ctx, "udp", ss.Addr())
|
2020-12-31 10:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bindUDPAddr.IP = serverAddr.IP
|
|
|
|
}
|
|
|
|
|
2022-12-13 03:18:32 +00:00
|
|
|
pc, err := dialer.ListenPacket(ctx, dialer.ParseNetwork("udp", bindUDPAddr.AddrPort().Addr()), "", ss.Base.DialOptions(opts...)...)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
io.Copy(io.Discard, c)
|
|
|
|
c.Close()
|
|
|
|
// A UDP association terminates when the TCP connection that the UDP
|
|
|
|
// ASSOCIATE request arrived on terminates. RFC1928
|
|
|
|
pc.Close()
|
|
|
|
}()
|
|
|
|
|
2020-12-31 10:58:03 +00:00
|
|
|
return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindUDPAddr, tcpConn: c}, ss), nil
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 05:42:28 +00:00
|
|
|
func NewSocks5(option Socks5Option) (*Socks5, error) {
|
2018-11-01 03:54:45 +00:00
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if option.TLS {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: option.SkipCertVerify,
|
|
|
|
ServerName: option.Server,
|
|
|
|
}
|
2022-07-11 05:42:28 +00:00
|
|
|
|
|
|
|
if len(option.Fingerprint) == 0 {
|
2023-01-14 13:08:06 +00:00
|
|
|
tlsConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
2022-07-11 05:42:28 +00:00
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
if tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 03:54:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 05:50:54 +00:00
|
|
|
return &Socks5{
|
2018-12-22 15:56:42 +00:00
|
|
|
Base: &Base{
|
2022-08-28 05:41:19 +00:00
|
|
|
name: option.Name,
|
|
|
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
|
|
|
tp: C.Socks5,
|
|
|
|
udp: option.UDP,
|
2023-02-24 05:53:44 +00:00
|
|
|
tfo: option.TFO,
|
2022-08-28 05:41:19 +00:00
|
|
|
iface: option.Interface,
|
|
|
|
rmark: option.RoutingMark,
|
|
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
2018-12-22 15:56:42 +00:00
|
|
|
},
|
2018-11-09 09:36:30 +00:00
|
|
|
user: option.UserName,
|
|
|
|
pass: option.Password,
|
2018-10-29 12:16:43 +00:00
|
|
|
tls: option.TLS,
|
|
|
|
skipCertVerify: option.SkipCertVerify,
|
2022-07-11 05:42:28 +00:00
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
}, nil
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
2019-07-25 09:47:39 +00:00
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
type socksPacketConn struct {
|
2019-07-25 09:47:39 +00:00
|
|
|
net.PacketConn
|
2020-01-31 06:43:54 +00:00
|
|
|
rAddr net.Addr
|
2019-08-12 06:01:32 +00:00
|
|
|
tcpConn net.Conn
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
func (uc *socksPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
2020-01-31 06:43:54 +00:00
|
|
|
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
|
2019-07-25 09:47:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-31 06:43:54 +00:00
|
|
|
return uc.PacketConn.WriteTo(packet, uc.rAddr)
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
func (uc *socksPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
2020-02-29 17:46:02 +00:00
|
|
|
n, _, e := uc.PacketConn.ReadFrom(b)
|
2019-10-11 12:11:18 +00:00
|
|
|
if e != nil {
|
|
|
|
return 0, nil, e
|
|
|
|
}
|
2019-07-25 09:47:39 +00:00
|
|
|
addr, payload, err := socks5.DecodeUDPPacket(b)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2020-03-02 15:47:23 +00:00
|
|
|
|
|
|
|
udpAddr := addr.UDPAddr()
|
|
|
|
if udpAddr == nil {
|
|
|
|
return 0, nil, errors.New("parse udp addr error")
|
|
|
|
}
|
|
|
|
|
2019-10-11 12:11:18 +00:00
|
|
|
// due to DecodeUDPPacket is mutable, record addr length
|
2019-07-25 09:47:39 +00:00
|
|
|
copy(b, payload)
|
2020-03-02 15:47:23 +00:00
|
|
|
return n - len(addr) - 3, udpAddr, nil
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
2019-08-12 06:01:32 +00:00
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
func (uc *socksPacketConn) Close() error {
|
2019-08-12 06:01:32 +00:00
|
|
|
uc.tcpConn.Close()
|
|
|
|
return uc.PacketConn.Close()
|
|
|
|
}
|