fix: fallback to protocols default port for onAuthRequired

This commit is contained in:
Tim 2024-06-01 12:23:56 +01:00
parent 62e34f55b0
commit a7e7aa0e93
3 changed files with 23 additions and 9 deletions

View File

@ -1,7 +1,7 @@
import { Address6 } from "ip-address";
import type { ProxyInfo, ProxyScheme } from "../../types";
import type { ProxyInfo } from "../../types";
export const proxySchemes: { [key: string]: ProxyScheme } = {
export const proxySchemes = {
direct: "DIRECT",
http: "PROXY",
https: "HTTPS",
@ -9,19 +9,32 @@ export const proxySchemes: { [key: string]: ProxyScheme } = {
socks4: "SOCKS4",
socks5: "SOCKS5",
quic: "QUIC",
} as const;
type Protocol = keyof typeof proxySchemes;
const defaultPorts: Partial<{
[key in keyof typeof proxySchemes]: number;
}> = {
http: 80,
https: 443,
socks: 1080,
socks4: 1080,
socks5: 1080,
quic: 443,
};
export function getProxyInfoFromUrl(url: string) {
let protocol = "";
if (url.includes("://")) {
let [proto, urlWithoutProtocol] = url.split("://");
protocol = proto;
let [_protocol, urlWithoutProtocol] = url.split("://");
protocol = _protocol;
url = urlWithoutProtocol;
}
const lastIndexOfAt = url.lastIndexOf("@");
let hostname = url.substring(lastIndexOfAt + 1, url.length);
const lastIndexOfColon = getLastIndexOfColon(hostname);
hostname;
let host: string | undefined = undefined;
let port: number | undefined = undefined;
if (lastIndexOfColon === -1) {
@ -46,8 +59,10 @@ export function getProxyInfoFromUrl(url: string) {
password = credentials.substring(indexOfColon + 1, credentials.length);
}
port = port ? port : defaultPorts[protocol as Protocol];
return {
type: proxySchemes[protocol] ?? "PROXY",
type: proxySchemes[protocol as Protocol] ?? "PROXY",
protocol,
host,
port,

View File

@ -16,9 +16,10 @@ import sendAdLog from "../common/ts/sendAdLog";
import store from "../store";
import getDefaultState from "../store/getDefaultState";
import type { State } from "../store/types";
import { AllowedResult, KeyOfType, ProxyRequestType } from "../types";
import { KeyOfType, ProxyRequestType } from "../types";
//#region Types
type AllowedResult = [boolean, string?];
type InsertMode = "append" | "prepend" | "both";
type StoreStringArrayKey = KeyOfType<typeof store.state, string[]>;
type ListOptions = {

View File

@ -3,8 +3,6 @@ export type KeyOfType<T, V> = keyof {
[P in keyof T as T[P] extends V ? P : never]: any;
};
export type AllowedResult = [boolean, string?];
// From https://chromium.googlesource.com/chromium/src/+/HEAD/net/docs/proxy.md#proxy-server-schemes
export type ProxyScheme =
| "DIRECT"