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"
|
2021-03-18 11:40:34 +00:00
|
|
|
"crypto/tls"
|
2020-02-17 09:34:19 +00:00
|
|
|
"errors"
|
2018-09-06 02:53:29 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2020-03-31 08:07:21 +00:00
|
|
|
"net/http"
|
2018-09-06 02:53:29 +00:00
|
|
|
"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-06 02:53:29 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/gun"
|
|
|
|
"github.com/Dreamacro/clash/transport/vmess"
|
2021-03-18 11:40:34 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/http2"
|
2018-09-06 02:53:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Vmess struct {
|
2018-12-22 15:56:42 +00:00
|
|
|
*Base
|
2018-09-06 02:53:29 +00:00
|
|
|
client *vmess.Client
|
2020-03-31 08:07:21 +00:00
|
|
|
option *VmessOption
|
2021-03-18 11:40:34 +00:00
|
|
|
|
|
|
|
// for gun mux
|
|
|
|
gunTLSConfig *tls.Config
|
|
|
|
gunConfig *gun.Config
|
|
|
|
transport *http2.Transport
|
2018-09-06 02:53:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:36 +00:00
|
|
|
type VmessOption struct {
|
2021-11-07 08:48:51 +00:00
|
|
|
BasicOption
|
2021-10-05 04:42:21 +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"`
|
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
|
|
|
Network string `proxy:"network,omitempty"`
|
|
|
|
TLS bool `proxy:"tls,omitempty"`
|
|
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
|
|
|
ServerName string `proxy:"servername,omitempty"`
|
|
|
|
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
|
|
|
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
|
|
|
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
|
|
|
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
|
|
|
|
|
|
|
// TODO: remove these until 2022
|
|
|
|
WSHeaders map[string]string `proxy:"ws-headers,omitempty"`
|
|
|
|
WSPath string `proxy:"ws-path,omitempty"`
|
2018-10-02 07:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 08:07:21 +00:00
|
|
|
type HTTPOptions struct {
|
|
|
|
Method string `proxy:"method,omitempty"`
|
|
|
|
Path []string `proxy:"path,omitempty"`
|
|
|
|
Headers map[string][]string `proxy:"headers,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-09-26 12:33:57 +00:00
|
|
|
type HTTP2Options struct {
|
|
|
|
Host []string `proxy:"host,omitempty"`
|
|
|
|
Path string `proxy:"path,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-03-18 11:40:34 +00:00
|
|
|
type GrpcOptions struct {
|
|
|
|
GrpcServiceName string `proxy:"grpc-service-name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:25:29 +00:00
|
|
|
type WSOptions struct {
|
|
|
|
Path string `proxy:"path,omitempty"`
|
|
|
|
Headers map[string]string `proxy:"headers,omitempty"`
|
|
|
|
MaxEarlyData int `proxy:"max-early-data,omitempty"`
|
|
|
|
EarlyDataHeaderName string `proxy:"early-data-header-name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// StreamConn implements C.ProxyAdapter
|
2020-03-21 15:46:49 +00:00
|
|
|
func (v *Vmess) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
2020-03-31 08:07:21 +00:00
|
|
|
var err error
|
|
|
|
switch v.option.Network {
|
|
|
|
case "ws":
|
2021-08-21 16:25:29 +00:00
|
|
|
if v.option.WSOpts.Path == "" {
|
|
|
|
v.option.WSOpts.Path = v.option.WSPath
|
|
|
|
}
|
|
|
|
if len(v.option.WSOpts.Headers) == 0 {
|
|
|
|
v.option.WSOpts.Headers = v.option.WSHeaders
|
|
|
|
}
|
|
|
|
|
2020-03-31 08:07:21 +00:00
|
|
|
host, port, _ := net.SplitHostPort(v.addr)
|
|
|
|
wsOpts := &vmess.WebsocketConfig{
|
2021-08-21 16:25:29 +00:00
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
Path: v.option.WSOpts.Path,
|
|
|
|
MaxEarlyData: v.option.WSOpts.MaxEarlyData,
|
|
|
|
EarlyDataHeaderName: v.option.WSOpts.EarlyDataHeaderName,
|
2020-03-31 08:07:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 16:25:29 +00:00
|
|
|
if len(v.option.WSOpts.Headers) != 0 {
|
2020-03-31 08:07:21 +00:00
|
|
|
header := http.Header{}
|
2021-09-08 06:44:24 +00:00
|
|
|
for key, value := range v.option.WSOpts.Headers {
|
2020-03-31 08:07:21 +00:00
|
|
|
header.Add(key, value)
|
|
|
|
}
|
|
|
|
wsOpts.Headers = header
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.option.TLS {
|
|
|
|
wsOpts.TLS = true
|
2021-10-16 12:19:59 +00:00
|
|
|
wsOpts.TLSConfig = &tls.Config{
|
|
|
|
ServerName: host,
|
|
|
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
|
|
|
NextProtos: []string{"http/1.1"},
|
|
|
|
}
|
|
|
|
if v.option.ServerName != "" {
|
|
|
|
wsOpts.TLSConfig.ServerName = v.option.ServerName
|
|
|
|
} else if host := wsOpts.Headers.Get("Host"); host != "" {
|
|
|
|
wsOpts.TLSConfig.ServerName = host
|
|
|
|
}
|
2020-03-31 08:07:21 +00:00
|
|
|
}
|
|
|
|
c, err = vmess.StreamWebsocketConn(c, wsOpts)
|
|
|
|
case "http":
|
2020-09-04 13:27:19 +00:00
|
|
|
// readability first, so just copy default TLS logic
|
|
|
|
if v.option.TLS {
|
|
|
|
host, _, _ := net.SplitHostPort(v.addr)
|
|
|
|
tlsOpts := &vmess.TLSConfig{
|
|
|
|
Host: host,
|
|
|
|
SkipCertVerify: v.option.SkipCertVerify,
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.option.ServerName != "" {
|
|
|
|
tlsOpts.Host = v.option.ServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = vmess.StreamTLSConn(c, tlsOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 08:07:21 +00:00
|
|
|
host, _, _ := net.SplitHostPort(v.addr)
|
|
|
|
httpOpts := &vmess.HTTPConfig{
|
|
|
|
Host: host,
|
|
|
|
Method: v.option.HTTPOpts.Method,
|
|
|
|
Path: v.option.HTTPOpts.Path,
|
|
|
|
Headers: v.option.HTTPOpts.Headers,
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:04:24 +00:00
|
|
|
c = vmess.StreamHTTPConn(c, httpOpts)
|
2020-09-26 12:33:57 +00:00
|
|
|
case "h2":
|
|
|
|
host, _, _ := net.SplitHostPort(v.addr)
|
|
|
|
tlsOpts := vmess.TLSConfig{
|
|
|
|
Host: host,
|
|
|
|
SkipCertVerify: v.option.SkipCertVerify,
|
|
|
|
NextProtos: []string{"h2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.option.ServerName != "" {
|
|
|
|
tlsOpts.Host = v.option.ServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = vmess.StreamTLSConn(c, &tlsOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
h2Opts := &vmess.H2Config{
|
|
|
|
Hosts: v.option.HTTP2Opts.Host,
|
|
|
|
Path: v.option.HTTP2Opts.Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = vmess.StreamH2Conn(c, h2Opts)
|
2021-03-18 11:40:34 +00:00
|
|
|
case "grpc":
|
|
|
|
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig)
|
2020-04-03 08:04:24 +00:00
|
|
|
default:
|
|
|
|
// handle TLS
|
|
|
|
if v.option.TLS {
|
|
|
|
host, _, _ := net.SplitHostPort(v.addr)
|
|
|
|
tlsOpts := &vmess.TLSConfig{
|
|
|
|
Host: host,
|
|
|
|
SkipCertVerify: v.option.SkipCertVerify,
|
|
|
|
}
|
2020-05-31 16:27:04 +00:00
|
|
|
|
|
|
|
if v.option.ServerName != "" {
|
|
|
|
tlsOpts.Host = v.option.ServerName
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:04:24 +00:00
|
|
|
c, err = vmess.StreamTLSConn(c, tlsOpts)
|
|
|
|
}
|
2020-03-31 08:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.client.StreamConn(c, parseVmessAddr(metadata))
|
2020-03-21 15:46:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
2021-03-22 15:26:20 +00:00
|
|
|
// gun transport
|
2021-11-07 08:48:51 +00:00
|
|
|
if v.transport != nil && len(opts) == 0 {
|
2021-03-18 11:40:34 +00:00
|
|
|
c, err := gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-22 15:26:20 +00:00
|
|
|
defer safeConnClose(c, err)
|
2021-03-18 11:40:34 +00:00
|
|
|
|
|
|
|
c, err = v.client.StreamConn(c, parseVmessAddr(metadata))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewConn(c, v), nil
|
|
|
|
}
|
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", v.addr, v.Base.DialOptions(opts...)...)
|
2018-09-06 02:53:29 +00:00
|
|
|
if err != nil {
|
2020-06-11 14:07:20 +00:00
|
|
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
2018-09-06 02:53:29 +00:00
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
2021-03-22 15:26:20 +00:00
|
|
|
defer safeConnClose(c, err)
|
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
|
|
|
}
|
|
|
|
|
2021-10-15 13:44:53 +00:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
2021-03-18 11:40:34 +00:00
|
|
|
// vmess use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
2020-02-17 09:34:19 +00:00
|
|
|
if !metadata.Resolved() {
|
|
|
|
ip, err := resolver.ResolveIP(metadata.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't resolve ip")
|
|
|
|
}
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
|
|
|
|
2021-03-22 15:26:20 +00:00
|
|
|
var c net.Conn
|
|
|
|
// gun transport
|
2021-11-07 08:48:51 +00:00
|
|
|
if v.transport != nil && len(opts) == 0 {
|
2021-03-22 15:26:20 +00:00
|
|
|
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
2021-03-18 11:40:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-22 15:26:20 +00:00
|
|
|
defer safeConnClose(c, err)
|
2021-03-18 11:40:34 +00:00
|
|
|
|
|
|
|
c, err = v.client.StreamConn(c, parseVmessAddr(metadata))
|
2021-03-22 15:26:20 +00:00
|
|
|
} else {
|
2021-11-07 08:48:51 +00:00
|
|
|
c, err = dialer.DialContext(ctx, "tcp", v.addr, v.Base.DialOptions(opts...)...)
|
2021-03-18 11:40:34 +00:00
|
|
|
if err != nil {
|
2021-03-22 15:26:20 +00:00
|
|
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
2021-03-18 11:40:34 +00:00
|
|
|
}
|
2021-03-22 15:26:20 +00:00
|
|
|
tcpKeepAlive(c)
|
|
|
|
defer safeConnClose(c, err)
|
2021-03-18 11:40:34 +00:00
|
|
|
|
2021-03-22 15:26:20 +00:00
|
|
|
c, err = v.StreamConn(c, metadata)
|
2021-03-18 11:40:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-03-22 15:26:20 +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{
|
2020-03-31 08:07:21 +00:00
|
|
|
UUID: option.UUID,
|
|
|
|
AlterID: uint16(option.AlterID),
|
|
|
|
Security: security,
|
|
|
|
HostName: option.Server,
|
|
|
|
Port: strconv.Itoa(option.Port),
|
2021-03-30 09:34:16 +00:00
|
|
|
IsAead: option.AlterID == 0,
|
2018-09-06 02:53:29 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-18 11:40:34 +00:00
|
|
|
|
|
|
|
switch option.Network {
|
|
|
|
case "h2", "grpc":
|
|
|
|
if !option.TLS {
|
|
|
|
return nil, fmt.Errorf("TLS must be true with h2/grpc network")
|
|
|
|
}
|
2020-09-26 12:33:57 +00:00
|
|
|
}
|
2018-09-08 11:53:24 +00:00
|
|
|
|
2021-03-18 11:40:34 +00:00
|
|
|
v := &Vmess{
|
2018-12-22 15:56:42 +00:00
|
|
|
Base: &Base{
|
2021-11-07 08:48:51 +00:00
|
|
|
name: option.Name,
|
|
|
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
|
|
|
tp: C.Vmess,
|
|
|
|
udp: option.UDP,
|
|
|
|
iface: option.Interface,
|
2018-12-22 15:56:42 +00:00
|
|
|
},
|
2018-09-06 02:53:29 +00:00
|
|
|
client: client,
|
2020-03-31 08:07:21 +00:00
|
|
|
option: &option,
|
2021-03-18 11:40:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 10:15:46 +00:00
|
|
|
switch option.Network {
|
|
|
|
case "h2":
|
|
|
|
if len(option.HTTP2Opts.Host) == 0 {
|
2021-04-10 04:10:10 +00:00
|
|
|
option.HTTP2Opts.Host = append(option.HTTP2Opts.Host, "www.example.com")
|
2021-04-09 10:15:46 +00:00
|
|
|
}
|
|
|
|
case "grpc":
|
2021-03-18 11:40:34 +00:00
|
|
|
dialFn := func(network, addr string) (net.Conn, error) {
|
2021-11-07 08:48:51 +00:00
|
|
|
c, err := dialer.DialContext(context.Background(), "tcp", v.addr, v.Base.DialOptions()...)
|
2021-03-18 11:40:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gunConfig := &gun.Config{
|
|
|
|
ServiceName: v.option.GrpcOpts.GrpcServiceName,
|
|
|
|
Host: v.option.ServerName,
|
|
|
|
}
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
|
|
|
ServerName: v.option.ServerName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.option.ServerName == "" {
|
|
|
|
host, _, _ := net.SplitHostPort(v.addr)
|
|
|
|
tlsConfig.ServerName = host
|
|
|
|
gunConfig.Host = host
|
|
|
|
}
|
|
|
|
|
|
|
|
v.gunTLSConfig = tlsConfig
|
|
|
|
v.gunConfig = gunConfig
|
|
|
|
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
2018-09-06 02:53:29 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-07 16:31:08 +00:00
|
|
|
port, _ := strconv.ParseUint(metadata.DstPort, 10, 16)
|
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) 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
|
|
|
}
|