forked from yinqiwen/gsnova
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quic.go
executable file
·64 lines (56 loc) · 1.51 KB
/
quic.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
package quic
import (
"crypto/tls"
"net"
"net/url"
quic "github.com/lucas-clemente/quic-go"
"github.com/yinqiwen/gsnova/common/logger"
"github.com/yinqiwen/gsnova/common/mux"
"github.com/yinqiwen/gsnova/common/netx"
"github.com/yinqiwen/gsnova/local/proxy"
)
type QUICProxy struct {
//proxy.BaseProxy
}
func (p *QUICProxy) Features() proxy.ProxyFeatureSet {
return proxy.ProxyFeatureSet{
AutoExpire: true,
Pingable: false,
}
}
func (tc *QUICProxy) CreateMuxSession(server string, conf *proxy.ProxyChannelConfig) (mux.MuxSession, error) {
rurl, err := url.Parse(server)
if nil != err {
return nil, err
}
hostport := rurl.Host
tcpHost, tcpPort, _ := net.SplitHostPort(hostport)
if net.ParseIP(tcpHost) == nil {
iphost, err := proxy.DnsGetDoaminIP(tcpHost)
if nil != err {
return nil, err
}
hostport = net.JoinHostPort(iphost, tcpPort)
}
var quicSession quic.Session
udpAddr, err := net.ResolveUDPAddr("udp", hostport)
if err != nil {
return nil, err
}
udpConn, err := netx.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
return nil, err
}
quicConfig := &quic.Config{
KeepAlive: true,
}
quicSession, err = quic.Dial(udpConn, udpAddr, hostport, &tls.Config{InsecureSkipVerify: true}, quicConfig)
if err != nil {
return nil, err
}
logger.Debug("Connect %s success.", server)
return &mux.QUICMuxSession{Session: quicSession}, nil
}
func init() {
proxy.RegisterProxyType("quic", &QUICProxy{})
}