-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arweave.init default removes subdomain
- Loading branch information
1 parent
568a0f1
commit c066e0e
Showing
6 changed files
with
129 additions
and
123 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,69 @@ | ||
export interface NetConfig { | ||
protocol: string, | ||
host: string, | ||
port?: number, | ||
protocol: string; | ||
host: string; | ||
port?: number; | ||
} | ||
|
||
/** exhaustive localhost testing */ | ||
const isLocal = (protocol: string, hostname: string)=>{ | ||
const regexLocalIp = /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ | ||
const isLocal = (protocol: string, hostname: string) => { | ||
const regexLocalIp = /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/; | ||
|
||
const split = hostname.split('.') | ||
const tld = split[split.length - 1] // check if subdomain on the localhost | ||
const split = hostname.split("."); | ||
const tld = split[split.length - 1]; // check if subdomain on the localhost | ||
|
||
const localStrings = ["localhost", "[::1]"] | ||
const localStrings = ["localhost", "[::1]"]; | ||
|
||
return localStrings.includes(hostname) || protocol == "file" | ||
|| localStrings.includes(tld) || !!hostname.match(regexLocalIp) || !!tld.match(regexLocalIp) | ||
} | ||
return ( | ||
localStrings.includes(hostname) || | ||
protocol == "file" || | ||
localStrings.includes(tld) || | ||
!!hostname.match(regexLocalIp) || | ||
!!tld.match(regexLocalIp) | ||
); | ||
}; | ||
|
||
/** simplified tests for ip addresses */ | ||
const isIpAdress = (host: string)=> { | ||
// an IPv6 location.hostname (and only IPv6 hostnames) must be surrounded by square brackets | ||
const isIpv6 = host.charAt(0) === '[' | ||
// Potential speed-up for IPv4 detection: | ||
// the tld of a domain name cannot be a number (IDN location.hostnames appear to be converted, needs further clarification) | ||
const regexMatchIpv4 = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/ | ||
|
||
return !!host.match(regexMatchIpv4) || isIpv6 | ||
} | ||
|
||
export const getDefaultConfig = (protocol: string, host: string): NetConfig=>{ | ||
const isIpAdress = (host: string) => { | ||
// an IPv6 location.hostname (and only IPv6 hostnames) must be surrounded by square brackets | ||
const isIpv6 = host.charAt(0) === "["; | ||
// Potential speed-up for IPv4 detection: | ||
// the tld of a domain name cannot be a number (IDN location.hostnames appear to be converted, needs further clarification) | ||
const regexMatchIpv4 = | ||
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/; | ||
|
||
// If we're running in what looks like a local dev environment | ||
// then default to using arweave.net | ||
if(isLocal(protocol, host)){ | ||
return { | ||
protocol: "https", | ||
host: "arweave.net", | ||
port: 443, | ||
}; | ||
} | ||
return !!host.match(regexMatchIpv4) || isIpv6; | ||
}; | ||
|
||
//check if hostname is an IP address before removing first subdomain | ||
if(!isIpAdress(host)){ | ||
let split = host.split('.') | ||
if(split.length >=3){ | ||
split.shift() | ||
const parentDomain = split.join('.') | ||
return { | ||
protocol, | ||
host: parentDomain, | ||
} | ||
} | ||
} | ||
export const getDefaultConfig = (protocol: string, host: string): NetConfig => { | ||
// If we're running in what looks like a local dev environment | ||
// then default to using arweave.net | ||
if (isLocal(protocol, host)) { | ||
return { | ||
protocol: "https", | ||
host: "arweave.net", | ||
port: 443, | ||
}; | ||
} | ||
|
||
// there are 2 potential garbage returns here: | ||
// a non-GW ip address & a non-GW hostname without ArNS. garbage in, garbage out. | ||
// they should be overridden with user inputs in apiConfig. | ||
// otherwise we have a valid ip based GW address. | ||
return { | ||
protocol, | ||
host, | ||
}; | ||
} | ||
//check if hostname is an IP address before removing first subdomain | ||
if (!isIpAdress(host)) { | ||
let split = host.split("."); | ||
if (split.length >= 3) { | ||
split.shift(); | ||
const parentDomain = split.join("."); | ||
return { | ||
protocol, | ||
host: parentDomain, | ||
}; | ||
} | ||
} | ||
|
||
// there are 2 potential garbage returns here: | ||
// a non-GW ip address & a non-GW hostname without ArNS. garbage in, garbage out. | ||
// they should be overridden with user inputs in apiConfig. | ||
// otherwise we have a valid ip based GW address. | ||
return { | ||
protocol, | ||
host, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,54 @@ | ||
import { expect } from "chai"; | ||
import {} from "mocha"; | ||
import { getDefaultConfig } from '../src/web/net-config' | ||
import { getDefaultConfig } from "../src/web/net-config"; | ||
|
||
|
||
describe('Net Config', function () { | ||
describe("Net Config", function () { | ||
this.timeout(2_000); | ||
|
||
it("should detect localhost dev environment", async function () { | ||
const file = getDefaultConfig('file', '') | ||
expect(file.protocol).equal('https') | ||
expect(file.host).equal('arweave.net') | ||
expect(file.port).equal(443) | ||
const localhost = getDefaultConfig('http', 'sub.fake.localhost') | ||
expect(localhost.protocol).equal('https') | ||
expect(localhost.host).equal('arweave.net') | ||
expect(localhost.port).equal(443) | ||
const ipv4 = getDefaultConfig('http', '127.0.0.255') | ||
expect(ipv4.protocol).equal('https') | ||
expect(ipv4.host).equal('arweave.net') | ||
expect(ipv4.port).equal(443) | ||
const ipv6 = getDefaultConfig('http', '[::1]') | ||
expect(ipv6.protocol).equal('https') | ||
expect(ipv6.host).equal('arweave.net') | ||
expect(ipv6.port).equal(443) | ||
it("should detect localhost dev environment", async function () { | ||
const file = getDefaultConfig("file", ""); | ||
expect(file.protocol).equal("https"); | ||
expect(file.host).equal("arweave.net"); | ||
expect(file.port).equal(443); | ||
const localhost = getDefaultConfig("http", "sub.fake.localhost"); | ||
expect(localhost.protocol).equal("https"); | ||
expect(localhost.host).equal("arweave.net"); | ||
expect(localhost.port).equal(443); | ||
const ipv4 = getDefaultConfig("http", "127.0.0.255"); | ||
expect(ipv4.protocol).equal("https"); | ||
expect(ipv4.host).equal("arweave.net"); | ||
expect(ipv4.port).equal(443); | ||
const ipv6 = getDefaultConfig("http", "[::1]"); | ||
expect(ipv6.protocol).equal("https"); | ||
expect(ipv6.host).equal("arweave.net"); | ||
expect(ipv6.port).equal(443); | ||
}); | ||
|
||
it("should remove first subdomain when appropriate", async()=> { | ||
const subdomain = getDefaultConfig('https', 'arnsname.example.com') | ||
expect(subdomain.protocol).equal('https') | ||
expect(subdomain.host).equal('example.com') | ||
expect(subdomain.port).undefined | ||
const generated = getDefaultConfig('https', 'ngnrj2ntoigcuduz2xwowwzaxojwinwb7qugblukljxkhrymozaq.example.com') | ||
expect(generated.protocol).equal('https') | ||
expect(generated.host).equal('example.com') | ||
expect(generated.port).undefined | ||
}) | ||
|
||
it("should let ip addresses pass through", async()=> { | ||
const ipv4 = getDefaultConfig('https', '123.123.123.123') | ||
expect(ipv4.protocol).equal('https') | ||
expect(ipv4.host).equal('123.123.123.123') | ||
expect(ipv4.port).undefined | ||
const ipv6 = getDefaultConfig('https', '[2001:db8:3333:4444:5555:6666:7777:8888]') | ||
expect(ipv6.protocol).equal('https') | ||
expect(ipv6.host).equal('[2001:db8:3333:4444:5555:6666:7777:8888]') | ||
expect(ipv6.port).undefined | ||
}) | ||
|
||
it("should remove first subdomain when appropriate", async () => { | ||
const subdomain = getDefaultConfig("https", "arnsname.example.com"); | ||
expect(subdomain.protocol).equal("https"); | ||
expect(subdomain.host).equal("example.com"); | ||
expect(subdomain.port).undefined; | ||
const generated = getDefaultConfig( | ||
"https", | ||
"ngnrj2ntoigcuduz2xwowwzaxojwinwb7qugblukljxkhrymozaq.example.com" | ||
); | ||
expect(generated.protocol).equal("https"); | ||
expect(generated.host).equal("example.com"); | ||
expect(generated.port).undefined; | ||
}); | ||
|
||
}) | ||
it("should let ip addresses pass through", async () => { | ||
const ipv4 = getDefaultConfig("https", "123.123.123.123"); | ||
expect(ipv4.protocol).equal("https"); | ||
expect(ipv4.host).equal("123.123.123.123"); | ||
expect(ipv4.port).undefined; | ||
const ipv6 = getDefaultConfig( | ||
"https", | ||
"[2001:db8:3333:4444:5555:6666:7777:8888]" | ||
); | ||
expect(ipv6.protocol).equal("https"); | ||
expect(ipv6.host).equal("[2001:db8:3333:4444:5555:6666:7777:8888]"); | ||
expect(ipv6.port).undefined; | ||
}); | ||
}); |