Skip to content

Commit

Permalink
Merge pull request #137 from DIG-Network/release/v0.0.1-alpha.152
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.152
  • Loading branch information
MichaelTaylor3D authored Oct 6, 2024
2 parents 7c8e5da + 7fa4af5 commit d69e5a8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
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.152](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.151...v0.0.1-alpha.152) (2024-10-06)


### Features

* support hostnames in publicip env ([96a721b](https://github.com/DIG-Network/dig-chia-sdk/commit/96a721ba1aeba4e1abdfb86874fc340b568b6526))

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


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

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

2 changes: 1 addition & 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.151",
"version": "0.0.1-alpha.152",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/blockchain/ServerCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { NconfManager } from "../utils/NconfManager";
import { CoinData, ServerCoinData } from "../types";
import { DataStore } from "./DataStore";
import NodeCache from "node-cache";
import { getPublicIpAddress } from "../utils/network";
import { getPublicHost } from "../utils/network";
import { Environment } from "../utils/Environment";

const serverCoinCollateral = 300_000_000;
Expand Down Expand Up @@ -265,7 +265,7 @@ export class ServerCoin {
blacklist: string[] = []
): Promise<string[]> {
// We dont want our own IP to be included
const myIp = await getPublicIpAddress();
const myIp = await getPublicHost();
if (myIp) {
blacklist.push(myIp);
}
Expand Down
3 changes: 0 additions & 3 deletions src/utils/PeerRanker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class PeerRanker {
private async measureLatency(ip: string): Promise<number> {
const cachedMetrics = peerCache.get<PeerMetrics>(ip);
if (cachedMetrics && cachedMetrics.latency) {
console.log(`Latency for IP ${ip} retrieved from cache.`);
return cachedMetrics.latency;
}

Expand Down Expand Up @@ -97,7 +96,6 @@ export class PeerRanker {

return latency;
} catch (error: any) {
console.error(`Latency measurement failed for IP ${ip}:`, error.message);
throw new Error(`Latency measurement failed for IP ${ip}`);
}
}
Expand Down Expand Up @@ -148,7 +146,6 @@ export class PeerRanker {

return bandwidth;
} catch (error: any) {
console.error(`Bandwidth measurement failed for IP ${ip}:`, error.message);
throw new Error(`Bandwidth measurement failed for IP ${ip}`);
}
}
Expand Down
41 changes: 22 additions & 19 deletions src/utils/network.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
* Stopgap until better solution for finding public IPS found
*/
import superagent from "superagent";
import { Environment } from "./Environment";

Expand All @@ -11,22 +8,27 @@ const RETRY_DELAY = 2000; // in milliseconds
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,4}:){1,7}|:):(([0-9a-fA-F]{1,4}:){1,6}|:):([0-9a-fA-F]{1,4}|:):([0-9a-fA-F]{1,4}|:)|::)$/;
/^(([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}|:))$/;

// Helper function to validate the IP address
const isValidIp = (ip: string): boolean => {
return ipv4Regex.test(ip) || ipv6Regex.test(ip);
// 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 getPublicIpAddress = async (): Promise<string | undefined> => {
const publicIp = Environment.PUBLIC_IP;
export const getPublicHost = async (): Promise<string | undefined> => {
const publicHost = Environment.PUBLIC_IP;

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

if (publicIp) {
console.log("Public IP address from env:", publicIp);
if (isValidIp(publicIp)) {
return publicIp;
if (isValidHost(publicHost)) {
return publicHost;
}
console.error("Invalid public IP address in environment variable");

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

Expand All @@ -41,22 +43,22 @@ export const getPublicIpAddress = async (): Promise<string | undefined> => {
if (response.body && response.body.success) {
const ipAddress = response.body.ip_address;

if (isValidIp(ipAddress)) {
if (isValidHost(ipAddress)) {
return ipAddress;
}
throw new Error("Invalid IP address format received");
throw new Error("Invalid IP address or hostname format received");
}
throw new Error("Failed to retrieve public IP address");
throw new Error("Failed to retrieve public host");
} catch (error: any) {
attempt++;
console.error(
`Error fetching public IP address (Attempt ${attempt}):`,
`Error fetching public host (Attempt ${attempt}):`,
error.message
);

if (attempt >= MAX_RETRIES) {
throw new Error(
"Could not retrieve public IP address after several attempts"
"Could not retrieve public host after several attempts"
);
}

Expand All @@ -65,6 +67,7 @@ export const getPublicIpAddress = async (): Promise<string | undefined> => {
}
};


// Helper function to wrap IPv6 addresses in brackets
export const formatHost = (host: string): string => {
const ipv6Pattern = /^[a-fA-F0-9:]+$/; // Simple regex to match raw IPv6 addresses (without brackets)
Expand Down

0 comments on commit d69e5a8

Please sign in to comment.