From a7e7aa0e9312e8d975a8d36c2be128bac84747b3 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 1 Jun 2024 12:23:56 +0100 Subject: [PATCH] fix: fallback to protocols default port for onAuthRequired --- src/common/ts/proxyInfo.ts | 27 +++++++++++++++++++++------ src/options/options.ts | 3 ++- src/types.ts | 2 -- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/common/ts/proxyInfo.ts b/src/common/ts/proxyInfo.ts index 9b06313..4e8b482 100644 --- a/src/common/ts/proxyInfo.ts +++ b/src/common/ts/proxyInfo.ts @@ -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, diff --git a/src/options/options.ts b/src/options/options.ts index 640a648..0737953 100644 --- a/src/options/options.ts +++ b/src/options/options.ts @@ -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; type ListOptions = { diff --git a/src/types.ts b/src/types.ts index ce657e3..2502934 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,8 +3,6 @@ export type KeyOfType = 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"