moved some things related to DDNS into separate folder
This commit is contained in:
parent
cfcc1acb8c
commit
8930a528bc
21
lib/app.js
21
lib/app.js
|
@ -15,25 +15,6 @@ module.exports = function (myDeps, conf, overrideHttp) {
|
||||||
//var server;
|
//var server;
|
||||||
var goldilocksApis;
|
var goldilocksApis;
|
||||||
var app;
|
var app;
|
||||||
var request;
|
|
||||||
|
|
||||||
function createGoldilocksApis() {
|
|
||||||
var PromiseA = require('bluebird');
|
|
||||||
var OAUTH3 = require('../packages/assets/org.oauth3');
|
|
||||||
require('../packages/assets/org.oauth3/oauth3.domains.js');
|
|
||||||
require('../packages/assets/org.oauth3/oauth3.dns.js');
|
|
||||||
require('../packages/assets/org.oauth3/oauth3.tunnel.js');
|
|
||||||
OAUTH3._hooks = require('../packages/assets/org.oauth3/oauth3.node.storage.js');
|
|
||||||
|
|
||||||
request = request || PromiseA.promisify(require('request'));
|
|
||||||
|
|
||||||
myDeps.PromiseA = PromiseA;
|
|
||||||
myDeps.OAUTH3 = OAUTH3;
|
|
||||||
myDeps.recase = require('recase').create({});
|
|
||||||
myDeps.request = request;
|
|
||||||
|
|
||||||
return require('../packages/apis/com.daplie.goldilocks').create(myDeps, conf);
|
|
||||||
}
|
|
||||||
|
|
||||||
app = express();
|
app = express();
|
||||||
|
|
||||||
|
@ -116,7 +97,7 @@ module.exports = function (myDeps, conf, overrideHttp) {
|
||||||
|
|
||||||
function _goldApis(req, res, next) {
|
function _goldApis(req, res, next) {
|
||||||
if (!goldilocksApis) {
|
if (!goldilocksApis) {
|
||||||
goldilocksApis = createGoldilocksApis();
|
goldilocksApis = require('../packages/apis/com.daplie.goldilocks').create(myDeps, conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof goldilocksApis[req.params.name] === 'function') {
|
if (typeof goldilocksApis[req.params.name] === 'function') {
|
||||||
|
|
|
@ -1,11 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports.create = function (deps, conf) {
|
module.exports.create = function (deps, conf) {
|
||||||
var PromiseA = deps.PromiseA;
|
var loopback = require('./loopback').create(deps, conf);
|
||||||
var request = PromiseA.promisify(require('request'));
|
|
||||||
var OAUTH3 = require('../packages/assets/org.oauth3');
|
|
||||||
require('../packages/assets/org.oauth3/oauth3.dns.js');
|
|
||||||
OAUTH3._hooks = require('../packages/assets/org.oauth3/oauth3.node.storage.js');
|
|
||||||
|
|
||||||
function dnsType(addr) {
|
function dnsType(addr) {
|
||||||
if (/^\d+\.\d+\.\d+\.\d+$/.test(addr)) {
|
if (/^\d+\.\d+\.\d+\.\d+$/.test(addr)) {
|
||||||
|
@ -36,10 +32,10 @@ module.exports.create = function (deps, conf) {
|
||||||
|
|
||||||
async function setDeviceAddress(addr) {
|
async function setDeviceAddress(addr) {
|
||||||
var session = await getSession();
|
var session = await getSession();
|
||||||
var directives = await OAUTH3.discover(session.token.aud);
|
var directives = await deps.OAUTH3.discover(session.token.aud);
|
||||||
|
|
||||||
// Set the address of the device to our public address.
|
// Set the address of the device to our public address.
|
||||||
await request({
|
await deps.request({
|
||||||
url: directives.api+'/api/com.daplie.domains/acl/devices/' + conf.device.hostname
|
url: directives.api+'/api/com.daplie.domains/acl/devices/' + conf.device.hostname
|
||||||
, method: 'POST'
|
, method: 'POST'
|
||||||
, headers: {
|
, headers: {
|
||||||
|
@ -56,7 +52,7 @@ module.exports.create = function (deps, conf) {
|
||||||
// Then update all of the records attached to our hostname, first removing the old records
|
// Then update all of the records attached to our hostname, first removing the old records
|
||||||
// to remove the reference to the old address, then creating new records for the same domains
|
// to remove the reference to the old address, then creating new records for the same domains
|
||||||
// using our new address.
|
// using our new address.
|
||||||
var allDns = OAUTH3.api(directives.api, {session: session, api: 'dns.list'});
|
var allDns = deps.OAUTH3.api(directives.api, {session: session, api: 'dns.list'});
|
||||||
var ourDomains = allDns.filter(function (record) {
|
var ourDomains = allDns.filter(function (record) {
|
||||||
return record.device === conf.device.hostname;
|
return record.device === conf.device.hostname;
|
||||||
}).map(function (record) {
|
}).map(function (record) {
|
||||||
|
@ -73,8 +69,8 @@ module.exports.create = function (deps, conf) {
|
||||||
, session: session
|
, session: session
|
||||||
, device: conf.device.hostname
|
, device: conf.device.hostname
|
||||||
};
|
};
|
||||||
await PromiseA.all(ourDomains.map(function (record) {
|
await deps.PromiseA.all(ourDomains.map(function (record) {
|
||||||
return OAUTH3.api(directives.api, Object.assign({}, common, record));
|
return deps.OAUTH3.api(directives.api, Object.assign({}, common, record));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
common = {
|
common = {
|
||||||
|
@ -84,16 +80,16 @@ module.exports.create = function (deps, conf) {
|
||||||
, ip: addr
|
, ip: addr
|
||||||
, ttl: 300
|
, ttl: 300
|
||||||
};
|
};
|
||||||
await PromiseA.all(ourDomains.map(function (record) {
|
await deps.PromiseA.all(ourDomains.map(function (record) {
|
||||||
return OAUTH3.api(directives.api, Object.assign({}, common, record));
|
return deps.OAUTH3.api(directives.api, Object.assign({}, common, record));
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDeviceAddresses() {
|
async function getDeviceAddresses() {
|
||||||
var session = await getSession();
|
var session = await getSession();
|
||||||
var directives = await OAUTH3.discover(session.token.aud);
|
var directives = await deps.OAUTH3.discover(session.token.aud);
|
||||||
|
|
||||||
var result = await request({
|
var result = await deps.request({
|
||||||
url: directives.api+'/api/org.oauth3.dns/acl/devices'
|
url: directives.api+'/api/org.oauth3.dns/acl/devices'
|
||||||
, method: 'GET'
|
, method: 'GET'
|
||||||
, headers: {
|
, headers: {
|
||||||
|
@ -123,8 +119,8 @@ module.exports.create = function (deps, conf) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var session = await getSession();
|
var session = await getSession();
|
||||||
var directives = await OAUTH3.discover(session.token.aud);
|
var directives = await deps.OAUTH3.discover(session.token.aud);
|
||||||
var addr = await deps.loopback.checkPublicAddr(directives.api);
|
var addr = await loopback.checkPublicAddr(directives.api);
|
||||||
|
|
||||||
if (publicAddress === addr) {
|
if (publicAddress === addr) {
|
||||||
return;
|
return;
|
||||||
|
@ -142,7 +138,8 @@ module.exports.create = function (deps, conf) {
|
||||||
setInterval(recheckPubAddr, 5*60*1000);
|
setInterval(recheckPubAddr, 5*60*1000);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
setDeviceAddress: setDeviceAddress
|
loopbackServer: loopback.server
|
||||||
|
, setDeviceAddress: setDeviceAddress
|
||||||
, getDeviceAddresses: getDeviceAddresses
|
, getDeviceAddresses: getDeviceAddresses
|
||||||
, recheckPubAddr: recheckPubAddr
|
, recheckPubAddr: recheckPubAddr
|
||||||
};
|
};
|
|
@ -1,12 +1,10 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports.create = function (deps, conf) {
|
module.exports.create = function (deps, conf) {
|
||||||
var PromiseA = require('bluebird');
|
|
||||||
var request = PromiseA.promisify(require('request'));
|
|
||||||
var pending = {};
|
var pending = {};
|
||||||
|
|
||||||
async function checkPublicAddr(host) {
|
async function checkPublicAddr(host) {
|
||||||
var result = await request({
|
var result = await deps.request({
|
||||||
method: 'GET'
|
method: 'GET'
|
||||||
, url: host+'/api/org.oauth3.tunnel/checkip'
|
, url: host+'/api/org.oauth3.tunnel/checkip'
|
||||||
, json: true
|
, json: true
|
||||||
|
@ -42,7 +40,7 @@ module.exports.create = function (deps, conf) {
|
||||||
|
|
||||||
var result;
|
var result;
|
||||||
try {
|
try {
|
||||||
result = await request(reqObj);
|
result = await deps.request(reqObj);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
delete pending[token];
|
delete pending[token];
|
||||||
throw err;
|
throw err;
|
||||||
|
@ -64,10 +62,12 @@ module.exports.create = function (deps, conf) {
|
||||||
async function loopback(provider) {
|
async function loopback(provider) {
|
||||||
var directives = await deps.OAUTH3.discover(provider);
|
var directives = await deps.OAUTH3.discover(provider);
|
||||||
var address = await checkPublicAddr(directives.api);
|
var address = await checkPublicAddr(directives.api);
|
||||||
|
if (conf.debug) {
|
||||||
console.log('checking to see if', address, 'gets back to us');
|
console.log('checking to see if', address, 'gets back to us');
|
||||||
|
}
|
||||||
|
|
||||||
var ports = require('./servers').listeners.tcp.list();
|
var ports = require('../servers').listeners.tcp.list();
|
||||||
var values = await PromiseA.all(ports.map(function (port) {
|
var values = await deps.PromiseA.all(ports.map(function (port) {
|
||||||
return checkSinglePort(directives.api, address, port);
|
return checkSinglePort(directives.api, address, port);
|
||||||
}));
|
}));
|
||||||
|
|
|
@ -178,7 +178,7 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
|
||||||
if (headers.url.indexOf('/.well-known/cloud-challenge/') !== 0) {
|
if (headers.url.indexOf('/.well-known/cloud-challenge/') !== 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return emitConnection(deps.loopback.server, conn, opts);
|
return emitConnection(deps.ddns.loopbackServer, conn, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpsRedirectServer;
|
var httpsRedirectServer;
|
||||||
|
|
|
@ -19,10 +19,20 @@ function update(conf) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function create(conf) {
|
function create(conf) {
|
||||||
|
var PromiseA = require('bluebird');
|
||||||
|
var OAUTH3 = require('../packages/assets/org.oauth3');
|
||||||
|
require('../packages/assets/org.oauth3/oauth3.domains.js');
|
||||||
|
require('../packages/assets/org.oauth3/oauth3.dns.js');
|
||||||
|
require('../packages/assets/org.oauth3/oauth3.tunnel.js');
|
||||||
|
OAUTH3._hooks = require('../packages/assets/org.oauth3/oauth3.node.storage.js');
|
||||||
|
|
||||||
config = conf;
|
config = conf;
|
||||||
var deps = {
|
var deps = {
|
||||||
messenger: process
|
messenger: process
|
||||||
, PromiseA: require('bluebird')
|
, PromiseA: PromiseA
|
||||||
|
, OAUTH3: OAUTH3
|
||||||
|
, request: PromiseA.promisify(require('request'))
|
||||||
|
, recase: require('recase').create({})
|
||||||
// Note that if a custom createConnections is used it will be called with different
|
// Note that if a custom createConnections is used it will be called with different
|
||||||
// sets of custom options based on what is actually being proxied. Most notably the
|
// sets of custom options based on what is actually being proxied. Most notably the
|
||||||
// HTTP proxying connection creation is not something we currently control.
|
// HTTP proxying connection creation is not something we currently control.
|
||||||
|
@ -31,7 +41,6 @@ function create(conf) {
|
||||||
deps.storage = require('./storage').create(deps, conf);
|
deps.storage = require('./storage').create(deps, conf);
|
||||||
deps.proxy = require('./proxy-conn').create(deps, conf);
|
deps.proxy = require('./proxy-conn').create(deps, conf);
|
||||||
deps.socks5 = require('./socks5-server').create(deps, conf);
|
deps.socks5 = require('./socks5-server').create(deps, conf);
|
||||||
deps.loopback = require('./loopback').create(deps, conf);
|
|
||||||
deps.ddns = require('./ddns').create(deps, conf);
|
deps.ddns = require('./ddns').create(deps, conf);
|
||||||
|
|
||||||
require('./goldilocks.js').create(deps, conf);
|
require('./goldilocks.js').create(deps, conf);
|
||||||
|
|
|
@ -315,29 +315,6 @@ module.exports.create = function (deps, conf) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
, loopback: function (req, res) {
|
|
||||||
if (handleCors(req, res, 'GET')) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
isAuthorized(req, res, function () {
|
|
||||||
var prom;
|
|
||||||
var query = require('querystring').parse(require('url').parse(req.url).query);
|
|
||||||
if (query.provider) {
|
|
||||||
prom = deps.loopback(query.provider);
|
|
||||||
} else {
|
|
||||||
prom = deps.storage.owners.get(req.userId).then(function (session) {
|
|
||||||
return deps.loopback(session.token.aud);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'application/json');
|
|
||||||
prom.then(function (result) {
|
|
||||||
res.end(JSON.stringify(result));
|
|
||||||
}, function (err) {
|
|
||||||
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
, paywall_check: function (req, res) {
|
, paywall_check: function (req, res) {
|
||||||
if (handleCors(req, res, 'GET')) {
|
if (handleCors(req, res, 'GET')) {
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue