2020-03-19 20:26:53 +08:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-18 19:40:34 +08:00
|
|
|
"crypto/tls"
|
2020-03-19 20:26:53 +08:00
|
|
|
"fmt"
|
2022-07-10 20:44:24 +08:00
|
|
|
tlsC "github.com/Dreamacro/clash/common/tls"
|
2020-03-19 20:26:53 +08:00
|
|
|
"net"
|
2021-10-16 20:19:59 +08:00
|
|
|
"net/http"
|
2020-03-19 20:26:53 +08:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 22:18:49 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/gun"
|
|
|
|
"github.com/Dreamacro/clash/transport/trojan"
|
2022-03-30 00:15:39 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/vless"
|
2020-03-19 20:26:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Trojan struct {
|
|
|
|
*Base
|
|
|
|
instance *trojan.Trojan
|
2021-10-16 20:19:59 +08:00
|
|
|
option *TrojanOption
|
2021-03-18 19:40:34 +08:00
|
|
|
|
|
|
|
// for gun mux
|
|
|
|
gunTLSConfig *tls.Config
|
|
|
|
gunConfig *gun.Config
|
2022-05-04 16:57:08 +08:00
|
|
|
transport *gun.TransportWrap
|
2020-03-19 20:26:53 +08:00
|
|
|
}
|
2022-04-22 18:55:28 +08:00
|
|
|
|
2020-03-19 20:26:53 +08:00
|
|
|
type TrojanOption struct {
|
2021-11-07 16:48:51 +08:00
|
|
|
BasicOption
|
2021-03-18 19:40:34 +08:00
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
Password string `proxy:"password"`
|
|
|
|
ALPN []string `proxy:"alpn,omitempty"`
|
|
|
|
SNI string `proxy:"sni,omitempty"`
|
|
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
|
|
|
Network string `proxy:"network,omitempty"`
|
|
|
|
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
2021-10-16 20:19:59 +08:00
|
|
|
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
2022-03-30 00:15:39 +08:00
|
|
|
Flow string `proxy:"flow,omitempty"`
|
|
|
|
FlowShow bool `proxy:"flow-show,omitempty"`
|
2021-10-16 20:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trojan) plainStream(c net.Conn) (net.Conn, error) {
|
|
|
|
if t.option.Network == "ws" {
|
|
|
|
host, port, _ := net.SplitHostPort(t.addr)
|
|
|
|
wsOpts := &trojan.WebsocketOption{
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
Path: t.option.WSOpts.Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.option.SNI != "" {
|
|
|
|
wsOpts.Host = t.option.SNI
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(t.option.WSOpts.Headers) != 0 {
|
|
|
|
header := http.Header{}
|
|
|
|
for key, value := range t.option.WSOpts.Headers {
|
|
|
|
header.Add(key, value)
|
|
|
|
}
|
|
|
|
wsOpts.Headers = header
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.instance.StreamWebsocketConn(c, wsOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.instance.StreamConn(c)
|
2020-03-19 20:26:53 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// StreamConn implements C.ProxyAdapter
|
2020-03-21 23:46:49 +08:00
|
|
|
func (t *Trojan) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
2021-03-18 19:40:34 +08:00
|
|
|
var err error
|
|
|
|
if t.transport != nil {
|
|
|
|
c, err = gun.StreamGunWithConn(c, t.gunTLSConfig, t.gunConfig)
|
|
|
|
} else {
|
2021-10-16 20:19:59 +08:00
|
|
|
c, err = t.plainStream(c)
|
2021-03-18 19:40:34 +08:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
|
|
|
}
|
|
|
|
|
2022-03-30 00:15:39 +08:00
|
|
|
c, err = t.instance.PresetXTLSConn(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-30 22:26:38 +08:00
|
|
|
if metadata.NetWork == C.UDP {
|
|
|
|
err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata))
|
|
|
|
return c, err
|
|
|
|
}
|
2020-03-21 23:46:49 +08:00
|
|
|
err = t.instance.WriteHeader(c, trojan.CommandTCP, serializesSocksAddr(metadata))
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
2021-03-22 23:26:20 +08:00
|
|
|
// gun transport
|
2021-11-07 16:48:51 +08:00
|
|
|
if t.transport != nil && len(opts) == 0 {
|
2021-03-18 19:40:34 +08:00
|
|
|
c, err := gun.StreamGunWithTransport(t.transport, t.gunConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-30 00:15:39 +08:00
|
|
|
c, err = t.instance.PresetXTLSConn(c)
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:40:34 +08:00
|
|
|
if err = t.instance.WriteHeader(c, trojan.CommandTCP, serializesSocksAddr(metadata)); err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewConn(c, t), nil
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", t.addr, t.Base.DialOptions(opts...)...)
|
2020-03-19 20:26:53 +08:00
|
|
|
if err != nil {
|
2020-03-21 23:46:49 +08:00
|
|
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
2020-03-19 20:26:53 +08:00
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
2021-03-22 23:26:20 +08:00
|
|
|
|
|
|
|
defer safeConnClose(c, err)
|
|
|
|
|
2020-03-21 23:46:49 +08:00
|
|
|
c, err = t.StreamConn(c, metadata)
|
2020-03-19 20:26:53 +08:00
|
|
|
if err != nil {
|
2020-03-21 23:46:49 +08:00
|
|
|
return nil, err
|
2020-03-19 20:26:53 +08:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:46:49 +08:00
|
|
|
return NewConn(c, t), err
|
2020-03-19 20:26:53 +08:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:44:53 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
2021-03-22 23:26:20 +08:00
|
|
|
var c net.Conn
|
|
|
|
|
|
|
|
// grpc transport
|
2021-11-07 16:48:51 +08:00
|
|
|
if t.transport != nil && len(opts) == 0 {
|
2021-03-22 23:26:20 +08:00
|
|
|
c, err = gun.StreamGunWithTransport(t.transport, t.gunConfig)
|
2021-03-18 19:40:34 +08:00
|
|
|
if err != nil {
|
2021-03-22 23:26:20 +08:00
|
|
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
2021-03-18 19:40:34 +08:00
|
|
|
}
|
2021-04-19 12:20:37 +08:00
|
|
|
defer safeConnClose(c, err)
|
2021-03-22 23:26:20 +08:00
|
|
|
} else {
|
2021-11-07 16:48:51 +08:00
|
|
|
c, err = dialer.DialContext(ctx, "tcp", t.addr, t.Base.DialOptions(opts...)...)
|
2021-03-22 23:26:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
2021-03-18 19:40:34 +08:00
|
|
|
}
|
2021-04-19 12:20:37 +08:00
|
|
|
defer safeConnClose(c, err)
|
2021-03-22 23:26:20 +08:00
|
|
|
tcpKeepAlive(c)
|
2021-10-16 20:19:59 +08:00
|
|
|
c, err = t.plainStream(c)
|
2021-04-05 23:26:13 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
|
|
|
}
|
2021-03-18 19:40:34 +08:00
|
|
|
}
|
|
|
|
|
2022-05-02 06:27:45 +08:00
|
|
|
err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pc := t.instance.PacketConn(c)
|
|
|
|
return newPacketConn(pc, t), err
|
2022-04-30 11:36:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
|
|
|
func (t *Trojan) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
2020-03-19 20:26:53 +08:00
|
|
|
pc := t.instance.PacketConn(c)
|
2020-06-12 23:39:03 +08:00
|
|
|
return newPacketConn(pc, t), err
|
2020-03-19 20:26:53 +08:00
|
|
|
}
|
|
|
|
|
2022-04-30 11:36:42 +08:00
|
|
|
// SupportUOT implements C.ProxyAdapter
|
|
|
|
func (t *Trojan) SupportUOT() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:26:53 +08:00
|
|
|
func NewTrojan(option TrojanOption) (*Trojan, error) {
|
2020-03-21 23:46:49 +08:00
|
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
2020-03-19 20:26:53 +08:00
|
|
|
|
|
|
|
tOption := &trojan.Option{
|
2021-07-03 20:34:44 +08:00
|
|
|
Password: option.Password,
|
|
|
|
ALPN: option.ALPN,
|
|
|
|
ServerName: option.Server,
|
|
|
|
SkipCertVerify: option.SkipCertVerify,
|
2022-03-30 00:15:39 +08:00
|
|
|
FlowShow: option.FlowShow,
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.Network != "ws" && len(option.Flow) >= 16 {
|
|
|
|
option.Flow = option.Flow[:16]
|
|
|
|
switch option.Flow {
|
|
|
|
case vless.XRO, vless.XRD, vless.XRS:
|
|
|
|
tOption.Flow = option.Flow
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported xtls flow type: %s", option.Flow)
|
|
|
|
}
|
2020-03-19 20:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if option.SNI != "" {
|
|
|
|
tOption.ServerName = option.SNI
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:40:34 +08:00
|
|
|
t := &Trojan{
|
2020-03-19 20:26:53 +08:00
|
|
|
Base: &Base{
|
2021-11-07 16:48:51 +08:00
|
|
|
name: option.Name,
|
|
|
|
addr: addr,
|
|
|
|
tp: C.Trojan,
|
|
|
|
udp: option.UDP,
|
|
|
|
iface: option.Interface,
|
2022-03-19 13:29:30 +08:00
|
|
|
rmark: option.RoutingMark,
|
2020-03-19 20:26:53 +08:00
|
|
|
},
|
|
|
|
instance: trojan.New(tOption),
|
2021-10-16 20:19:59 +08:00
|
|
|
option: &option,
|
2021-03-18 19:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if option.Network == "grpc" {
|
|
|
|
dialFn := func(network, addr string) (net.Conn, error) {
|
2021-11-07 16:48:51 +08:00
|
|
|
c, err := dialer.DialContext(context.Background(), "tcp", t.addr, t.Base.DialOptions()...)
|
2021-03-18 19:40:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %s", t.addr, err.Error())
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2022-07-10 20:44:24 +08:00
|
|
|
tlsConfig := tlsC.MixinTLSConfig(&tls.Config{
|
2021-03-18 19:40:34 +08:00
|
|
|
NextProtos: option.ALPN,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
InsecureSkipVerify: tOption.SkipCertVerify,
|
|
|
|
ServerName: tOption.ServerName,
|
2022-07-10 20:44:24 +08:00
|
|
|
})
|
2021-03-18 19:40:34 +08:00
|
|
|
|
2022-03-30 00:15:39 +08:00
|
|
|
if t.option.Flow != "" {
|
|
|
|
t.transport = gun.NewHTTP2XTLSClient(dialFn, tlsConfig)
|
|
|
|
} else {
|
|
|
|
t.transport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:40:34 +08:00
|
|
|
t.gunTLSConfig = tlsConfig
|
|
|
|
t.gunConfig = &gun.Config{
|
|
|
|
ServiceName: option.GrpcOpts.GrpcServiceName,
|
|
|
|
Host: tOption.ServerName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
2020-03-19 20:26:53 +08:00
|
|
|
}
|