This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
97 lines (88 loc) · 2.94 KB
/
server.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
const Koa = require("koa");
const Router = require("koa-router");
const bodyParser = require("koa-bodyparser");
const fork = require("child_process").fork;
const Mongolass = require("mongolass");
const mongolass = new Mongolass();
const Schema = Mongolass.Schema;
mongolass.connect(process.env.mongo_uri);
const captchaSchema = new Schema("captchaSchema", {
errorId: { type: "number", default: 0 },
status: { type: "string", default: "processing" },
error: { type: "string", require: false },
taskId: { type: "number", required: true },
solution: {
gRecaptchaResponse: { type: "string", default: "", required: false }
},
createdAt: { type: Mongolass.Types.Date, default: Date.now }
});
const Task = mongolass.model("captcha", captchaSchema);
const app = new Koa();
app.use(bodyParser({
detectJSON: function (ctx) {
return true;
}
}));
const router = new Router();
function getRandomArbitrary(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
(async () => {
router.post("/createTask", async (ctx, next) => {
const randomNumber = getRandomArbitrary(0, 999999);
if (ctx.request.body.task.websiteKey && ctx.request.body.task.websiteURL) {
await fork("./main.js", {
env: {
taskId: Number(randomNumber),
siteKey: ctx.request.body.task.websiteKey,
refererURL: ctx.request.body.task.websiteURL,
mongo_uri: process.env.mongo_uri
}
});
await Task
.insertOne({
taskId: Number(randomNumber),
solution: {
gRecaptchaResponse: ""
}
})
.exec();
ctx.body = {
"errorId": 0,
"taskId": Number(randomNumber)
};
}
else {
ctx.response.status = 400;
ctx.body = {
"errorId": 1,
"error": "websiteKey and/or websiteURL missing"
};
}
});
router.post("/getTaskResult", async (ctx, next) => {
if (ctx.request.body.taskId) {
ctx.body = await Task
.findOne({ taskId: ctx.request.body.taskId })
.exec()
// ctx.body = {
// "errorId": 0,
// "status": "ready",
// "solution": {
// "gRecaptchaResponse": await fsPromises.readFile("/tmp/" + ctx.request.body.taskId, "utf8")
// }
// };
}
else {
ctx.body = {
"errorId": 1,
"error": "taskId missing"
};
}
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(process.env.PORT || 3000);
console.log("Server started on port " + (process.env.app_port || 3000) + "!");
})();