@dignetwork/dig-sdk 0.0.1-alpha.161 → 0.0.1-alpha.162
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/network.d.ts.map +1 -1
- package/dist/utils/network.js +42 -43
- package/package.json +2 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/utils/network.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/utils/network.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,aAAa,QAAa,OAAO,CAAC,MAAM,GAAG,SAAS,CAwBhE,CAAC;AAIF,eAAO,MAAM,UAAU,SAAU,MAAM,KAAG,MAUzC,CAAC"}
|
package/dist/utils/network.js
CHANGED
|
@@ -1,53 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
4
24
|
};
|
|
5
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
26
|
exports.formatHost = exports.getPublicHost = void 0;
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
//
|
|
11
|
-
const
|
|
12
|
-
const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,7}|:):){1,7}([0-9a-fA-F]{1,4}|:))$/;
|
|
13
|
-
// Regular expression for validating hostnames
|
|
14
|
-
const hostnameRegex = /^(([a-zA-Z0-9](-*[a-zA-Z0-9])*)\.)*[a-zA-Z]{2,}$/;
|
|
15
|
-
// Helper function to validate the IP address or hostname
|
|
16
|
-
const isValidHost = (host) => {
|
|
17
|
-
return ipv4Regex.test(host) || ipv6Regex.test(host) || hostnameRegex.test(host);
|
|
18
|
-
};
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
const node_stun_1 = __importStar(require("node-stun"));
|
|
29
|
+
// Get the Coturn server hostname from the environment variable or default to "localhost"
|
|
30
|
+
const COTURN_SERVER = process.env.STUN_SERVER || 'coturn'; // "coturn" refers to the Coturn service in Docker Compose
|
|
31
|
+
const COTURN_PORT = 3478; // Standard STUN port
|
|
19
32
|
const getPublicHost = async () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
console.error("Invalid public IP/Hostname in environment variable");
|
|
27
|
-
return undefined;
|
|
28
|
-
}
|
|
29
|
-
let attempt = 0;
|
|
30
|
-
while (attempt < MAX_RETRIES) {
|
|
31
|
-
try {
|
|
32
|
-
const response = await superagent_1.default.get("https://api.datalayer.storage/user/v1/get_user_ip");
|
|
33
|
-
if (response.body && response.body.success) {
|
|
34
|
-
const ipAddress = response.body.ip_address;
|
|
35
|
-
if (isValidHost(ipAddress)) {
|
|
36
|
-
return ipAddress;
|
|
37
|
-
}
|
|
38
|
-
throw new Error("Invalid IP address or hostname format received");
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const client = node_stun_1.default.createClient();
|
|
35
|
+
client.request(COTURN_SERVER, COTURN_PORT, { request: node_stun_1.STUN_BINDING_REQUEST }, (err, response) => {
|
|
36
|
+
if (err) {
|
|
37
|
+
reject(`Failed to connect to Coturn server: ${err.message}`);
|
|
38
|
+
return;
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
console.error(`Error fetching public host (Attempt ${attempt}):`, error.message);
|
|
45
|
-
if (attempt >= MAX_RETRIES) {
|
|
46
|
-
throw new Error("Could not retrieve public host after several attempts");
|
|
40
|
+
// Extract public IP and port from the STUN response
|
|
41
|
+
const { address, port } = response.getXorAddress();
|
|
42
|
+
if (address && port) {
|
|
43
|
+
resolve(address);
|
|
47
44
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
else {
|
|
46
|
+
reject('Failed to obtain public IP from Coturn server');
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
51
50
|
};
|
|
52
51
|
exports.getPublicHost = getPublicHost;
|
|
53
52
|
// Helper function to wrap IPv6 addresses in brackets
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dignetwork/dig-sdk",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.162",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"nanospinner": "^1.1.0",
|
|
48
48
|
"nconf": "^0.12.1",
|
|
49
49
|
"node-cache": "^5.1.2",
|
|
50
|
+
"node-stun": "^0.1.2",
|
|
50
51
|
"progress-stream": "^2.0.0",
|
|
51
52
|
"proper-lockfile": "^4.1.2",
|
|
52
53
|
"superagent": "^10.0.0",
|