-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.go
176 lines (161 loc) · 3.08 KB
/
endpoint.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mdp
import (
"io"
"net"
"time"
"github.com/poohvpn/icmdp"
"github.com/poohvpn/pooh"
"github.com/rs/zerolog/log"
)
type endpointType byte
const (
endpointTCP endpointType = 0x6
endpointUDP endpointType = 0x11
endpointICMDP endpointType = 0x1
)
// auto-reconnect
type endpoint struct {
index uint64
typ endpointType
dst bool
addr *Addr
conn net.Conn
lastRecv time.Time
lastSent time.Time
recvCount int
sendCount int
}
func (e *endpoint) send(data []byte) (err error) {
if debug {
log.Debug().
Str("local", e.conn.LocalAddr().String()).
Str("remote", e.conn.RemoteAddr().String()).
Bytes("data", data).
Msg("endpoint.output")
}
defer func() {
if err != nil {
e.sendCount++
}
}()
_, err = e.conn.Write(data)
return
}
func (e *endpoint) recv(data []byte) bool {
e.lastRecv = time.Now()
e.recvCount++
return e.addr.sess.input(data, e.dst)
}
func (e *endpoint) run() {
if e.dst {
e.forwardLoop()
} else {
e.inputLoop()
}
e.drop()
}
func (e *endpoint) inputLoop() {
switch conn := e.conn.(type) {
case *writeOnlyConn:
// does not need to handle server side PacketConn, which is already received by PacketConn loop
return
default:
defer e.conn.Close()
buf := make([]byte, pooh.BufferSize)
for {
n, err := conn.Read(buf)
if err != nil {
if debug && err != io.EOF {
log.Debug().Err(err).Msg("mdp: endpoint.conn is broken")
}
return
}
if !e.recv(pooh.Duplicate(buf[:n])) {
return
}
}
}
}
func (e *endpoint) forwardLoop() {
for {
}
}
func (e *endpoint) dial() {
var (
conn net.Conn
err error
)
switch e.typ {
case endpointTCP:
conn, err = dialTcpDatagram(
&net.TCPAddr{
IP: e.addr.IP,
Port: e.addr.Port,
Zone: e.addr.Zone,
},
e.addr.sess.config.SessionID,
e.addr.sess.config.Obfuscator,
)
case endpointUDP:
conn, err = net.DialUDP("udp", nil, &net.UDPAddr{
IP: e.addr.IP,
Port: e.addr.Port,
Zone: e.addr.Zone,
})
case endpointICMDP:
if pooh.IsIPv4(e.addr.IP) {
conn, err = icmdp.DialICMDP("udp4", nil, &icmdp.Addr{
IP: e.addr.IP,
})
if err != nil {
conn, err = icmdp.DialICMDP("icmdp4", nil, &icmdp.Addr{
IP: e.addr.IP,
})
}
} else {
conn, err = icmdp.DialICMDP("udp6", nil, &icmdp.Addr{
IP: e.addr.IP,
Zone: e.addr.Zone,
})
if err != nil {
conn, err = icmdp.DialICMDP("icmdp6", nil, &icmdp.Addr{
IP: e.addr.IP,
Zone: e.addr.Zone,
})
}
}
default:
panic(e.typ)
}
if err != nil {
return
}
switch network {
case "udp4", "udp6", "icmdp4", "icmdp6":
conn = ob.ObfuscateDatagramConn(conn)
}
if s.localAddr == nil {
s.setInputAddr(conn.LocalAddr())
}
if s.srcAddr == nil {
s.setInputAddr(remote)
}
// append new conn to srcEndpoints
ep := &endpoint{
super: s,
conn: conn,
}
s.srcEndpoints.Store(connIndex(conn), ep)
go ep.run()
return
}
func (e *endpoint) drop() {
if e.dst {
e.addr.sess.srcEndpoints.Delete(e.index)
} else {
e.addr.sess.dstEndpoints.Delete(e.index)
}
}
func (e *endpoint) available() bool {
panic("implement me")
}