-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
159 lines (144 loc) · 3.38 KB
/
transport.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package kcp
import (
"context"
"fmt"
xkcp "github.com/xtaci/kcp-go/v5"
"github.com/xtaci/tcpraw"
"google.golang.org/grpc"
"net"
)
// Dialer for grpc client dial
func Dialer(c *Config) grpc.DialOption {
c.fixture()
fun := func(ctx context.Context, addr string) (kcp net.Conn, err error) {
block, err := getCryptBlock(c.Crypt, c.Key)
if err != nil {
return nil, err
}
var kcpconn *xkcp.UDPSession
if c.TCP {
conn, err := tcpraw.Dial("tcp", addr)
if err != nil {
return nil, err
}
kcpconn, err = xkcp.NewConn(addr, block, c.DataShard, c.ParityShard, conn)
} else {
kcpconn, err = xkcp.DialWithOptions(addr, block, c.DataShard, c.ParityShard)
}
if err != nil {
return nil, err
}
kcpconn.SetStreamMode(true)
kcpconn.SetWriteDelay(false)
kcpconn.SetNoDelay(c.NoDelay, c.Interval, c.Resend, c.NoCongestion)
kcpconn.SetWindowSize(c.SndWnd, c.RcvWnd)
kcpconn.SetMtu(c.MTU)
kcpconn.SetACKNoDelay(c.AckNodelay)
_ = kcpconn.SetDSCP(c.DSCP)
_ = kcpconn.SetReadBuffer(c.SockBuf)
_ = kcpconn.SetWriteBuffer(c.SockBuf)
return kcpconn, err
}
return grpc.WithContextDialer(fun)
}
type listener struct {
*xkcp.Listener
c *Config
}
func (l *listener) Accept() (net.Conn, error) {
conn, err := l.Listener.AcceptKCP()
if err != nil {
return nil, err
}
conn.SetStreamMode(true)
conn.SetWriteDelay(false)
conn.SetNoDelay(l.c.NoDelay, l.c.Interval, l.c.Resend, l.c.NoCongestion)
conn.SetMtu(l.c.MTU)
conn.SetWindowSize(l.c.SndWnd, l.c.RcvWnd)
conn.SetACKNoDelay(l.c.AckNodelay)
return conn, nil
}
// ServeGrpc for grpc server
func ServeGrpc(address string, server *grpc.Server, c *Config) error {
// tcp mode
if c.TCP {
lis, err := Listener(address, c)
if err != nil {
return err
}
return server.Serve(lis)
}
// udp mode
laddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return err
}
if !laddr.IP.IsUnspecified() {
listen, err := Listener(address, c)
if err != nil {
return err
}
return server.Serve(listen)
}
ipList := getLocalAddress()
localIp := ipList[0]
ipList = ipList[1:]
for _, ip := range ipList {
addr := fmt.Sprintf("%s:%d", ip, laddr.Port)
listen, err := Listener(addr, c)
if err != nil {
return err
}
go func() {
_ = server.Serve(listen)
}()
}
addr := fmt.Sprintf("%s:%d", localIp, laddr.Port)
listen, err := Listener(addr, c)
if err != nil {
return err
}
return server.Serve(listen)
}
func getLocalAddress() []string {
ips := make([]string, 0)
addrs, _ := net.InterfaceAddrs()
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.String())
}
}
}
return ips
}
// Listener Listen for grpc server listen, addr is like "127.0.0.1:8080"
func Listener(address string, c *Config) (l net.Listener, err error) {
c.fixture()
block, err := getCryptBlock(c.Crypt, c.Key)
if err != nil {
return nil, err
}
var ln *xkcp.Listener
if c.TCP {
var conn *tcpraw.TCPConn
conn, err = tcpraw.Listen("tcp", address)
if err != nil {
return nil, err
}
ln, err = xkcp.ServeConn(block, c.DataShard, c.ParityShard, conn)
} else {
ln, err = xkcp.ListenWithOptions(address, block, c.DataShard, c.ParityShard)
}
if err != nil {
return nil, err
}
_ = ln.SetDSCP(c.DSCP)
_ = ln.SetReadBuffer(c.SockBuf)
_ = ln.SetWriteBuffer(c.SockBuf)
l = &listener{
Listener: ln,
c: c,
}
return
}