-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirehoseTest.go
222 lines (181 loc) · 4.87 KB
/
firehoseTest.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
package main
import (
"bufio"
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/json"
"io/ioutil"
"log"
)
type AircraftData map[string]Aircraft
type Aircraft struct {
Adshex string `json:"adshex"`
Callsign string `json:"callsign"`
OriginalCallsign *string `json:"original_callsign"`
Altitude int `json:"altitude"`
Heading int `json:"heading"`
VertRate int `json:"vert_rate"`
Speed int `json:"speed"`
Squawk string `json:"squawk"`
Category string `json:"category"`
IsOnGround bool `json:"is_on_ground"`
DataSource int `json:"data_source"`
LastSeenTime int `json:"last_seen_time"`
PosUpdateTime int `json:"pos_update_time"`
FlightNumber string `json:"flight_number"`
Route *string `json:"route"`
IsBlocked bool `json:"is_blocked"`
Reg string `json:"reg"`
AcType string `json:"ac_type"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
StationID string `json:"station_id"`
}
const (
dle = 0x10
stx = 0x02
etx = 0x03
)
func main() {
// Address of remote endpoint
serverAddress := "hostname.planefinder.net:80"
// Construct authentication credentials
loginJSON := "{\"username\":\"USER\",\"password\":\"PASS\"}\n"
// Example config, can skip SSL verification but not recommended!
conf := &tls.Config{
InsecureSkipVerify: true,
}
// Connect to the remote server
conn, err := tls.Dial("tcp", serverAddress, conf)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
// Send the authentication payload
// Client will be disconnected if wrong details are sent
n, err := conn.Write([]byte(loginJSON))
if err != nil {
log.Println(n, err)
return
}
reader := bufio.NewReader(conn)
clientBuffer := new(bytes.Buffer)
for {
// Read a byte
byte, err := reader.ReadByte()
if err != nil {
conn.Close()
return
}
// Write the byte to a buffer
wrErr := clientBuffer.WriteByte(byte)
if wrErr != nil {
log.Println("Write Error:", wrErr)
}
// Check to see if we now have a valid packet in the buffer
packet := popPacketFromBuffer(clientBuffer)
if packet != nil {
// Buffer had a packet so place into a new buffer for decompression.
packetBuffer := bytes.NewBuffer(packet)
// Decompress the packet
jsonData, err := uncompress(packetBuffer.Bytes())
if err != nil {
log.Println("Unzip Error:", err)
} else {
// json will contain the json that needs parsing/processing
// Suggest processing on different thread to not hold this up!
var data AircraftData
err := json.Unmarshal(jsonData, &data)
if err != nil {
panic(err)
}
for key, value := range data {
log.Println(
key,
value.Reg,
value.Callsign,
value.Lat,
value.Lon)
}
}
}
}
}
func popPacketFromBuffer(buffer *bytes.Buffer) []byte {
bufferLength := buffer.Len()
if bufferLength >= 750000 {
log.Println("Buffer is too large ", bufferLength)
buffer.Reset()
return nil
}
tempBuffer := buffer.Bytes()
length := len(tempBuffer)
// Return on small packet length
if length < 3 {
return nil
}
if (tempBuffer[length-2] == dle) && (tempBuffer[length-1] == etx) {
dleCount := 0
for i := range tempBuffer {
// Skip the first one!
if i == 0 {
continue
}
if tempBuffer[len(tempBuffer)-1-i] == dle {
dleCount += 1
} else {
break
}
}
isEven := dleCount%2 == 0
// If this is even then this is not the end but a byte stuffed DLE packet
if isEven == true {
return nil
}
// Grab the contents of the provided packet
extractedPacket := buffer.Bytes()
// Clear the main buffer now we have extracted a packet from it
buffer.Reset()
// Ensure packet begins with a valid startDelimiter
if extractedPacket[0] != dle && extractedPacket[1] != stx {
log.Println("Popped a packet without a valid start delimiter", extractedPacket)
return nil
}
// Remove the start and end caps
slice := extractedPacket[2 : len(extractedPacket)-2]
return deStuffPacket(slice)
}
return nil
}
// Removes duplicate delimiters from the packet
func deStuffPacket(packet []byte) []byte {
lengthOfPacket := len(packet)
newByteArray := new(bytes.Buffer)
for i := 0; i < lengthOfPacket; i++ {
if packet[i] == dle && packet[i+1] == dle {
newByteArray.WriteByte(packet[i])
i++
} else {
newByteArray.WriteByte(packet[i])
}
}
return newByteArray.Bytes()
}
// Uses gzip to uncompress the packet
// Packet should already be destuffed
func uncompress(packet []byte) ([]byte, error) {
r, gzErr := gzip.NewReader(bytes.NewBuffer(packet))
if gzErr != nil {
log.Println("Gzip Error:", gzErr)
blankBytes := []byte{}
return blankBytes, gzErr
}
defer r.Close()
bytesRead, err := ioutil.ReadAll(r)
if err != nil {
log.Println(err)
}
return bytesRead, err
}