-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-redirect.go
215 lines (180 loc) · 6.41 KB
/
http-redirect.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
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Inbound support for the HTTP V1 safecast topic
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
ttdata "github.com/Safecast/safecast-go"
)
// Debugging
const redirectDebug bool = true
// Handle inbound HTTP requests from the Teletype Gateway
func inboundWebRedirectHandler(rw http.ResponseWriter, req *http.Request) {
var sdV1 *SafecastDataV1
var sdV1Emit *SafecastDataV1ToEmit
var body []byte
var err error
// Remember when it was uploaded to us
UploadedAt := NowInUTC()
// Process the request URI, looking for things that will indicate "dev"
method := req.Method
if method == "" {
method = "GET"
}
// See if this is a test measurement
isTestMeasurement := strings.Contains(req.RequestURI, "test")
// Get the remote address, and only add this to the count if it's likely from
// the internal HTTP load balancer.
_, isReal, abusive := getRequestorIPv4(req)
if abusive {
return
}
// If this is a GET (a V1 Pointcast 3G), convert RequestURI into valid json
RequestURI := req.RequestURI
if method == "GET" {
// Before: /scripts/shorttest.php?api_key=q1LKu7RQ8s5pmyxunnDW&lat=34.4883&lon=136.165&cpm=0&id=100031&alt=535
// After: {"unit":"cpm","latitude":"34.4883","longitude":"136.165","value":"0","device_id":"100031","height":"535"}
str1 := strings.SplitN(RequestURI, "&", 2)
RequestURI = str1[0]
if len(str1) == 1 {
body = []byte("")
} else {
str2 := str1[len(str1)-1]
str3 := "unit=cpm&" + str2
str4 := strings.Replace(str3, "lat=", "latitude=", 1)
str5 := strings.Replace(str4, "lon=", "longitude=", 1)
str6 := strings.Replace(str5, "alt=", "height=", 1)
str7 := strings.Replace(str6, "cpm=", "value=", 1)
str8 := strings.Replace(str7, "id=", "device_id=", 1)
str9 := strings.Replace(str8, "typ=", "devicetype_id=", 1)
str10 := strings.Replace(str9, "=", "\":\"", -1)
str11 := strings.Replace(str10, "&", "\",\"", -1)
body = []byte("{\"" + str11 + "\"}")
}
} else {
// Read the body as a byte array
body, err = io.ReadAll(req.Body)
if err != nil {
stats.Count.HTTP++
fmt.Printf("Error reading HTTP request body: \n%v\n", req)
return
}
}
// Clean up the json. Specifically, Device ID 100049 puts a newline into
// the devietype_id string literal, which is choked on by the JSON parser
cleanBodyStr := string(body)
cleanBodyStr = strings.Replace(cleanBodyStr, "\n", "", -1)
cleanBodyStr = strings.Replace(cleanBodyStr, "\r", "", -1)
cleanBodyStr = strings.Replace(cleanBodyStr, "\\r", "", -1)
cleanBodyStr = strings.Replace(cleanBodyStr, "\":\" ", "\":\"", -1)
cleanBody := []byte(cleanBodyStr)
// Decode the request with custom marshaling
sdV1, sdV1Emit, err = SafecastV1Decode(bytes.NewReader(cleanBody))
if err != nil {
stats.Count.HTTP++
// Eliminate a bit of the noise caused by load balancer health checks
if isReal && req.RequestURI != "/" && req.RequestURI != "/favicon.ico" {
// See if this is nothing but a device ID
deviceUID := req.RequestURI[len("/"):]
file := GetDeviceStatusFilePath(deviceUID)
contents, err := os.ReadFile(file)
if err == nil {
GenerateDeviceSummaryWebPage(rw, contents)
return
}
io.WriteString(rw, fmt.Sprintf("Unknown device: %s\n", deviceUID))
return
}
io.WriteString(rw, "Live Free or Die.\n")
return
}
// A real request
stats.Count.HTTP++
// Fill in the minimum defaults
if sdV1.Unit == nil {
s := "cpm"
sdV1.Unit = &s
sdV1Emit.Unit = &s
}
if sdV1.Value == nil {
f64 := float64(0)
sdV1.Value = &f64
str := fmt.Sprintf("%f", f64)
sdV1Emit.Value = &str
}
if sdV1.CapturedAt == nil {
capturedAt := NowInUTC()
sdV1.CapturedAt = &capturedAt
sdV1Emit.CapturedAt = &capturedAt
}
// Debugging on 2017-06-24 with Rob; feel free to delete after 2017-07-01 if it's still here
if false {
if sdV1.DeviceID != nil {
devicetype, _, _ := SafecastV1DeviceType(*sdV1.DeviceID)
if devicetype == "safecast-air" {
fmt.Printf("*** DeviceID %d %t %s\n", *sdV1.DeviceID, isTestMeasurement, req.RequestURI)
}
}
}
// Convert it to text to emit
sdV1EmitJSON, _ := json.Marshal(sdV1Emit)
// If debugging, display it
if redirectDebug {
fmt.Printf("\n\n*** Redirect %s test:%v %s\n", method, isTestMeasurement, req.RequestURI)
fmt.Printf("*** Redirect received:\n%s\n", string(cleanBody))
fmt.Printf("*** Redirect decoded to V1:\n%s\n", sdV1EmitJSON)
}
// For backward compatibility,post it to V1 with an URL that is preserved. Also do normal post
_, result := SafecastV1Upload(sdV1EmitJSON, RequestURI, isTestMeasurement, *sdV1.Unit, fmt.Sprintf("%.3f", *sdV1.Value))
// Send a reply to Pointcast saying that the request was processed acceptably.
// If we fail to do this, Pointcast goes into an infinite reboot loop with comms errors
// due to GetMeasurementReply() returning 0.
io.WriteString(rw, result)
// Convert to current data format
deviceID, deviceType, sd := SafecastReformatFromV1(sdV1, isTestMeasurement)
if deviceID == 0 {
requestor, _, abusive := getRequestorIPv4(req)
if abusive {
return
}
transportStr := deviceType + ":" + requestor
fmt.Printf("\n%s ** Ignoring message with DeviceID == 0 from %s:\n%s\n", LogTime(), transportStr, string(cleanBody))
return
}
// If debugging, display it
if redirectDebug {
scJSON, _ := json.Marshal(sd)
fmt.Printf("*** Redirect reformatted to V2:\n%s\n\n\n", scJSON)
}
// Report where we got it from, and when we got it
var svc ttdata.Service
svc.UploadedAt = &UploadedAt
requestor, _, abusive := getRequestorIPv4(req)
if abusive {
return
}
transportStr := deviceType + ":" + requestor
svc.Transport = &transportStr
sd.Service = &svc
fmt.Printf("\n%s Received payload for %s from %s\n", LogTime(), sd.DeviceUID, transportStr)
fmt.Printf("%s\n", cleanBody)
// If the data doesn't have anything useful in it, optimize it completely away. This is
// observed to happen for Safecast Air from time to time
if sd.Opc == nil && sd.Pms == nil && sd.Pms2 == nil && sd.Env == nil && sd.Lnd == nil && sd.Bat == nil && sd.Dev == nil {
fmt.Printf("%s *** Ignoring because message contains no data\n", LogTime())
return
}
// If this is an air reading, annotate it with AQI if possible
aqiCalculate(&sd)
// Post to V2
SafecastUpload(sd)
SafecastLog(sd)
stats.Count.HTTPRedirect++
}