Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
fix: second network unused on Linux while another is plugged in
Anmol Kapil32c1a69881f8
diff --git a/electron-builder.yml b/electron-builder.ymlindex 3e4464d..17a2ed4 100644--- a/electron-builder.yml+++ b/electron-builder.yml@@ -8,6 +8,9 @@ files:- '!electron.vite.config.{js,ts,mjs,cjs}'- '!{.eslintcache,eslint.config.mjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}'- '!{.env,.env.*,.npmrc,pnpm-lock.yaml}'+ # koffi is only loaded on Linux (network/deviceBinding.ts) and ships prebuilds for every OS.+ - '!node_modules/koffi/{src,vendor,doc}/**'+ - '!node_modules/koffi/build/koffi/!(linux_x64|linux_arm64)/**'- '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}'asarUnpack:- resources/**diff --git a/package-lock.json b/package-lock.jsonindex 3e1e8db..758d41c 100644--- a/package-lock.json+++ b/package-lock.json@@ -14,6 +14,7 @@"@electron-toolkit/utils": "^4.0.0","class-variance-authority": "^0.7.1","cn": "^0.3.0",+ "koffi": "^2.16.3","lucide-react": "^1.47.0","zustand": "^5.0.15"},@@ -6884,6 +6885,16 @@"json-buffer": "3.0.1"}},+ "node_modules/koffi": {+ "version": "2.16.3",+ "resolved": "https://registry.npmjs.org/koffi/-/koffi-2.16.3.tgz",+ "integrity": "sha512-E9y1AsgYGlaxMhcZzHr8y96QF2U5XzA12GGVAfbWqIubTwPNMXQarfBzePNXHe0xtIEtNd6ifAv3GAKYGUeBAQ==",+ "hasInstallScript": true,+ "license": "MIT",+ "funding": {+ "url": "https://liberapay.com/Koromix"+ }+ },"node_modules/lazy-val": {"version": "1.0.5","resolved": "https://registry.npmjs.org/lazy-val/-/lazy-val-1.0.5.tgz",diff --git a/package.json b/package.jsonindex 4d0da13..3f18f21 100644--- a/package.json+++ b/package.json@@ -29,6 +29,7 @@"@electron-toolkit/utils": "^4.0.0","class-variance-authority": "^0.7.1","cn": "^0.3.0",+ "koffi": "^2.16.3","lucide-react": "^1.47.0","zustand": "^5.0.15"},diff --git a/src/main/download/chunkDownloader.ts b/src/main/download/chunkDownloader.tsindex df3ffc1..3c76b13 100644--- a/src/main/download/chunkDownloader.ts+++ b/src/main/download/chunkDownloader.ts@@ -2,6 +2,7 @@ import { createWriteStream, type WriteStream } from 'node:fs'import { request as httpRequest, type ClientRequest, type IncomingMessage } from 'node:http'import { request as httpsRequest } from 'node:https'import { URL } from 'node:url'+import { routeFrom } from '../network/deviceBinding'import { testKnobs } from '../testKnobs'import { compareVersion, type FileVersion, type VersionCheck } from './fileVersion'@@ -177,11 +178,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {hostname: targetUrl.hostname,port: targetUrl.port || undefined,path: `${targetUrl.pathname}${targetUrl.search}`,- localAddress,- // localAddress is always an IPv4 interface address — force the remote- // host to resolve to IPv4 too, or binding fails with EINVAL when DNS- // hands back an IPv6 address for it instead.- family: 4,+ ...routeFrom(localAddress, targetUrl),headers},(res: IncomingMessage) => {@@ -336,8 +333,7 @@ export function fetchRange(hostname: target.hostname,port: target.port || undefined,path: `${target.pathname}${target.search}`,- localAddress,- family: 4,+ ...routeFrom(localAddress, target),headers: { 'User-Agent': 'Plexo/1.0', Range: `bytes=${start}-${end}` }},(res) => {diff --git a/src/main/ipc/handlers.ts b/src/main/ipc/handlers.tsindex b26f607..f2f6861 100644--- a/src/main/ipc/handlers.ts+++ b/src/main/ipc/handlers.ts@@ -15,6 +15,7 @@ import type { NetworkInterfaceInfo, ThemeSource } from '../../shared/types'import { DownloadManager } from '../download/downloadManager'import { getDefaultDownloadsDir, getHomeDir } from '../download/paths'import { probeUrl } from '../download/probe'+import { deviceBindingSupported } from '../network/deviceBinding'import { measureLatencies } from '../network/latency'import { listActiveInterfaces } from '../network/interfaces'import { loadNetworkPreferences, saveNetworkPreference } from '../network/preferences'@@ -75,6 +76,10 @@ export function registerIpcHandlers(getWindow: () => BrowserWindow | null): Downhandle('pingInterfaces', async () => measureLatencies(cachedInterfaces))+ // Started now so it has settled before the first ping or download needs it.+ const bindingSupport = deviceBindingSupported()+ handle('deviceBindingSupported', async () => bindingSupport)+handle('getNetworkPreferences', async () => loadNetworkPreferences())handle('setNetworkPreference', async (_event, id, patch) => saveNetworkPreference(id, patch))diff --git a/src/main/network/deviceBinding.ts b/src/main/network/deviceBinding.tsnew file mode 100644index 0000000..b6c3f4c--- /dev/null+++ b/src/main/network/deviceBinding.ts@@ -0,0 +1,123 @@+import type { ClientRequestArgs } from 'node:http'+import { connect, isIP, Socket, type SocketConstructorOpts } from 'node:net'+import { networkInterfaces } from 'node:os'+import { connect as tlsConnect } from 'node:tls'++// Linux picks a socket's outgoing interface from the routing table alone: binding to an+// interface's IP (Node's `localAddress`) still sends the packets out the default route, where the+// network that doesn't own that IP drops them. SO_BINDTODEVICE pins the socket to the interface+// itself. Node has no setsockopt, so the socket is made through libc and handed to Node as an fd.+// Unprivileged since Linux 5.7. macOS and Windows already route by source address.++const AF_INET = 2+const SOCK_STREAM = 1+const SOCK_CLOEXEC = 0o2000000+const SOL_SOCKET = 1+const SO_BINDTODEVICE = 25++interface Libc {+ socket: (domain: number, type: number, protocol: number) => number+ setsockopt: (fd: number, level: number, name: number, value: string, length: number) => number+ close: (fd: number) => number+ errno: () => number+}++/** Set once the support check below passes; until then everything falls back to localAddress. */+let libc: Libc | null = null+let support: Promise<boolean> | null = null++function openOnDevice(lib: Libc, device: string): number {+ const fd = lib.socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)+ if (fd < 0) throw new Error(`socket() failed (errno ${lib.errno()})`)+ if (+ lib.setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, device, Buffer.byteLength(device) + 1) !== 0+ ) {+ const errno = lib.errno()+ lib.close(fd)+ throw new Error(`Couldn't bind a socket to ${device} (errno ${errno})`)+ }+ return fd+}++/** Whether each network can carry its own connections. Always true off Linux. */+export function deviceBindingSupported(): Promise<boolean> {+ support ??= (async () => {+ if (process.platform !== 'linux') return true+ try {+ const { default: koffi } = await import('koffi')+ const lib = koffi.load('libc.so.6')+ const candidate: Libc = {+ socket: lib.func('int socket(int, int, int)'),+ setsockopt: lib.func('int setsockopt(int, int, int, const char *, uint32_t)'),+ close: lib.func('int close(int)'),+ errno: () => koffi.errno()+ }+ // Loopback always exists, so this only fails when the kernel refuses (EPERM before 5.7).+ candidate.close(openOnDevice(candidate, 'lo'))+ libc = candidate+ return true+ } catch (error) {+ console.warn('Per-network binding unavailable, using the default route:', error)+ return false+ }+ })()+ return support+}++function deviceFor(localAddress: string): string | undefined {+ for (const [device, addresses] of Object.entries(networkInterfaces())) {+ if (addresses?.some((addr) => addr.address === localAddress)) return device+ }+ return undefined+}++/** A TCP connection to host:port that leaves through the interface owning `localAddress`. */+export function connectFrom(localAddress: string, host: string, port: number): Socket {+ const device = libc && deviceFor(localAddress)+ if (!libc || !device) return connect({ host, port, localAddress, family: 4 })++ let fd: number+ try {+ fd = openOnDevice(libc, device)+ } catch (error) {+ // The interface vanished since it was listed — surface it like any other connect error.+ const socket = new Socket()+ process.nextTick(() => socket.destroy(error as Error))+ return socket+ }+ // manualStart: a socket wrapped around an fd starts reading at once, before it's connected.+ // No localAddress: the device already picks the source IP, and a second bind would fail.+ return new Socket({ fd, manualStart: true } as SocketConstructorOpts).connect({+ host,+ port,+ family: 4+ })+}++/** Options that route an http(s) request for `target` through the interface owning+ * `localAddress`. Spread them after `port`. */+export function routeFrom(+ localAddress: string,+ target: URL+): Pick<+ ClientRequestArgs,+ 'localAddress' | 'family' | 'createConnection' | 'port' | 'defaultPort'+> {+ // localAddress is always an IPv4 interface address, so the remote host has to resolve to+ // IPv4 too, or binding fails with EINVAL when DNS hands back an IPv6 address instead.+ if (!libc || !deviceFor(localAddress)) return { localAddress, family: 4 }++ const secure = target.protocol === 'https:'+ const defaultPort = secure ? 443 : 80+ const port = Number(target.port) || defaultPort+ const host = target.hostname+ return {+ // Without an agent, Node can't infer the scheme's port and would write it into Host.+ port,+ defaultPort,+ createConnection: () => {+ const socket = connectFrom(localAddress, host, port)+ return secure ? tlsConnect({ socket, servername: isIP(host) ? undefined : host }) : socket+ }+ }+}diff --git a/src/main/network/interfaces.ts b/src/main/network/interfaces.tsindex a145b4b..780670b 100644--- a/src/main/network/interfaces.ts+++ b/src/main/network/interfaces.ts@@ -81,7 +81,7 @@ async function getWindowsAdapters(): Promise<Map<string, WindowsAdapter>> {/*** Active non-loopback IPv4 interfaces. Each one has its* own local IP, which is what lets us bind a download's outgoing connection- * to a specific interface (see chunkDownloader's `localAddress` option).+ * to a specific interface (see deviceBinding's `routeFrom`).*/export async function listActiveInterfaces(): Promise<NetworkInterfaceInfo[]> {const overridden = testInterfaces()diff --git a/src/main/network/latency.ts b/src/main/network/latency.tsindex c6e0ab5..f52732b 100644--- a/src/main/network/latency.ts+++ b/src/main/network/latency.ts@@ -1,5 +1,5 @@-import { connect } from 'node:net'import type { NetworkInterfaceInfo } from '../../shared/types'+import { connectFrom } from './deviceBinding'const PROBE_HOSTS = ['1.1.1.1', '8.8.8.8']const PROBE_PORT = 443@@ -10,7 +10,7 @@ const TIMEOUT_MS = 2000function measureLatencyToHost(localAddress: string, host: string): Promise<number | null> {return new Promise((resolve) => {const start = Date.now()- const socket = connect({ host, port: PROBE_PORT, localAddress, family: 4 })+ const socket = connectFrom(localAddress, host, PROBE_PORT)let settled = falseconst finish = (result: number | null): void => {diff --git a/src/preload/index.ts b/src/preload/index.tsindex c2930ed..55fa46c 100644--- a/src/preload/index.ts+++ b/src/preload/index.ts@@ -18,6 +18,7 @@ const plexoApi = {listInterfaces: () => invoke('listInterfaces'),pingInterfaces: () => invoke('pingInterfaces'),+ deviceBindingSupported: () => invoke('deviceBindingSupported'),openNetworkSettings: () => invoke('openNetworkSettings'),getNetworkPreferences: () => invoke('getNetworkPreferences'),setNetworkPreference: (id: string, patch: NetworkPreference) =>diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsxindex e7a8eed..4823d0b 100644--- a/src/renderer/src/App.tsx+++ b/src/renderer/src/App.tsx@@ -2,6 +2,7 @@ import type { DownloadState } from '@shared/types'import { useEffect } from 'react'import { DevToolsPanel } from './components/DevToolsPanel'import { TitleBar, type TitleBarStatus } from './components/TitleBar'+import { NetworkBindingDialog } from './components/NetworkBindingDialog'import { UpdateDialog } from './components/UpdateDialog'import { TooltipProvider } from './components/ui/tooltip'import { useDownloadEvents } from './hooks/useDownloadEvents'@@ -124,6 +125,7 @@ function App(): React.JSX.Element {<div className="min-h-0 flex-1">{screen}</div><DevToolsPanel /><UpdateDialog />+ <NetworkBindingDialog /></div></TooltipProvider>)diff --git a/src/renderer/src/components/NetworkBindingDialog.tsx b/src/renderer/src/components/NetworkBindingDialog.tsxnew file mode 100644index 0000000..264bf2f--- /dev/null+++ b/src/renderer/src/components/NetworkBindingDialog.tsx@@ -0,0 +1,47 @@+import { useEffect, useState } from 'react'+import { useAppStore } from '../store/useAppStore'+import {+ AlertDialog,+ AlertDialogAction,+ AlertDialogContent,+ AlertDialogDescription,+ AlertDialogFooter,+ AlertDialogHeader,+ AlertDialogTitle+} from './ui/alert-dialog'++/** Linux before 5.7 can't pin a connection to one network, so everything leaves through the+ * default route — only worth saying once there's a second network that won't get used. */+export function NetworkBindingDialog(): React.JSX.Element {+ const networkCount = useAppStore((store) => store.interfaces.length)+ const [supported, setSupported] = useState(true)+ const [dismissed, setDismissed] = useState(false)++ useEffect(() => {+ window.plexo.deviceBindingSupported().then(setSupported, () => {})+ }, [])++ return (+ <AlertDialog+ open={!supported && !dismissed && networkCount > 1}+ onOpenChange={(open) => {+ if (!open) setDismissed(true)+ }}+ >+ <AlertDialogContent>+ <AlertDialogHeader>+ <AlertDialogTitle>Only your default network can be used</AlertDialogTitle>+ <AlertDialogDescription>+ This system doesn't let apps send a connection through a specific network, which+ needs Linux kernel 5.7 or newer. Every connection will go through your default network,+ so the others will show no ping and won't add speed. Updating your system fixes+ this.+ </AlertDialogDescription>+ </AlertDialogHeader>+ <AlertDialogFooter>+ <AlertDialogAction>Got it</AlertDialogAction>+ </AlertDialogFooter>+ </AlertDialogContent>+ </AlertDialog>+ )+}diff --git a/src/shared/ipc-channels.ts b/src/shared/ipc-channels.tsindex 42327ce..2ffe7b9 100644--- a/src/shared/ipc-channels.ts+++ b/src/shared/ipc-channels.ts@@ -1,6 +1,7 @@export const IpcChannels = {listInterfaces: 'network:list-interfaces',pingInterfaces: 'network:ping-interfaces',+ deviceBindingSupported: 'network:device-binding-supported',openNetworkSettings: 'network:open-settings',getNetworkPreferences: 'network:get-preferences',setNetworkPreference: 'network:set-preference',diff --git a/src/shared/ipc-contract.ts b/src/shared/ipc-contract.tsindex 8ab5680..ed80283 100644--- a/src/shared/ipc-contract.ts+++ b/src/shared/ipc-contract.ts@@ -18,6 +18,7 @@ import type {export interface IpcContract {listInterfaces: { args: []; result: NetworkInterfaceInfo[] }pingInterfaces: { args: []; result: Record<string, number | null> }+ deviceBindingSupported: { args: []; result: boolean }openNetworkSettings: { args: []; result: void }getNetworkPreferences: { args: []; result: NetworkPreferences }setNetworkPreference: {