229 lines
5.6 KiB
JavaScript
229 lines
5.6 KiB
JavaScript
var CFClient = require('cloudflare');
|
|
var fs = require('fs');
|
|
var bodyParser = require('body-parser');
|
|
var express = require('express');
|
|
var config;
|
|
|
|
try {
|
|
file = fs.readFileSync("./config.json");
|
|
}
|
|
catch(exception) {
|
|
console.log(exception.toString());
|
|
}
|
|
|
|
if(file != null) {
|
|
var json = JSON.parse(file);
|
|
|
|
config = {
|
|
email: json.email,
|
|
key: json.key
|
|
};
|
|
|
|
handleDisconnect();
|
|
|
|
}
|
|
|
|
var client = new CFClient({
|
|
email: config.email,
|
|
key: config.key
|
|
});
|
|
|
|
var app = express();
|
|
|
|
app.use(bodyParser.urlencoded({
|
|
extended: true
|
|
}));
|
|
app.use(bodyParser.json());
|
|
|
|
function readFile(path, callback) {
|
|
if (!path.match(/\.\./g)) {
|
|
fs.readFile('./static/' + path, (err, data) => {
|
|
if (err) {
|
|
readFile("404.html", callback);
|
|
}
|
|
else {
|
|
callback(data);
|
|
}
|
|
|
|
});
|
|
}
|
|
else {
|
|
callback("Nope");
|
|
}
|
|
|
|
}
|
|
|
|
app.get("/dns/api/list", function(request, response) {
|
|
console.log("listZones");
|
|
|
|
client.browseZones().then(function(data){
|
|
response.status(200).json(JSON.stringify(data));
|
|
});
|
|
});
|
|
|
|
app.post("/dns/api/listDomains", function(request, response) {
|
|
var zoneId = request.body.zoneId;
|
|
var page = request.body.page || 0;
|
|
var perPage = request.body.perPage || 20;
|
|
|
|
console.log("listDomains "+zoneId+", "+page+", "+perPage);
|
|
|
|
client.readZone(zoneId).then(function(zone) {
|
|
client.browseDNS(zone, {page: page, per_page: perPage}).then(function(domains) {
|
|
response.status(200).json(JSON.stringify(domains));
|
|
});
|
|
});
|
|
});
|
|
|
|
app.post("/dns/api/add", function(request, response) {
|
|
var json = request.body.data;
|
|
var data = JSON.parse(json);
|
|
|
|
console.log(data);
|
|
|
|
client.readZone(data.zoneId).then(function(zone) {
|
|
var newRecord = CFClient.DNSRecord.create({
|
|
zoneId: zone.id,
|
|
type: data.type,
|
|
name: data.name,
|
|
content: data.content,
|
|
ttl: data.ttl,
|
|
proxied: data.proxied
|
|
});
|
|
|
|
client.addDNS(newRecord).then(function(data) {
|
|
response.status(200).json(JSON.stringify(data));
|
|
}, function(err) {
|
|
console.log(err);
|
|
response.status(400).send();
|
|
});
|
|
});
|
|
});
|
|
|
|
app.post("/dns/api/edit", function(request, response) {
|
|
var json = request.body.data;
|
|
var data = JSON.parse(json);
|
|
|
|
if(data.zoneId == null || data.id == null) {
|
|
response.status(400).send();
|
|
return;
|
|
}
|
|
|
|
console.log(request.body);
|
|
var zoneId = data.zoneId;
|
|
var dnsId = data.id;
|
|
|
|
console.log("edit "+zoneId+" "+dnsId);
|
|
|
|
if(zoneId == null || dnsId == null) {
|
|
response.status(400).send();
|
|
return;
|
|
}
|
|
|
|
console.log(typeof(data.type));
|
|
console.log(typeof(data.name));
|
|
console.log(typeof(data.content));
|
|
console.log(typeof(data.ttl));
|
|
console.log(typeof(data.proxied));
|
|
|
|
client.readZone(zoneId)
|
|
.then(function(zone) {
|
|
return client.readDNS(dnsId, zone);
|
|
}, function(err) {
|
|
console.log(err);
|
|
response.status(400).send();
|
|
return;
|
|
})
|
|
.then(function(dns) {
|
|
dns.type = data.type;
|
|
dns.name = data.name;
|
|
dns.content = data.content;
|
|
dns.ttl = data.ttl;
|
|
dns.proxied = data.proxied;
|
|
|
|
return client.editDNS(dns);
|
|
}, function(err) {
|
|
console.log(err);
|
|
response.status(400).send();
|
|
return;
|
|
})
|
|
.then(function(dns) {
|
|
response.status(200).json(JSON.stringify(dns));
|
|
return;
|
|
}, function(err) {
|
|
console.log(err);
|
|
response.status(400).send();
|
|
return;
|
|
});
|
|
|
|
});
|
|
|
|
app.post("/dns/api/delete", function(request, response) {
|
|
console.log(request.body);
|
|
var zoneId = request.body.data.zoneId;
|
|
var dnsId = request.body.data.dnsId;
|
|
|
|
console.log("delete "+typeof(zoneId)+" "+typeof(dnsId));
|
|
|
|
if(zoneId == null || dnsId == null) {
|
|
response.status(400).send();
|
|
return;
|
|
}
|
|
|
|
client.readZone(zoneId).then(function(zone) {
|
|
console.log("0");
|
|
return client.readDNS(dnsId, zone).then(function(dns) {
|
|
console.log("1");
|
|
return client.deleteDNS(dns);
|
|
}, function(err) {
|
|
console.log(err);
|
|
console.log("2");
|
|
response.status(400).send();
|
|
return;
|
|
}).then(function(id) {
|
|
console.log("3");
|
|
response.status(200).send();
|
|
return;
|
|
}, function(err) {
|
|
console.log("4");
|
|
console.log(err);
|
|
response.status(400).send();
|
|
return;
|
|
});
|
|
}, function(err) {
|
|
console.log(err);
|
|
});
|
|
});
|
|
|
|
app.get("/dns/", function(request, response) {
|
|
readFile("index.html", function(data) {
|
|
response.setHeader('content-type', 'text/html');
|
|
response.end(data.toString());
|
|
});
|
|
});
|
|
|
|
app.get("/dns/static/:filename", function(request, response) {
|
|
readFile(request.params.filename, function(data) {
|
|
var f = request.params.filename;
|
|
var end = f.split(".")[f.split(".").length-1];
|
|
switch(end){
|
|
case "css":
|
|
response.setHeader('content-type', 'text/css');
|
|
break;
|
|
case "js":
|
|
response.setHeader('content-type', 'text/javascript');
|
|
break;
|
|
}
|
|
response.end(data);
|
|
});
|
|
});
|
|
|
|
|
|
|
|
app.port = 1338;
|
|
app.host = "127.0.0.1";
|
|
|
|
app.listen(1338, "127.0.0.1", 511, function() {
|
|
|
|
});
|