-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (212 loc) · 4.76 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net"
"regexp"
)
type Server struct {
Address string `json:"address"`
Regexp string `json:"regexp"`
addr *net.TCPAddr
regexp *regexp.Regexp
name string
}
type Config struct {
Deadline int `json:"deadline"`
Address string `json:"host"`
Servers map[string]*Server `json:"servers"`
}
func main() {
REQUEST, PONG := []byte{1, 0}, []byte{9, 1, 0, 0, 0, 0, 0, 0, 0, 0}
data, err := ioutil.ReadFile("./config.json")
config := Config{
30000,
":25565",
map[string]*Server{
"default": {":25566", ".", nil, nil, "default"},
},
}
if err != nil {
data, err = json.MarshalIndent(&config, "", " ")
err = ioutil.WriteFile("./config.json", data, 0644)
if err != nil {
log.Panicln(err)
}
}
err = json.Unmarshal(data, &config)
if err != nil {
log.Panicln(err)
}
notOnlyOne := len(config.Servers) > 1
for key, server := range config.Servers {
addr, err := net.ResolveTCPAddr("tcp", server.Address)
if err != nil {
log.Panicln(err)
}
server.addr = addr
server.regexp = regexp.MustCompile(server.Regexp)
server.name = key
}
defaultServer, ok := config.Servers["default"]
if !ok {
log.Panicln(errors.New("no a feedback server named default"))
}
tcpAddr, err := net.ResolveTCPAddr("tcp", config.Address)
if err != nil {
log.Panicln(err)
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Panicln(err)
}
log.Println("Listening on:", config.Address)
for {
conn, err := listener.AcceptTCP()
if err != nil {
log.Println(err)
continue
}
//err = conn.SetKeepAlive(true)
if err != nil {
continue
}
go func() {
// noinspection GoUnhandledErrorResult
defer conn.Close()
arr := make([]byte, 5)
_, err := conn.Read(arr)
if err != nil || arr[0] == 254 || arr[1] != 0 ||
arr[2] < 127 || // Protocol Version < 200 (> 1.9.*)
arr[3] > 100 {
return
} // Server Address length > 127
allLen := arr[0] - 4
if allLen < 6 || allLen > 70 {
return
}
received := make([]byte, allLen)
_, err = conn.Read(received)
if err != nil {
return
}
state := received[allLen-1]
switch state {
case 1:
data := make([]byte, 2)
_, err = conn.Read(data)
if err != nil || data[0] != 1 || data[1] != 0 {
return
}
case 2:
default:
return
}
port := uint16(received[allLen-3])<<8 | uint16(received[allLen-2])
if port > 65535 || port == 0 {
return
}
address := string(received[0:(allLen - 3)])
matched := defaultServer
if notOnlyOne {
for _, server := range config.Servers {
if server.regexp.MatchString(address + ":" + string(port)) {
matched = server
}
}
}
src := conn.RemoteAddr().String()
if state == 2 {
log.Println("New connection:", src, "->", matched.name)
}
remote, err := net.DialTCP("tcp", nil, matched.addr)
if err != nil {
log.Println(err)
return
}
// noinspection GoUnhandledErrorResult
defer remote.Close()
switch state {
case 1:
_, err = remote.Write(arr)
if err != nil {
return
}
_, err = remote.Write(received)
if err != nil {
return
}
// noinspection GoUnhandledErrorResult
_, err = remote.Write(REQUEST)
if err != nil {
return
}
arr := make([]byte, 1)
_, err = remote.Read(arr)
if err != nil {
return
}
arr0, arr1 := arr[0], byte(0)
length := int(arr0)
if length > 127 {
_, err = remote.Read(arr)
if err != nil {
return
}
arr1 = arr[0]
length = 0 | (length & 0b01111111) | (int(arr1) << 7)
}
data := make([]byte, length)
size, err := io.ReadFull(remote, data)
if err != nil || size != length {
return
}
if arr1 == 0 {
_, err = conn.Write(arr)
} else {
_, err = conn.Write([]byte{arr0, arr1})
}
if err != nil {
return
}
_, _ = conn.Write(data)
data = make([]byte, 10)
_, err = conn.Read(data)
if err != nil || data[0] != 9 || data[1] != 1 {
return
}
_, _ = conn.Write(PONG)
case 2:
host, _, err := net.SplitHostPort(src)
if err != nil {
return
}
address += "\x00"
address += host
address += "\x0000000000-0000-0000-0000-000000000000"
addressBytes := []byte(address)
length := len(addressBytes)
arr[0] = byte(7 + length)
arr[4] = byte(length)
_, err = remote.Write(arr)
if err != nil {
return
}
_, err = remote.Write(addressBytes)
if err != nil {
return
}
_, err = remote.Write([]byte{received[allLen-3], received[allLen-2], 2})
if err != nil {
return
}
// noinspection GoUnhandledErrorResult
go io.Copy(conn, remote)
_, err = io.Copy(remote, conn)
}
}()
}
}