2021-11-17 07:00:32 +00:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-12-20 04:59:06 +00:00
|
|
|
"encoding/binary"
|
2021-11-17 07:00:32 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-03-29 15:50:41 +00:00
|
|
|
"io"
|
2021-11-17 07:00:32 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2021-12-20 04:59:06 +00:00
|
|
|
"sync"
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2022-11-11 01:19:28 +00:00
|
|
|
"github.com/Dreamacro/clash/common/convert"
|
2023-05-11 07:34:28 +00:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2023-06-07 15:03:36 +00:00
|
|
|
"github.com/Dreamacro/clash/common/utils"
|
2021-11-17 07:00:32 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2023-04-10 03:20:28 +00:00
|
|
|
"github.com/Dreamacro/clash/component/proxydialer"
|
2021-11-17 07:00:32 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2022-11-11 01:19:28 +00:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
2021-11-17 07:00:32 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2023-03-10 04:54:43 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2021-11-17 07:00:32 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/gun"
|
2022-11-11 01:19:28 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2021-11-17 07:00:32 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/vless"
|
|
|
|
"github.com/Dreamacro/clash/transport/vmess"
|
2023-02-01 14:16:06 +00:00
|
|
|
|
|
|
|
vmessSing "github.com/sagernet/sing-vmess"
|
|
|
|
"github.com/sagernet/sing-vmess/packetaddr"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2021-11-17 07:00:32 +00:00
|
|
|
)
|
|
|
|
|
2021-12-20 04:59:06 +00:00
|
|
|
const (
|
|
|
|
// max packet length
|
2022-03-29 15:50:41 +00:00
|
|
|
maxLength = 1024 << 3
|
2021-12-20 04:59:06 +00:00
|
|
|
)
|
|
|
|
|
2021-11-17 07:00:32 +00:00
|
|
|
type Vless struct {
|
|
|
|
*Base
|
|
|
|
client *vless.Client
|
|
|
|
option *VlessOption
|
|
|
|
|
|
|
|
// for gun mux
|
|
|
|
gunTLSConfig *tls.Config
|
|
|
|
gunConfig *gun.Config
|
2022-05-04 08:57:08 +00:00
|
|
|
transport *gun.TransportWrap
|
2023-03-08 09:18:46 +00:00
|
|
|
|
|
|
|
realityConfig *tlsC.RealityConfig
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type VlessOption struct {
|
|
|
|
BasicOption
|
2023-02-01 14:16:06 +00:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
UUID string `proxy:"uuid"`
|
|
|
|
Flow string `proxy:"flow,omitempty"`
|
|
|
|
FlowShow bool `proxy:"flow-show,omitempty"`
|
|
|
|
TLS bool `proxy:"tls,omitempty"`
|
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
|
|
|
PacketAddr bool `proxy:"packet-addr,omitempty"`
|
|
|
|
XUDP bool `proxy:"xudp,omitempty"`
|
|
|
|
PacketEncoding string `proxy:"packet-encoding,omitempty"`
|
|
|
|
Network string `proxy:"network,omitempty"`
|
2023-03-08 09:18:46 +00:00
|
|
|
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
2023-02-01 14:16:06 +00:00
|
|
|
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
|
|
|
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
|
|
|
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
|
|
|
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
|
|
|
WSPath string `proxy:"ws-path,omitempty"`
|
|
|
|
WSHeaders map[string]string `proxy:"ws-headers,omitempty"`
|
|
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
|
|
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
|
|
|
ServerName string `proxy:"servername,omitempty"`
|
|
|
|
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 05:15:08 +00:00
|
|
|
func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
2021-11-17 07:00:32 +00:00
|
|
|
var err error
|
2023-02-06 17:26:08 +00:00
|
|
|
|
2023-02-07 08:08:59 +00:00
|
|
|
if tlsC.HaveGlobalFingerprint() && len(v.option.ClientFingerprint) == 0 {
|
|
|
|
v.option.ClientFingerprint = tlsC.GetGlobalFingerprint()
|
2023-02-06 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 07:00:32 +00:00
|
|
|
switch v.option.Network {
|
|
|
|
case "ws":
|
|
|
|
host, port, _ := net.SplitHostPort(v.addr)
|
|
|
|
wsOpts := &vmess.WebsocketConfig{
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
Path: v.option.WSOpts.Path,
|
|
|
|
MaxEarlyData: v.option.WSOpts.MaxEarlyData,
|
|
|
|
EarlyDataHeaderName: v.option.WSOpts.EarlyDataHeaderName,
|
2023-02-01 14:16:06 +00:00
|
|
|
ClientFingerprint: v.option.ClientFingerprint,
|
2022-06-15 17:20:33 +00:00
|
|
|
Headers: http.Header{},
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(v.option.WSOpts.Headers) != 0 {
|
|
|
|
for key, value := range v.option.WSOpts.Headers {
|
2022-06-15 17:20:33 +00:00
|
|
|
wsOpts.Headers.Add(key, value)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-15 17:20:33 +00:00
|
|
|
if v.option.TLS {
|
|
|
|
wsOpts.TLS = true
|
2022-07-11 05:42:28 +00:00
|
|
|
tlsConfig := &tls.Config{
|
2022-06-15 17:20:33 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
ServerName: host,
|
|
|
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
|
|
|
NextProtos: []string{"http/1.1"},
|
2022-07-11 05:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(v.option.Fingerprint) == 0 {
|
2023-01-14 13:08:06 +00:00
|
|
|
wsOpts.TLSConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
2022-07-11 05:42:28 +00:00
|
|
|
} else {
|
|
|
|
wsOpts.TLSConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint)
|
2023-01-31 07:26:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-11 05:42:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 17:20:33 +00:00
|
|
|
if v.option.ServerName != "" {
|
|
|
|
wsOpts.TLSConfig.ServerName = v.option.ServerName
|
|
|
|
} else if host := wsOpts.Headers.Get("Host"); host != "" {
|
|
|
|
wsOpts.TLSConfig.ServerName = host
|
|
|
|
}
|
2022-06-07 17:47:50 +00:00
|
|
|
} else {
|
2022-06-15 17:20:33 +00:00
|
|
|
if host := wsOpts.Headers.Get("Host"); host == "" {
|
|
|
|
wsOpts.Headers.Set("Host", convert.RandHost())
|
|
|
|
convert.SetUserAgent(wsOpts.Headers)
|
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
2023-05-18 05:15:08 +00:00
|
|
|
c, err = vmess.StreamWebsocketConn(ctx, c, wsOpts)
|
2021-11-17 07:00:32 +00:00
|
|
|
case "http":
|
|
|
|
// readability first, so just copy default TLS logic
|
2023-05-18 05:15:08 +00:00
|
|
|
c, err = v.streamTLSOrXTLSConn(ctx, c, false)
|
2021-11-17 07:00:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
c = vmess.StreamHTTPConn(c, httpOpts)
|
|
|
|
case "h2":
|
2023-05-18 05:15:08 +00:00
|
|
|
c, err = v.streamTLSOrXTLSConn(ctx, c, true)
|
2021-11-17 07:00:32 +00:00
|
|
|
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)
|
|
|
|
case "grpc":
|
2023-03-10 02:01:05 +00:00
|
|
|
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig, v.realityConfig)
|
2021-11-17 07:00:32 +00:00
|
|
|
default:
|
2022-03-28 23:18:09 +00:00
|
|
|
// default tcp network
|
2021-11-17 07:00:32 +00:00
|
|
|
// handle TLS And XTLS
|
2023-05-18 05:15:08 +00:00
|
|
|
c, err = v.streamTLSOrXTLSConn(ctx, c, false)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-12 13:49:22 +00:00
|
|
|
return v.streamConn(c, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Vless) streamConn(c net.Conn, metadata *C.Metadata) (conn net.Conn, err error) {
|
|
|
|
if metadata.NetWork == C.UDP {
|
|
|
|
if v.option.PacketAddr {
|
|
|
|
metadata = &C.Metadata{
|
|
|
|
NetWork: C.UDP,
|
|
|
|
Host: packetaddr.SeqPacketMagicAddress,
|
|
|
|
DstPort: "443",
|
|
|
|
}
|
2023-04-20 01:38:08 +00:00
|
|
|
} else {
|
|
|
|
metadata = &C.Metadata{ // a clear metadata only contains ip
|
|
|
|
NetWork: C.UDP,
|
|
|
|
DstIP: metadata.DstIP,
|
|
|
|
DstPort: metadata.DstPort,
|
|
|
|
}
|
2023-04-12 13:49:22 +00:00
|
|
|
}
|
|
|
|
conn, err = v.client.StreamConn(c, parseVlessAddr(metadata, v.option.XUDP))
|
|
|
|
if v.option.PacketAddr {
|
|
|
|
conn = packetaddr.NewBindConn(conn)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conn, err = v.client.StreamConn(c, parseVlessAddr(metadata, false))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
conn = nil
|
|
|
|
}
|
|
|
|
return
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 05:15:08 +00:00
|
|
|
func (v *Vless) streamTLSOrXTLSConn(ctx context.Context, conn net.Conn, isH2 bool) (net.Conn, error) {
|
2021-11-17 07:00:32 +00:00
|
|
|
host, _, _ := net.SplitHostPort(v.addr)
|
|
|
|
|
2023-02-25 05:12:19 +00:00
|
|
|
if v.isLegacyXTLSEnabled() && !isH2 {
|
2021-11-17 07:00:32 +00:00
|
|
|
xtlsOpts := vless.XTLSConfig{
|
|
|
|
Host: host,
|
|
|
|
SkipCertVerify: v.option.SkipCertVerify,
|
2023-01-11 14:01:15 +00:00
|
|
|
Fingerprint: v.option.Fingerprint,
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if v.option.ServerName != "" {
|
|
|
|
xtlsOpts.Host = v.option.ServerName
|
|
|
|
}
|
|
|
|
|
2023-05-18 05:15:08 +00:00
|
|
|
return vless.StreamXTLSConn(ctx, conn, &xtlsOpts)
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2022-03-30 16:08:43 +00:00
|
|
|
} else if v.option.TLS {
|
2021-11-17 07:00:32 +00:00
|
|
|
tlsOpts := vmess.TLSConfig{
|
2023-02-01 14:16:06 +00:00
|
|
|
Host: host,
|
|
|
|
SkipCertVerify: v.option.SkipCertVerify,
|
|
|
|
FingerPrint: v.option.Fingerprint,
|
|
|
|
ClientFingerprint: v.option.ClientFingerprint,
|
2023-03-08 09:18:46 +00:00
|
|
|
Reality: v.realityConfig,
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if isH2 {
|
|
|
|
tlsOpts.NextProtos = []string{"h2"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.option.ServerName != "" {
|
|
|
|
tlsOpts.Host = v.option.ServerName
|
|
|
|
}
|
|
|
|
|
2023-05-18 05:15:08 +00:00
|
|
|
return vmess.StreamTLSConn(ctx, conn, &tlsOpts)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
2022-03-30 16:08:43 +00:00
|
|
|
|
|
|
|
return conn, nil
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 05:12:19 +00:00
|
|
|
func (v *Vless) isLegacyXTLSEnabled() bool {
|
|
|
|
return v.client.Addons != nil && v.client.Addons.Flow != vless.XRV
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements C.ProxyAdapter
|
|
|
|
func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
|
|
|
// gun transport
|
|
|
|
if v.transport != nil && len(opts) == 0 {
|
|
|
|
c, err := gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-22 04:31:45 +00:00
|
|
|
defer func(c net.Conn) {
|
2022-12-13 05:20:40 +00:00
|
|
|
safeConnClose(c, err)
|
2022-12-22 04:31:45 +00:00
|
|
|
}(c)
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-01-11 14:01:15 +00:00
|
|
|
c, err = v.client.StreamConn(c, parseVlessAddr(metadata, v.option.XUDP))
|
2021-11-17 07:00:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewConn(c, v), nil
|
|
|
|
}
|
2022-12-19 16:11:02 +00:00
|
|
|
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 13:34:07 +00:00
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2022-12-19 13:34:07 +00:00
|
|
|
// DialContextWithDialer implements C.ProxyAdapter
|
|
|
|
func (v *Vless) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
2023-04-10 03:20:28 +00:00
|
|
|
if len(v.option.DialerProxy) > 0 {
|
|
|
|
dialer, err = proxydialer.NewByName(v.option.DialerProxy, dialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 13:34:07 +00:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
2021-11-17 07:00:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
2022-12-16 14:15:44 +00:00
|
|
|
defer func(c net.Conn) {
|
2022-12-13 05:20:40 +00:00
|
|
|
safeConnClose(c, err)
|
2022-12-16 14:15:44 +00:00
|
|
|
}(c)
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-05-18 05:15:08 +00:00
|
|
|
c, err = v.StreamConnContext(ctx, c, metadata)
|
2023-01-31 10:05:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
return NewConn(c, v), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
|
|
|
func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
2023-01-31 10:05:46 +00:00
|
|
|
// vless use stream-oriented udp with a special address, so we need a net.UDPAddr
|
2021-11-17 07:00:32 +00:00
|
|
|
if !metadata.Resolved() {
|
2022-11-12 05:18:36 +00:00
|
|
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
2021-11-17 07:00:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't resolve ip")
|
|
|
|
}
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
|
|
|
var c net.Conn
|
|
|
|
// gun transport
|
|
|
|
if v.transport != nil && len(opts) == 0 {
|
|
|
|
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-22 04:31:45 +00:00
|
|
|
defer func(c net.Conn) {
|
2022-12-13 05:20:40 +00:00
|
|
|
safeConnClose(c, err)
|
2022-12-22 04:31:45 +00:00
|
|
|
}(c)
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2023-04-12 13:49:22 +00:00
|
|
|
c, err = v.streamConn(c, metadata)
|
2021-11-17 07:00:32 +00:00
|
|
|
if err != nil {
|
2022-12-19 13:34:07 +00:00
|
|
|
return nil, fmt.Errorf("new vless client error: %v", err)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 13:49:22 +00:00
|
|
|
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
2022-12-19 16:11:02 +00:00
|
|
|
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
2022-12-19 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
|
|
func (v *Vless) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
2023-04-10 03:20:28 +00:00
|
|
|
if len(v.option.DialerProxy) > 0 {
|
|
|
|
dialer, err = proxydialer.NewByName(v.option.DialerProxy, dialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 13:49:22 +00:00
|
|
|
|
2023-01-31 10:05:46 +00:00
|
|
|
// vless use stream-oriented udp with a special address, so we need a net.UDPAddr
|
2022-12-19 13:34:07 +00:00
|
|
|
if !metadata.Resolved() {
|
|
|
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't resolve ip")
|
|
|
|
}
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
2023-04-12 13:49:22 +00:00
|
|
|
|
2022-12-19 13:34:07 +00:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
2022-12-22 04:31:45 +00:00
|
|
|
defer func(c net.Conn) {
|
2022-12-19 13:34:07 +00:00
|
|
|
safeConnClose(c, err)
|
2022-12-22 04:31:45 +00:00
|
|
|
}(c)
|
2022-12-19 13:34:07 +00:00
|
|
|
|
2023-05-18 05:15:08 +00:00
|
|
|
c, err = v.StreamConnContext(ctx, c, metadata)
|
2021-11-17 07:00:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("new vless client error: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-04-12 13:49:22 +00:00
|
|
|
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
2022-04-30 03:36:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 13:34:07 +00:00
|
|
|
// SupportWithDialer implements C.ProxyAdapter
|
2023-04-11 04:51:24 +00:00
|
|
|
func (v *Vless) SupportWithDialer() C.NetWork {
|
|
|
|
return C.ALLNet
|
2022-12-19 13:34:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 03:36:42 +00:00
|
|
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
2023-04-12 13:49:22 +00:00
|
|
|
func (v *Vless) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
|
|
// vless use stream-oriented udp with a special address, so we need a net.UDPAddr
|
|
|
|
if !metadata.Resolved() {
|
|
|
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't resolve ip")
|
|
|
|
}
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:01:15 +00:00
|
|
|
if v.option.XUDP {
|
2023-06-08 07:57:51 +00:00
|
|
|
var globalID [8]byte
|
|
|
|
if metadata.SourceValid() {
|
|
|
|
globalID = utils.GlobalID(metadata.SourceAddress())
|
|
|
|
}
|
2023-05-11 07:34:28 +00:00
|
|
|
return newPacketConn(N.NewThreadSafePacketConn(
|
2023-06-07 15:03:36 +00:00
|
|
|
vmessSing.NewXUDPConn(c,
|
2023-06-08 07:57:51 +00:00
|
|
|
globalID,
|
2023-06-07 15:03:36 +00:00
|
|
|
M.SocksaddrFromNet(metadata.UDPAddr())),
|
2023-05-11 07:34:28 +00:00
|
|
|
), v), nil
|
2023-01-11 14:01:15 +00:00
|
|
|
} else if v.option.PacketAddr {
|
2023-05-11 07:34:28 +00:00
|
|
|
return newPacketConn(N.NewThreadSafePacketConn(
|
|
|
|
packetaddr.NewConn(&vlessPacketConn{
|
2023-01-11 14:01:15 +00:00
|
|
|
Conn: c, rAddr: metadata.UDPAddr(),
|
2023-04-20 01:38:08 +00:00
|
|
|
}, M.SocksaddrFromNet(metadata.UDPAddr())),
|
2023-05-11 07:34:28 +00:00
|
|
|
), v), nil
|
2023-01-11 14:01:15 +00:00
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
return newPacketConn(&vlessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}, v), nil
|
|
|
|
}
|
|
|
|
|
2022-04-30 03:36:42 +00:00
|
|
|
// SupportUOT implements C.ProxyAdapter
|
|
|
|
func (v *Vless) SupportUOT() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:01:15 +00:00
|
|
|
func parseVlessAddr(metadata *C.Metadata, xudp bool) *vless.DstAddr {
|
2021-11-17 07:00:32 +00:00
|
|
|
var addrType byte
|
|
|
|
var addr []byte
|
2022-11-11 01:19:28 +00:00
|
|
|
switch metadata.AddrType() {
|
|
|
|
case socks5.AtypIPv4:
|
2022-04-21 10:56:33 +00:00
|
|
|
addrType = vless.AtypIPv4
|
2021-11-17 07:00:32 +00:00
|
|
|
addr = make([]byte, net.IPv4len)
|
2022-04-19 17:52:51 +00:00
|
|
|
copy(addr[:], metadata.DstIP.AsSlice())
|
2022-11-11 01:19:28 +00:00
|
|
|
case socks5.AtypIPv6:
|
2022-04-21 10:56:33 +00:00
|
|
|
addrType = vless.AtypIPv6
|
2021-11-17 07:00:32 +00:00
|
|
|
addr = make([]byte, net.IPv6len)
|
2022-04-19 17:52:51 +00:00
|
|
|
copy(addr[:], metadata.DstIP.AsSlice())
|
2022-11-11 01:19:28 +00:00
|
|
|
case socks5.AtypDomainName:
|
2022-04-21 10:56:33 +00:00
|
|
|
addrType = vless.AtypDomainName
|
2021-11-17 07:00:32 +00:00
|
|
|
addr = make([]byte, len(metadata.Host)+1)
|
|
|
|
addr[0] = byte(len(metadata.Host))
|
2022-04-21 10:56:33 +00:00
|
|
|
copy(addr[1:], metadata.Host)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 23:18:09 +00:00
|
|
|
port, _ := strconv.ParseUint(metadata.DstPort, 10, 16)
|
2021-11-17 07:00:32 +00:00
|
|
|
return &vless.DstAddr{
|
|
|
|
UDP: metadata.NetWork == C.UDP,
|
|
|
|
AddrType: addrType,
|
|
|
|
Addr: addr,
|
2023-01-11 14:01:15 +00:00
|
|
|
Port: uint16(port),
|
|
|
|
Mux: metadata.NetWork == C.UDP && xudp,
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type vlessPacketConn struct {
|
|
|
|
net.Conn
|
2022-01-27 04:25:53 +00:00
|
|
|
rAddr net.Addr
|
2021-12-20 04:59:06 +00:00
|
|
|
remain int
|
|
|
|
mux sync.Mutex
|
2022-03-30 05:15:45 +00:00
|
|
|
cache [2]byte
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 05:15:45 +00:00
|
|
|
func (c *vlessPacketConn) writePacket(payload []byte) (int, error) {
|
|
|
|
binary.BigEndian.PutUint16(c.cache[:], uint16(len(payload)))
|
|
|
|
|
|
|
|
if _, err := c.Conn.Write(c.cache[:]); err != nil {
|
|
|
|
return 0, err
|
2021-12-20 04:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 05:15:45 +00:00
|
|
|
return c.Conn.Write(payload)
|
2021-12-20 04:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *vlessPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
2022-03-29 15:50:41 +00:00
|
|
|
total := len(b)
|
|
|
|
if total == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 05:15:45 +00:00
|
|
|
if total <= maxLength {
|
|
|
|
return c.writePacket(b)
|
2021-12-20 04:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset := 0
|
2022-03-30 05:15:45 +00:00
|
|
|
|
2021-12-20 04:59:06 +00:00
|
|
|
for offset < total {
|
|
|
|
cursor := offset + maxLength
|
|
|
|
if cursor > total {
|
|
|
|
cursor = total
|
|
|
|
}
|
|
|
|
|
2022-03-30 05:15:45 +00:00
|
|
|
n, err := c.writePacket(b[offset:cursor])
|
2021-12-20 04:59:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return offset + n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = cursor
|
2022-03-29 15:50:41 +00:00
|
|
|
if offset == total {
|
|
|
|
break
|
|
|
|
}
|
2021-12-20 04:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total, nil
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 04:59:06 +00:00
|
|
|
func (c *vlessPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
c.mux.Lock()
|
|
|
|
defer c.mux.Unlock()
|
|
|
|
|
|
|
|
if c.remain > 0 {
|
2022-03-29 15:50:41 +00:00
|
|
|
length := len(b)
|
2021-12-20 04:59:06 +00:00
|
|
|
if c.remain < length {
|
|
|
|
length = c.remain
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := c.Conn.Read(b[:length])
|
|
|
|
if err != nil {
|
2022-03-30 05:15:45 +00:00
|
|
|
return 0, c.rAddr, err
|
2021-12-20 04:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.remain -= n
|
|
|
|
return n, c.rAddr, nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 05:15:45 +00:00
|
|
|
if _, err := c.Conn.Read(b[:2]); err != nil {
|
|
|
|
return 0, c.rAddr, err
|
2021-12-20 04:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 15:50:41 +00:00
|
|
|
total := int(binary.BigEndian.Uint16(b[:2]))
|
|
|
|
if total == 0 {
|
2022-03-30 05:15:45 +00:00
|
|
|
return 0, c.rAddr, nil
|
2021-12-20 04:59:06 +00:00
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2022-03-29 15:50:41 +00:00
|
|
|
length := len(b)
|
|
|
|
if length > total {
|
|
|
|
length = total
|
2021-11-17 11:09:01 +00:00
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
|
2022-03-30 05:15:45 +00:00
|
|
|
if _, err := io.ReadFull(c.Conn, b[:length]); err != nil {
|
|
|
|
return 0, c.rAddr, errors.New("read packet error")
|
2022-03-29 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 05:15:45 +00:00
|
|
|
c.remain = total - length
|
2022-02-22 17:00:27 +00:00
|
|
|
|
2022-03-30 05:15:45 +00:00
|
|
|
return length, c.rAddr, nil
|
2022-03-29 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewVless(option VlessOption) (*Vless, error) {
|
2021-11-17 07:00:32 +00:00
|
|
|
var addons *vless.Addons
|
|
|
|
if option.Network != "ws" && len(option.Flow) >= 16 {
|
|
|
|
option.Flow = option.Flow[:16]
|
|
|
|
switch option.Flow {
|
2023-03-10 04:54:43 +00:00
|
|
|
case vless.XRV:
|
|
|
|
log.Warnln("To use %s, ensure your server is upgrade to Xray-core v1.8.0+", vless.XRV)
|
|
|
|
fallthrough
|
|
|
|
case vless.XRO, vless.XRD, vless.XRS:
|
2021-11-17 07:00:32 +00:00
|
|
|
addons = &vless.Addons{
|
|
|
|
Flow: option.Flow,
|
|
|
|
}
|
|
|
|
default:
|
2022-03-29 15:50:41 +00:00
|
|
|
return nil, fmt.Errorf("unsupported xtls flow type: %s", option.Flow)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:07:20 +00:00
|
|
|
switch option.PacketEncoding {
|
|
|
|
case "packetaddr", "packet":
|
|
|
|
option.PacketAddr = true
|
2023-01-28 06:58:52 +00:00
|
|
|
option.XUDP = false
|
|
|
|
default: // https://github.com/XTLS/Xray-core/pull/1567#issuecomment-1407305458
|
|
|
|
if !option.PacketAddr {
|
|
|
|
option.XUDP = true
|
|
|
|
}
|
2023-01-27 16:07:20 +00:00
|
|
|
}
|
2023-04-12 13:49:22 +00:00
|
|
|
if option.XUDP {
|
|
|
|
option.PacketAddr = false
|
|
|
|
}
|
2023-01-27 16:07:20 +00:00
|
|
|
|
2022-05-06 05:28:09 +00:00
|
|
|
client, err := vless.NewClient(option.UUID, addons, option.FlowShow)
|
2021-11-17 07:00:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := &Vless{
|
|
|
|
Base: &Base{
|
2022-08-28 05:41:19 +00:00
|
|
|
name: option.Name,
|
|
|
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
|
|
|
tp: C.Vless,
|
|
|
|
udp: option.UDP,
|
2023-01-27 16:07:20 +00:00
|
|
|
xudp: option.XUDP,
|
2023-02-24 05:53:44 +00:00
|
|
|
tfo: option.TFO,
|
2022-08-28 05:41:19 +00:00
|
|
|
iface: option.Interface,
|
2023-01-09 15:15:17 +00:00
|
|
|
rmark: option.RoutingMark,
|
2022-08-28 05:41:19 +00:00
|
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
2021-11-17 07:00:32 +00:00
|
|
|
},
|
|
|
|
client: client,
|
|
|
|
option: &option,
|
|
|
|
}
|
|
|
|
|
2023-03-10 02:01:05 +00:00
|
|
|
v.realityConfig, err = v.option.RealityOpts.Parse()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-17 07:00:32 +00:00
|
|
|
switch option.Network {
|
|
|
|
case "h2":
|
|
|
|
if len(option.HTTP2Opts.Host) == 0 {
|
|
|
|
option.HTTP2Opts.Host = append(option.HTTP2Opts.Host, "www.example.com")
|
|
|
|
}
|
|
|
|
case "grpc":
|
|
|
|
dialFn := func(network, addr string) (net.Conn, error) {
|
2023-04-10 03:20:28 +00:00
|
|
|
var err error
|
|
|
|
var cDialer C.Dialer = dialer.NewDialer(v.Base.DialOptions()...)
|
|
|
|
if len(v.option.DialerProxy) > 0 {
|
|
|
|
cDialer, err = proxydialer.NewByName(v.option.DialerProxy, cDialer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c, err := cDialer.DialContext(context.Background(), "tcp", v.addr)
|
2021-11-17 07:00:32 +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{
|
2023-02-01 14:16:06 +00:00
|
|
|
ServiceName: v.option.GrpcOpts.GrpcServiceName,
|
|
|
|
Host: v.option.ServerName,
|
|
|
|
ClientFingerprint: v.option.ClientFingerprint,
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
2023-05-13 01:38:14 +00:00
|
|
|
if option.ServerName == "" {
|
|
|
|
gunConfig.Host = v.addr
|
|
|
|
}
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if option.TLS {
|
|
|
|
tlsConfig = tlsC.GetGlobalTLSConfig(&tls.Config{
|
|
|
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
|
|
|
ServerName: v.option.ServerName,
|
|
|
|
})
|
|
|
|
if option.ServerName == "" {
|
|
|
|
host, _, _ := net.SplitHostPort(v.addr)
|
|
|
|
tlsConfig.ServerName = host
|
|
|
|
}
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v.gunTLSConfig = tlsConfig
|
|
|
|
v.gunConfig = gunConfig
|
2023-02-01 14:16:06 +00:00
|
|
|
|
2023-03-10 02:01:05 +00:00
|
|
|
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig, v.option.ClientFingerprint, v.realityConfig)
|
2021-11-17 07:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|