2017-12-13 22:05:07 +08:00
|
|
|
/*
|
|
|
|
ztncui - ZeroTier network controller UI
|
|
|
|
Copyright (C) 2017 Key Networks (https://key-networks.com)
|
|
|
|
Licensed under GPLv3 - see LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const got = require('got');
|
|
|
|
const ipaddr = require('ip-address');
|
|
|
|
const token = require('./token');
|
|
|
|
|
2017-12-14 01:25:16 +08:00
|
|
|
ZT_ADDR = process.env.ZT_ADDR || 'localhost:9993';
|
|
|
|
|
2017-12-13 22:05:07 +08:00
|
|
|
init_options = async function() {
|
|
|
|
let tok = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tok = await token.get();
|
|
|
|
} catch (err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
|
|
|
json: true,
|
|
|
|
headers: {
|
|
|
|
'X-ZT1-Auth': tok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_zt_address = async function() {
|
|
|
|
const options = await init_options();
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/status', options);
|
2017-12-13 22:05:07 +08:00
|
|
|
return response.body.address;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.get_zt_address = get_zt_address;
|
|
|
|
|
|
|
|
exports.network_list = async function() {
|
|
|
|
const options = await init_options();
|
|
|
|
|
|
|
|
let network = {};
|
|
|
|
let networks = [];
|
|
|
|
let nwids = [];
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network', options);
|
2017-12-13 22:05:07 +08:00
|
|
|
nwids = response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let nwid of nwids) {
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid, options);
|
|
|
|
network = (({name, nwid}) => ({name, nwid}))(response.body);
|
|
|
|
networks.push(network);
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return networks;
|
|
|
|
}
|
|
|
|
|
|
|
|
network_detail = async function(nwid) {
|
|
|
|
const options = await init_options();
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid, options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.network_detail = network_detail;
|
|
|
|
|
|
|
|
exports.network_create = async function(name) {
|
|
|
|
const options = await init_options();
|
|
|
|
options.method = 'POST';
|
|
|
|
options.body = name;
|
|
|
|
|
|
|
|
const zt_address = await get_zt_address();
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ zt_address + '______', options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.network_delete = async function(nwid) {
|
|
|
|
const options = await init_options();
|
|
|
|
options.method = 'DELETE';
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid, options);
|
|
|
|
response.body.deleted = true;
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.ipAssignmentPools = async function(nwid, ipAssignmentPool, action) {
|
|
|
|
const options = await init_options();
|
|
|
|
options.method = 'POST';
|
|
|
|
|
|
|
|
const network = await network_detail(nwid);
|
|
|
|
let ipAssignmentPools = network.ipAssignmentPools;
|
|
|
|
|
|
|
|
if (action === 'add') {
|
|
|
|
ipAssignmentPools.push(ipAssignmentPool);
|
|
|
|
} else if (action === 'delete') {
|
|
|
|
const pool = ipAssignmentPools.find(pool =>
|
|
|
|
pool.ipRangeStart === ipAssignmentPool.ipRangeStart &&
|
|
|
|
pool.ipRangeEnd === ipAssignmentPool.ipRangeEnd);
|
|
|
|
ipAssignmentPools = ipAssignmentPools.filter(p => p != pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.body = { ipAssignmentPools: ipAssignmentPools };
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid, options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.routes = async function(nwid, route, action) {
|
|
|
|
const options = await init_options();
|
|
|
|
options.method = 'POST';
|
|
|
|
|
|
|
|
const network = await network_detail(nwid);
|
|
|
|
let routes = network.routes;
|
|
|
|
const target6 = new ipaddr.Address6(route.target);
|
|
|
|
if (target6.isValid()) {
|
|
|
|
const parts = route.target.split('/');
|
|
|
|
route.target = target6.canonicalForm() + '/' + parts[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
const route_to_del = routes.find(rt => rt.target === route.target);
|
|
|
|
|
|
|
|
if (!route_to_del) {
|
|
|
|
if (action === 'add') {
|
|
|
|
routes.push(route);
|
|
|
|
} else if (action === 'delete') {
|
|
|
|
throw new Error('Cannot delete non-existent route target');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (action === 'add') {
|
|
|
|
throw new Error('Route target is not unique');
|
|
|
|
} else if (action === 'delete') {
|
|
|
|
routes = routes.filter(rt => rt != route_to_del);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
options.body = { routes: routes };
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid, options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.network_object = async function(nwid, object) {
|
|
|
|
const options = await init_options();
|
|
|
|
options.method = 'POST';
|
|
|
|
options.body = object;
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid, options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.members = async function(nwid) {
|
|
|
|
const options = await init_options();
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid + '/member', options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.member_detail = async function(nwid, id) {
|
|
|
|
const options = await init_options();
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid + '/member/' + id, options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.member_object = async function(nwid, id, object) {
|
|
|
|
const options = await init_options();
|
|
|
|
options.method = 'POST';
|
|
|
|
options.body = object;
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid + '/member/' + id, options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.member_delete = async function(nwid, id) {
|
|
|
|
const options = await init_options();
|
|
|
|
options.method = 'DELETE';
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid + '/member/' + id, options);
|
|
|
|
response.body.deleted = true;
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.network_easy_setup = async function(nwid,
|
|
|
|
routes,
|
|
|
|
ipAssignmentPools,
|
|
|
|
v4AssignMode) {
|
|
|
|
const options = await init_options();
|
|
|
|
options.method = 'POST';
|
|
|
|
options.body =
|
|
|
|
{
|
|
|
|
ipAssignmentPools: ipAssignmentPools,
|
|
|
|
routes: routes,
|
|
|
|
v4AssignMode: v4AssignMode
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2017-12-14 01:25:16 +08:00
|
|
|
const response = await got(ZT_ADDR + '/controller/network/'
|
2017-12-13 22:05:07 +08:00
|
|
|
+ nwid, options);
|
|
|
|
return response.body;
|
|
|
|
} catch(err) {
|
|
|
|
throw(err);
|
|
|
|
}
|
|
|
|
}
|