mihomo/adapters/outbound/socks5.go

173 lines
3.8 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outbound
2018-08-12 13:50:54 +08:00
import (
"context"
"crypto/tls"
2018-08-12 13:50:54 +08:00
"fmt"
"io"
"io/ioutil"
2018-08-12 13:50:54 +08:00
"net"
2018-11-04 21:12:16 +08:00
"strconv"
2018-08-12 13:50:54 +08:00
2019-04-25 13:48:47 +08:00
"github.com/Dreamacro/clash/component/socks5"
2018-08-12 13:50:54 +08:00
C "github.com/Dreamacro/clash/constant"
)
type Socks5 struct {
2018-12-22 23:56:42 +08:00
*Base
addr string
user string
pass string
tls bool
skipCertVerify bool
tlsConfig *tls.Config
2018-08-12 13:50:54 +08:00
}
type Socks5Option struct {
Name string `proxy:"name"`
Server string `proxy:"server"`
Port int `proxy:"port"`
UserName string `proxy:"username,omitempty"`
Password string `proxy:"password,omitempty"`
TLS bool `proxy:"tls,omitempty"`
2019-04-23 23:29:36 +08:00
UDP bool `proxy:"udp,omitempty"`
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
}
func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
c, err := dialContext(ctx, "tcp", ss.addr)
if err == nil && ss.tls {
cc := tls.Client(c, ss.tlsConfig)
err = cc.Handshake()
c = cc
}
2018-08-12 13:50:54 +08:00
if err != nil {
2019-12-05 17:51:21 +08:00
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
2018-08-12 13:50:54 +08:00
}
tcpKeepAlive(c)
2019-04-25 13:48:47 +08:00
var user *socks5.User
if ss.user != "" {
user = &socks5.User{
Username: ss.user,
Password: ss.pass,
}
}
if _, err := socks5.ClientHandshake(c, serializesSocksAddr(metadata), socks5.CmdConnect, user); err != nil {
2018-08-12 13:50:54 +08:00
return nil, err
}
return newConn(c, ss), nil
2018-11-21 13:47:46 +08:00
}
2020-01-31 14:43:54 +08:00
func (ss *Socks5) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) {
ctx, cancel := context.WithTimeout(context.Background(), tcpTimeout)
defer cancel()
c, err := dialContext(ctx, "tcp", ss.addr)
if err != nil {
2019-12-05 17:51:21 +08:00
err = fmt.Errorf("%s connect error: %w", ss.addr, err)
return
}
2018-08-12 13:50:54 +08:00
if ss.tls {
2019-04-25 13:48:47 +08:00
cc := tls.Client(c, ss.tlsConfig)
err = cc.Handshake()
c = cc
2018-08-12 13:50:54 +08:00
}
defer func() {
if err != nil {
c.Close()
}
}()
2019-04-25 13:48:47 +08:00
tcpKeepAlive(c)
var user *socks5.User
if ss.user != "" {
user = &socks5.User{
Username: ss.user,
Password: ss.pass,
}
2018-08-12 13:50:54 +08:00
}
bindAddr, err := socks5.ClientHandshake(c, serializesSocksAddr(metadata), socks5.CmdUDPAssociate, user)
if err != nil {
2019-12-05 17:51:21 +08:00
err = fmt.Errorf("client hanshake error: %w", err)
return
2018-08-12 13:50:54 +08:00
}
pc, err := net.ListenPacket("udp", "")
if err != nil {
return
}
go func() {
io.Copy(ioutil.Discard, c)
c.Close()
// A UDP association terminates when the TCP connection that the UDP
// ASSOCIATE request arrived on terminates. RFC1928
pc.Close()
}()
2020-01-31 14:43:54 +08:00
return newPacketConn(&socksUDPConn{PacketConn: pc, rAddr: bindAddr.UDPAddr(), tcpConn: c}, ss), nil
2018-08-12 13:50:54 +08:00
}
func NewSocks5(option Socks5Option) *Socks5 {
var tlsConfig *tls.Config
if option.TLS {
tlsConfig = &tls.Config{
InsecureSkipVerify: option.SkipCertVerify,
ClientSessionCache: getClientSessionCache(),
ServerName: option.Server,
}
}
2018-08-12 13:50:54 +08:00
return &Socks5{
2018-12-22 23:56:42 +08:00
Base: &Base{
name: option.Name,
tp: C.Socks5,
2019-04-23 23:29:36 +08:00
udp: option.UDP,
2018-12-22 23:56:42 +08:00
},
2018-11-04 21:12:16 +08:00
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
user: option.UserName,
pass: option.Password,
tls: option.TLS,
skipCertVerify: option.SkipCertVerify,
tlsConfig: tlsConfig,
2018-08-12 13:50:54 +08:00
}
}
type socksUDPConn struct {
net.PacketConn
2020-01-31 14:43:54 +08:00
rAddr net.Addr
tcpConn net.Conn
}
func (uc *socksUDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
2020-01-31 14:43:54 +08:00
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
if err != nil {
return
}
2020-01-31 14:43:54 +08:00
return uc.PacketConn.WriteTo(packet, uc.rAddr)
}
func (uc *socksUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, a, e := uc.PacketConn.ReadFrom(b)
2019-10-11 20:11:18 +08:00
if e != nil {
return 0, nil, e
}
addr, payload, err := socks5.DecodeUDPPacket(b)
if err != nil {
return 0, nil, err
}
2019-10-11 20:11:18 +08:00
// due to DecodeUDPPacket is mutable, record addr length
addrLength := len(addr)
copy(b, payload)
2019-10-11 20:11:18 +08:00
return n - addrLength - 3, a, nil
}
func (uc *socksUDPConn) Close() error {
uc.tcpConn.Close()
return uc.PacketConn.Close()
}