-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
104 lines (85 loc) · 2.74 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
// Copyright (C) 2024 Christian Rößner
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bufio"
"fmt"
"net"
"strings"
"github.com/go-kit/log/level"
"github.com/segmentio/ksuid"
)
func clientConnections(listener net.Listener) chan net.Conn {
clientConnectionsChan := make(chan net.Conn)
go func() {
for {
client, err := listener.Accept()
if client == nil {
level.Error(logger).Log("error", err.Error())
continue
}
level.Debug(logger).Log("msg", "Client connected", "client_ip", client.RemoteAddr().String())
clientConnectionsChan <- client
}
}()
return clientConnectionsChan
}
//goland:noinspection GoUnhandledErrorResult
func handleConnection(client net.Conn) {
b := bufio.NewReader(client)
policyRequest := make(map[string]string)
for {
lineBytes, err := b.ReadBytes('\n')
if err != nil { // EOF, or worse
level.Debug(logger).Log("msg", "Client disconnected", "client_ip", client.RemoteAddr().String())
client.Close()
break
}
lineStr := strings.TrimSpace(string(lineBytes))
//nolint:gomnd // Split into key and "list" of values
items := strings.SplitN(lineStr, "=", 2)
//nolint:gomnd // Either items is a key=value pair or it is empty, indicating the end of the request
if len(items) == 2 {
policyRequest[strings.TrimSpace(items[0])] = strings.TrimSpace(items[1])
} else {
var (
prefix string
actionText string
policyResponse *PolicyResponse
)
policyResponse, err = getPolicyResponse(policyRequest, ksuid.New().String())
if err != nil {
prefix = "DEFER "
actionText = deferText
level.Error(logger).Log("error", err.Error())
} else {
if policyResponse.fired {
prefix = "REJECT "
actionText = rejectText
} else {
if policyResponse.whitelisted {
prefix = "INFO "
actionText = fmt.Sprintf("Client IP address <%s> is defined in ignore-networks", policyRequest["client_address"])
} else {
prefix = "DUNNO"
}
}
}
client.Write([]byte(fmt.Sprintf("action=%s%s\n\n", prefix, actionText)))
// Clear policy request for next connection
policyRequest = make(map[string]string)
}
}
}