2019-12-08 04:17:24 +00:00
|
|
|
package outbound
|
2018-09-06 02:53:29 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-12 15:55:39 +00:00
|
|
|
"context"
|
2020-02-17 09:34:19 +00:00
|
|
|
"errors"
|
2018-09-06 02:53:29 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-02-15 13:42:46 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2020-02-17 09:34:19 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2018-09-30 04:25:52 +00:00
|
|
|
"github.com/Dreamacro/clash/component/vmess"
|
2018-09-06 02:53:29 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Vmess struct {
|
2018-12-22 15:56:42 +00:00
|
|
|
*Base
|
2018-09-06 02:53:29 +00:00
|
|
|
client *vmess.Client
|
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
type VmessOption struct {
|
2018-12-10 16:25:05 +00:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
UUID string `proxy:"uuid"`
|
|
|
|
AlterID int `proxy:"alterId"`
|
|
|
|
Cipher string `proxy:"cipher"`
|
|
|
|
TLS bool `proxy:"tls,omitempty"`
|
2019-04-25 08:32:15 +00:00
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
2018-12-10 16:25:05 +00:00
|
|
|
Network string `proxy:"network,omitempty"`
|
|
|
|
WSPath string `proxy:"ws-path,omitempty"`
|
|
|
|
WSHeaders map[string]string `proxy:"ws-headers,omitempty"`
|
|
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
2018-10-02 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 15:46:49 +00:00
|
|
|
func (v *Vmess) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|
|
|
return v.client.New(c, parseVmessAddr(metadata))
|
|
|
|
}
|
|
|
|
|
2019-10-12 15:55:39 +00:00
|
|
|
func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
2020-03-21 15:46:49 +00:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
2018-09-06 02:53:29 +00:00
|
|
|
if err != nil {
|
2020-03-21 15:46:49 +00:00
|
|
|
return nil, fmt.Errorf("%s connect error", v.addr)
|
2018-09-06 02:53:29 +00:00
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
2020-03-21 15:46:49 +00:00
|
|
|
|
|
|
|
c, err = v.StreamConn(c, metadata)
|
|
|
|
return NewConn(c, v), err
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 06:43:54 +00:00
|
|
|
func (v *Vmess) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
2020-02-17 09:34:19 +00:00
|
|
|
// vmess use stream-oriented udp, so clash needs a net.UDPAddr
|
|
|
|
if !metadata.Resolved() {
|
|
|
|
ip, err := resolver.ResolveIP(metadata.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't resolve ip")
|
|
|
|
}
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
|
|
|
|
2019-10-12 15:55:39 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), tcpTimeout)
|
|
|
|
defer cancel()
|
2020-03-21 15:46:49 +00:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
2019-04-25 08:32:15 +00:00
|
|
|
if err != nil {
|
2020-03-21 15:46:49 +00:00
|
|
|
return nil, fmt.Errorf("%s connect error", v.addr)
|
2019-04-25 08:32:15 +00:00
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
c, err = v.client.New(c, parseVmessAddr(metadata))
|
2019-07-25 09:47:39 +00:00
|
|
|
if err != nil {
|
2020-01-31 06:43:54 +00:00
|
|
|
return nil, fmt.Errorf("new vmess client error: %v", err)
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
2020-02-17 09:34:19 +00:00
|
|
|
return newPacketConn(&vmessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}, v), nil
|
2019-04-25 08:32:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
func NewVmess(option VmessOption) (*Vmess, error) {
|
|
|
|
security := strings.ToLower(option.Cipher)
|
2018-09-06 02:53:29 +00:00
|
|
|
client, err := vmess.NewClient(vmess.Config{
|
2018-12-10 16:25:05 +00:00
|
|
|
UUID: option.UUID,
|
|
|
|
AlterID: uint16(option.AlterID),
|
|
|
|
Security: security,
|
|
|
|
TLS: option.TLS,
|
|
|
|
HostName: option.Server,
|
|
|
|
Port: strconv.Itoa(option.Port),
|
|
|
|
NetWork: option.Network,
|
|
|
|
WebSocketPath: option.WSPath,
|
|
|
|
WebSocketHeaders: option.WSHeaders,
|
|
|
|
SkipCertVerify: option.SkipCertVerify,
|
2019-07-29 04:25:29 +00:00
|
|
|
SessionCache: getClientSessionCache(),
|
2018-09-06 02:53:29 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-08 11:53:24 +00:00
|
|
|
|
2018-09-06 02:53:29 +00:00
|
|
|
return &Vmess{
|
2018-12-22 15:56:42 +00:00
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
2020-03-21 15:46:49 +00:00
|
|
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
2018-12-22 15:56:42 +00:00
|
|
|
tp: C.Vmess,
|
2019-07-25 09:47:39 +00:00
|
|
|
udp: true,
|
2018-12-22 15:56:42 +00:00
|
|
|
},
|
2018-09-06 02:53:29 +00:00
|
|
|
client: client,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
func parseVmessAddr(metadata *C.Metadata) *vmess.DstAddr {
|
2018-09-06 02:53:29 +00:00
|
|
|
var addrType byte
|
|
|
|
var addr []byte
|
2018-09-30 04:25:52 +00:00
|
|
|
switch metadata.AddrType {
|
2018-09-06 02:53:29 +00:00
|
|
|
case C.AtypIPv4:
|
|
|
|
addrType = byte(vmess.AtypIPv4)
|
|
|
|
addr = make([]byte, net.IPv4len)
|
2019-05-09 13:00:29 +00:00
|
|
|
copy(addr[:], metadata.DstIP.To4())
|
2018-09-06 02:53:29 +00:00
|
|
|
case C.AtypIPv6:
|
|
|
|
addrType = byte(vmess.AtypIPv6)
|
|
|
|
addr = make([]byte, net.IPv6len)
|
2019-05-09 13:00:29 +00:00
|
|
|
copy(addr[:], metadata.DstIP.To16())
|
2018-09-06 02:53:29 +00:00
|
|
|
case C.AtypDomainName:
|
|
|
|
addrType = byte(vmess.AtypDomainName)
|
2018-09-30 04:25:52 +00:00
|
|
|
addr = make([]byte, len(metadata.Host)+1)
|
|
|
|
addr[0] = byte(len(metadata.Host))
|
|
|
|
copy(addr[1:], []byte(metadata.Host))
|
2018-09-06 02:53:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 13:00:29 +00:00
|
|
|
port, _ := strconv.Atoi(metadata.DstPort)
|
2018-09-06 02:53:29 +00:00
|
|
|
return &vmess.DstAddr{
|
2019-04-25 08:32:15 +00:00
|
|
|
UDP: metadata.NetWork == C.UDP,
|
2018-09-06 02:53:29 +00:00
|
|
|
AddrType: addrType,
|
|
|
|
Addr: addr,
|
|
|
|
Port: uint(port),
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 12:11:18 +00:00
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
type vmessPacketConn struct {
|
2019-10-11 12:11:18 +00:00
|
|
|
net.Conn
|
2020-02-12 05:12:07 +00:00
|
|
|
rAddr net.Addr
|
2019-10-11 12:11:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
func (uc *vmessPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
2019-10-11 12:11:18 +00:00
|
|
|
return uc.Conn.Write(b)
|
|
|
|
}
|
|
|
|
|
2020-02-17 09:34:19 +00:00
|
|
|
func (uc *vmessPacketConn) WriteWithMetadata(p []byte, metadata *C.Metadata) (n int, err error) {
|
|
|
|
return uc.Conn.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uc *vmessPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
2019-10-11 12:11:18 +00:00
|
|
|
n, err := uc.Conn.Read(b)
|
2020-02-12 05:12:07 +00:00
|
|
|
return n, uc.rAddr, err
|
2019-10-11 12:11:18 +00:00
|
|
|
}
|