forked from carcabot/tiktok-signature
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.js
81 lines (71 loc) · 2.33 KB
/
listen.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
const Signer = require("./index");
const http = require("http");
const PORT = process.env.PORT || 8080;
(async function main() {
try {
const signer = new Signer();
const start = new Date();
const server = http
.createServer()
.listen(PORT)
.on("listening", function () {
console.log("TikTok Signature server started on PORT " + PORT);
});
// Uncomment if you want to auto-exit this application after a period of time
// If you use PM2 or Supervisord, it will attempt to open it
// setTimeout(function () {
// server.close(() => {
// console.log("Server shutdown completed.");
// process.exit(1);
// });
// }, 1 * 60 * 60 * 1000);
signer.init();
server.on("request", (request, response) => {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "*");
if (request.method === "OPTIONS") {
response.writeHead(200);
response.end();
return;
}
if (request.method === "POST" && request.url === "/signature") {
var url = "";
request.on("data", function (chunk) {
url += chunk;
});
request.on("end", async function () {
console.log("Received url: " + url);
try {
const sign = await signer.sign(url);
const navigator = await signer.navigator();
let output = JSON.stringify({
status: "ok",
data: {
...sign,
navigator: navigator,
},
});
response.writeHead(200, { "Content-Type": "application/json" });
response.end(output);
console.log(output);
} catch (err) {
console.log(err);
// Uncomment if you want to auto-exit this application when an error thrown
// If you use PM2 or Supervisord, it will attempt to open it
// var timeElapsed = new Date() - start;
// console.info("Execution time: %dms", timeElapsed);
// if (timeElapsed > 2500) {
// process.exit(1);
// }
}
});
} else {
response.statusCode = 404;
response.end();
}
});
await signer.close();
} catch (err) {
console.error(err);
}
})();