-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinkhole.go
156 lines (121 loc) · 3.37 KB
/
sinkhole.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
package main
import (
"fmt"
"net"
"os"
"time"
"encoding/hex"
"encoding/json"
"bytes"
"bufio"
"regexp"
)
const (
CONN_HOST = "0.0.0.0"
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
type log_req struct {
F_src_ip string `json:"src_ip,omitempty"`
F_src_port uint16 `json:"src_port,omitempty"`
F_dst_name string `json:"dst_name,omitempty"`
F_url_path string `json:"url_path,omitempty"`
F_bytes_client uint32 `json:"bytes_client,omitempty"`
F_http_method string `json:"http_method,omitempty"`
F_http_version string `json:"http_version,omitempty"`
F_x_forwarded_for string `json:"x_forwarded_for,omitempty"`
F_http_referer string `json:"http_referer,omitempty"`
F_http_user_agent string `json:"http_referer,omitempty"`
}
func main() {
// Listen for incoming connections.
sock, err := net.Listen(CONN_TYPE, CONN_HOST + ":" + CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
// Close the listener when the application closes.
defer sock.Close()
fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
for {
// Listen for an incoming connection.
conn, err := sock.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go handleRequest(conn)
}
}
// Handles incoming requests.
func handleRequest(conn net.Conn) {
var req log_req
// Close the socket when we're done
defer conn.Close()
// Make a buffer to hold incoming data.
buf := make([]byte, 4096)
// Limit the time we'll spend reading
conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
// Read the incoming connection into the buffer.
readlen, err := conn.Read(buf)
if (err != nil) {
fmt.Println("Error reading:", err.Error())
return
}
hexstr := make([]byte, hex.EncodedLen(readlen))
hex.Encode(hexstr, buf[:readlen])
fmt.Printf("Recieved %s\n", hexstr)
bufreader := bufio.NewReader(bytes.NewReader(buf[:readlen]))
req_re := regexp.MustCompile(`^(GET)\s(\S+)\s(HTTP\/1\.[01])$`)
// read first line of HTTP request
bufline, lineprefix, err := bufreader.ReadLine()
if (err == nil) {
if (lineprefix == false) {
fmt.Printf("Got first line: %s\n", bufline)
matches := req_re.FindStringSubmatch(string(bufline))
if (matches != nil) {
fmt.Printf("method: %s; path: %s; ver: %s\n",
matches[1], matches[2], matches[3])
req.F_http_method = string(matches[1])
req.F_url_path = string(matches[2])
req.F_http_version = string(matches[3])
} else {
fmt.Printf("Got nil matches!\n")
}
} else {
fmt.Printf("Got truncated first line: %s\n", bufline)
}
} else {
return; // Couldn't read first line
}
header_re := regexp.MustCompile(`^([A-Za-z][A-Za-z0-9-]*):\s(.*)$`)
// Read any headers
for {
bufline, lineprefix, err := bufreader.ReadLine()
if (err != nil) {
break;
}
if (lineprefix == true) {
break;
}
bufstr := string(bufline)
if (bufstr == "") {
break;
}
matches := header_re.FindStringSubmatch(bufstr)
if (matches != nil) {
fmt.Printf("Header: %s; Value: %s\n",
matches[1], matches[2])
} else {
break;
}
}
fmt.Printf("Struct method is %s\n", req.F_http_method)
json_req, err := json.Marshal(req)
if (err == nil) {
fmt.Printf("JSON: %s\n", json_req)
}
// Send a response back to person contacting us.
conn.Write([]byte("Message received.\r\n"))
}