2018-08-12 05:50:54 +00:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2018-10-28 11:46:49 +00:00
|
|
|
"crypto/tls"
|
2018-08-12 05:50:54 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-11-04 13:12:16 +00:00
|
|
|
"strconv"
|
2018-08-12 05:50:54 +00:00
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
"github.com/Dreamacro/clash/component/socks5"
|
2018-08-12 05:50:54 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Socks5 struct {
|
2018-12-22 15:56:42 +00:00
|
|
|
*Base
|
2018-10-29 12:16:43 +00:00
|
|
|
addr string
|
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
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
type Socks5Option struct {
|
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"`
|
2018-10-02 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:59:07 +00:00
|
|
|
func (ss *Socks5) Dial(metadata *C.Metadata) (net.Conn, error) {
|
2019-06-28 16:58:59 +00:00
|
|
|
c, err := dialTimeout("tcp", ss.addr, tcpTimeout)
|
2018-10-28 11:46:49 +00:00
|
|
|
|
|
|
|
if err == nil && ss.tls {
|
2018-11-01 03:54:45 +00:00
|
|
|
cc := tls.Client(c, ss.tlsConfig)
|
|
|
|
err = cc.Handshake()
|
|
|
|
c = cc
|
2018-10-28 11:46:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 05:50:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error", ss.addr)
|
|
|
|
}
|
2018-08-29 07:00:12 +00:00
|
|
|
tcpKeepAlive(c)
|
2019-04-25 05:48:47 +00: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 05:50:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-22 15:56:42 +00:00
|
|
|
return c, nil
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
func (ss *Socks5) DialUDP(metadata *C.Metadata) (net.PacketConn, net.Addr, error) {
|
2019-06-28 16:58:59 +00:00
|
|
|
c, err := dialTimeout("tcp", ss.addr, tcpTimeout)
|
2018-08-12 05:50:54 +00:00
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
if err == nil && ss.tls {
|
|
|
|
cc := tls.Client(c, ss.tlsConfig)
|
|
|
|
err = cc.Handshake()
|
|
|
|
c = cc
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("%s connect error", ss.addr)
|
2018-11-09 09:36:30 +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-04-25 05:48:47 +00:00
|
|
|
if err := socks5.ClientHandshake(c, serializesSocksAddr(metadata), socks5.CmdUDPAssociate, user); err != nil {
|
|
|
|
return nil, nil, err
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
2019-04-25 05:48:47 +00:00
|
|
|
return &fakeUDPConn{Conn: c}, c.LocalAddr(), nil
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
func NewSocks5(option Socks5Option) *Socks5 {
|
2018-11-01 03:54:45 +00:00
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if option.TLS {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: option.SkipCertVerify,
|
|
|
|
ClientSessionCache: getClientSessionCache(),
|
|
|
|
ServerName: option.Server,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 05:50:54 +00:00
|
|
|
return &Socks5{
|
2018-12-22 15:56:42 +00:00
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
tp: C.Socks5,
|
2019-04-23 15:29:36 +00:00
|
|
|
udp: option.UDP,
|
2018-12-22 15:56:42 +00:00
|
|
|
},
|
2018-11-04 13:12:16 +00:00
|
|
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
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,
|
2018-11-01 03:54:45 +00:00
|
|
|
tlsConfig: tlsConfig,
|
2018-08-12 05:50:54 +00:00
|
|
|
}
|
|
|
|
}
|