2022-07-03 18:22:56 +08:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2022-11-27 11:09:56 +08:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/metacubex/quic-go"
|
|
|
|
|
2022-07-03 18:22:56 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/hysteria/conns/faketcp"
|
|
|
|
"github.com/Dreamacro/clash/transport/hysteria/conns/udp"
|
|
|
|
"github.com/Dreamacro/clash/transport/hysteria/conns/wechat"
|
2022-09-11 15:24:56 +08:00
|
|
|
obfsPkg "github.com/Dreamacro/clash/transport/hysteria/obfs"
|
2022-11-27 11:09:56 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/hysteria/utils"
|
2022-07-03 18:22:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClientTransport struct {
|
2022-08-15 15:46:07 +08:00
|
|
|
Dialer *net.Dialer
|
2022-07-03 18:22:56 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 11:09:56 +08:00
|
|
|
func (ct *ClientTransport) quicPacketConn(proto string, server string, obfs obfsPkg.Obfuscator, hopInterval time.Duration, dialer utils.PacketDialer) (net.PacketConn, error) {
|
2022-07-03 18:22:56 +08:00
|
|
|
if len(proto) == 0 || proto == "udp" {
|
|
|
|
conn, err := dialer.ListenPacket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if obfs != nil {
|
2022-11-27 11:09:56 +08:00
|
|
|
if isMultiPortAddr(server) {
|
|
|
|
return udp.NewObfsUDPHopClientPacketConn(server, hopInterval, obfs, dialer)
|
|
|
|
}
|
2022-07-03 18:22:56 +08:00
|
|
|
oc := udp.NewObfsUDPConn(conn, obfs)
|
|
|
|
return oc, nil
|
|
|
|
} else {
|
2022-11-27 11:09:56 +08:00
|
|
|
if isMultiPortAddr(server) {
|
|
|
|
return udp.NewObfsUDPHopClientPacketConn(server, hopInterval, nil, dialer)
|
|
|
|
}
|
2022-07-03 18:22:56 +08:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
} else if proto == "wechat-video" {
|
|
|
|
conn, err := dialer.ListenPacket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-11 15:24:56 +08:00
|
|
|
if obfs == nil {
|
|
|
|
obfs = obfsPkg.NewDummyObfuscator()
|
2022-07-03 18:22:56 +08:00
|
|
|
}
|
2022-09-11 15:24:56 +08:00
|
|
|
return wechat.NewObfsWeChatUDPConn(conn, obfs), nil
|
2022-07-03 18:22:56 +08:00
|
|
|
} else if proto == "faketcp" {
|
|
|
|
var conn *faketcp.TCPConn
|
|
|
|
conn, err := faketcp.Dial("tcp", server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if obfs != nil {
|
|
|
|
oc := faketcp.NewObfsFakeTCPConn(conn, obfs)
|
|
|
|
return oc, nil
|
|
|
|
} else {
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unsupported protocol: %s", proto)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 11:09:56 +08:00
|
|
|
func (ct *ClientTransport) QUICDial(proto string, server string, tlsConfig *tls.Config, quicConfig *quic.Config, obfs obfsPkg.Obfuscator, hopInterval time.Duration, dialer utils.PacketDialer) (quic.Connection, error) {
|
2022-08-28 13:41:19 +08:00
|
|
|
serverUDPAddr, err := dialer.RemoteAddr(server)
|
2022-07-03 18:22:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-27 11:09:56 +08:00
|
|
|
pktConn, err := ct.quicPacketConn(proto, serverUDPAddr.String(), obfs, hopInterval, dialer)
|
2022-07-03 18:22:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-28 13:41:19 +08:00
|
|
|
|
|
|
|
qs, err := quic.DialContext(dialer.Context(), pktConn, serverUDPAddr, server, tlsConfig, quicConfig)
|
2022-07-03 18:22:56 +08:00
|
|
|
if err != nil {
|
|
|
|
_ = pktConn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return qs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *ClientTransport) DialTCP(raddr *net.TCPAddr) (*net.TCPConn, error) {
|
|
|
|
conn, err := ct.Dialer.Dial("tcp", raddr.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.(*net.TCPConn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *ClientTransport) ListenUDP() (*net.UDPConn, error) {
|
|
|
|
return net.ListenUDP("udp", nil)
|
|
|
|
}
|
2022-11-27 11:09:56 +08:00
|
|
|
|
|
|
|
func isMultiPortAddr(addr string) bool {
|
|
|
|
_, portStr, err := net.SplitHostPort(addr)
|
|
|
|
if err == nil && (strings.Contains(portStr, ",") || strings.Contains(portStr, "-")) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|