2018-09-06 10:53:29 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
"github.com/Dreamacro/clash/component/vmess"
|
2018-09-06 10:53:29 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VmessAdapter is a vmess adapter
|
|
|
|
type VmessAdapter struct {
|
|
|
|
conn net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to close connection
|
|
|
|
func (v *VmessAdapter) Close() {
|
|
|
|
v.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VmessAdapter) Conn() net.Conn {
|
|
|
|
return v.conn
|
|
|
|
}
|
|
|
|
|
|
|
|
type Vmess struct {
|
|
|
|
name string
|
|
|
|
server string
|
|
|
|
client *vmess.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *Vmess) Name() string {
|
|
|
|
return ss.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *Vmess) Type() C.AdapterType {
|
|
|
|
return C.Vmess
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func (ss *Vmess) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
2018-09-06 10:53:29 +08:00
|
|
|
c, err := net.Dial("tcp", ss.server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error", ss.server)
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
2018-09-30 12:25:52 +08:00
|
|
|
c = ss.client.New(c, parseVmessAddr(metadata))
|
2018-09-06 10:53:29 +08:00
|
|
|
return &VmessAdapter{conn: c}, err
|
|
|
|
}
|
|
|
|
|
2018-09-08 19:53:24 +08:00
|
|
|
func NewVmess(name string, server string, uuid string, alterID uint16, security string, option map[string]string) (*Vmess, error) {
|
2018-09-06 10:53:29 +08:00
|
|
|
security = strings.ToLower(security)
|
|
|
|
client, err := vmess.NewClient(vmess.Config{
|
|
|
|
UUID: uuid,
|
|
|
|
AlterID: alterID,
|
|
|
|
Security: security,
|
2018-09-08 19:53:24 +08:00
|
|
|
TLS: option["tls"] == "true",
|
2018-09-06 10:53:29 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-08 19:53:24 +08:00
|
|
|
|
2018-09-06 10:53:29 +08:00
|
|
|
return &Vmess{
|
|
|
|
name: name,
|
|
|
|
server: server,
|
|
|
|
client: client,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func parseVmessAddr(metadata *C.Metadata) *vmess.DstAddr {
|
2018-09-06 10:53:29 +08:00
|
|
|
var addrType byte
|
|
|
|
var addr []byte
|
2018-09-30 12:25:52 +08:00
|
|
|
switch metadata.AddrType {
|
2018-09-06 10:53:29 +08:00
|
|
|
case C.AtypIPv4:
|
|
|
|
addrType = byte(vmess.AtypIPv4)
|
|
|
|
addr = make([]byte, net.IPv4len)
|
2018-09-30 12:25:52 +08:00
|
|
|
copy(addr[:], metadata.IP.To4())
|
2018-09-06 10:53:29 +08:00
|
|
|
case C.AtypIPv6:
|
|
|
|
addrType = byte(vmess.AtypIPv6)
|
|
|
|
addr = make([]byte, net.IPv6len)
|
2018-09-30 12:25:52 +08:00
|
|
|
copy(addr[:], metadata.IP.To16())
|
2018-09-06 10:53:29 +08:00
|
|
|
case C.AtypDomainName:
|
|
|
|
addrType = byte(vmess.AtypDomainName)
|
2018-09-30 12:25:52 +08:00
|
|
|
addr = make([]byte, len(metadata.Host)+1)
|
|
|
|
addr[0] = byte(len(metadata.Host))
|
|
|
|
copy(addr[1:], []byte(metadata.Host))
|
2018-09-06 10:53:29 +08:00
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
port, _ := strconv.Atoi(metadata.Port)
|
2018-09-06 10:53:29 +08:00
|
|
|
return &vmess.DstAddr{
|
|
|
|
AddrType: addrType,
|
|
|
|
Addr: addr,
|
|
|
|
Port: uint(port),
|
|
|
|
}
|
|
|
|
}
|