2018-10-21 09:32:04 +00:00
|
|
|
;(function (exports) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/* global Promise */
|
|
|
|
var PromiseA;
|
|
|
|
if ('undefined' !== typeof Promise) {
|
|
|
|
PromiseA = Promise;
|
|
|
|
} else {
|
|
|
|
throw new Error("no Promise implementation defined");
|
|
|
|
}
|
|
|
|
|
|
|
|
var common = exports.TELEBIT || require('./lib/common.js');
|
|
|
|
|
2018-10-22 06:17:49 +00:00
|
|
|
common.authorize = common.getToken = function getToken(state, showOtp) {
|
2018-10-21 09:32:04 +00:00
|
|
|
state.relay = state.config.relay;
|
|
|
|
|
|
|
|
// { _otp, config: {} }
|
|
|
|
return common.api.token(state, {
|
2018-10-22 06:17:49 +00:00
|
|
|
error: function (err) { console.error("[Error] common.api.token handlers.error: \n", err); return PromiseA.reject(err); }
|
2018-10-21 09:32:04 +00:00
|
|
|
, directory: function (dir) {
|
2018-10-22 06:17:49 +00:00
|
|
|
/*console.log('[directory] Telebit Relay Discovered:', dir);*/
|
2018-10-21 09:32:04 +00:00
|
|
|
state._apiDirectory = dir;
|
|
|
|
return PromiseA.resolve();
|
|
|
|
}
|
|
|
|
, tunnelUrl: function (tunnelUrl) {
|
|
|
|
//console.log('[tunnelUrl] Telebit Relay Tunnel Socket:', tunnelUrl);
|
|
|
|
state.wss = tunnelUrl;
|
|
|
|
return PromiseA.resolve();
|
|
|
|
}
|
2018-10-22 06:17:49 +00:00
|
|
|
, requested: function (authReq, pollUrl) {
|
2018-10-21 09:32:04 +00:00
|
|
|
console.log("[requested] Pairing Requested");
|
2018-10-22 06:17:49 +00:00
|
|
|
state._otp = state._otp = authReq.otp;
|
2018-10-21 09:32:04 +00:00
|
|
|
|
|
|
|
if (!state.config.token && state._can_pair) {
|
2018-10-22 06:17:49 +00:00
|
|
|
console.info("0000".replace(/0000/g, state._otp));
|
|
|
|
showOtp(authReq.otp, pollUrl);
|
2018-10-21 09:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PromiseA.resolve();
|
|
|
|
}
|
|
|
|
, connect: function (pretoken) {
|
|
|
|
console.log("[connect] Enabling Pairing Locally...");
|
|
|
|
state.config.pretoken = pretoken;
|
|
|
|
state._connecting = true;
|
|
|
|
|
2018-10-22 06:17:49 +00:00
|
|
|
// This will only be saved to the session
|
|
|
|
state.config._otp = state._otp;
|
|
|
|
return common.reqLocalAsync({ url: '/api/config', method: 'POST', body: state.config, json: true }).then(function () {
|
2018-10-21 09:32:04 +00:00
|
|
|
console.info("waiting...");
|
|
|
|
return PromiseA.resolve();
|
|
|
|
}).catch(function (err) {
|
|
|
|
state._error = err;
|
|
|
|
console.error("Error while initializing config [connect]:");
|
|
|
|
console.error(err);
|
|
|
|
return PromiseA.reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
, offer: function (token) {
|
|
|
|
//console.log("[offer] Pairing Enabled by Relay");
|
2018-10-22 06:17:49 +00:00
|
|
|
state.token = token;
|
2018-10-21 09:32:04 +00:00
|
|
|
state.config.token = token;
|
|
|
|
if (state._error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state._connecting = true;
|
|
|
|
try {
|
|
|
|
//require('jsonwebtoken').decode(token);
|
|
|
|
token = token.split('.');
|
|
|
|
token[0] = token[0].replace(/_/g, '/').replace(/-/g, '+');
|
|
|
|
while (token[0].length % 4) { token[0] += '='; }
|
|
|
|
btoa(token[0]);
|
|
|
|
token[1] = token[1].replace(/_/g, '/').replace(/-/g, '+');
|
|
|
|
while (token[1].length % 4) { token[1] += '='; }
|
|
|
|
btoa(token[1]);
|
|
|
|
//console.log(require('jsonwebtoken').decode(token));
|
|
|
|
} catch(e) {
|
|
|
|
console.warn("[warning] could not decode token");
|
|
|
|
}
|
2018-10-22 06:17:49 +00:00
|
|
|
return common.reqLocalAsync({ url: '/api/config', method: 'POST', body: state.config, json: true }).then(function () {
|
2018-10-21 09:32:04 +00:00
|
|
|
//console.log("Pairing Enabled Locally");
|
|
|
|
return PromiseA.resolve();
|
|
|
|
}).catch(function (err) {
|
|
|
|
state._error = err;
|
|
|
|
console.error("Error while initializing config [offer]:");
|
|
|
|
console.error(err);
|
|
|
|
return PromiseA.reject(err);
|
|
|
|
});
|
|
|
|
}
|
2018-10-22 06:17:49 +00:00
|
|
|
, granted: function (/*_*/) { /*console.log("[grant] Pairing complete!");*/ return PromiseA.resolve(); }
|
2018-10-21 09:32:04 +00:00
|
|
|
, end: function () {
|
2018-10-22 06:17:49 +00:00
|
|
|
return common.reqLocalAsync({ url: '/api/enable', method: 'POST', body: [], json: true }).then(function () {
|
2018-10-21 09:32:04 +00:00
|
|
|
console.info("Success");
|
|
|
|
|
|
|
|
// workaround for https://github.com/nodejs/node/issues/21319
|
|
|
|
if (state._useTty) {
|
|
|
|
setTimeout(function () {
|
|
|
|
console.info("Some fun things to try first:\n");
|
|
|
|
console.info(" ~/telebit http ~/public");
|
|
|
|
console.info(" ~/telebit tcp 5050");
|
|
|
|
console.info(" ~/telebit ssh auto");
|
|
|
|
console.info();
|
|
|
|
console.info("Press any key to continue...");
|
|
|
|
console.info();
|
|
|
|
process.exit(0);
|
|
|
|
}, 0.5 * 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// end workaround
|
|
|
|
|
|
|
|
//parseCli(state);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
}('undefined' === typeof module ? window : module.exports));
|