Skip to content

Commit

Permalink
upgraded deps, switched requests timeout to AbortSignal.timeout()
Browse files Browse the repository at this point in the history
  • Loading branch information
ortexx committed Feb 12, 2024
1 parent 522bb31 commit 9cc5085
Show file tree
Hide file tree
Showing 13 changed files with 2,086 additions and 1,618 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v2
- name: Run tests with ${{ matrix.node-version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16
node-version: 18
- run: npm i -g npm@latest
- run: npm ci
- run: npm ddp
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,13 @@ const Client = require('spreadable').Client;
})();
```

In both cases the network is closed, but not completely secure. If we use http protocol the data transferred is not encrypted. In some cases this is a suitable option, otherwise you need to use https.
## Ban system

By default, nodes are configured to check each other's behavior. And if a node commits malicious actions for a significant time, then it will be banned by others for some time, depending on the severity of the crime. This system is not perfect, since it is not completely free from undeserved bans rerated to problems with the Internet provider, DNS cache, etc. Therefore, if you notice that some of your servers have stopped working with others, then first of all check the banlists and remove it if you are sure that these are technical problems. Access to these actions occurs through terminal commands. You should pause the server and run the necessary commands, for example:

``` spreadable -a getBanlist --no-s ```

``` spreadable -a emptyBanlist --no-s ```

## Versioning

Expand Down
12 changes: 6 additions & 6 deletions dist/client/spreadable.client.js

Large diffs are not rendered by default.

3,609 changes: 2,021 additions & 1,588 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spreadable",
"version": "0.2.28",
"version": "0.2.29",
"description": "Decentralized network mechanism",
"bin": {
"spreadable": "./bin/index.js"
Expand Down Expand Up @@ -76,8 +76,7 @@
"lokijs": "^1.5.10",
"ms": "^2.1.3",
"node-fetch": "^2.6.0",
"qiao-get-ip": "^0.2.5",
"sharp": "^0.30.7",
"sharp": "^0.33.2",
"signal-exit": "^3.0.3",
"tcp-port-used": "^1.0.2",
"text-to-svg": "^3.1.5",
Expand Down
5 changes: 3 additions & 2 deletions src/approval/transports/captcha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ module.exports = (Parent) => {
for(let i = 0; i < approvers.length; i++) {
const address = approvers[i];
const count = Math.floor(length / (approvers.length - i));
const from = String(answer).substr(this.captchaLength - length, count).toLowerCase();
const to = String(approver.answer).substr(0, count).toLowerCase();
const cLength = this.captchaLength - length;
const from = String(answer).slice(cLength, cLength + count).toLowerCase();
const to = String(approver.answer).slice(0, count).toLowerCase();

if(address == this.node.address) {
return from === to;
Expand Down
11 changes: 9 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ module.exports = (Parent) => {
* Make a group request
*
* @async
* @param {aray} arr
* @param {array} arr
* @param {string} action
* @param {object} [options]
* @returns {object}
Expand Down Expand Up @@ -354,12 +354,17 @@ module.exports = (Parent) => {
options.headers['content-type'] = 'application/json';
options.body = JSON.stringify(body);
}

if(options.timeout && !options.signal) {
options.signal = AbortSignal.timeout(options.timeout);
}

options.url = this.createRequestUrl(endpoint, options);
const start = Date.now();
let response = {};

try {
const response = await fetch(options.url, options);
response = await fetch(options.url, options);
this.logger.info(`Request to "${options.url}": ${ms(Date.now() - start)}`);

if(response.ok) {
Expand All @@ -380,8 +385,10 @@ module.exports = (Parent) => {
throw new errors.WorkError(body.message, body.code);
}
catch(err) {
options.timeout && err.type == 'aborted' && (err.type = 'request-timeout');
//eslint-disable-next-line no-ex-assign
utils.isRequestTimeoutError(err) && (err = utils.createRequestTimeoutError());
err.response = response;
err.requestOptions = options;
throw err;
}
Expand Down
9 changes: 7 additions & 2 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,10 @@ module.exports = (Parent) => {
options.headers['content-type'] = 'application/json';
options.body = Object.keys(body).length? JSON.stringify(body): undefined;
}

if(options.timeout && !options.signal) {
options.signal = AbortSignal.timeout(options.timeout);
}

const start = Date.now();
let response = {};
Expand All @@ -1242,7 +1246,8 @@ module.exports = (Parent) => {

throw new errors.WorkError(body.message, body.code);
}
catch(err) {
catch(err) {
options.timeout && err.type == 'aborted' && (err.type = 'request-timeout');
//eslint-disable-next-line no-ex-assign
utils.isRequestTimeoutError(err) && (err = utils.createRequestTimeoutError());
err.response = response;
Expand Down Expand Up @@ -1314,7 +1319,7 @@ module.exports = (Parent) => {
* Group request to the node
*
* @async
* @param {aray} arr
* @param {array} arr
* @param {string} url
* @param {object} [options]
* @returns {object}
Expand Down
36 changes: 27 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const path = require('path');
const uniqBy = require('lodash/uniqBy');
const dns = require('dns');
const tcpPortUsed = require('tcp-port-used');
const publicIp = require('qiao-get-ip');
const fetch = require('node-fetch');
const crypto = require('crypto');
const ip6addr = require('ip6addr');
const errors = require('./errors');
Expand All @@ -16,7 +16,13 @@ const utils = {
domainValidationRegex: /^localhost|[\p{L}\p{N}-][\p{L}\p{N}-]{1,61}[\p{L}\p{N}](?:\.[\p{L}]{2,})+$/iu,
dnsCache: new Map(),
dnsCacheLimit: 10000,
dnsCachePeriod: 1000 * 60 * 10
dnsCachePeriod: 1000 * 60 * 10,
ipLookupPoints: [
'https://api.ipify.org/',
'https://ipinfo.io/ip',
'https://ifconfig.me/ip',
'https://checkip.amazonaws.com/'
]
};

/**
Expand Down Expand Up @@ -348,12 +354,24 @@ utils.getRequestTimer = function (timeout, options = {}) {
* @returns {string}
*/
utils.getExternalIp = async function () {
try {
return await publicIp.getIp();
}
catch(err) {
return null;
}
let ip = null;

for(let url of this.ipLookupPoints) {
try {
const res = await fetch(url, { signal: AbortSignal.timeout(1000) });
const text = await res.text();

if(this.isValidIp(text)) {
ip = text;
break;
}
}
catch(err) {
continue;
}
}

return ip;
}

/**
Expand Down Expand Up @@ -627,7 +645,7 @@ utils.getRandomHexColor = function () {
* @return {string}
*/
utils.invertHexColor = function (color) {
return '#'+ (Number(`0x1${ color.substr(1) }`) ^ 0xFFFFFF).toString(16).substr(1).toUpperCase();
return '#'+ (Number(`0x1${ color.slice(1) }`) ^ 0xFFFFFF).toString(16).slice(1).toUpperCase();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/approval/captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const isPng = require('is-png');
const ApprovalCaptcha = require('../../src/approval/transports/captcha')();
const utils = require('../../src/utils');

describe('Approval', () => {
describe('ApprovalCaptcha', () => {
let approval;

describe('instance creation', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('group communication', () => {
for(let i = 0; i < approvers.length; i++) {
const address = approvers[i];
const count = Math.floor(length / (approvers.length - i));
answer += answers[address].substr(0, count);
answer += answers[address].slice(0, count);
length -= count;
}

Expand Down
1 change: 0 additions & 1 deletion webpack.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = (options = {}, wp) => {
"fs-extra": true,
"chalk": true,
"ip6addr": true,
"qiao-get-ip": true,
"tcp-port-used": true,
"validate-ip-node": true,
"crypto": true,
Expand Down

0 comments on commit 9cc5085

Please sign in to comment.