-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.go
167 lines (132 loc) · 4.09 KB
/
connection.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
package goproxy
import (
"net"
"github.com/Irmine/GoRakLib/protocol"
)
const (
IdUnconnectedPingOpenConnection = 0x01 //Client to Server
IdUnconnectedPongOpenConnection = 0x1c //Server to Client as response to IdConnectedPingOpenConnection (0x01)
IdOpenConnectionRequest1 = 0x05 //Client to Server
IdOpenConnectionReply1 = 0x06 //Server to Client
IdOpenConnectionRequest2 = 0x07 //Client to Server
IdOpenConnectionReply2 = 0x08 //Server to Client
)
type Connection struct {
Proxy *Proxy
Client *Client
Server *Server
pkHandler *PacketHandler
}
func NewConnection(proxy *Proxy) Connection {
conn := Connection{}
conn.Proxy = proxy
conn.Client = NewClient(proxy, &conn)
conn.Server = NewServer(proxy, &conn)
conn.pkHandler = NewPacketHandler()
conn.pkHandler.Conn = &conn
RegisterDefaultHandlers(conn.pkHandler)
return conn
}
func (conn *Connection) GetClient() *Client {
return conn.Client
}
func (conn *Connection) GetServer() *Server {
return conn.Server
}
func (conn *Connection) GetPacketHandler() *PacketHandler {
return conn.pkHandler
}
func (conn *Connection) IsClient(addr net.UDPAddr) bool {
return conn.Client.addr.IP.Equal(addr.IP)
}
func (conn *Connection) IsServer(addr net.UDPAddr) bool {
return conn.Server.GetAddress().IP.Equal(addr.IP)
}
func (conn *Connection) handleUnconnectedPing(addr net.UDPAddr, buffer []byte) {
conn.Client.SetAddress(addr)
conn.Server.WritePacket(buffer)
//stringAddr := addr.IP.String()
//Info("Received unconnected ping from Client address: " + stringAddr)
}
func (conn *Connection) handleUnconnectedPong(addr net.UDPAddr, buffer []byte) {
pk := protocol.NewUnconnectedPong()
pk.SetBuffer(buffer)
pk.Decode()
pk.Encode()
conn.Client.WritePacket(pk.Buffer)
conn.Server.Data.ParseFromString(pk.PongData)
//stringAddr := addr.IP.String()
//Info("Received unconnected pong from Server address: " + stringAddr)
}
func (conn *Connection) handleConnectionRequest1(addr net.UDPAddr, buffer []byte) {
conn.Client.SetAddress(addr)
conn.Server.WritePacket(buffer)
//stringAddr := addr.IP.String()
//
//Info("Received Connection request 1 from Client address: " + stringAddr)
}
func (conn *Connection) handleConnectionReply1(addr net.UDPAddr, buffer []byte) {
//stringAddr := addr.IP.String()
//Info("Received Connection reply 1 from Server address: " + stringAddr)
pk := protocol.NewOpenConnectionReply1()
pk.SetBuffer(buffer)
pk.Decode()
pk.Encode()
conn.Client.WritePacket(pk.Buffer)
}
func (conn *Connection) handleConnectionRequest2(addr net.UDPAddr, buffer []byte) {
conn.Client.SetAddress(addr)
conn.Server.WritePacket(buffer)
//stringAddr := addr.IP.String()
//
//Info("Received Connection request 2 from Client address: " + stringAddr)
}
func (conn *Connection) handleConnectionReply2(addr net.UDPAddr, buffer []byte) {
conn.Client.SetConnected(true)
//stringAddr := addr.IP.String()
//Info("Received Connection reply 2 from Server address: " + stringAddr)
pk := protocol.NewOpenConnectionReply2()
pk.SetBuffer(buffer)
pk.Decode()
pk.Encode()
conn.Client.WritePacket(pk.Buffer)
}
func (conn *Connection) HandleIncomingPackets() {
for {
buffer := make([]byte, 2048)
_, addr, err := conn.Proxy.UDPConn.ReadFromUDP(buffer)
if err != nil {
Alert(err.Error())
continue
}
MessageId := buffer[0]
if conn.Client.IsConnected() {
if conn.IsServer(*addr) {
conn.pkHandler.HandleIncomingPacket(buffer, conn.Client, *addr)
} else {
conn.pkHandler.HandleIncomingPacket(buffer, conn.Server, *addr)
}
continue
}
switch MessageId {
case byte(IdUnconnectedPingOpenConnection):
conn.handleUnconnectedPing(*addr, buffer)
break
case byte(IdUnconnectedPongOpenConnection):
conn.handleUnconnectedPong(*addr, buffer)
break
case byte(IdOpenConnectionRequest1):
conn.handleConnectionRequest1(*addr, buffer)
break
case byte(IdOpenConnectionReply1):
conn.handleConnectionReply1(*addr, buffer)
break
case byte(IdOpenConnectionRequest2):
conn.handleConnectionRequest2(*addr, buffer)
break
case byte(IdOpenConnectionReply2):
conn.handleConnectionReply2(*addr, buffer)
break
}
}
}