-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
121 lines (109 loc) · 3 KB
/
index.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
const express = require('express');
const cors = require('cors');
var bodyParser = require('body-parser');
const fetch = require('node-fetch');
const {verify} = require('hcaptcha');
const rateLimit = require('express-rate-limit');
const ejs = require('ejs');
const app = express();
require('dotenv').config();
const { PORT, CF_GLOBAL_APIKEY, CF_ZONE_ID, CF_EMAIL, HCAPTCHA_SECRET, HCAPTCHA_SITEKEY } = process.env;
const baseUrl = 'https://api.cloudflare.com/client/v4/zones/' + CF_ZONE_ID;
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }))
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
const addLimit = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 250,
message: 'Too many request from this IP, please try again after 15 minutes',
standardHeaders: true,
legacyHeaders: false,
handler: function(req, res) {
// lmao 200 OK
res.status(200).send({
success: false,
errors: [
{
message: "Rate Limited",
error_chain: [{
message: "Slow down dude! You're too fast. Wait 15 minutes."
}]
}
]});
},
})
app.get('/', async(req, res) => {
res.render('index.html', { hcaptcha_sitekey: HCAPTCHA_SITEKEY })
})
app.get('/success', async(req, res) => {
res.sendFile(__dirname + '/views/success.html')
})
app.get('/github', async(req, res) => {
res.redirect('https://github.com/JastinXyz/withcoding.me')
})
app.get('/isexists', async(req, res) => {
const q = req.query.q;
const type = req.query.type;
const dom = [];
fetch(baseUrl + '/dns_records?type=' + type + '&match=all', {
method: "GET",
headers: {
"Content-type": "application/json",
"X-Auth-Email": CF_EMAIL,
"X-Auth-Key": CF_GLOBAL_APIKEY,
},
}).then((d) => d.json()).then((x) => {
if(x.success) {
let result = x.result;
for (var i = 0; i < result.length; i++) {
dom.push(result[i].name)
}
res.status(200).json({
result: dom.includes(q)
})
} else {
console.log(x);
res.sendStatus(500)
}
}).catch((x) => {
console.log(x);
res.sendStatus(500)
})
})
app.post('/add', addLimit, async(req, res) => {
const hcap = req.body['h-captcha-response'];
let { subdomain, content } = req.body;
const verif = await verify(HCAPTCHA_SECRET, hcap)
// console.log(verif)
// let verif = {
// success: true
// }
if(!verif.success) {
res.sendStatus(400)
} else {
const data = {
type: "CNAME",
name: subdomain + ".withcoding.me",
content,
ttl: 1,
proxied: false
}
fetch(baseUrl + '/dns_records', {
method: "POST",
headers: {
"Content-type": "application/json",
"X-Auth-Email": CF_EMAIL,
"X-Auth-Key": CF_GLOBAL_APIKEY,
},
body: JSON.stringify(data),
}).then((d) => d.json()).then((x) => {
res.send(x)
}).catch((x) => {
res.sendStatus(500)
})
}
})
app.listen(PORT, () => {
console.log("listen on port", PORT);
})