-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,425 additions
and
1,898 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,50 @@ | ||
import { WebRequest } from "webextension-polyfill"; | ||
import getProxyInfoFromUrl from "../../common/ts/getProxyInfoFromUrl"; | ||
import store from "../../store"; | ||
|
||
const pendingRequests = []; | ||
|
||
export default function onAuthRequired( | ||
details: WebRequest.OnAuthRequiredDetailsType | ||
) { | ||
): void | WebRequest.BlockingResponseOrPromise { | ||
if (!details.isProxy) return; | ||
|
||
if (pendingRequests.includes(details.requestId)) { | ||
console.error( | ||
`🔐 Incorrect credentials provided for proxy ${details.challenger.host}:${details.challenger.port}.` | ||
`🔐 Provided invalid credentials for proxy ${details.challenger.host}:${details.challenger.port}.` | ||
); | ||
// TODO: Remove the proxy from the list of online proxies. | ||
return; | ||
} | ||
pendingRequests.push(details.requestId); | ||
|
||
let predicate = (proxy: string) => | ||
proxy.endsWith(`@${details.challenger.host}:${details.challenger.port}`); | ||
if (details.challenger.port === 3128) { | ||
// Default port | ||
predicate = (proxy: string) => | ||
proxy.endsWith( | ||
`@${details.challenger.host}:${details.challenger.port}` | ||
) || proxy.endsWith(`@${details.challenger.host}`); | ||
} | ||
|
||
const proxies = store.state.optimizedProxiesEnabled | ||
? store.state.optimizedProxies | ||
: store.state.normalProxies; | ||
const proxy = proxies.find(proxy => | ||
proxy.includes(`@${details.challenger.host}`) | ||
); | ||
if (!proxy) return; | ||
const [username, password] = proxy | ||
.substring(0, proxy.lastIndexOf("@")) | ||
.split(":"); | ||
console.log("Provided credentials for proxy", details.challenger.host); | ||
return { authCredentials: { username, password } }; | ||
const proxy = proxies.find(predicate); | ||
if (!proxy) { | ||
console.error( | ||
`🔐 No credentials found for proxy ${details.challenger.host}:${details.challenger.port}.` | ||
); | ||
return; | ||
} | ||
|
||
console.log(`🔑 Providing credentials for proxy ${proxy}.`); | ||
const proxyInfo = getProxyInfoFromUrl(proxy); | ||
return { | ||
authCredentials: { | ||
username: proxyInfo.username, | ||
password: proxyInfo.password, | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import ip from "ip"; | ||
import getProxyInfoFromUrl from "./getProxyInfoFromUrl"; | ||
|
||
/** | ||
* Anonymize an IP address by masking the last 2 octets of an IPv4 address | ||
* or the last 8 octets of an IPv6 address. | ||
* @param url | ||
* @returns | ||
*/ | ||
export function anonymizeIpAddress(url: string): string { | ||
const proxyInfo = getProxyInfoFromUrl(url); | ||
|
||
let proxyHost = proxyInfo.host; | ||
const withinBrackets = /^\[.*\]$/.test(proxyHost); | ||
if (withinBrackets) proxyHost = proxyHost.slice(1, -1); | ||
|
||
const isIPv4 = ip.isV4Format(proxyHost); | ||
const isIPv6 = ip.isV6Format(proxyHost); | ||
const isIP = isIPv4 || isIPv6; | ||
const isPublicIP = isIP && !ip.isPrivate(proxyHost); | ||
|
||
if (isPublicIP) { | ||
if (isIPv4) { | ||
proxyHost = ip.mask(proxyHost, "255.255.0.0").replace(/\.0\.0$/, ".*.*"); | ||
} else if (isIPv6) { | ||
proxyHost = ip.mask(proxyHost, "ffff:ffff:ffff:ffff:0000:0000:0000:0000"); | ||
} | ||
} | ||
|
||
if (withinBrackets) proxyHost = `[${proxyHost}]`; | ||
|
||
return `${proxyHost}:${proxyInfo.port}`; | ||
} | ||
|
||
/** | ||
* Anonymize an array of IP addresses. See {@link anonymizeIpAddress}. | ||
* @param urls | ||
* @returns | ||
*/ | ||
export function anonymizeIpAddresses(urls: string[]): string[] { | ||
return urls.map(url => anonymizeIpAddress(url)); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { ProxyInfo } from "../../types"; | ||
|
||
export default function getProxyInfoFromUrl(url: string): ProxyInfo { | ||
const lastIndexOfAt = url.lastIndexOf("@"); | ||
const hostname = url.substring(lastIndexOfAt + 1, url.length); | ||
const lastIndexOfColon = getLastIndexOfColon(hostname); | ||
|
||
let host: string | undefined = undefined; | ||
let port: number | undefined = undefined; | ||
if (lastIndexOfColon === -1) { | ||
host = hostname; | ||
port = 3128; // Default port | ||
} else { | ||
host = hostname.substring(0, lastIndexOfColon); | ||
port = Number(hostname.substring(lastIndexOfColon + 1, hostname.length)); | ||
} | ||
|
||
let username: string | undefined = undefined; | ||
let password: string | undefined = undefined; | ||
if (lastIndexOfAt !== -1) { | ||
const credentials = url.substring(0, lastIndexOfAt); | ||
const indexOfColon = credentials.indexOf(":"); | ||
username = credentials.substring(0, indexOfColon); | ||
password = credentials.substring(indexOfColon + 1, credentials.length); | ||
} | ||
|
||
return { | ||
type: "http", | ||
host, | ||
port, | ||
username, | ||
password, | ||
}; | ||
} | ||
|
||
/** | ||
* Returns the last index of a colon in a hostname, ignoring colons inside brackets. | ||
* Supports IPv6 addresses. | ||
* @param hostname | ||
* @returns Returns -1 if no colon is found. | ||
*/ | ||
function getLastIndexOfColon(hostname: string): number { | ||
let lastIndexOfColon = -1; | ||
let bracketDepth = 0; | ||
for (let i = hostname.length - 1; i >= 0; i--) { | ||
const char = hostname[i]; | ||
if (char === "]") { | ||
bracketDepth++; | ||
} else if (char === "[") { | ||
bracketDepth--; | ||
} else if (char === ":" && bracketDepth === 0) { | ||
lastIndexOfColon = i; | ||
break; | ||
} | ||
} | ||
return lastIndexOfColon; | ||
} |
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
Oops, something went wrong.