-
Notifications
You must be signed in to change notification settings - Fork 4
/
proxy.go
206 lines (168 loc) · 4.55 KB
/
proxy.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package minecraft
import (
"errors"
"fmt"
"github.com/aimjel/minecraft/packet"
"github.com/aimjel/minecraft/protocol/encoding"
"net"
)
type ProxyConfig struct {
// OnReceive called when a packet is received from the client or server.
// Returning false will drop the packet.
OnReceive func(conn *Conn, pk packet.Packet, fromServer bool, state int) bool
//ErrCh receives errors from the client and server
ErrCh chan ProxyError
}
type ProxyListener struct {
cfg *ProxyConfig
ln *net.TCPListener
}
type ProxyError struct {
//State is the protocol state the error occurred in
State int
Addr net.Addr
Err error
}
func (p ProxyError) Error() string {
return fmt.Sprintf("[%v] %v: %v", p.State, p.Addr, p.Err)
}
func (cfg *ProxyConfig) Listen(addr, targetAddr string) (*ProxyListener, error) {
ln, err := net.Listen("tcp4", addr)
if err != nil {
return nil, err
}
go func() {
for {
c, err := ln.Accept()
if err != nil {
fmt.Println(err)
if errors.Is(err, net.ErrClosed) {
fmt.Println(err)
return
}
}
go cfg.handleConn(newConn(c.(*net.TCPConn)), targetAddr)
}
}()
return &ProxyListener{cfg: cfg, ln: ln.(*net.TCPListener)}, nil
}
func (l *ProxyListener) Close() error {
return l.ln.Close()
}
func (cfg *ProxyConfig) handleConn(client *Conn, targetAddr string) {
var hs packet.Handshake
if err := client.DecodePacket(&hs); err != nil {
cfg.ErrCh <- ProxyError{State: 0, Addr: client.RemoteAddr(), Err: err}
client.Close(nil)
return
}
if hs.NextState != 1 && hs.NextState != 2 && hs.ProtocolVersion != 763 {
client.Close(nil)
return
}
dialC, err := net.Dial("tcp4", targetAddr)
if err != nil {
cfg.ErrCh <- ProxyError{State: 0, Addr: client.RemoteAddr(), Err: err}
client.Close(nil)
return
}
serverConn := newConn(dialC.(*net.TCPConn))
if err = serverConn.SendPacket(&hs); err != nil {
cfg.ErrCh <- ProxyError{State: 0, Addr: serverConn.RemoteAddr(), Err: err}
client.Close(nil)
return
}
switch hs.NextState {
case 0x01:
cfg.proxy(client, serverConn)
client.Close(nil)
serverConn.Close(nil)
return
case 0x02:
if err = cfg.handleLogin(client, serverConn); err != nil {
cfg.ErrCh <- err.(ProxyError)
client.Close(nil)
return
}
cfg.proxy(client, serverConn)
}
}
func (cfg *ProxyConfig) handleLogin(client, serverConn *Conn) error {
var ls packet.LoginStart
if err := client.DecodePacket(&ls); err != nil {
return ProxyError{State: 2, Addr: client.RemoteAddr(), Err: err}
}
if err := serverConn.SendPacket(&ls); err != nil {
return ProxyError{State: 2, Addr: client.RemoteAddr(), Err: err}
}
for {
pack, err := serverConn.ReadPacket()
if err != nil {
return ProxyError{State: 2, Addr: serverConn.RemoteAddr(), Err: err}
}
pk := pack.(packet.Unknown)
switch pk.ID() {
//encryption request
case 0x01:
serverConn.Close(nil)
client.Close(nil)
return ProxyError{State: 2, Addr: client.RemoteAddr(), Err: fmt.Errorf("online mode is not supported")}
case 0x02:
var lgSuc packet.LoginSuccess
if err = lgSuc.Decode(encoding.NewReader(pk.Payload)); err != nil {
return ProxyError{State: 2, Addr: client.RemoteAddr(), Err: err}
}
serverConn.name, client.name = lgSuc.Name, lgSuc.Name
serverConn.uuid, client.uuid = lgSuc.UUID, lgSuc.UUID
serverConn.properties, client.properties = lgSuc.Properties, client.properties
if err = client.SendPacket(&lgSuc); err != nil {
return ProxyError{State: 2, Addr: client.RemoteAddr(), Err: err}
}
return nil
case 0x03:
var com packet.SetCompression
if err = com.Decode(encoding.NewReader(pk.Payload)); err != nil {
return ProxyError{State: 2, Addr: client.RemoteAddr(), Err: err}
}
if err = client.SendPacket(&com); err != nil {
return ProxyError{State: 2, Addr: client.RemoteAddr(), Err: err}
}
serverConn.enableCompression(com.Threshold)
client.enableCompression(com.Threshold)
}
}
}
func (cfg *ProxyConfig) proxy(client, serverConn *Conn) {
go func(srv, client *Conn) {
//reads from the server and forwards to the client
for {
pk, err := srv.ReadPacket()
if err != nil {
return
}
if cfg.OnReceive != nil {
if !cfg.OnReceive(srv, pk, true, 3) {
continue
}
}
if err = client.SendPacket(pk); err != nil {
return
}
}
}(serverConn, client)
//reads from the client and forwards to the target server
for {
pk, err := client.ReadPacket()
if err != nil {
return
}
if cfg.OnReceive != nil {
if !cfg.OnReceive(client, pk, false, 3) {
continue
}
}
if err = serverConn.SendPacket(pk); err != nil {
return
}
}
}