Feature: support vmess tls mode

This commit is contained in:
Dreamacro 2018-09-08 19:53:24 +08:00
parent 834baa9e27
commit fcb46e7706
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
}
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,

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}