Skip to content

Commit

Permalink
Merge pull request #74 from dappnode/v0.1.15
Browse files Browse the repository at this point in the history
V0.1.15
  • Loading branch information
eduadiez authored Oct 18, 2018
2 parents 2afe424 + 3ccc96d commit 536f516
Show file tree
Hide file tree
Showing 15 changed files with 357 additions and 184 deletions.
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ RUN chmod 755 check_upnp.sh
RUN mkdir -p /usr/src/app/secrets
# envs for init.sh -> node communication
ENV DB_PATH /usr/src/app/secrets/db.json
ENV INSTALLATION_STATIC_IP /usr/src/dappnode/ip.value
ENV INSTALLATION_STATIC_IP /usr/src/app/config/static_ip
ENV KEYPAIR_PATH /usr/src/app/secrets/keypair
ENV CREDENTIALS_PATH /usr/src/app/secrets/chap-secrets
ENV PUBLIC_IP_PATH /usr/src/app/secrets/server-ip
Expand Down
48 changes: 48 additions & 0 deletions build/src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"eslint-config-google": "^0.9.1",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"proxyquire": "^2.1.0",
"sinon": "^5.1.0",
"sinon-chai": "^3.1.0"
}
Expand Down
18 changes: 7 additions & 11 deletions build/src/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const dyndnsClient = require('./dyndnsClient');
const calls = require('./calls');
const logAdminCredentials = require('./logAdminCredentials');
const fetchVpnParameters = require('./fetchVpnParameters');
const getInstallationStaticIp = require('./utils/getInstallationStaticIp');

const URL = 'ws://my.wamp.dnp.dappnode.eth:8080/ws';
const REALM = 'dappnode_admin';
Expand Down Expand Up @@ -55,16 +54,8 @@ async function start() {
logs.info('Loading VPN parameters... It may take a while');
await fetchVpnParameters();

// Load the static IP defined in the installation
if (!db.get('staticIp').value()) {
const installationStaticIp = await getInstallationStaticIp();
if (installationStaticIp) {
logs.info(`Static IP was set during installation: ${installationStaticIp}`);
db.set('staticIp', installationStaticIp).value();
}
}

// If the user has not defined a static IP use dynamic DNS
// > staticIp is set in `await fetchVpnParameters();`
if (!db.get('staticIp').value()) {
logs.info('Registering to the dynamic DNS...');
await dyndnsClient.updateIp();
Expand All @@ -87,7 +78,12 @@ async function start() {
}
}, publicIpCheckInterval);

logs.info('VPN credentials fetched');
logs.info('VPN credentials fetched: ');
// Print db censoring privateKey
const dbClone = JSON.parse(JSON.stringify(db.getState()));
dbClone.keypair.privateKey = dbClone.keypair.privateKey.replace(/./g, '*');
logs.info(JSON.stringify(dbClone, null, 2 ));

logAdminCredentials();
}

Expand Down
40 changes: 13 additions & 27 deletions build/src/src/dyndnsClient/getKeys.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const EthCrypto = require('eth-crypto');
const fs = require('file-system');
const util = require('util');
const logs = require('../logs.js')(module);
const db = require('../db');

/**
* EthCrypto reference
Expand All @@ -27,12 +25,10 @@ const logs = require('../logs.js')(module);

// dyndnsHost has to be stripped of http(s):// tag
// process.env.DYNDNS_HOST should include said tag
const {DEV, DYNDNS_HOST} = process.env;
const dyndnsHost = DEV
? 'dyn.test.io'
: (DYNDNS_HOST && DYNDNS_HOST.includes('://'))
? DYNDNS_HOST.split('://')[1]
: DYNDNS_HOST;
const {DYNDNS_HOST} = process.env;
const dyndnsHost = DYNDNS_HOST && DYNDNS_HOST.includes('://')
? DYNDNS_HOST.split('://')[1]
: DYNDNS_HOST;

function generateKeys() {
const identity = EthCrypto.createIdentity();
Expand All @@ -43,26 +39,16 @@ function generateKeys() {
};
}

const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);

function getKeys() {
let path = process.env.DEV ? './mockFiles/keypair' : process.env.KEYPAIR_PATH;
if (!path) {
path = '/usr/src/app/secrets/keypair';
logs.warn('KEYPAIR_FILE_path is not defined. Defaulting to /usr/src/app/secrets/keypair');
const currentKeypair = db.get('keypair').value();
if (currentKeypair) {
return currentKeypair;
} else {
const newKeypair = generateKeys();
db.set('keypair', newKeypair).write();
db.set('domain', newKeypair.domain).write();
return newKeypair;
}
return readFile(path)
.then(JSON.parse)
.catch((err) => {
if (err.code === 'ENOENT') {
const keydata = generateKeys();
return writeFile(path, JSON.stringify(keydata))
.then(() => keydata);
} else {
logs.error('Error getting keys from '+path+': '+ err.stack);
}
});
}

module.exports = getKeys;
18 changes: 0 additions & 18 deletions build/src/src/dyndnsClient/test.js

This file was deleted.

88 changes: 44 additions & 44 deletions build/src/src/dyndnsClient/updateIp.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,53 +37,53 @@ const db = require('../db');
*/
function updateIp() {
// get keys
return getKeys().then((identity) => {
// From identity
const {privateKey} = identity;
const identity = getKeys();

// Prepare message
const timestamp = Math.floor(new Date() / 1000);
const messageHash = EthCrypto.hash.keccak256(timestamp.toString());
const signature = EthCrypto.sign(privateKey, messageHash);
const publicKey = EthCrypto.publicKeyByPrivateKey(privateKey);
const address = EthCrypto.publicKey.toAddress(publicKey);
// From identity
const {privateKey} = identity;

// Send message
const parameters = [
`address=${address}`,
`timestamp=${timestamp}`,
`sig=${signature}`,
];
// dyndnsHost has to contain http(s):// tag
// process.env.DYNDNS_HOST should include said tag
const dyndnsHost = process.env.DYNDNS_HOST;
const url = `${dyndnsHost}/?${parameters.join('&')}`;
// Prepare message
const timestamp = Math.floor(new Date() / 1000);
const messageHash = EthCrypto.hash.keccak256(timestamp.toString());
const signature = EthCrypto.sign(privateKey, messageHash);
const publicKey = EthCrypto.publicKeyByPrivateKey(privateKey);
const address = EthCrypto.publicKey.toAddress(publicKey);

return httpGetRequest(url, {format: 'json'})
.then((res) => {
const data = res.data || {};
// Deal with the answer
// Sample res:
// res.data = {
// 'ip': '63.84.220.164',
// 'domain': '1234abcd1234acbd.dyndns.dappnode.io',
// 'message': 'Your dynamic domain 1234abcd1234acbd.dyndns.dappnode.io
// has been updated to 63.11.22.164',
// };
if (res.code === 200) {
logs.info(`dyndns client success: ${data.message}`);
return data.domain;
} else {
const errorMsg = data.message || JSON.stringify(data);
logs.error(`dyndns client error code ${res.code}: ${errorMsg}`);
}
})
.then((domain) => {
db.set('domain', domain).write();
})
.catch((err) => {
logs.error(`httpGetRequest error: ${err.stack || err.message}`);
});
// Send message
const parameters = [
`address=${address}`,
`timestamp=${timestamp}`,
`sig=${signature}`,
];
// dyndnsHost has to contain http(s):// tag
// process.env.DYNDNS_HOST should include said tag
const dyndnsHost = process.env.DYNDNS_HOST;
const url = `${dyndnsHost}/?${parameters.join('&')}`;

return httpGetRequest(url, {format: 'json'})
.then((res) => {
const data = res.data || {};
// Deal with the answer
// Sample res:
// res.data = {
// 'ip': '63.84.220.164',
// 'domain': '1234abcd1234acbd.dyndns.dappnode.io',
// 'message': 'Your dynamic domain 1234abcd1234acbd.dyndns.dappnode.io
// has been updated to 63.11.22.164',
// };
if (res.code === 200) {
logs.info(`dyndns client success: ${data.message}`);
return data.domain;
} else {
const errorMsg = data.message || JSON.stringify(data);
logs.error(`dyndns client error code ${res.code}: ${errorMsg}`);
}
})
.then((domain) => {
db.set('domain', domain).write();
})
.catch((err) => {
logs.error(`httpGetRequest error: ${err.stack || err.message}`);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ function getInstallationStaticIp() {
.then((data) => String(data).trim())
// If the file is empty return null
.then((data) => data.length ? data : null)
.then((ip) => ipRegex({exact: true}).test(ip))
.then((ip) => {
if (ipRegex({exact: true}).test(ip)) return ip;
else return null;
})
.catch((err) => {
logs.error(`Error reading INSTALLATION_STATIC_IP ${INSTALLATION_STATIC_IP}: ${err.stack || err.message}`);
if (err.code === 'ENOENT') {
logs.warn(`INSTALLATION_STATIC_IP file not found at ${INSTALLATION_STATIC_IP}: ${err.stack || err.message}`);
} else {
logs.error(`Error reading INSTALLATION_STATIC_IP ${INSTALLATION_STATIC_IP}: ${err.stack || err.message}`);
}
return null;
});
}
Expand Down
Loading

0 comments on commit 536f516

Please sign in to comment.