Feature: support vmess tls mode

This commit is contained in:
gVisor bot 2018-09-08 19:53:24 +08:00
parent e5b4e29507
commit 050c236e78
4 changed files with 33 additions and 2 deletions

View file

@ -48,16 +48,18 @@ func (ss *Vmess) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
return &VmessAdapter{conn: c}, err 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) security = strings.ToLower(security)
client, err := vmess.NewClient(vmess.Config{ client, err := vmess.NewClient(vmess.Config{
UUID: uuid, UUID: uuid,
AlterID: alterID, AlterID: alterID,
Security: security, Security: security,
TLS: option["tls"] == "true",
}) })
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Vmess{ return &Vmess{
name: name, name: name,
server: server, server: server,

View file

@ -1,6 +1,7 @@
package vmess package vmess
import ( import (
"crypto/tls"
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
@ -35,6 +36,10 @@ var CipherMapping = map[string]byte{
"chacha20-poly1305": SecurityCHACHA20POLY1305, "chacha20-poly1305": SecurityCHACHA20POLY1305,
} }
var tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
// Command types // Command types
const ( const (
CommandTCP byte = 1 CommandTCP byte = 1
@ -60,6 +65,7 @@ type Client struct {
user []*ID user []*ID
uuid *uuid.UUID uuid *uuid.UUID
security Security security Security
tls bool
} }
// Config of vmess // Config of vmess
@ -67,11 +73,15 @@ type Config struct {
UUID string UUID string
AlterID uint16 AlterID uint16
Security string Security string
TLS bool
} }
// New return a Conn with net.Conn and DstAddr // New return a Conn with net.Conn and DstAddr
func (c *Client) New(conn net.Conn, dst *DstAddr) net.Conn { func (c *Client) New(conn net.Conn, dst *DstAddr) net.Conn {
r := rand.Intn(len(c.user)) r := rand.Intn(len(c.user))
if c.tls {
conn = tls.Client(conn, tlsConfig)
}
return newConn(conn, c.user[r], dst, c.security) 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), user: newAlterIDs(newID(&uid), config.AlterID),
uuid: &uid, uuid: &uid,
security: security, security: security,
tls: config.TLS,
}, nil }, nil
} }

View file

@ -246,7 +246,8 @@ func (c *Config) parseProxies(cfg *ini.File) error {
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }

View file

@ -27,3 +27,20 @@ func or(pointers ...*int) *int {
} }
return pointers[len(pointers)-1] 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
}