feat: Use built-in IP address check

This commit is contained in:
Tony Stipanic 2021-08-18 17:53:44 +02:00
parent e4db4e462e
commit 4878e46c9d
No known key found for this signature in database
GPG key ID: 3026BCCB6C9CC6BD
3 changed files with 89 additions and 9 deletions

88
package-lock.json generated
View file

@ -3769,6 +3769,12 @@
"@types/node": "*"
}
},
"@types/config": {
"version": "0.0.39",
"resolved": "https://registry.npmjs.org/@types/config/-/config-0.0.39.tgz",
"integrity": "sha512-EBHj9lSIyw62vwqCwkeJXjiV6C2m2o+RJZlRWLkHduGYiNBoMXcY6AhSLqjQQ+uPdrPYrOMYvVa41zjo00LbFQ==",
"dev": true
},
"@types/connect": {
"version": "3.4.35",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz",
@ -5288,6 +5294,11 @@
"integrity": "sha512-4/rOEg86jivtPTeOUUT61jJO1Ya1TrR/OkqCSZDyq84WJh3LuuiphBYJN+fm5xufIk4XAFcEwte/8WzC8If/1g==",
"dev": true
},
"buffer-writer": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz",
"integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw=="
},
"builtin-modules": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz",
@ -10342,11 +10353,6 @@
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
"integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA="
},
"isipaddress": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/isipaddress/-/isipaddress-0.0.2.tgz",
"integrity": "sha1-qeRIRlEGrwHmCFHPI146wwEUUNM="
},
"isnumeric": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/isnumeric/-/isnumeric-0.2.0.tgz",
@ -12397,6 +12403,11 @@
"integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
"dev": true
},
"packet-reader": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz",
"integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ=="
},
"pacote": {
"version": "11.3.4",
"resolved": "https://registry.npmjs.org/pacote/-/pacote-11.3.4.tgz",
@ -12690,6 +12701,68 @@
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns="
},
"pg": {
"version": "8.7.1",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.7.1.tgz",
"integrity": "sha512-7bdYcv7V6U3KAtWjpQJJBww0UEsWuh4yQ/EjNf2HeO/NnvKjpvhEIe/A/TleP6wtmSKnUnghs5A9jUoK6iDdkA==",
"requires": {
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "^2.5.0",
"pg-pool": "^3.4.1",
"pg-protocol": "^1.5.0",
"pg-types": "^2.1.0",
"pgpass": "1.x"
}
},
"pg-connection-string": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz",
"integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ=="
},
"pg-hstore": {
"version": "2.3.4",
"resolved": "https://registry.npmjs.org/pg-hstore/-/pg-hstore-2.3.4.tgz",
"integrity": "sha512-N3SGs/Rf+xA1M2/n0JBiXFDVMzdekwLZLAO0g7mpDY9ouX+fDI7jS6kTq3JujmYbtNSJ53TJ0q4G98KVZSM4EA==",
"requires": {
"underscore": "^1.13.1"
}
},
"pg-int8": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw=="
},
"pg-pool": {
"version": "3.4.1",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.4.1.tgz",
"integrity": "sha512-TVHxR/gf3MeJRvchgNHxsYsTCHQ+4wm3VIHSS19z8NC0+gioEhq1okDY1sm/TYbfoP6JLFx01s0ShvZ3puP/iQ=="
},
"pg-protocol": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.5.0.tgz",
"integrity": "sha512-muRttij7H8TqRNu/DxrAJQITO4Ac7RmX3Klyr/9mJEOBeIpgnF8f9jAfRz5d3XwQZl5qBjF9gLsUtMPJE0vezQ=="
},
"pg-types": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
"requires": {
"pg-int8": "1.0.1",
"postgres-array": "~2.0.0",
"postgres-bytea": "~1.0.0",
"postgres-date": "~1.0.4",
"postgres-interval": "^1.1.0"
}
},
"pgpass": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.4.tgz",
"integrity": "sha512-YmuA56alyBq7M59vxVBfPJrGSozru8QAdoNlWuW3cz8l+UX3cWge0vTvjKhsSHSJpo3Bom8/Mm6hf0TR5GY0+w==",
"requires": {
"split2": "^3.1.1"
}
},
"picomatch": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.0.tgz",
@ -17908,6 +17981,11 @@
}
}
},
"underscore": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.1.tgz",
"integrity": "sha512-hzSoAVtJF+3ZtiFX0VgfFPHEDRm7Y/QPjGyNo4TVdnDTdft3tr8hEkD25a1jC+TjTuE7tkHGKkhwCgs9dgBB2g=="
},
"unicode-canonical-property-names-ecmascript": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz",

View file

@ -34,7 +34,6 @@
"config": "^3.3.6",
"dns-then": "^0.1.0",
"express": "^4.17.1",
"isipaddress": "0.0.2",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"matrix-bot-sdk": "^0.3.8",
@ -44,6 +43,8 @@
"moment": "^2.29.1",
"netmask": "^2.0.2",
"ngx-ui-switch": "^12.0.1",
"pg": "^8.7.1",
"pg-hstore": "^2.3.4",
"postcss": "^8.3.5",
"random-string": "^0.2.0",
"request": "^2.88.2",
@ -94,6 +95,7 @@
"@ng-bootstrap/ng-bootstrap": "^11.0.0-beta.2",
"@types/bluebird": "^3.5.36",
"@types/body-parser": "^1.19.1",
"@types/config": "0.0.39",
"@types/jquery": "^3.5.6",
"@types/node": "^14.17.4",
"@types/validator": "^13.6.3",

View file

@ -4,8 +4,8 @@ import { Cache, CACHE_FEDERATION } from "../MemoryCache";
import * as request from "request";
import config from "../config";
import splitHost from 'split-host';
import * as isIP from "isipaddress";
import * as requestPromise from "request-promise";
import { isIP } from "net";
export interface IFederationConnectionInfo {
hostname: string;
@ -46,7 +46,7 @@ export async function getFederationConnInfo(serverName: string): Promise<IFedera
}
// Step 1 of the discovery process: if the hostname is an IP, use that with explicit or default port
if (isIP.test(hp.host)) {
if (isIP(hp.host) != 0) {
const fedUrl = `https://${hp.host}:${hp.port}`;
const fedObj = {url: fedUrl, hostname: serverName};
Cache.for(CACHE_FEDERATION).put(serverName, fedObj, expirationMs);
@ -81,7 +81,7 @@ export async function getFederationConnInfo(serverName: string): Promise<IFedera
}
// Step 3a: if the delegated host is an IP address, use that (regardless of port)
if (isIP.test(wkHp.host)) {
if (isIP(wkHp.host) != 0) {
const fedUrl = `https://${wkHp.host}:${wkHp.port}`;
const fedObj = {url: fedUrl, hostname: wkServerAddr};
Cache.for(CACHE_FEDERATION).put(serverName, fedObj, expirationMs);