-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
client.go
108 lines (97 loc) · 2.97 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package transport
import (
"crypto/tls"
"fmt"
"net"
"strings"
"time"
"github.com/metacubex/quic-go"
"github.com/metacubex/mihomo/transport/hysteria/conns/faketcp"
"github.com/metacubex/mihomo/transport/hysteria/conns/udp"
"github.com/metacubex/mihomo/transport/hysteria/conns/wechat"
obfsPkg "github.com/metacubex/mihomo/transport/hysteria/obfs"
"github.com/metacubex/mihomo/transport/hysteria/utils"
)
type ClientTransport struct {
Dialer *net.Dialer
}
func (ct *ClientTransport) quicPacketConn(proto string, rAddr net.Addr, serverPorts string, obfs obfsPkg.Obfuscator, hopInterval time.Duration, dialer utils.PacketDialer) (net.PacketConn, error) {
server := rAddr.String()
if len(proto) == 0 || proto == "udp" {
conn, err := dialer.ListenPacket(rAddr)
if err != nil {
return nil, err
}
if obfs != nil {
if serverPorts != "" {
return udp.NewObfsUDPHopClientPacketConn(server, serverPorts, hopInterval, obfs, dialer)
}
oc := udp.NewObfsUDPConn(conn, obfs)
return oc, nil
} else {
if serverPorts != "" {
return udp.NewObfsUDPHopClientPacketConn(server, serverPorts, hopInterval, nil, dialer)
}
return conn, nil
}
} else if proto == "wechat-video" {
conn, err := dialer.ListenPacket(rAddr)
if err != nil {
return nil, err
}
if obfs == nil {
obfs = obfsPkg.NewDummyObfuscator()
}
return wechat.NewObfsWeChatUDPConn(conn, obfs), nil
} 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)
}
}
func (ct *ClientTransport) QUICDial(proto string, server string, serverPorts string, tlsConfig *tls.Config, quicConfig *quic.Config, obfs obfsPkg.Obfuscator, hopInterval time.Duration, dialer utils.PacketDialer) (quic.Connection, error) {
serverUDPAddr, err := dialer.RemoteAddr(server)
if err != nil {
return nil, err
}
pktConn, err := ct.quicPacketConn(proto, serverUDPAddr, serverPorts, obfs, hopInterval, dialer)
if err != nil {
return nil, err
}
transport := quic.Transport{Conn: pktConn}
transport.SetCreatedConn(true) // auto close conn
transport.SetSingleUse(true) // auto close transport
qs, err := transport.Dial(dialer.Context(), serverUDPAddr, tlsConfig, quicConfig)
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)
}
func isMultiPortAddr(addr string) bool {
_, portStr, err := net.SplitHostPort(addr)
if err == nil && (strings.Contains(portStr, ",") || strings.Contains(portStr, "-")) {
return true
}
return false
}