Improve: add tls, sni options to socks5 outbound adapter

This commit is contained in:
gVisor bot 2018-10-28 19:46:49 +08:00
parent 009b985c49
commit 3042a47f9d

View file

@ -2,6 +2,7 @@ package adapters
import ( import (
"bytes" "bytes"
"crypto/tls"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -29,12 +30,16 @@ func (ss *Socks5Adapter) Conn() net.Conn {
type Socks5 struct { type Socks5 struct {
addr string addr string
name string name string
tls bool
sni bool
} }
type Socks5Option struct { type Socks5Option struct {
Name string `proxy:"name"` Name string `proxy:"name"`
Server string `proxy:"server"` Server string `proxy:"server"`
Port int `proxy:"port"` Port int `proxy:"port"`
TLS bool `proxy:"tls"`
SNI bool `proxy:"sni"`
} }
func (ss *Socks5) Name() string { func (ss *Socks5) Name() string {
@ -47,6 +52,15 @@ func (ss *Socks5) Type() C.AdapterType {
func (ss *Socks5) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { func (ss *Socks5) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
c, err := net.DialTimeout("tcp", ss.addr, tcpTimeout) c, err := net.DialTimeout("tcp", ss.addr, tcpTimeout)
if err == nil && ss.tls {
tlsConfig := tls.Config{
InsecureSkipVerify: ss.sni,
MaxVersion: tls.VersionTLS12,
}
c = tls.Client(c, &tlsConfig)
}
if err != nil { if err != nil {
return nil, fmt.Errorf("%s connect error", ss.addr) return nil, fmt.Errorf("%s connect error", ss.addr)
} }
@ -92,5 +106,7 @@ func NewSocks5(option Socks5Option) *Socks5 {
return &Socks5{ return &Socks5{
addr: fmt.Sprintf("%s:%d", option.Server, option.Port), addr: fmt.Sprintf("%s:%d", option.Server, option.Port),
name: option.Name, name: option.Name,
tls: option.TLS,
sni: option.SNI,
} }
} }