-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
238 lines (218 loc) · 8.16 KB
/
index.js
File metadata and controls
238 lines (218 loc) · 8.16 KB
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
const express = require("express");
const app = express();
const fetch = require("node-fetch").default;
const puppeteer = require("puppeteer");
if (process.env.ENABLE_UI) {
app.use(express.static('./ui/public'))
}
app.get("/api", async function (req, res) {
res.redirect("https://github.com/aboutdavid/hctb-api")
})
app.post("/api/session", async function (req, res) {
const { cookie, person, name, time } = req.query
if (!cookie || !person || !name || !time) return res.json({ success: false, error: "Please provide all query object" }).status(400)
fetch("https://login.herecomesthebus.com/Map.aspx/RefreshMap", {
headers: {
accept: "application/json, text/javascript, */*; q=0.01",
"accept-language": "en-US,en;q=0.9,ja;q=0.8",
"content-type": "application/json; charset=UTF-8",
"sec-ch-ua":
'"Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
cookie: cookie,
Referer: "https://login.herecomesthebus.com/Map.aspx",
"Referrer-Policy": "strict-origin-when-cross-origin",
},
body: JSON.stringify({
legacyID: person,
name: name,
timeSpanId: time,
wait: "false",
}),
method: "POST",
})
.then((resp) => resp.json())
.then((json) => {
var lat, lon
if (json.d.includes("SetBusPushPin")) {
var strip = /SetBusPushPin\(([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?,([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?\)/i.exec("SetBusPushPin(123.456,679.012)" + json.d)[0]
var coords = /([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?,([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?/i.exec(strip)[0].split(",")
lat = coords[0]
lon = coords[1]
}
res.json({ success: true, ...value, lat: lat, lon: lon, cookie })
});
})
app.post("/api/passengers", async function (req, res) {
if (!req.query.user || !req.query.pass || !req.query.code)
return res.json({
success: false,
error: "Please provide all credentials",
}).status(400);
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
});
console.log("[Info] Browser instance started");
const page = await browser.newPage();
console.log("[Info] Opened a new page");
await page.goto("https://login.herecomesthebus.com/Authenticate.aspx");
await page.type(
`input[name="ctl00$ctl00$cphWrapper$cphContent$tbxUserName"]`,
req.query.user
);
await page.type(
`input[name="ctl00$ctl00$cphWrapper$cphContent$tbxPassword"]`,
req.query.pass
);
await page.type(
`input[name="ctl00$ctl00$cphWrapper$cphContent$tbxAccountNumber"]`,
req.query.code
);
await Promise.all([
page.click(
'input[name="ctl00$ctl00$cphWrapper$cphContent$btnAuthenticate"]'
),
page.waitForNavigation({ waitUntil: 'networkidle2' })
]);
console.log("[Info] Logging in to Here Comes The Bus");
const cookies = await page.cookies();
var i = 0;
var cookie = "";
console.log("[Info] Parsing cookie");
while (i < cookies.length) {
cookie += `${cookies[i].name}=${cookies[i].value}; `;
i++;
}
if (!cookie.includes(".ASPXFORMSAUTH")) return res.json({ success: false, error: "Incorrect credentials" }).status(400)
const passengers = await page.evaluate(() => {
const selectElement = document.getElementById('ctl00_ctl00_cphWrapper_cphControlPanel_ddlSelectPassenger');
const options = Array.from(selectElement.querySelectorAll('option'));
return options.map(option => ({
text: option.textContent,
value: option.getAttribute('value')
}));
});
res.json({
success: true, cookie, passengers
})
await browser.close();
console.log("[Info] Browser instance closed");
});
app.post("/api/login", async function (req, res) {
if (!req.query.user || !req.query.pass || !req.query.code)
return res.json({
success: false,
error: "Please provide all credentials",
}).status(400);
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
});
console.log("[Info] Browser instance started");
const page = await browser.newPage();
console.log("[Info] Opened a new page");
await page.goto("https://login.herecomesthebus.com/Authenticate.aspx");
await page.type(
`input[name="ctl00$ctl00$cphWrapper$cphContent$tbxUserName"]`,
req.query.user
);
await page.type(
`input[name="ctl00$ctl00$cphWrapper$cphContent$tbxPassword"]`,
req.query.pass
);
await page.type(
`input[name="ctl00$ctl00$cphWrapper$cphContent$tbxAccountNumber"]`,
req.query.code
);
await Promise.all([
page.click(
'input[name="ctl00$ctl00$cphWrapper$cphContent$btnAuthenticate"]'
),
page.waitForNavigation({ waitUntil: 'networkidle2' })
]);
console.log("[Info] Logging in to Here Comes The Bus");
const cookies = await page.cookies();
var i = 0;
var cookie = "";
console.log("[Info] Parsing cookie");
while (i < cookies.length) {
cookie += `${cookies[i].name}=${cookies[i].value}; `;
i++;
}
if (!cookie.includes(".ASPXFORMSAUTH")) return res.json({ success: false, error: "Incorrect credentials" }).status(400)
const value = await page.$$eval('option[selected="selected"]', (el) => {
return { name: el[1].innerHTML, person: el[1].value, time: el[2].value };
});
const times = await page.evaluate(() => {
const selectElement = document.getElementById('ctl00_ctl00_cphWrapper_cphControlPanel_ddlSelectTimeOfDay');
const optionElements = selectElement.querySelectorAll('option');
return Array.from(optionElements).map((option, index) => {
return {
id: option.getAttribute('value'),
selected: option.hasAttribute('selected'),
time: option.textContent,
};
});
});
fetch("https://login.herecomesthebus.com/Map.aspx/RefreshMap", {
headers: {
accept: "application/json, text/javascript, */*; q=0.01",
"accept-language": "en-US,en;q=0.9,ja;q=0.8",
"content-type": "application/json; charset=UTF-8",
"sec-ch-ua":
'"Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
cookie: cookie,
Referer: "https://login.herecomesthebus.com/Map.aspx",
"Referrer-Policy": "strict-origin-when-cross-origin",
},
body: JSON.stringify({
legacyID: value.person,
name: value.name,
timeSpanId: value.time,
wait: "false",
}),
method: "POST",
})
.then((resp) => resp.json())
.then((json) => {
var lat, lon
if (json.d.includes("SetBusPushPin")) {
var strip = /SetBusPushPin\(([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?,([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?\)/i.exec("SetBusPushPin(123.456,679.012)" + json.d)[0]
var coords = /([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?,([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*))(?:[eE]([+-]?\d+))?/i.exec(strip)[0].split(",")
lat = coords[0]
lon = coords[1]
}
res.json({ success: true, ...value, lat: lat, lon: lon, cookie, times })
});
await browser.close();
console.log("[Info] Browser instance closed");
});
app.post("/api/reverse", function (request, response) {
const { lat, lon } = request.query
fetch(
`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`,
{
headers: {
accept: "application/json, text/javascript, */*; q=0.01",
},
method: "GET",
}
)
.then((res) => res.json())
.then((json) => response.json({ success: true, name: res.display_name }));
});
// listen for requests :)
const listener = app.listen(process.env.PORT || 8080, function () {
console.log("[Info] HCTB-API is listening on port " + listener.address().port + " :)");
if (process.env.ENABLE_UI) console.log("[Info] Web UI Enabled");
});