Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.0.1 alpha.162 #146

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.162](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.161...v0.0.1-alpha.162) (2024-10-07)


### Features

* add stun ip lookup ([fe17eaa](https://github.com/DIG-Network/dig-chia-sdk/commit/fe17eaa05aa4a6721dc6525f027d22a666f87a33))

### [0.0.1-alpha.161](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.160...v0.0.1-alpha.161) (2024-10-07)


Expand Down
48 changes: 46 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.161",
"version": "0.0.1-alpha.162",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down Expand Up @@ -47,6 +47,7 @@
"nanospinner": "^1.1.0",
"nconf": "^0.12.1",
"node-cache": "^5.1.2",
"node-stun": "^0.1.2",
"progress-stream": "^2.0.0",
"proper-lockfile": "^4.1.2",
"superagent": "^10.0.0",
Expand Down
85 changes: 25 additions & 60 deletions src/utils/network.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,35 @@
import superagent from "superagent";
import { Environment } from "./Environment";
// @ts-ignore
import stun, { STUN_BINDING_REQUEST } from 'node-stun';

const MAX_RETRIES = 5;
const RETRY_DELAY = 2000; // in milliseconds
// Get the Coturn server hostname from the environment variable or default to "localhost"
const COTURN_SERVER = process.env.STUN_SERVER || 'coturn'; // "coturn" refers to the Coturn service in Docker Compose
const COTURN_PORT = 3478; // Standard STUN port

// Regular expression for validating both IPv4 and IPv6 addresses
const ipv4Regex =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
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}|:))$/;

// Regular expression for validating hostnames
const hostnameRegex = /^(([a-zA-Z0-9](-*[a-zA-Z0-9])*)\.)*[a-zA-Z]{2,}$/;

// Helper function to validate the IP address or hostname
const isValidHost = (host: string): boolean => {
return ipv4Regex.test(host) || ipv6Regex.test(host) || hostnameRegex.test(host);
};

export const getPublicHost = async (): Promise<string | undefined> => {
const publicHost = process.env.PUBLIC_IP;

if (publicHost) {
console.log("Public IP/Hostname from env:", publicHost);

if (isValidHost(publicHost)) {
return publicHost;
}

console.error("Invalid public IP/Hostname in environment variable");
return undefined;
}

let attempt = 0;

while (attempt < MAX_RETRIES) {
try {
const response = await superagent.get(
"https://api.datalayer.storage/user/v1/get_user_ip"
);

if (response.body && response.body.success) {
const ipAddress = response.body.ip_address;

if (isValidHost(ipAddress)) {
return ipAddress;
return new Promise((resolve, reject) => {
const client = stun.createClient();

client.request(
COTURN_SERVER,
COTURN_PORT,
{ request: STUN_BINDING_REQUEST },
(err: any, response: any) => {
if (err) {
reject(`Failed to connect to Coturn server: ${err.message}`);
return;
}
throw new Error("Invalid IP address or hostname format received");
}
throw new Error("Failed to retrieve public host");
} catch (error: any) {
attempt++;
console.error(
`Error fetching public host (Attempt ${attempt}):`,
error.message
);

if (attempt >= MAX_RETRIES) {
throw new Error(
"Could not retrieve public host after several attempts"
);
// Extract public IP and port from the STUN response
const { address, port } = response.getXorAddress();
if (address && port) {
resolve(address);
} else {
reject('Failed to obtain public IP from Coturn server');
}
}

await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
}
}
);
});
};


Expand Down
Loading