diff --git a/adapters/remote/vmess.go b/adapters/remote/vmess.go index fafe2ca6..e98828c3 100644 --- a/adapters/remote/vmess.go +++ b/adapters/remote/vmess.go @@ -48,16 +48,18 @@ func (ss *Vmess) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { return &VmessAdapter{conn: c}, err } -func NewVmess(name string, server string, uuid string, alterID uint16, security string) (*Vmess, error) { +func NewVmess(name string, server string, uuid string, alterID uint16, security string, option map[string]string) (*Vmess, error) { security = strings.ToLower(security) client, err := vmess.NewClient(vmess.Config{ UUID: uuid, AlterID: alterID, Security: security, + TLS: option["tls"] == "true", }) if err != nil { return nil, err } + return &Vmess{ name: name, server: server, diff --git a/common/vmess/vmess.go b/common/vmess/vmess.go index 464b6c66..8b294b94 100644 --- a/common/vmess/vmess.go +++ b/common/vmess/vmess.go @@ -1,6 +1,7 @@ package vmess import ( + "crypto/tls" "fmt" "math/rand" "net" @@ -35,6 +36,10 @@ var CipherMapping = map[string]byte{ "chacha20-poly1305": SecurityCHACHA20POLY1305, } +var tlsConfig = &tls.Config{ + InsecureSkipVerify: true, +} + // Command types const ( CommandTCP byte = 1 @@ -60,6 +65,7 @@ type Client struct { user []*ID uuid *uuid.UUID security Security + tls bool } // Config of vmess @@ -67,11 +73,15 @@ type Config struct { UUID string AlterID uint16 Security string + TLS bool } // New return a Conn with net.Conn and DstAddr func (c *Client) New(conn net.Conn, dst *DstAddr) net.Conn { r := rand.Intn(len(c.user)) + if c.tls { + conn = tls.Client(conn, tlsConfig) + } return newConn(conn, c.user[r], dst, c.security) } @@ -102,5 +112,6 @@ func NewClient(config Config) (*Client, error) { user: newAlterIDs(newID(&uid), config.AlterID), uuid: &uid, security: security, + tls: config.TLS, }, nil } diff --git a/config/config.go b/config/config.go index e40f8b03..02a9c105 100644 --- a/config/config.go +++ b/config/config.go @@ -246,7 +246,8 @@ func (c *Config) parseProxies(cfg *ini.File) error { if err != nil { return err } - vmess, err := adapters.NewVmess(key.Name(), addr, proxy[3], uint16(alterID), proxy[5]) + option := parseOptions(6, proxy...) + vmess, err := adapters.NewVmess(key.Name(), addr, proxy[3], uint16(alterID), proxy[5], option) if err != nil { return err } diff --git a/config/utils.go b/config/utils.go index e196394b..b0cec245 100644 --- a/config/utils.go +++ b/config/utils.go @@ -27,3 +27,20 @@ func or(pointers ...*int) *int { } return pointers[len(pointers)-1] } + +func parseOptions(startIdx int, params ...string) map[string]string { + mapping := make(map[string]string) + if len(params) <= startIdx { + return mapping + } + + for _, option := range params[startIdx:] { + pair := strings.SplitN(option, "=", 2) + if len(pair) != 2 { + continue + } + + mapping[strings.Trim(pair[0], " ")] = strings.Trim(pair[1], " ") + } + return mapping +}