-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
135 lines (115 loc) · 2.93 KB
/
server.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
package main
import (
"bufio"
"net"
"regexp"
"github.com/rs/zerolog/log"
)
type WhitelistEntry struct {
IP string
Port int
}
type AuthenticatedClient struct {
conn net.Conn
serverID int
serverInfo *ServerInfo
initialized bool
hidden bool
}
type ServerInfo struct {
Group string
IP string
Name string
Port string
Hidden bool
}
func handleConnection(conn net.Conn, whitelist []WhitelistEntry) {
remoteAddr, ok := conn.RemoteAddr().(*net.TCPAddr)
if !ok {
log.Warn().Msg("Could not get remote TCPAddr, rejecting")
_ = conn.Close()
return
}
ip := remoteAddr.IP.String()
port := remoteAddr.Port
if !isWhitelistedOrPrivate(ip, port, whitelist) {
log.Warn().Str("ip", ip).Int("port", port).
Msg("Connection not whitelisted, closing")
_ = conn.Close()
return
}
client := &AuthenticatedClient{
conn: conn,
serverID: findSpareServerID(),
}
addClient(client)
log.Info().Str("ip", ip).Int("port", port).
Msg("New client connection established")
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
line := scanner.Text()
log.Debug().
Str("from", ip).
Int("port", port).
Str("msg", line).
Msg("Received line")
if err := handleLine(client, line); err != nil {
log.Error().Err(err).Msg("Failed to handle line")
}
}
if err := scanner.Err(); err != nil {
log.Error().Err(err).Msg("Scanner error on connection read")
}
onClientDisconnect(client)
}
func onClientDisconnect(client *AuthenticatedClient) {
_ = client.conn.Close()
removeClient(client)
if client.initialized && !client.hidden && client.serverInfo != nil {
discMsg := Message{
Context: "Disconnect",
Data: client.serverID,
Target: "i",
}
broadcastGroup(client, &discMsg)
log.Info().
Str("ip", client.serverInfo.IP).
Str("port", client.serverInfo.Port).
Msg("Client disconnected")
} else {
log.Info().
Str("addr", client.conn.RemoteAddr().String()).
Msg("Uninitialized or hidden client disconnected")
}
}
func isWhitelistedOrPrivate(ip string, port int, whitelist []WhitelistEntry) bool {
if isPrivateIP(ip) {
return true
}
for _, w := range whitelist {
if w.IP == ip && w.Port == port {
return true
}
}
return false
}
// isPrivateIP uses regex checks
var privateIPRegexes = []*regexp.Regexp{
regexp.MustCompile(`^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$`),
regexp.MustCompile(`^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$`),
regexp.MustCompile(`^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$`),
regexp.MustCompile(`^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$`),
regexp.MustCompile(`^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$`),
regexp.MustCompile(`^f[cd][0-9a-f]{2}:`),
regexp.MustCompile(`^fe80:`),
regexp.MustCompile(`^::1$`),
regexp.MustCompile(`^::$`),
}
func isPrivateIP(addr string) bool {
for _, r := range privateIPRegexes {
if r.MatchString(addr) {
return true
}
}
return false
}