glider/proxy/vmess/client.go

291 lines
5.9 KiB
Go
Raw Normal View History

2018-07-03 00:31:43 +08:00
package vmess
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"encoding/binary"
"errors"
"hash/fnv"
"io"
"math/rand"
"net"
"strings"
2018-07-03 00:31:43 +08:00
"time"
2018-07-11 00:26:05 +08:00
"golang.org/x/crypto/chacha20poly1305"
2018-07-03 00:31:43 +08:00
)
// Request Options
const (
2018-07-11 00:26:05 +08:00
OptBasicFormat byte = 0
OptChunkStream byte = 1
// OptReuseTCPConnection byte = 2
// OptMetadataObfuscate byte = 4
2018-07-03 00:31:43 +08:00
)
// Security types
2018-07-03 00:31:43 +08:00
const (
SecurityAES128GCM byte = 3
SecurityChacha20Poly1305 byte = 4
SecurityNone byte = 5
2018-07-03 00:31:43 +08:00
)
// CMD types
const (
CmdTCP byte = 1
CmdUDP byte = 2
)
// Client vmess client
type Client struct {
users []*User
count int
2018-07-09 23:42:33 +08:00
opt byte
security byte
2018-07-03 14:30:56 +08:00
}
// Conn is a connection to vmess server
type Conn struct {
user *User
2018-07-09 23:42:33 +08:00
opt byte
security byte
2018-07-03 14:30:56 +08:00
2018-07-07 11:07:38 +08:00
atyp Atyp
addr Addr
port Port
2018-07-03 00:31:43 +08:00
reqBodyIV [16]byte
reqBodyKey [16]byte
reqRespV byte
respBodyIV [16]byte
2018-07-11 00:26:05 +08:00
respBodyKey [16]byte
2018-07-03 00:31:43 +08:00
net.Conn
2018-07-09 23:42:33 +08:00
dataReader io.Reader
dataWriter io.Writer
2018-07-03 00:31:43 +08:00
}
// NewClient .
func NewClient(uuidStr, security string, alterID int) (*Client, error) {
2018-07-03 14:30:56 +08:00
uuid, err := StrToUUID(uuidStr)
2018-07-03 00:31:43 +08:00
if err != nil {
return nil, err
}
2018-07-03 14:30:56 +08:00
c := &Client{}
user := NewUser(uuid)
c.users = append(c.users, user)
c.users = append(c.users, user.GenAlterIDUsers(alterID)...)
c.count = len(c.users)
2018-07-10 00:53:15 +08:00
c.opt = OptChunkStream
2018-07-09 23:42:33 +08:00
security = strings.ToLower(security)
switch security {
case "aes-128-gcm":
c.security = SecurityAES128GCM
case "chacha20-poly1305":
c.security = SecurityChacha20Poly1305
2018-07-11 00:26:05 +08:00
case "none":
c.security = SecurityNone
case "":
// NOTE: use basic format when no method specified
c.opt = OptBasicFormat
c.security = SecurityNone
2018-07-11 00:26:05 +08:00
default:
return nil, errors.New("unknown security type: " + security)
}
// NOTE: give rand a new seed to avoid the same sequence of values
rand.Seed(time.Now().UnixNano())
2018-07-03 14:30:56 +08:00
return c, nil
}
// NewConn .
func (c *Client) NewConn(rc net.Conn, target string) (*Conn, error) {
r := rand.Intn(c.count)
2018-07-09 23:42:33 +08:00
conn := &Conn{user: c.users[r], opt: c.opt, security: c.security}
2018-07-03 00:31:43 +08:00
2018-07-03 14:30:56 +08:00
var err error
2018-07-07 11:07:38 +08:00
conn.atyp, conn.addr, conn.port, err = ParseAddr(target)
2018-07-03 00:31:43 +08:00
if err != nil {
return nil, err
}
randBytes := make([]byte, 33)
rand.Read(randBytes)
2018-07-03 14:30:56 +08:00
copy(conn.reqBodyIV[:], randBytes[:16])
copy(conn.reqBodyKey[:], randBytes[16:32])
conn.reqRespV = randBytes[32]
2018-07-03 00:31:43 +08:00
2018-07-03 14:30:56 +08:00
conn.respBodyIV = md5.Sum(conn.reqBodyIV[:])
conn.respBodyKey = md5.Sum(conn.reqBodyKey[:])
2018-07-03 00:31:43 +08:00
2018-07-03 14:30:56 +08:00
// AuthInfo
rc.Write(conn.EncodeAuthInfo())
// Request
req, err := conn.EncodeRequest()
if err != nil {
return nil, err
}
rc.Write(req)
conn.Conn = rc
return conn, nil
2018-07-03 00:31:43 +08:00
}
// EncodeAuthInfo returns HMAC("md5", UUID, UTC) result
2018-07-03 14:30:56 +08:00
func (c *Conn) EncodeAuthInfo() []byte {
2018-07-03 00:31:43 +08:00
ts := make([]byte, 8)
binary.BigEndian.PutUint64(ts, uint64(time.Now().UTC().Unix()))
h := hmac.New(md5.New, c.user.UUID[:])
h.Write(ts)
return h.Sum(nil)
}
// EncodeRequest encodes requests to network bytes
2018-07-03 14:30:56 +08:00
func (c *Conn) EncodeRequest() ([]byte, error) {
2018-07-03 00:31:43 +08:00
buf := new(bytes.Buffer)
// Request
buf.WriteByte(1) // Ver
buf.Write(c.reqBodyIV[:]) // IV
buf.Write(c.reqBodyKey[:]) // Key
buf.WriteByte(c.reqRespV) // V
2018-07-09 23:42:33 +08:00
buf.WriteByte(c.opt) // Opt
2018-07-03 00:31:43 +08:00
// pLen and Sec
paddingLen := rand.Intn(16)
pSec := byte(paddingLen<<4) | c.security // P(4bit) and Sec(4bit)
2018-07-03 00:31:43 +08:00
buf.WriteByte(pSec)
buf.WriteByte(0) // reserved
buf.WriteByte(CmdTCP) // cmd
// target
binary.Write(buf, binary.BigEndian, uint16(c.port)) // port
2018-07-07 11:07:38 +08:00
buf.WriteByte(byte(c.atyp)) // atyp
2018-07-03 00:31:43 +08:00
buf.Write(c.addr) // addr
// padding
if paddingLen > 0 {
padding := make([]byte, paddingLen)
rand.Read(padding)
buf.Write(padding)
}
// F
fnv1a := fnv.New32a()
fnv1a.Write(buf.Bytes())
buf.Write(fnv1a.Sum(nil))
2018-07-03 01:07:28 +08:00
block, err := aes.NewCipher(c.user.CmdKey[:])
2018-07-03 00:31:43 +08:00
if err != nil {
return nil, err
}
2018-07-03 01:07:28 +08:00
stream := cipher.NewCFBEncrypter(block, TimestampHash(time.Now().UTC()))
2018-07-03 00:31:43 +08:00
stream.XORKeyStream(buf.Bytes(), buf.Bytes())
return buf.Bytes(), nil
}
// DecodeRespHeader .
2018-07-03 14:30:56 +08:00
func (c *Conn) DecodeRespHeader() error {
2018-07-03 00:31:43 +08:00
block, err := aes.NewCipher(c.respBodyKey[:])
if err != nil {
return err
}
stream := cipher.NewCFBDecrypter(block, c.respBodyIV[:])
buf := make([]byte, 4)
io.ReadFull(c.Conn, buf)
stream.XORKeyStream(buf, buf)
if buf[0] != c.reqRespV {
return errors.New("unexpected response header")
}
2018-07-05 20:44:19 +08:00
// TODO: Dynamic port support
if buf[2] != 0 {
// dataLen := int32(buf[3])
return errors.New("dynamic port is not supported now")
}
2018-07-03 00:31:43 +08:00
return nil
}
2018-07-11 00:26:05 +08:00
func (c *Conn) Write(b []byte) (n int, err error) {
if c.dataWriter != nil {
return c.dataWriter.Write(b)
2018-07-03 00:31:43 +08:00
}
2018-07-11 00:26:05 +08:00
c.dataWriter = c.Conn
if c.opt&OptChunkStream == OptChunkStream {
switch c.security {
case SecurityNone:
c.dataWriter = ChunkedWriter(c.Conn)
case SecurityAES128GCM:
block, _ := aes.NewCipher(c.reqBodyKey[:])
aead, _ := cipher.NewGCM(block)
c.dataWriter = AEADWriter(c.Conn, aead, c.reqBodyIV[:])
case SecurityChacha20Poly1305:
2018-07-11 08:23:26 +08:00
key := make([]byte, 32)
t := md5.Sum(c.reqBodyKey[:])
copy(key, t[:])
t = md5.Sum(key[:16])
copy(key[16:], t[:])
2018-07-11 00:26:05 +08:00
aead, _ := chacha20poly1305.New(key)
c.dataWriter = AEADWriter(c.Conn, aead, c.reqBodyIV[:])
2018-07-09 23:42:33 +08:00
}
2018-07-11 00:26:05 +08:00
}
2018-07-09 23:42:33 +08:00
2018-07-11 00:26:05 +08:00
return c.dataWriter.Write(b)
}
func (c *Conn) Read(b []byte) (n int, err error) {
if c.dataReader != nil {
2018-07-09 23:42:33 +08:00
return c.dataReader.Read(b)
}
2018-07-11 00:26:05 +08:00
err = c.DecodeRespHeader()
if err != nil {
return 0, err
}
2018-07-03 00:31:43 +08:00
2018-07-11 00:26:05 +08:00
c.dataReader = c.Conn
if c.opt&OptChunkStream == OptChunkStream {
switch c.security {
case SecurityNone:
c.dataReader = ChunkedReader(c.Conn)
case SecurityAES128GCM:
block, _ := aes.NewCipher(c.respBodyKey[:])
aead, _ := cipher.NewGCM(block)
c.dataReader = AEADReader(c.Conn, aead, c.respBodyIV[:])
case SecurityChacha20Poly1305:
2018-07-11 08:23:26 +08:00
key := make([]byte, 32)
t := md5.Sum(c.respBodyKey[:])
copy(key, t[:])
t = md5.Sum(key[:16])
copy(key[16:], t[:])
2018-07-11 00:26:05 +08:00
aead, _ := chacha20poly1305.New(key)
c.dataReader = AEADReader(c.Conn, aead, c.respBodyIV[:])
2018-07-09 23:42:33 +08:00
}
}
2018-07-11 00:26:05 +08:00
return c.dataReader.Read(b)
2018-07-03 00:31:43 +08:00
}