Merge pull request #196 from BeaudanBrown/localhost-loki

Localhost loki
pull/198/head
sachaaaaa 6 years ago committed by GitHub
commit 0460c62847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
{
"serverUrl": "random.snode",
"localUrl": "localhost.loki",
"cdnUrl": "random.snode",
"localServerPort": "8081",
"messageServerPort": "8080",

@ -9,29 +9,54 @@ const dns = require('dns');
const MINIMUM_SWARM_NODES = 1;
const FAILURE_THRESHOLD = 3;
const resolve4 = url =>
new Promise((resolve, reject) => {
dns.resolve4(url, (err, ip) => {
if (err) {
reject(err);
} else {
resolve(ip);
}
});
});
const resolveCname = url =>
new Promise((resolve, reject) => {
dns.resolveCname(url, (err, address) => {
if (err) {
reject(err);
} else {
resolve(address[0]);
}
});
});
class LokiSnodeAPI {
constructor({ url, swarmServerPort }) {
if (!is.string(url)) {
constructor({ serverUrl, localUrl, swarmServerPort }) {
if (!is.string(serverUrl)) {
throw new Error('WebAPI.initialize: Invalid server url');
}
this.url = url;
this.serverUrl = serverUrl;
this.localUrl = localUrl;
this.swarmServerPort = swarmServerPort ? `:${swarmServerPort}` : '';
this.swarmsPendingReplenish = {};
this.ourSwarmNodes = {};
this.contactSwarmNodes = {};
}
async getMyLokiIp() {
const address = await resolveCname(this.localUrl);
return resolve4(address);
}
async getMyLokiAddress() {
/* resolve our local loki address */
return resolveCname(this.localUrl);
}
getRandomSnodeAddress() {
/* resolve random snode */
return new Promise((resolve, reject) => {
dns.resolveCname(this.url, (err, address) => {
if (err) {
reject(err);
} else {
resolve(address[0]);
}
});
});
return resolveCname(this.serverUrl);
}
async unreachableNode(pubKey, nodeUrl) {

@ -24,8 +24,9 @@
}
async function sendOnlineBroadcastMessage(pubKey, forceP2p = false) {
const myLokiAddress = await window.lokiSnodeAPI.getMyLokiAddress();
const lokiAddressMessage = new textsecure.protobuf.LokiAddressMessage({
p2pAddress: 'http://localhost',
p2pAddress: `http://${myLokiAddress}`,
p2pPort: parseInt(window.localServerPort, 10),
});
const content = new textsecure.protobuf.Content({

@ -50,13 +50,13 @@ class LocalLokiServer extends EventEmitter {
});
}
async start(port) {
async start(port, ip) {
// Close the old server
await this.close();
// Start a listening on new server
return new Promise((res, rej) => {
this.server.listen(port, err => {
this.server.listen(port, ip, err => {
if (err) {
rej(err);
} else {

@ -83,11 +83,12 @@ MessageReceiver.prototype.extend({
this.onEmpty();
}
});
localLokiServer.start(localServerPort).then(port => {
window.log.info(`Local Server started at localhost:${port}`);
libloki.api.broadcastOnlineStatus();
localLokiServer.on('message', this.handleP2pMessage.bind(this));
window.lokiSnodeAPI.getMyLokiIp().then(myLokiIp => {
localLokiServer.start(localServerPort, myLokiIp).then(port => {
window.log.info(`Local Server started at localhost:${port}`);
libloki.api.broadcastOnlineStatus();
localLokiServer.on('message', this.handleP2pMessage.bind(this));
});
});
// TODO: Rework this socket stuff to work with online messaging

@ -144,6 +144,7 @@ function prepareURL(pathSegments, moreKeys) {
version: app.getVersion(),
buildExpiration: config.get('buildExpiration'),
serverUrl: config.get('serverUrl'),
localUrl: config.get('localUrl'),
cdnUrl: config.get('cdnUrl'),
messageServerPort: config.get('messageServerPort'),
swarmServerPort: config.get('swarmServerPort'),

@ -289,7 +289,8 @@ window.WebAPI = initializeWebAPI({
const LokiSnodeAPI = require('./js/modules/loki_snode_api');
window.lokiSnodeAPI = new LokiSnodeAPI({
url: config.serverUrl,
serverUrl: config.serverUrl,
localUrl: config.localUrl,
swarmServerPort: config.swarmServerPort,
});

Loading…
Cancel
Save