Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
feat: NZB downloads from a Usenet news server (#126) (#133)
dxmoc 🐛40cb26de99eb
diff --git a/README.md b/README.mdindex 3e7e2a3..c8694d8 100644--- a/README.md+++ b/README.md@@ -62,7 +62,7 @@ https://github.com/user-attachments/assets/e57728f4-fb63-441f-839c-174eef954b17### Download management-- Download direct links, magnet links and `.torrent` files+- Download direct links, magnet links, `.torrent` files and Usenet NZBs- Select torrent files before or during a download- Pause and resume supported downloads across restarts- Automatically reconnect downloads after sleep@@ -158,6 +158,8 @@ For direct downloads, Plexo uses HTTP range requests to fetch chunks in parallelFor torrents, Plexo distributes peer connections across your selected networks. New peers connect through the network with the fewest peers.+For Usenet NZBs, Plexo shares your news server account's connections evenly across your selected networks. Each article is decoded and written straight to its place in its file; articles the server no longer has are left as gaps. Plexo doesn't repair or unpack downloads.+Each network needs its own internet connection to add bandwidth. Plexo combines connections for its own downloads and requires no VPN or virtual network adapter.## Build from sourcediff --git a/e2e/fixtures.ts b/e2e/fixtures.tsindex f64fc5f..fbea467 100644--- a/e2e/fixtures.ts+++ b/e2e/fixtures.ts@@ -21,6 +21,7 @@ import type {DownloadStatus,DownloadUpdate,HttpDownloadState,+ NzbDownloadState,TorrentDownloadState} from '../src/shared/types'import { Origin, sha256, type OriginOptions } from './origin'@@ -39,6 +40,12 @@ export function expectTorrent(state: DownloadState): TorrentDownloadState {return state}+export function expectNzb(state: DownloadState): NzbDownloadState {+ expect(state.kind).toBe('nzb')+ if (state.kind !== 'nzb') throw new Error('Expected an NZB download')+ return state+}+export const PROJECT_ROOT = resolve(__dirname, '..')/** Every raw Electron launch needs the same CI sandbox policy. */@@ -264,7 +271,9 @@ export class PlexoApp {infoHash: probe.torrent.infoHash,selectedFiles: options.selectedFiles}- : { ...common, kind: 'http', streamsPerNetwork: options.streamsPerNetwork }+ : probe.kind === 'nzb'+ ? { ...common, kind: 'nzb', nzbHash: probe.nzb.hash }+ : { ...common, kind: 'http', streamsPerNetwork: options.streamsPerNetwork })this.tracked.set(id, { expectedSha, destBefore, destinationDir })return id@@ -302,9 +311,9 @@ export class PlexoApp {const finished = (await this.api.listHistory()).map((entry): DownloadState => {const last = seen.findLast((state) => state.id === entry.id)if (last?.status === 'completed') return last- return entry.kind === 'http'- ? { ...entry, blocks: [], streams: [] }- : { ...entry, pieces: [], peers: [] }+ if (entry.kind === 'http') return { ...entry, blocks: [], streams: [] }+ if (entry.kind === 'nzb') return { ...entry, blocks: [], streams: [] }+ return { ...entry, pieces: [], peers: [] }})// One that finished between the two calls is in both: history has its final state.const done = new Set(finished.map((state) => state.id))@@ -345,6 +354,13 @@ export class PlexoApp {return expectTorrent(await this.waitForStatus(status, timeout))}+ async waitForNzbStatus(+ status: DownloadStatus | DownloadStatus[],+ timeout = 20_000+ ): Promise<NzbDownloadState> {+ return expectNzb(await this.waitForStatus(status, timeout))+ }+async currentHttp(): Promise<HttpDownloadState | null> {const state = await this.current()return state ? expectHttp(state) : null@@ -372,7 +388,7 @@ export class PlexoApp {(network) => `${network.id}:${network.status}${network.error ? ` (${network.error})` : ''}`)const connections = state- ? (state.kind === 'http' ? state.streams : state.peers).map(+ ? (state.kind === 'torrent' ? state.peers : state.streams).map((connection) => `${connection.interfaceId}:${connection.status}`): undefined@@ -413,7 +429,7 @@ export function checkEvents(sessions: DownloadState[][]): void {`${label}: bytesDownloaded ≤ totalBytes`)}- const units = state.kind === 'http' ? state.blocks : state.pieces+ const units = state.kind === 'torrent' ? state.pieces : state.blocksassert.equal(units.every((unit, index) => unit.index === index),true,@@ -421,7 +437,7 @@ export function checkEvents(sessions: DownloadState[][]): void {)assert.equal(units.length,- state.kind === 'http' ? state.totalBlocks : state.totalPieces,+ state.kind === 'torrent' ? state.totalPieces : state.totalBlocks,`${label}: as many work units as planned`)for (const unit of units) {diff --git a/e2e/network.spec.ts b/e2e/network.spec.tsindex e5ea58f..34fbbe2 100644--- a/e2e/network.spec.ts+++ b/e2e/network.spec.ts@@ -1,8 +1,13 @@import { expect, test } from '@playwright/test'import { createServer, type IncomingMessage } from 'node:http'-import { connect as netConnect, type AddressInfo } from 'node:net'+import {+ connect as netConnect,+ createServer as createTcpServer,+ type AddressInfo,+ type Socket+} from 'node:net'import type { NetworkInterfaceInfo, ProxyConfig } from '../src/shared/types'-import { StreamConnection } from '../src/main/network/routes'+import { connectThrough, StreamConnection } from '../src/main/network/routes'const iface: NetworkInterfaceInfo = {id: 'wifi',@@ -295,6 +300,64 @@ test('valid HTTP proxy tunnels requests through the proxy @smoke', async () => {}})+// A news server speaks first, so bytes that arrive with the proxy's answer must not be lost.+for (const type of ['http', 'socks5'] as const) {+ test(`a non-HTTP connection (NNTP) goes through the network’s ${type} proxy @smoke`, async () => {+ const tunneled: string[] = []+ const newsServer = createTcpServer((socket) => socket.end('200 news ready\r\n'))+ await new Promise<void>((resolve) => newsServer.listen(0, '127.0.0.1', resolve))+ const newsPort = (newsServer.address() as AddressInfo).port++ const tunnel = (client: Socket, target: string, established: Buffer): void => {+ tunneled.push(target)+ const [tHost, tPort] = target.split(':')+ const targetSocket = netConnect(Number(tPort), tHost, () => {+ client.write(established)+ targetSocket.pipe(client)+ client.pipe(targetSocket)+ })+ targetSocket.on('error', () => client.destroy())+ client.on('error', () => targetSocket.destroy())+ }+ const proxyServer =+ type === 'http'+ ? createServer().on('connect', (req, client: Socket) =>+ tunnel(client, req.url!, Buffer.from('HTTP/1.1 200 Connection Established\r\n\r\n'))+ )+ : // SOCKS5 without auth, IPv4 targets only: greeting, then CONNECT.+ createTcpServer((client) => {+ client.once('data', () => {+ client.write(Buffer.from([0x05, 0x00]))+ client.once('data', (request: Buffer) => {+ const target = `${[...request.subarray(4, 8)].join('.')}:${request.readUInt16BE(8)}`+ tunnel(client, target, Buffer.from([0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]))+ })+ })+ })+ await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve))+ const proxyPort = (proxyServer.address() as AddressInfo).port++ try {+ const socket = await connectThrough(+ loopback,+ '127.0.0.1',+ newsPort,+ false,+ 2000,+ new AbortController().signal,+ { enabled: true, type, host: '127.0.0.1', port: proxyPort }+ )+ let greeting = ''+ for await (const chunk of socket) greeting += chunk+ expect(greeting).toBe('200 news ready\r\n')+ expect(tunneled).toEqual([`127.0.0.1:${newsPort}`])+ } finally {+ await new Promise<void>((resolve) => proxyServer.close(() => resolve()))+ await new Promise<void>((resolve) => newsServer.close(() => resolve()))+ }+ })+}+test('proxy settings update dynamically on reconnect @smoke', async () => {const targetServer = createServer((_req, res) => {res.end('dynamic ok')diff --git a/e2e/newsServer.ts b/e2e/newsServer.tsnew file mode 100644index 0000000..1eb6ef8--- /dev/null+++ b/e2e/newsServer.ts@@ -0,0 +1,316 @@+import { createServer, type AddressInfo, type Server, type Socket } from 'node:net'+import { crc32 } from 'node:zlib'+import { treeSha } from './fixtures'++/**+ * A news server for the NZB tests: it posts files as yEnc articles, writes the NZB that lists+ * them, and serves them over NNTP (just what downloading takes: the greeting, AUTHINFO, BODY,+ * QUIT). Like the HTTP origin (origin.ts) it can be told to misbehave — an article it doesn't+ * have, one it sends damaged, a connection it drops partway or lets stall, more connections than+ * the account allows — so each test can say exactly what the app must cope with.+ */++export interface NewsFile {+ name: string+ data: Buffer+}++/** One article, as posted. */+interface Article {+ messageId: string+ file: number+ /** Its place in its file, from 1. */+ number: number+ begin: number+ data: Buffer+ /** As it goes over the wire: yEnc, lines ended by CRLF, not yet dot-stuffed. */+ body: Buffer+}++export interface NewsServerOptions {+ files: NewsFile[]+ /** Decoded bytes per article. */+ articleBytes?: number+ username?: string+ password?: string+ /** More connections at once than this are turned away with 502. */+ maxConnections?: number+ /** Articles the server doesn't have (430), by message ID. */+ missing?: (article: { file: number; number: number }) => boolean+ /** How the server answers this BODY, by its how-manyth request for the article (from 1). */+ fault?: (article: { file: number; number: number }, request: number) => ArticleFault+ /** Waits this long before answering each BODY: slow enough for a test to act partway. */+ delayMs?: number+}++/** - ok: the article as posted.+ * - corrupt: one byte of its data flipped, its checksum unchanged.+ * - cut: half of it, then the connection dropped.+ * - stall: half of it, then nothing. */+export type ArticleFault = 'ok' | 'corrupt' | 'cut' | 'stall'++export interface ServedArticle {+ messageId: string+ /** The client's address: which network it came in on. */+ from: string+ connection: number+ fault: ArticleFault | 'missing'+}++const LINE = 128++/** yEnc lines of `data`, each ended by CRLF: every byte plus 42, the critical ones escaped (no+ * RFC, but every poster does this). Written straight into one buffer: a test may post gigabytes. */+export function yencEncode(data: Buffer): Buffer {+ // At worst every byte escaped, plus a CRLF for every line.+ const out = Buffer.allocUnsafe(data.length * 2 + Math.ceil((data.length * 2) / LINE) * 2 + 2)+ let length = 0+ let column = 0+ for (let i = 0; i < data.length; i++) {+ const byte = (data[i] + 42) & 0xff+ const atEdge = column === 0 || column >= LINE - 1 || i === data.length - 1+ // NUL, LF, CR and '=' always; a leading or trailing space or tab, and a leading dot.+ if (+ byte === 0x00 ||+ byte === 0x0a ||+ byte === 0x0d ||+ byte === 0x3d ||+ (atEdge && (byte === 0x20 || byte === 0x09)) ||+ (column === 0 && byte === 0x2e)+ ) {+ out[length++] = 0x3d+ out[length++] = (byte + 64) & 0xff+ column += 2+ } else {+ out[length++] = byte+ column+++ }+ if (column >= LINE) {+ out[length++] = 0x0d+ out[length++] = 0x0a+ column = 0+ }+ }+ if (column > 0) {+ out[length++] = 0x0d+ out[length++] = 0x0a+ }+ return out.subarray(0, length)+}++function post(files: NewsFile[], articleBytes: number): Article[] {+ const articles: Article[] = []+ files.forEach((file, index) => {+ const total = Math.max(1, Math.ceil(file.data.length / articleBytes))+ for (let part = 1; part <= total; part++) {+ const begin = (part - 1) * articleBytes+ const data = file.data.subarray(begin, Math.min(begin + articleBytes, file.data.length))+ const header = [+ `=ybegin part=${part} total=${total} line=${LINE} size=${file.data.length} name=${file.name}`,+ `=ypart begin=${begin + 1} end=${begin + data.length}`+ ]+ const trailer = `=yend size=${data.length} part=${part} pcrc32=${(crc32(data) >>> 0).toString(16).padStart(8, '0')}`+ const body = Buffer.concat([+ Buffer.from(`${header.join('\r\n')}\r\n`),+ yencEncode(data),+ Buffer.from(`${trailer}\r\n`)+ ])+ articles.push({+ messageId: `part${part}of${total}.${index}.${file.name.replace(/[^\w.-]/g, '_')}@plexo.test`,+ file: index,+ number: part,+ begin,+ data,+ body+ })+ }+ })+ return articles+}++/** A line that starts with a dot gets another (RFC 3977 §3.1.1), and the body ends with one. */+function dotStuffed(body: Buffer): Buffer {+ const parts: Buffer[] = []+ let from = 0+ if (body[0] === 0x2e) parts.push(Buffer.from('.'))+ for (let at = body.indexOf('\n.'); at >= 0; at = body.indexOf('\n.', at + 1)) {+ parts.push(body.subarray(from, at + 1), Buffer.from('.'))+ from = at + 1+ }+ parts.push(body.subarray(from), Buffer.from('.\r\n'))+ return Buffer.concat(parts)+}++const escapeXml = (text: string): string =>+ text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')++export class NewsServer {+ private readonly server: Server+ private readonly articles: Article[]+ private readonly byId = new Map<string, Article>()+ private readonly requests = new Map<string, number>()+ private readonly sockets = new Set<Socket>()+ private connections = 0+ readonly served: ServedArticle[] = []+ /** Connections turned away for going over maxConnections. */+ refused = 0+ port = 0++ constructor(private readonly options: NewsServerOptions) {+ this.articles = post(options.files, options.articleBytes ?? 16 * 1024)+ for (const article of this.articles) this.byId.set(article.messageId, article)+ this.server = createServer((socket) => this.accept(socket))+ }++ async start(): Promise<this> {+ await new Promise<void>((resolve) => this.server.listen(0, '0.0.0.0', resolve))+ this.port = (this.server.address() as AddressInfo).port+ return this+ }++ async stop(): Promise<void> {+ for (const socket of this.sockets) socket.destroy()+ await new Promise<void>((resolve) => this.server.close(() => resolve()))+ }++ get articleCount(): number {+ return this.articles.length+ }++ /** The NZB listing every article, as an indexer would serve it. */+ nzb(): Buffer {+ const files = this.options.files.map((file, index) => {+ const articles = this.articles.filter((article) => article.file === index)+ const segments = articles+ .map(+ (article) =>+ ` <segment bytes="${article.body.length}" number="${article.number}">${escapeXml(article.messageId)}</segment>`+ )+ .join('\n')+ return ` <file poster="plexo@test" date="1700000000" subject="${escapeXml(`[${index + 1}/${this.options.files.length}] - "${file.name}" yEnc (1/${articles.length})`)}">+ <groups><group>alt.binaries.test</group></groups>+ <segments>+${segments}+ </segments>+ </file>`+ })+ return Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.1//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd">+<nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">+ <head><meta type="name">Test release</meta></head>+${files.join('\n')}+</nzb>+`)+ }++ /** What a completed download's folder must hash to: the files, with each article the server+ * doesn't have (or `gaps` names) left as zeros. */+ expectedSha(+ gaps: (article: { file: number; number: number }) => boolean = (article) =>+ this.options.missing?.(article) ?? false+ ): string {+ const files = this.options.files.map((file, index) => {+ const data = Buffer.from(file.data)+ for (const article of this.articles) {+ if (article.file === index && gaps(article)) {+ data.fill(0, article.begin, article.begin + article.data.length)+ }+ }+ return { path: file.name, data }+ })+ return treeSha(files)+ }++ /** How many articles the server doesn't have. */+ get missingCount(): number {+ return this.articles.filter((article) => this.options.missing?.(article)).length+ }++ private accept(socket: Socket): void {+ this.sockets.add(socket)+ const connection = ++this.connections+ socket.once('close', () => this.sockets.delete(socket))+ socket.on('error', () => {})+ const { maxConnections } = this.options+ if (maxConnections !== undefined && this.sockets.size > maxConnections) {+ this.refused+++ this.sockets.delete(socket)+ socket.end('502 Too many connections for this account\r\n')+ return+ }+ let signedIn = this.options.username === undefined+ let user = ''+ let buffered = ''+ socket.write('200 Plexo test news server ready\r\n')+ socket.on('data', (chunk) => {+ buffered += chunk.toString('latin1')+ let end: number+ while ((end = buffered.indexOf('\r\n')) >= 0) {+ const line = buffered.slice(0, end)+ buffered = buffered.slice(end + 2)+ const [command, ...rest] = line.split(' ')+ const argument = rest.join(' ')+ switch (command.toUpperCase()) {+ case 'AUTHINFO': {+ const [kind, ...value] = rest+ if (kind.toUpperCase() === 'USER') {+ user = value.join(' ')+ socket.write('381 Password required\r\n')+ } else if (+ user === this.options.username &&+ value.join(' ') === this.options.password+ ) {+ signedIn = true+ socket.write('281 Authentication accepted\r\n')+ } else {+ socket.write('481 Authentication failed\r\n')+ }+ break+ }+ case 'BODY': {+ const messageId = argument.replace(/^<|>$/g, '')+ if (!signedIn) socket.write('480 Authentication required\r\n')+ else if (this.options.delayMs) {+ setTimeout(() => this.body(socket, connection, messageId), this.options.delayMs)+ } else this.body(socket, connection, messageId)+ break+ }+ case 'QUIT':+ socket.end('205 Bye\r\n')+ return+ default:+ socket.write('500 Unknown command\r\n')+ }+ }+ })+ }++ private body(socket: Socket, connection: number, messageId: string): void {+ if (socket.destroyed) return+ const article = this.byId.get(messageId)+ const from = (socket.remoteAddress ?? '').replace(/^::ffff:/, '')+ if (!article || this.options.missing?.(article)) {+ this.served.push({ messageId, from, connection, fault: 'missing' })+ socket.write('430 No such article\r\n')+ return+ }+ const request = (this.requests.get(messageId) ?? 0) + 1+ this.requests.set(messageId, request)+ const fault = this.options.fault?.(article, request) ?? 'ok'+ this.served.push({ messageId, from, connection, fault })+ let body = article.body+ if (fault === 'corrupt') {+ body = Buffer.from(body)+ // A data byte in the middle of the article: past the two header lines.+ const at = body.indexOf('\r\n', body.indexOf('\r\n') + 2) + 2 + 10+ body[at] = body[at] === 0x41 ? 0x42 : 0x41+ }+ const wire = Buffer.concat([Buffer.from(`222 0 <${messageId}>\r\n`), dotStuffed(body)])+ if (fault === 'cut' || fault === 'stall') {+ socket.write(wire.subarray(0, wire.length >> 1))+ if (fault === 'cut') socket.destroy()+ return+ }+ socket.write(wire)+ }+}diff --git a/e2e/nzb.spec.ts b/e2e/nzb.spec.tsnew file mode 100644index 0000000..2ee7883--- /dev/null+++ b/e2e/nzb.spec.ts@@ -0,0 +1,391 @@+import { createServer, type Server } from 'node:http'+import { connect as netConnect, createServer as createTcpServer, type AddressInfo } from 'node:net'+import { mkdtemp, rm, writeFile } from 'node:fs/promises'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import type { NzbDownloadState, UsenetServerInput } from '../src/shared/types'+import { expect, LAN_ADDRESS, test, type PlexoApp } from './fixtures'+import { NewsServer, type NewsServerOptions } from './newsServer'+import { seededBytes } from './origin'++// Downloading from Usenet: an NZB's articles fetched from a local news server (newsServer.ts),+// decoded and written into their files byte for byte, over every network in use, through the+// faults a real server has. The checks fixture verifies each finished download's folder.++const KB = 1024++const RELEASE = [+ { name: 'Test.Release.part1.rar', data: seededBytes(200 * KB, 41) },+ { name: 'Test.Release.part2.rar', data: seededBytes(90 * KB + 7, 42) },+ { name: 'Test.Release.par2', data: seededBytes(3 * KB + 1, 43) }+]++let servers: NewsServer[] = []+let folder: string+test.beforeEach(async () => {+ folder = await mkdtemp(join(tmpdir(), 'plexo-nzb-'))+})+test.afterEach(async () => {+ await Promise.all(servers.map((server) => server.stop()))+ servers = []+ await rm(folder, { recursive: true, force: true })+})++async function newsServer(options: Partial<NewsServerOptions> = {}): Promise<NewsServer> {+ const server = await new NewsServer({ files: RELEASE, ...options }).start()+ servers.push(server)+ return server+}++/** The server's NZB, saved where a browser would have put it. */+async function nzbOnDisk(server: NewsServer, name = 'Test.Release.nzb'): Promise<string> {+ const path = join(folder, name)+ await writeFile(path, server.nzb())+ return path+}++async function useServer(+ plexo: PlexoApp,+ server: NewsServer,+ settings: Partial<UsenetServerInput> = {}+): Promise<void> {+ await plexo.api.saveUsenetServer({+ host: '127.0.0.1',+ port: server.port,+ tls: false,+ username: '',+ password: '',+ connections: 4,+ ...settings+ })+}++test.describe('NZB downloads', () => {+ test('an NZB’s files, decoded byte for byte into their folder @smoke', async ({ plexo }) => {+ const server = await newsServer()+ await useServer(plexo, server)++ const id = await plexo.start(await nzbOnDisk(server), server.expectedSha())+ const done = await plexo.waitForNzbStatus('completed', 30_000)+ expect(done.id).toBe(id)+ expect(done.fileName).toBe('Test.Release')+ expect(done.server).toBe('127.0.0.1')+ expect(done.totalBlocks).toBe(server.articleCount)+ expect(done.missingBytes).toBe(0)+ // Every file cut to its real size, which only the articles knew.+ expect(done.nzbFiles.map((file) => [file.name, file.size])).toEqual(+ RELEASE.map((file) => [file.name, file.data.length])+ )+ expect(new Set(server.served.map((article) => article.messageId)).size).toBe(+ server.articleCount+ )+ })++ test('an indexer’s NZB link is downloaded and named from its headers @smoke', async ({+ plexo+ }) => {+ const server = await newsServer()+ await useServer(plexo, server)+ const indexer: Server = createServer((_request, response) => {+ response.writeHead(200, {+ 'Content-Type': 'application/x-nzb',+ 'Content-Disposition': 'attachment; filename="Indexer.Release.nzb"'+ })+ response.end(server.nzb())+ })+ await new Promise<void>((resolve) => indexer.listen(0, '127.0.0.1', resolve))+ try {+ const { port } = indexer.address() as AddressInfo+ const link = `http://127.0.0.1:${port}/api?t=get&id=1&apikey=secret`+ const probe = await plexo.api.probeUrl(link)+ expect(probe.kind).toBe('nzb')+ expect(probe.suggestedFileName).toBe('Indexer.Release')++ await plexo.start(link, server.expectedSha())+ const done = await plexo.waitForNzbStatus('completed', 30_000)+ expect(done.fileName).toBe('Indexer.Release')+ } finally {+ indexer.close()+ }+ })++ test('an indexer’s answer in plain XML: an NZB is taken, its error is shown', async ({+ plexo+ }) => {+ const server = await newsServer()+ await useServer(plexo, server)+ const indexer: Server = createServer((request, response) => {+ response.writeHead(200, { 'Content-Type': 'application/xml; charset=utf-8' })+ response.end(+ request.url?.includes('id=limit')+ ? '<?xml version="1.0" encoding="UTF-8"?>\n<error code="500" description="Request limit reached"/>'+ : server.nzb()+ )+ })+ await new Promise<void>((resolve) => indexer.listen(0, '127.0.0.1', resolve))+ try {+ const { port } = indexer.address() as AddressInfo+ await expect(+ plexo.api.probeUrl(`http://127.0.0.1:${port}/api?t=get&id=limit&apikey=secret`)+ ).rejects.toThrow(/The indexer refused this link: Request limit reached \(error 500\)/)+ const probe = await plexo.api.probeUrl(+ `http://127.0.0.1:${port}/api?t=get&id=1&apikey=secret`+ )+ expect(probe.kind).toBe('nzb')+ } finally {+ indexer.close()+ }+ })++ test('articles the server doesn’t have are left as gaps, and counted', async ({ plexo }) => {+ const server = await newsServer({+ missing: (article) => article.file === 0 && (article.number === 3 || article.number === 7)+ })+ await useServer(plexo, server)++ await plexo.start(await nzbOnDisk(server), server.expectedSha())+ const done = await plexo.waitForNzbStatus('completed', 30_000)+ expect(done.nzbFiles[0].missing).toBe(2)+ expect(done.missingBytes).toBeGreaterThan(0)+ expect(done.blocks.filter((block) => block.status === 'missing')).toHaveLength(2)+ // Asked for once: a missing article doesn't turn up by asking again.+ const asked = server.served.filter((article) => article.fault === 'missing')+ expect(asked).toHaveLength(2)+ })++ test('a server with none of the articles fails the download, leaving nothing', async ({+ plexo+ }) => {+ const server = await newsServer({ missing: () => true })+ await useServer(plexo, server)++ await plexo.start(await nzbOnDisk(server), server.expectedSha())+ const failed = await plexo.waitForNzbStatus('error', 30_000)+ expect(failed.error).toMatch(/none of this NZB’s articles/)+ expect(failed.resumable).toBe(false)+ })++ test('a damaged article is fetched again; one damaged every time counts as missing', async ({+ plexo+ }) => {+ const server = await newsServer({+ fault: (article, request) =>+ article.file === 1 && article.number === 2 && request === 1+ ? 'corrupt'+ : article.file === 0 && article.number === 5+ ? 'corrupt'+ : 'ok'+ })+ await useServer(plexo, server)++ // The article damaged every time is a gap in its file.+ const expected = server.expectedSha((article) => article.file === 0 && article.number === 5)+ await plexo.start(await nzbOnDisk(server), expected)+ const done = await plexo.waitForNzbStatus('completed', 30_000)+ expect(done.nzbFiles[1].missing).toBe(0)+ expect(done.nzbFiles[0].missing).toBe(1)+ const fetches = (file: number, number: number): number =>+ server.served.filter(+ (article) =>+ article.messageId.startsWith(`part${number}of`) && article.messageId.includes(`.${file}.`)+ ).length+ expect(fetches(1, 2)).toBe(2)+ expect(fetches(0, 5)).toBe(2)+ })++ test('connections dropped or stalled mid-article are opened again', async ({ plexo }) => {+ const server = await newsServer({+ fault: (article, request) =>+ request > 1 ? 'ok' : article.number === 2 ? 'cut' : article.number === 4 ? 'stall' : 'ok'+ })+ await useServer(plexo, server)++ await plexo.start(await nzbOnDisk(server), server.expectedSha())+ const done = await plexo.waitForNzbStatus('completed', 30_000)+ expect(done.missingBytes).toBe(0)+ expect(done.networks[0].retries).toBeGreaterThan(0)+ })++ test('signs in, and says so when the password is wrong', async ({ plexo }) => {+ const server = await newsServer({ username: 'reader', password: 'right' })+ await useServer(plexo, server, { username: 'reader', password: 'wrong' })+ await expect(+ plexo.api.testUsenetServer({+ host: '127.0.0.1',+ port: server.port,+ tls: false,+ username: 'reader',+ connections: 4+ })+ ).rejects.toThrow(/username or password/)++ await plexo.start(await nzbOnDisk(server), server.expectedSha())+ const failed = await plexo.waitForNzbStatus('error', 30_000)+ expect(failed.error).toMatch(/username or password/)+ expect(failed.resumable).toBe(true)++ // Fixed in the settings, the same download resumes and finishes.+ await useServer(plexo, server, { username: 'reader', password: 'right' })+ await plexo.api.resumeDownload(failed.id)+ await plexo.waitForNzbStatus('completed', 30_000)+ })++ test('a wrong password is put right from the failed download, then it starts again', async ({+ plexo,+ dirs+ }) => {+ const server = await newsServer({ username: 'reader', password: 'right' })+ await useServer(plexo, server, { username: 'reader', password: 'wrong' })+ const nzb = await nzbOnDisk(server)+ await plexo.start(nzb, server.expectedSha())+ const failed = await plexo.waitForNzbStatus('error', 30_000)+ plexo.tracked.delete(failed.id)++ const { page } = plexo+ await page.getByText('Test.Release', { exact: true }).first().click()+ await page.getByRole('button', { name: /^Usenet server 127\.0\.0\.1/ }).click()+ const dialog = page.getByRole('dialog', { name: 'Usenet server' })+ await expect(dialog.getByLabel('Password')).toHaveAttribute('placeholder', 'Saved')+ await dialog.getByLabel('Password').fill('right')+ await dialog.getByRole('button', { name: 'Save' }).click()+ await expect(dialog).toBeHidden()++ // Nothing of it came in, so it starts over from its NZB, as any such download does.+ await page.getByRole('button', { name: 'Download again' }).click()+ await expect(page.getByRole('textbox', { name: 'Link' })).toHaveValue(nzb)+ // Saved where the test looks, as the folder picker would choose it.+ await plexo.evaluateMain(({ dialog }, dest) => {+ dialog.showOpenDialog = (async () => ({ canceled: false, filePaths: [dest] })) as never+ }, dirs.dest)+ await page+ .getByText('Save to', { exact: true })+ .locator('..')+ .getByRole('button', { name: 'Change' })+ .click()+ // The failed one is gone by now: the new one is what the folder gains.+ await plexo.expectNextDownload(server.expectedSha())+ await expect(page.getByText('127.0.0.1', { exact: false }).first()).toBeVisible()+ await page.getByRole('button', { name: 'Download', exact: true }).click()+ await plexo.waitForNzbStatus('completed', 30_000)+ })++ test('a server that turns every connection away is unreachable, not a strike', async ({+ plexo+ }) => {+ const server = await newsServer()+ const { port } = server+ await server.stop()+ servers = []+ await useServer(plexo, server, { port })++ await plexo.start(await nzbOnDisk(server), server.expectedSha())+ // Shown as it is for an HTTP server out of reach: one connection keeps trying, backing off.+ await plexo.waitUntil(+ (state) =>+ state.kind === 'nzb' &&+ state.networks.find((network) => network.id === 'a')?.status === 'unreachable',+ 15_000+ )+ })++ test('never more connections than the account allows', async ({ plexo }) => {+ const server = await newsServer({ maxConnections: 2, delayMs: 5 })+ // Set to more than the account has: the server turns the rest away.+ await useServer(plexo, server, { connections: 6 })++ await plexo.start(await nzbOnDisk(server), server.expectedSha())+ const done = await plexo.waitForNzbStatus('completed', 30_000)+ expect(server.refused).toBeGreaterThan(0)+ expect(done.missingBytes).toBe(0)+ })++ test('without a news server set up, it says so before starting', async ({ plexo }) => {+ const server = await newsServer()+ const probe = await plexo.api.probeUrl(await nzbOnDisk(server))+ expect(probe.kind).toBe('nzb')+ expect(await plexo.api.usenetServer()).toBeNull()+ await expect(plexo.start(await nzbOnDisk(server), server.expectedSha())).rejects.toThrow(+ /Set up a news server/+ )+ })++ test('pause, quit, relaunch and resume: articles already in aren’t fetched again', async ({+ plexo+ }) => {+ const server = await newsServer({ delayMs: 40 })+ await useServer(plexo, server, { connections: 2 })++ const id = await plexo.start(await nzbOnDisk(server), server.expectedSha())+ await plexo.waitUntil((state) => state.kind === 'nzb' && countDone(state) >= 4)+ await plexo.api.pauseDownload(id)+ const paused = await plexo.waitForNzbStatus('paused')+ const doneBefore = paused.blocks.filter((block) => block.status === 'completed').length++ await plexo.relaunch()+ // No keychain on a test runner: an account without a password is what survives a relaunch.+ expect((await plexo.api.usenetServer())?.host).toBe('127.0.0.1')+ const servedBefore = server.served.length+ await plexo.api.resumeDownload(id)+ await plexo.waitForNzbStatus('completed', 30_000)+ expect(server.served.length - servedBefore).toBeLessThanOrEqual(+ server.articleCount - doneBefore + 2+ )+ })++ test('goes through the proxy set on the network', async ({ plexo }) => {+ const server = await newsServer()+ await useServer(plexo, server, { connections: 2 })+ // SOCKS5 without auth, IPv4 targets only: greeting, then CONNECT.+ const targets: string[] = []+ const proxy = createTcpServer((client) => {+ client.once('data', () => {+ client.write(Buffer.from([0x05, 0x00]))+ client.once('data', (request: Buffer) => {+ const host = [...request.subarray(4, 8)].join('.')+ const port = request.readUInt16BE(8)+ targets.push(`${host}:${port}`)+ const upstream = netConnect(port, host, () => {+ client.write(Buffer.from([0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]))+ upstream.pipe(client)+ client.pipe(upstream)+ })+ upstream.on('error', () => client.destroy())+ client.on('error', () => upstream.destroy())+ })+ })+ })+ await new Promise<void>((resolve) => proxy.listen(0, '127.0.0.1', resolve))+ try {+ const port = (proxy.address() as AddressInfo).port+ await plexo.api.updateSettings({+ networkPreferences: {+ a: { proxy: { enabled: true, type: 'socks5', host: '127.0.0.1', port } }+ }+ })+ await plexo.start(await nzbOnDisk(server), server.expectedSha())+ await plexo.waitForNzbStatus('completed', 30_000)+ expect(targets.length).toBeGreaterThan(0)+ expect(new Set(targets)).toEqual(new Set([`127.0.0.1:${server.port}`]))+ } finally {+ await new Promise<void>((resolve) => proxy.close(() => resolve()))+ }+ })++ test('shares the connections across both networks', async ({ plexo }) => {+ test.skip(!LAN_ADDRESS, 'needs a second local address')+ const server = await newsServer({ delayMs: 5 })+ await useServer(plexo, server, { connections: 4 })++ await plexo.start(await nzbOnDisk(server), server.expectedSha(), { networks: ['a', 'b'] })+ const done = await plexo.waitForNzbStatus('completed', 30_000)+ const from = new Set(server.served.map((article) => article.from))+ expect(from).toEqual(new Set(['127.0.0.1', LAN_ADDRESS]))+ for (const network of done.networks.filter((network) => network.enabled)) {+ expect(network.bytesDownloaded).toBeGreaterThan(0)+ }+ })+})++function countDone(state: NzbDownloadState): number {+ return state.blocks.filter((block) => block.status === 'completed').length+}diff --git a/e2e/taskbarProgress.spec.ts b/e2e/taskbarProgress.spec.tsindex 9f81930..d44e9f8 100644--- a/e2e/taskbarProgress.spec.ts+++ b/e2e/taskbarProgress.spec.ts@@ -53,6 +53,18 @@ test.describe('taskbar progress @smoke', () => {expect(taskbarProgress(mixed)).toEqual({ progress: 0.75, mode: 'normal' })})+ test('NZB with missing articles reaches 100% using wanted bytes', () => {+ const nzb: TaskbarDownload = {+ status: 'downloading',+ kind: 'nzb',+ totalBytes: 1000,+ missingBytes: 100,+ bytesDownloaded: 900+ }+ expect(wantedBytes(nzb)).toBe(900)+ expect(taskbarProgress([nzb])).toEqual({ progress: 1, mode: 'normal' })+ })+test('torrent with skipped files reaches 100% using wanted bytes', () => {const torrent: TaskbarDownload = {status: 'downloading',diff --git a/electron-builder.yml b/electron-builder.ymlindex bce5ebd..af5840b 100644--- a/electron-builder.yml+++ b/electron-builder.yml@@ -53,8 +53,8 @@ mac:entitlementsInherit: build/entitlements.mac.plistextendInfo:- NSDownloadsFolderUsageDescription: Plexo saves downloaded files to your Downloads folder.- # Magnet links and .torrent files can be opened with Plexo, as one choice among others: it's- # never made the default over a torrent client the user already has (LSHandlerRank).+ # Magnet links, .torrent and .nzb files can be opened with Plexo, as one choice among others:+ # it's never made the default over a client the user already has (LSHandlerRank).# plexo:// is Plexo's own scheme, so registering it takes nothing from another app.- CFBundleURLTypes:- CFBundleURLName: Magnet link@@ -67,6 +67,10 @@ mac:LSHandlerRank: AlternateCFBundleTypeExtensions: [torrent]LSItemContentTypes: [org.bittorrent.torrent]+ - CFBundleTypeName: NZB+ CFBundleTypeRole: Viewer+ LSHandlerRank: Alternate+ CFBundleTypeExtensions: [nzb]# Signed when CSC_LINK/CSC_KEY_PASSWORD are set, and then notarized when APPLE_API_KEY,# APPLE_API_KEY_ID and APPLE_API_ISSUER are (see .github/workflows/release.yml). An unsigned# build still works, but can't update itself: macOS only installs signed updates.@@ -90,8 +94,9 @@ linux:artifactName: ${name}-${version}-${arch}.${ext}maintainer: electronjs.orgcategory: Utility- # Offered for magnet links and .torrent files ("Open with"), not made their default.- mimeTypes: [x-scheme-handler/magnet, application/x-bittorrent, x-scheme-handler/plexo]+ # Offered for magnet links, .torrent and .nzb files ("Open with"), not made their default.+ mimeTypes:+ [x-scheme-handler/magnet, application/x-bittorrent, application/x-nzb, x-scheme-handler/plexo]appImage:artifactName: ${name}-${version}-${arch}.${ext}deb:diff --git a/src/main/download/blockProgress.ts b/src/main/download/blockProgress.tsindex 819fa3a..ad67752 100644--- a/src/main/download/blockProgress.ts+++ b/src/main/download/blockProgress.ts@@ -1,4 +1,7 @@-import type { HttpBlockState } from '../../shared/types'+import type { HttpBlockState, NzbSegmentState } from '../../shared/types'++/** A block whose bytes are secured from its first one on: an HTTP block, or an NZB's article. */+type Block = HttpBlockState | NzbSegmentState// A block's `bytesDownloaded` is its frontier: how much of it, from its first byte, is secured.// Near the end of a download two attempts can race for one block, so the frontier is the furthest@@ -6,14 +9,14 @@ import type { HttpBlockState } from '../../shared/types'// and always adds up to it — these three functions are the only ways either changes./** Records that `networkId` delivered `bytes` more of this block. */-function credit(block: HttpBlockState, networkId: string, bytes: number): void {+function credit(block: Block, networkId: string, bytes: number): void {block.bytesByInterface[networkId] = (block.bytesByInterface[networkId] ?? 0) + bytes}/** Drops attribution for bytes that turned out not to be secured, so the per-network tallies* keep summing to the block's real byte count. The lost bytes are always at the tail, so they* come off `blamed` first (the network that wrote last) before spilling over to the rest. */-function trim(block: HttpBlockState, keepBytes: number, blamed?: string): void {+function trim(block: Block, keepBytes: number, blamed?: string): void {let attributed = 0for (const bytes of Object.values(block.bytesByInterface)) attributed += bytes@@ -36,7 +39,7 @@ function trim(block: HttpBlockState, keepBytes: number, blamed?: string): void {/** Moves the frontier forward to `position` (a byte offset into the block), crediting the bytes* it gains to `networkId`. Does nothing if something already got further. Returns the gain. */-export function advanceBlock(block: HttpBlockState, networkId: string, position: number): number {+export function advanceBlock(block: Block, networkId: string, position: number): number {const length = block.rangeEnd === null ? Infinity : block.rangeEnd - block.rangeStart + 1const next = Math.min(position, length)const gained = next - block.bytesDownloaded@@ -49,7 +52,7 @@ export function advanceBlock(block: HttpBlockState, networkId: string, position:/** Pulls the frontier back to `position`, for bytes that turned out not to be there or a racing* attempt that lost. `blamed` is the network they came from.* Returns how much it removed. */-export function retractBlock(block: HttpBlockState, position: number, blamed?: string): number {+export function retractBlock(block: Block, position: number, blamed?: string): number {const removed = block.bytesDownloaded - positionif (removed <= 0) return 0trim(block, position, blamed)diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex eb63556..3362f48 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -27,8 +27,11 @@ import {type NetworkPreferences,type NetworkStatus,type HttpProbeResult,+ type NzbDownloadState,+ type NzbSegmentState,type StartDownloadRequest,type StartHttpDownloadRequest,+ type StartNzbDownloadRequest,type StartTorrentDownloadRequest,type TorrentDownloadNetwork,type TorrentDownloadState,@@ -45,23 +48,41 @@ import { compareSamples, HttpTransfer, pickedStreams, splittable } from './httpTimport { normalizeEtag } from './fileVersion'import { ensureDirectory, pathExists, reserveDestinationPath } from './paths'import { planBlocks, planDownload, planPieces } from './plan'-import { restoreBlocks, restorePieces, saveBlocks, type SavedBlocks } from './savedProgress'+import {+ restoreBlocks,+ restorePieces,+ restoreSegments,+ saveBlocks,+ type SavedBlocks+} from './savedProgress'import { chosenFiles, finishedFiles, wantedPieces } from './torrent/files'import { probeUrl } from './probe'import { browserContextFrom } from './browserContext'import { describeTorrent, probedTorrentFile } from './torrent/metadata'import { TorrentDestination } from './torrent/torrentDestination'import { TorrentTransfer } from './torrent/torrentTransfer'+import { NzbDestination } from './nzb/nzbDestination'+import {+ parseNzb,+ planSegments,+ probedNzbFile,+ segmentArticles,+ type ParsedNzb+} from './nzb/nzbFile'+import { NzbTransfer } from './nzb/nzbTransfer'+import { loadUsenetServer } from './nzb/usenetServer'import {clearSpeeds,delay,Meters,recomputeAggregates,+ unwantedBytes,updateSpeeds,updateTimeLeft,type Transfer,type TransferHost,type HttpTransferTarget,+ type NzbTransferTarget,type TorrentTransferTarget} from './transfer'import type { NetworkMonitor } from '../network/interfaces'@@ -117,7 +138,12 @@ interface TorrentDownloadRuntime extends RuntimeFields, TorrentTransferTarget {transfer: TorrentTransfer}-type DownloadRuntime = HttpDownloadRuntime | TorrentDownloadRuntime+interface NzbDownloadRuntime extends RuntimeFields, NzbTransferTarget {+ kind: 'nzb'+ transfer: NzbTransfer+}++type DownloadRuntime = HttpDownloadRuntime | TorrentDownloadRuntime | NzbDownloadRuntimeinterface PersistedDownloadBase {savedAt: number@@ -130,11 +156,15 @@ interface PersistedDownloadBase {}type PersistedState =- Omit<HttpDownloadState, 'blocks' | 'streams'> | Omit<TorrentDownloadState, 'pieces' | 'peers'>+ | Omit<HttpDownloadState, 'blocks' | 'streams'>+ | Omit<TorrentDownloadState, 'pieces' | 'peers'>+ | Omit<NzbDownloadState, 'blocks' | 'streams'>type PersistedDownload = PersistedDownloadBase & {version: 7state: PersistedState+ /** An NZB download's articles the server didn't have, by index (see NzbSegmentStatus). */+ missing?: number[]} & SavedBlocks/** Cookies are never written in the clear. Without a keychain (Linux falls back to a fixed key)@@ -250,12 +280,24 @@ function newTorrentNetwork(iface: NetworkInterfaceInfo, enabled: boolean): Torre}}-/** A block that needs nothing more: in, or skipped (see BlockStatus). */+/** A block that needs nothing more: in, skipped, or missing from the news server (see+ * TorrentPieceStatus, NzbSegmentStatus). */const isDone = (unit: DownloadUnitState): boolean =>- unit.status === 'completed' || unit.status === 'skipped'+ unit.status === 'completed' || unit.status === 'skipped' || unit.status === 'missing'const unitsOf = (runtime: DownloadRuntime): DownloadUnitState[] =>- runtime.kind === 'http' ? runtime.blocks : runtime.pieces+ runtime.kind === 'torrent' ? runtime.pieces : runtime.blocks++/** An NZB download's missing articles, counted into its state: per file, and their bytes. */+function countMissing(state: NzbDownloadState, articles: { file: number }[]): void {+ state.missingBytes = 0+ for (const file of state.nzbFiles) file.missing = 0+ for (const block of state.blocks) {+ if (block.status !== 'missing') continue+ state.missingBytes += block.rangeEnd! - block.rangeStart + 1+ state.nzbFiles[articles[block.index].file].missing += 1+ }+}/** A torrent download's files as its state shows them, given those `chosen` (null: every one). */function choiceOf(chosen: Set<number> | null, total: number): TorrentDownloadState['files'] {@@ -579,6 +621,11 @@ export class DownloadManager {return join(this.downloadDir(id), 'metadata.torrent')}+ /** Where an NZB download keeps its NZB, beside its manifest. */+ private nzbFilePath(id: string): string {+ return join(this.downloadDir(id), 'metadata.nzb')+ }+private runtimeFields(): RuntimeFields {return {publishing: false,@@ -656,6 +703,39 @@ export class DownloadManager {return runtime}+ private newNzbRuntime(+ state: NzbDownloadState,+ requestPayload: StartNzbDownloadRequest,+ file: NzbDestination,+ blocks: NzbSegmentState[],+ nzb: ParsedNzb+ ): NzbDownloadRuntime {+ const target: NzbTransferTarget = {+ state,+ requestPayload,+ stop: new AbortController(),+ file,+ blocks,+ meters: new Meters()+ }+ file.fileSizes = () => state.nzbFiles.map((entry) => entry.size)+ const host: TransferHost = {+ networks: this.networks,+ limits: this.limits,+ downloadsOn: (networkId) => this.httpDownloadsOn(networkId),+ reconcile: () => this.reconcile(runtime),+ failDownload: (message, discard) => this.failDownload(runtime, message, discard),+ failNetwork: (network, message) => this.failNetwork(runtime, network, message),+ scheduleUpdate: () => this.scheduleUpdate(runtime),+ getProxyForInterface: (networkId) => this.networkPreferences[networkId]?.proxy+ }+ const runtime: NzbDownloadRuntime = Object.assign(target, this.runtimeFields(), {+ kind: 'nzb' as const,+ transfer: new NzbTransfer(target, host, segmentArticles(nzb), loadUsenetServer)+ })+ return runtime+ }+private async restorePersistedDownloads(): Promise<void> {let entries: string[]try {@@ -735,6 +815,14 @@ export class DownloadManager {void omittedBlocksvoid omittedStreamsentry = { ...state, unitsWritten: state.totalBlocks }+ } else if (runtime.kind === 'nzb') {+ const { blocks, streams: omittedStreams, ...state } = runtime.state+ void omittedStreams+ entry = {+ ...state,+ unitsWritten: blocks.filter((block) => block.status === 'completed').length,+ downloadedFiles: state.nzbFiles.map((file) => file.name)+ }} else {const { pieces, peers: omittedPeers, ...state } = runtime.statevoid omittedPeers@@ -830,6 +918,34 @@ export class DownloadManager {pieces,torrentFile)+ } else if (persisted.state.kind === 'nzb' && requestPayload.kind === 'nzb') {+ const nzbFile = await readFile(this.nzbFilePath(id)).catch((error: unknown) => {+ if (isNotFound(error)) return null+ throw error+ })+ if (!nzbFile) return null+ let nzb: ParsedNzb+ try {+ nzb = parseNzb(nzbFile)+ } catch {+ return null+ }+ const old = persisted.state+ const blocks = restoreSegments(planSegments(nzb), old, saved, persisted.missing)+ if (!blocks) return null+ state = { ...old, streams: [], blocks, totalBlocks: blocks.length }+ countMissing(state, segmentArticles(nzb))+ runtime = this.newNzbRuntime(+ state,+ requestPayload,+ new NzbDestination(+ persisted.partialPath,+ nzb.files.map((file) => file.name),+ state.totalBytes+ ),+ blocks,+ nzb+ )} else if (persisted.state.kind === 'http' && requestPayload.kind === 'http') {const old = persisted.stateconst blocks = restoreBlocks(old, saved)@@ -931,11 +1047,15 @@ export class DownloadManager {await this.initializationreturn [...this.runtimes.values()].sort((a, b) => a.state.startedAt - b.state.startedAt)- .map((runtime) => {+ .map((runtime): DownloadUpdate => {if (runtime.kind === 'http') {const { blocks, ...state } = runtime.statereturn structuredClone({ seq: runtime.sentUpdates, state, blocks })}+ if (runtime.kind === 'nzb') {+ const { blocks, ...state } = runtime.state+ return structuredClone({ seq: runtime.sentUpdates, state, blocks })+ }const { pieces, ...state } = runtime.statereturn structuredClone({ seq: runtime.sentUpdates, state, pieces })})@@ -1004,6 +1124,8 @@ export class DownloadManager {startedAt: Date.now()}runtime = this.newTorrentRuntime(state, requestPayload, file, pieces, torrentFile)+ } else if (requestPayload.kind === 'nzb') {+ runtime = await this.startNzb(id, requestPayload, available, selected)} else {const target = new URL(requestPayload.url)const usable = compatibleInterfaces(@@ -1068,6 +1190,67 @@ export class DownloadManager {return id}+ /** An NZB download, set up to start: its articles planned, its folder claimed, its NZB kept+ * beside its manifest. */+ private async startNzb(+ id: string,+ requestPayload: StartNzbDownloadRequest,+ available: NetworkInterfaceInfo[],+ selected: NetworkInterfaceInfo[]+ ): Promise<NzbDownloadRuntime> {+ const nzbFile = probedNzbFile(requestPayload.nzbHash)+ if (!nzbFile) throw new Error('Add the NZB again to start downloading.')+ const server = await loadUsenetServer()+ if (!server) throw new Error('Set up a news server to download from Usenet.')+ const nzb = parseNzb(nzbFile)+ const usable = compatibleInterfaces(+ selected,+ await resolveTargetWithin(server.host, testKnobs.stallTimeoutMs)+ )+ if (usable.length === 0) throw new NoCompatibleRouteError(server.host)+ const blocks = planSegments(nzb)+ const totalBytes = blocks.reduce(+ (sum, block) => sum + block.rangeEnd! - block.rangeStart + 1,+ 0+ )+ await ensureDiskSpace(requestPayload.destinationDir, totalBytes)+ const names = nzb.files.map((file) => file.name)+ const file = await NzbDestination.create(+ requestPayload.destinationDir,+ requestPayload.suggestedFileName,+ names,+ totalBytes+ )+ await ensureDirectory(this.downloadDir(id))+ await writeFile(this.nzbFilePath(id), nzbFile)+ const state: NzbDownloadState = {+ id,+ kind: 'nzb',+ url: requestPayload.url,+ fileName: basename(file.path),+ destinationPath: file.path,+ totalBytes,+ bytesDownloaded: 0,+ speedBytesPerSec: 0,+ status: 'downloading',+ networks: available.map((iface) => newHttpNetwork(iface, usable.includes(iface))),+ streams: [],+ peakStreams: 0,+ blocks,+ totalBlocks: blocks.length,+ nzbFiles: nzb.files.map((entry) => ({+ name: entry.name,+ bytes: entry.segments.reduce((sum, segment) => sum + segment.bytes, 0),+ segments: entry.segments.length,+ missing: 0+ })),+ missingBytes: 0,+ server: server.host,+ startedAt: Date.now()+ }+ return this.newNzbRuntime(state, requestPayload, file, blocks, nzb)+ }+/** Runs the download, and once the run ends makes room for the next one queued. */private launch(runtime: DownloadRuntime): void {runtime.runPromise = this.run(runtime).finally(() => this.pump())@@ -1102,7 +1285,7 @@ export class DownloadManager {}runtime.state.pausedAt = Date.now()clearSpeeds(runtime.state)- if (runtime.kind === 'http') {+ if (runtime.kind !== 'torrent') {for (const stream of runtime.state.streams) {if (stream.status !== 'completed') {stream.status = 'paused'@@ -1341,7 +1524,7 @@ export class DownloadManager {}if (unit.kind === 'torrent') unit.provisionalBytes = 0}- if (runtime.kind === 'http') {+ if (runtime.kind !== 'torrent') {for (const stream of runtime.state.streams) {if (stream.status !== 'completed') {stream.status = 'pending'@@ -1558,7 +1741,7 @@ export class DownloadManager {await runtime.file.discard()await this.removePersistedDownload(runtime, false)runtime.state.status = 'cancelled'- if (runtime.kind === 'http') {+ if (runtime.kind !== 'torrent') {for (const stream of runtime.state.streams) stream.status = 'cancelled'}this.pushUpdate(runtime, false)@@ -1605,7 +1788,9 @@ export class DownloadManager {? (await this.torrentFiles(id)).filter((file) => file.chosen).map((file) => file.path.split(/[\\/]/).slice(1).join(sep))- : undefined+ : runtime.kind === 'nzb'+ ? runtime.state.nzbFiles.map((file) => file.name)+ : undefinedawait trashDownload(runtime.state, owned)}this.runtimes.delete(id)@@ -1743,6 +1928,8 @@ export class DownloadManager {samplePeak(runtime, Date.now())runtime.stop.abort()await Promise.all(runtime.transfer.running())+ // An NZB's files are open while its connections write them; they open again on a resume.+ if (runtime.kind === 'nzb') await runtime.file.close()// However it ended — paused, failed, cancelled, done — nothing is moving now.clearSpeeds(runtime.state)@@ -1785,9 +1972,7 @@ export class DownloadManager {runtime.state.peakSpeedBytesPerSec ??= (end!.bytes - start.bytes) / ranFor}runtime.state.bytesDownloaded =- runtime.state.totalBytes -- (runtime.state.kind === 'torrent' ? runtime.state.skippedBytes : 0) ||- runtime.state.bytesDownloaded+ runtime.state.totalBytes - unwantedBytes(runtime.state) || runtime.state.bytesDownloadedawait this.persistNow(runtime)await runtime.file.discardLeftover().catch(() => {})this.notify(@@ -1852,7 +2037,7 @@ export class DownloadManager {// Until the monitor has looked, nothing is known to be gone.const known = present !== nullif (known) {- if (state.kind === 'http') {+ if (state.kind !== 'torrent') {state.networks = state.networks.filter((network) =>network.enabled ||@@ -1872,7 +2057,7 @@ export class DownloadManager {}for (const iface of present) {if (!state.networks.some((network) => network.id === iface.id)) {- if (state.kind === 'http') state.networks.push(newHttpNetwork(iface, false))+ if (state.kind !== 'torrent') state.networks.push(newHttpNetwork(iface, false))else state.networks.push(newTorrentNetwork(iface, false))}}@@ -1993,6 +2178,12 @@ export class DownloadManager {void omittedBlocksreturn structuredClone({ seq: ++runtime.sentUpdates, state, blocks: changed })}+ if (runtime.kind === 'nzb') {+ const changed = this.changedUnits(runtime, runtime.blocks)+ const { blocks: omittedBlocks, ...state } = runtime.state+ void omittedBlocks+ return structuredClone({ seq: ++runtime.sentUpdates, state, blocks: changed })+ }const changed = this.changedUnits(runtime, runtime.pieces)const { pieces: omittedPieces, ...state } = runtime.statevoid omittedPieces@@ -2057,11 +2248,17 @@ export class DownloadManager {const path = this.manifestPath(runtime.state.id)const temporaryPath = `${path}.tmp`let state: PersistedState+ let missing: number[] | undefinedif (runtime.kind === 'http') {const { blocks: omittedBlocks, streams: omittedStreams, ...rest } = runtime.statevoid omittedBlocksvoid omittedStreamsstate = rest+ } else if (runtime.kind === 'nzb') {+ const { blocks, streams: omittedStreams, ...rest } = runtime.state+ void omittedStreams+ state = rest+ missing = blocks.filter((block) => block.status === 'missing').map((block) => block.index)} else {const { pieces: omittedPieces, peers: omittedPeers, ...rest } = runtime.statevoid omittedPieces@@ -2073,6 +2270,7 @@ export class DownloadManager {savedAt: Date.now(),state: structuredClone(state),...saveBlocks(unitsOf(runtime)),+ ...(missing?.length && { missing }),partialPath: runtime.file.path,publicationPath: runtime.publicationPath,publicationIdentity: runtime.publicationIdentity,diff --git a/src/main/download/history.ts b/src/main/download/history.tsindex a179a2a..8ee2845 100644--- a/src/main/download/history.ts+++ b/src/main/download/history.ts@@ -24,7 +24,7 @@ function isEntry(value: unknown): value is FinishedDownload {if (!isRecord(value)) return falsereturn (typeof value.id === 'string' &&- (value.kind === 'http' || value.kind === 'torrent') &&+ (value.kind === 'http' || value.kind === 'torrent' || value.kind === 'nzb') &&typeof value.url === 'string' &&typeof value.fileName === 'string' &&typeof value.destinationPath === 'string' &&diff --git a/src/main/download/nzb/nntp.ts b/src/main/download/nzb/nntp.tsnew file mode 100644index 0000000..090c3c1--- /dev/null+++ b/src/main/download/nzb/nntp.ts@@ -0,0 +1,332 @@+import { connect as netConnect, isIP, type Socket } from 'node:net'+import { connect as tlsConnect } from 'node:tls'+import { asConnectionError } from '../../network/routes'+import { testKnobs } from '../../testKnobs'+import type { UsenetServerConfig } from './usenetServer'++/** The server answered a command with something other than what it should have. */+export class NntpError extends Error {+ constructor(+ readonly code: number,+ text: string+ ) {+ super(text ? `The news server answered ${code}: ${text}` : `The news server answered ${code}`)+ }+}++/** The server doesn't have the article (430): expired, taken down, or never fully posted. */+export class ArticleMissingError extends NntpError {}++/** The server didn't accept the username or password, or wants them and has none. */+export class NntpAuthError extends NntpError {}++/** The server turned the connection away: more connections than the account allows, or none+ * for now. One connection fewer is what helps, not trying again at once. */+export class NntpRefusedError extends NntpError {}++const CRLF = Buffer.from('\r\n')+/** A multi-line answer ends with a line holding a single dot. */+const TERMINATOR = Buffer.from('\r\n.\r\n')+/** A status line longer than this isn't one: the connection isn't speaking NNTP. */+const MAX_LINE_BYTES = 4096++interface Reading {+ /** Handed whatever is buffered, whenever more arrives: how much of it the read took, and+ * whether that finished it. */+ take: (buffered: Buffer) => { consumed: number; done: boolean }+ resolve: () => void+ reject: (error: Error) => void+}++/**+ * One connection to a news server, speaking just what downloading articles needs: the greeting,+ * AUTHINFO, and BODY by message ID (RFC 3977, RFC 4643). One command at a time. The socket comes+ * connected (through one network, TLS already on when the server wants it); this only reads and+ * writes it.+ *+ * Every read is watched: a server that goes silent for `timeoutMs` mid-answer has its connection+ * dropped, as a stalled HTTP response is.+ */+export class NntpConnection {+ private buffered: Buffer = Buffer.alloc(0)+ private reading: Reading | null = null+ private closed: Error | null = null+ private stallTimer: NodeJS.Timeout | undefined++ private constructor(+ private readonly socket: Socket,+ private readonly timeoutMs: number+ ) {+ socket.on('data', (chunk: Buffer) => this.onData(chunk))+ socket.on('error', (error) => this.fail(asConnectionError(error)))+ socket.on('close', () =>+ this.fail(asConnectionError(new Error('The news server closed the connection')))+ )+ }++ /** Greets the server and signs in. Throws NntpRefusedError when it turns the connection away,+ * NntpAuthError when it doesn't take the sign-in. */+ static async open(+ socket: Socket,+ options: { timeoutMs: number; username: string; password: string; signal: AbortSignal }+ ): Promise<NntpConnection> {+ const connection = new NntpConnection(socket, options.timeoutMs)+ const onAbort = (): void => connection.destroy()+ options.signal.addEventListener('abort', onAbort, { once: true })+ try {+ const greeting = await connection.status()+ if (greeting.code === 400 || greeting.code === 502) {+ throw new NntpRefusedError(greeting.code, greeting.text)+ }+ if (greeting.code !== 200 && greeting.code !== 201) {+ throw new NntpError(greeting.code, greeting.text)+ }+ if (options.username) await connection.authenticate(options.username, options.password)+ return connection+ } catch (error) {+ connection.destroy()+ throw options.signal.aborted ? abortError() : error+ } finally {+ options.signal.removeEventListener('abort', onAbort)+ }+ }++ private async authenticate(username: string, password: string): Promise<void> {+ let reply = await this.command(`AUTHINFO USER ${username}`)+ if (reply.code === 381) reply = await this.command(`AUTHINFO PASS ${password}`)+ if (reply.code === 281) return+ throw signInError(reply.code, reply.text)+ }++ /**+ * An article's body by message ID, dot-unstuffed: lines joined by CRLF, as posted. `onData` is+ * told each chunk's size as it arrives (for the speed meters and limits) and answers how long+ * to wait before reading more. Throws ArticleMissingError when the server doesn't have it.+ */+ async body(+ messageId: string,+ onData: (bytes: number) => number,+ signal: AbortSignal+ ): Promise<Buffer> {+ if (signal.aborted) throw abortError()+ const onAbort = (): void => this.destroy()+ signal.addEventListener('abort', onAbort, { once: true })+ try {+ const reply = await this.command(`BODY <${messageId}>`)+ if (reply.code === 430 || reply.code === 423) {+ throw new ArticleMissingError(reply.code, reply.text)+ }+ if (reply.code === 480 || reply.code === 481) throw signInError(reply.code, reply.text)+ if (reply.code === 400 || reply.code === 502) {+ throw new NntpRefusedError(reply.code, reply.text)+ }+ if (reply.code !== 222) throw new NntpError(reply.code, reply.text)+ return unstuff(await this.block(onData))+ } catch (error) {+ throw signal.aborted ? abortError() : error+ } finally {+ signal.removeEventListener('abort', onAbort)+ }+ }++ /** Says goodbye, as servers like, and closes. */+ close(): void {+ if (!this.closed && this.socket.writable) this.socket.end('QUIT\r\n')+ this.destroy()+ }++ destroy(): void {+ this.fail(asConnectionError(new Error('The connection was closed')))+ this.socket.destroy()+ }++ private async command(line: string): Promise<{ code: number; text: string }> {+ if (this.closed) throw this.closed+ this.socket.write(`${line}\r\n`)+ return this.status()+ }++ /** The next status line: a three-digit code and its text. */+ private status(): Promise<{ code: number; text: string }> {+ let line = ''+ return this.read((buffered) => {+ const end = buffered.indexOf(CRLF)+ if (end < 0) {+ if (buffered.length > MAX_LINE_BYTES) throw new Error('The server isn’t a news server')+ return { consumed: 0, done: false }+ }+ line = buffered.subarray(0, end).toString('latin1')+ return { consumed: end + CRLF.length, done: true }+ }).then(() => {+ const match = /^(\d{3})(?:\s(.*))?$/.exec(line)+ if (!match) throw asConnectionError(new Error('The server isn’t a news server'))+ return { code: Number(match[1]), text: (match[2] ?? '').trim() }+ })+ }++ /** A multi-line answer, up to its terminating dot, as it came: still dot-stuffed. */+ private block(onData: (bytes: number) => number): Promise<Buffer> {+ const chunks: Buffer[] = []+ // A body that is empty ends at once with ".\r\n": as if the status line's CRLF came before it.+ let tail = CRLF+ let result = Buffer.alloc(0)+ return this.read((buffered) => {+ const window = Buffer.concat([tail, buffered])+ const found = window.indexOf(TERMINATOR)+ if (found < 0) {+ chunks.push(buffered)+ tail = window.subarray(Math.max(0, window.length - (TERMINATOR.length - 1)))+ this.throttle(onData(buffered.length))+ return { consumed: buffered.length, done: false }+ }+ // Where the terminator ends, in `buffered`; the body is everything before its ".\r\n".+ const consumed = found + TERMINATOR.length - tail.length+ const bodyEnd = consumed - 3+ if (bodyEnd > 0) chunks.push(buffered.subarray(0, bodyEnd))+ let body = Buffer.concat(chunks)+ // The CRLF ending the last line is the terminator's, but it's part of the body too.+ if (bodyEnd < 0) body = body.subarray(0, body.length + bodyEnd)+ result = body+ this.throttle(onData(consumed))+ return { consumed, done: true }+ }).then(() => result)+ }++ private throttle(wait: number): void {+ if (wait <= 0) return+ this.socket.pause()+ setTimeout(() => this.socket.resume(), wait)+ }++ private read(take: Reading['take']): Promise<void> {+ if (this.closed) return Promise.reject(this.closed)+ return new Promise<void>((resolve, reject) => {+ this.reading = { take, resolve, reject }+ this.armStall()+ this.drain()+ })+ }++ private onData(chunk: Buffer): void {+ this.buffered = this.buffered.length ? Buffer.concat([this.buffered, chunk]) : chunk+ if (this.reading) this.armStall()+ this.drain()+ }++ /** Hands what is buffered to the read waiting for it, if any. */+ private drain(): void {+ const reading = this.reading+ if (!reading || this.buffered.length === 0) return+ let taken: { consumed: number; done: boolean }+ try {+ taken = reading.take(this.buffered)+ } catch (error) {+ this.fail(error as Error)+ return+ }+ this.buffered = this.buffered.subarray(taken.consumed)+ if (!taken.done) return+ this.reading = null+ clearTimeout(this.stallTimer)+ reading.resolve()+ }++ private armStall(): void {+ clearTimeout(this.stallTimer)+ this.stallTimer = setTimeout(+ () =>+ this.fail(+ asConnectionError(new Error('Connection stalled: the news server stopped answering'))+ ),+ this.timeoutMs+ )+ }++ private fail(error: Error): void {+ clearTimeout(this.stallTimer)+ this.closed ??= error+ const reading = this.reading+ this.reading = null+ reading?.reject(error)+ if (!this.socket.destroyed) this.socket.destroy()+ }+}++function signInError(code: number, text: string): NntpError {+ // Some servers answer a sign-in over the account's connection limit with 481 or 502 too; their+ // text says so.+ if (/connection/i.test(text) && !/password|user|auth/i.test(text)) {+ return new NntpRefusedError(code, text)+ }+ return new NntpAuthError(code, text)+}++/** Lines that start with a dot are sent with another one before it (RFC 3977 §3.1.1). */+function unstuff(body: Buffer): Buffer {+ const out = Buffer.allocUnsafe(body.length)+ let length = 0+ let lineStart = true+ for (let i = 0; i < body.length; i++) {+ const byte = body[i]+ if (lineStart && byte === 0x2e && body[i + 1] === 0x2e) {+ lineStart = false+ continue+ }+ out[length++] = byte+ lineStart = byte === 0x0a+ }+ return out.subarray(0, length)+}++const abortError = (): DOMException => new DOMException('Aborted', 'AbortError')++/** Connects to `server` over the computer's default route and signs in, then says goodbye: what+ * the server settings check before they're used. Throws what went wrong, in words. */+export async function testUsenetServer(server: UsenetServerConfig): Promise<void> {+ const signal = AbortSignal.timeout(testKnobs.connectTimeoutMs)+ const socket = await new Promise<Socket>((resolve, reject) => {+ const { host, port } = server+ const socket = server.tls+ ? tlsConnect({ host, port, servername: isIP(host) ? undefined : host })+ : netConnect({ host, port })+ const fail = (error: Error): void => {+ socket.destroy()+ reject(error)+ }+ socket.once(server.tls ? 'secureConnect' : 'connect', () => {+ signal.removeEventListener('abort', onAbort)+ socket.off('error', fail)+ resolve(socket)+ })+ socket.once('error', fail)+ const onAbort = (): void => fail(new Error(`${host} didn’t answer on port ${port}`))+ signal.addEventListener('abort', onAbort, { once: true })+ }).catch((error: NodeJS.ErrnoException) => {+ if (error.code === 'ENOTFOUND' || error.code === 'EAI_AGAIN') {+ throw new Error(`Couldn’t find ${server.host}. Check the address.`)+ }+ if (error.code === 'ECONNREFUSED') {+ throw new Error(`${server.host} isn’t accepting connections on port ${server.port}.`)+ }+ throw error+ })+ try {+ const connection = await NntpConnection.open(socket, {+ timeoutMs: testKnobs.connectTimeoutMs,+ username: server.username,+ password: server.password,+ signal+ })+ connection.close()+ } catch (error) {+ if (error instanceof NntpAuthError) {+ throw new Error('The server didn’t accept the username or password.')+ }+ if (error instanceof NntpRefusedError) {+ throw new Error(+ `The server turned the connection away, often because every connection the account allows is in use. (${error.message})`+ )+ }+ throw error+ }+}diff --git a/src/main/download/nzb/nzbDestination.ts b/src/main/download/nzb/nzbDestination.tsnew file mode 100644index 0000000..57a5f74--- /dev/null+++ b/src/main/download/nzb/nzbDestination.ts@@ -0,0 +1,140 @@+import { constants } from 'node:fs'+import { open, rm, rmdir, stat, truncate, type FileHandle } from 'node:fs/promises'+import { join } from 'node:path'+import { DownloadFile } from '../downloadFile'+import { claimDestinationPath } from '../paths'++/**+ * Where an NZB downloads: a folder of its files, in place from the start, as a torrent's is.+ * Each article is decoded and written straight to its offset in its file, so files fill in as+ * their articles land, in any order. Their sizes are only known from the articles themselves:+ * publishing cuts each file to the size its yEnc headers gave.+ */+export class NzbDestination extends DownloadFile {+ /** Open files, by index; opened on their first write. */+ private handles = new Map<number, Promise<FileHandle>>()+ /** Files written to since the last sync. */+ private written = new Set<number>()++ /** `names`: its files, by index, in the folder. `totalBytes`: the NZB's own count (see+ * NzbSegmentState), what size() reports once the folder is there. */+ constructor(+ path: string,+ private readonly names: readonly string[],+ private readonly totalBytes: number+ ) {+ super(path)+ }++ /** Claims `name` in `directory`, or the first free `name (N)`, for the download's folder. */+ static async create(+ directory: string,+ name: string,+ names: readonly string[],+ totalBytes: number+ ): Promise<NzbDestination> {+ return new NzbDestination(await claimDestinationPath(directory, name, true), names, totalBytes)+ }++ private pathOf(index: number): string {+ return join(this.path, this.names[index])+ }++ /** Writes one decoded article into file `index`, at `offset`. Never truncates: articles land in+ * any order, each into its own part of the file. */+ async write(index: number, offset: number, data: Buffer): Promise<void> {+ let handle = this.handles.get(index)+ if (!handle) {+ handle = open(this.pathOf(index), constants.O_RDWR | constants.O_CREAT)+ this.handles.set(index, handle)+ // A failed open is tried again by the next write rather than kept.+ handle.catch(() => this.handles.delete(index))+ }+ const file = await handle+ let written = 0+ while (written < data.length) {+ const { bytesWritten } = await file.write(+ data,+ written,+ data.length - written,+ offset + written+ )+ written += bytesWritten+ }+ this.written.add(index)+ }++ /** Once the folder is there, every article has its place: as a torrent's, it reports the+ * download's size. Throws when it's gone. */+ async size(): Promise<number> {+ await stat(this.path)+ return this.totalBytes+ }++ async sync(): Promise<void> {+ const indexes = [...this.written]+ this.written.clear()+ try {+ for (const index of indexes) await (await this.handles.get(index))?.sync()+ } catch (error) {+ for (const index of indexes) this.written.add(index)+ throw error+ }+ }++ /** Closes its files: a run is over (they open again on the next write). */+ async close(): Promise<void> {+ await this.sync().catch(() => {})+ const handles = [...this.handles.values()]+ this.handles.clear()+ await Promise.all(handles.map(async (handle) => (await handle).close().catch(() => {})))+ }++ /** Each file cut to its real size (`sizes`, by index; unset for one no article of came in),+ * and synced. */+ private async finish(sizes: readonly (number | undefined)[]): Promise<void> {+ await this.close()+ for (const [index, size] of sizes.entries()) {+ if (size === undefined) continue+ await truncate(this.pathOf(index), size).catch((error: NodeJS.ErrnoException) => {+ // A file with every article missing was never written: there's nothing to cut.+ if (error.code !== 'ENOENT') throw error+ })+ }+ for (const index of sizes.keys()) {+ const handle = await open(this.pathOf(index), 'r+').catch(() => null)+ if (!handle) continue+ try {+ await handle.sync()+ } finally {+ await handle.close()+ }+ }+ }++ /** Its files' real sizes, by index, as the download has learned them (see finish). */+ fileSizes: () => readonly (number | undefined)[] = () => []++ /** Already in place: its files are cut to size (see finish), and it stays where it is. */+ async publish(+ _destinationPath: string,+ _expectedBytes: number,+ beforeAttempt: (candidate: string) => Promise<void>+ ): Promise<string> {+ await this.finish(this.fileSizes())+ await beforeAttempt(this.path)+ return this.path+ }++ /** It is what was published: nothing is left over. */+ discardLeftover(): Promise<void> {+ return Promise.resolve()+ }++ /** Its own files, and the folder once that leaves it empty. Anything else put in it stays. */+ async discard(): Promise<void> {+ await this.close()+ for (const index of this.names.keys()) await rm(this.pathOf(index), { force: true })+ await rmdir(this.path).catch(() => {})+ }+}diff --git a/src/main/download/nzb/nzbFile.ts b/src/main/download/nzb/nzbFile.tsnew file mode 100644index 0000000..8f8df0e--- /dev/null+++ b/src/main/download/nzb/nzbFile.ts@@ -0,0 +1,319 @@+import { createHash } from 'node:crypto'+import { open } from 'node:fs/promises'+import type { NzbProbeResult, NzbSegmentState } from '../../../shared/types'+import { STRIPPED_FROM_NAMES } from '../torrent/paths'++/** An NZB is a list of message IDs; a few MB for even a large release. A file or link much+ * larger than this isn't one. */+const MAX_NZB_FILE_BYTES = 64 * 1024 * 1024+const TOO_LARGE = 'Plexo supports NZB files up to 64 MB.'+const NOT_NZB = 'This isn’t an NZB file Plexo can read'+const MAX_NAME_BYTES = 255+const WINDOWS_RESERVED = /^(con|prn|aux|nul|com\d|lpt\d)(\..*)?$/i++/** One article, as the NZB lists it. */+export interface NzbSegment {+ /** Its place in its file, from 1. */+ number: number+ /** Its size as posted: the encoded article, a little more than the data it holds. */+ bytes: number+ messageId: string+}++export interface NzbFileEntry {+ /** Safe to save in the download's folder on any system (see safeName). */+ name: string+ segments: NzbSegment[]+}++export interface ParsedNzb {+ files: NzbFileEntry[]+ /** The archive password the poster put in the NZB's head, if any. */+ password?: string+}++function decodeEntities(text: string): string {+ return text.replace(/&(#x[0-9a-f]+|#\d+|amp|lt|gt|quot|apos);/gi, (whole, entity: string) => {+ const lower = entity.toLowerCase()+ if (lower === 'amp') return '&'+ if (lower === 'lt') return '<'+ if (lower === 'gt') return '>'+ if (lower === 'quot') return '"'+ if (lower === 'apos') return "'"+ const code = lower.startsWith('#x') ? parseInt(lower.slice(2), 16) : parseInt(lower.slice(1))+ return code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : whole+ })+}++function attributes(source: string): Map<string, string> {+ const found = new Map<string, string>()+ for (const match of source.matchAll(/([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g)) {+ found.set(match[1].toLowerCase(), decodeEntities(match[2] ?? match[3]))+ }+ return found+}++/** Every `<tag …>…</tag>` in `xml`, prefixed or not: its attributes and what's inside. */+function* elements(+ xml: string,+ tag: string+): Generator<{ attrs: Map<string, string>; body: string }> {+ // An attribute's quoted value may hold a `>`: XML allows it unescaped, and subjects do.+ const pattern = new RegExp(+ `<(?:[\\w-]+:)?${tag}\\b((?:[^>"']|"[^"]*"|'[^']*')*?)(?:/>|>([\\s\\S]*?)</(?:[\\w-]+:)?${tag}\\s*>)`,+ 'gi'+ )+ for (const match of xml.matchAll(pattern)) {+ yield { attrs: attributes(match[1]), body: match[2] ?? '' }+ }+}++/** A file's name from its subject. Posters quote it — `[01/20] - "Name.part01.rar" yEnc (1/50)` —+ * and the rest is a guess at what's left once the article count is taken off. */+export function nameFromSubject(subject: string): string {+ const quoted = /"([^"]+)"/.exec(subject)+ if (quoted) return quoted[1].trim()+ const withExtension = /(?:^|\s)([^\s"]+\.[a-z0-9]{2,5})(?:\s+yEnc\b|\s*\(\d+\/\d+\)|$)/i.exec(+ subject+ )+ if (withExtension) return withExtension[1]+ return subject+ .replace(/\s*yEnc\b.*$/i, '')+ .replace(/\s*\(\d+\/\d+\)\s*$/, '')+ .trim()+}++/**+ * A name that can be saved as is, inside the download's folder, on any of the systems Plexo runs+ * on: nothing that names another folder, no character Windows refuses, no name it reserves, and+ * no longer than a file system allows (the extension kept).+ */+export function safeName(raw: string, fallback: string): string {+ let name = raw.replace(STRIPPED_FROM_NAMES, '_').replace(/[\u007f]/g, '_')+ name = name.replace(/[. ]+$/, '').trim()+ if (!name || name === '.' || name === '..') name = fallback+ if (WINDOWS_RESERVED.test(name)) name = `_${name}`+ if (Buffer.byteLength(name) > MAX_NAME_BYTES) {+ const dot = name.lastIndexOf('.')+ const extension = dot > 0 && name.length - dot <= 16 ? name.slice(dot) : ''+ const characters = Array.from(name.slice(0, name.length - extension.length))+ while (Buffer.byteLength(characters.join('') + extension) > MAX_NAME_BYTES) characters.pop()+ name = characters.join('') + extension+ }+ return name+}++/** "Name (2).ext": a second file of the same name. Compared without case, as macOS and Windows+ * compare names. */+function uniqueNames(names: string[]): string[] {+ const taken = new Set<string>()+ return names.map((name) => {+ let candidate = name+ for (let index = 2; taken.has(candidate.toLowerCase()); index++) {+ const dot = name.lastIndexOf('.')+ candidate =+ dot > 0 ? `${name.slice(0, dot)} (${index})${name.slice(dot)}` : `${name} (${index})`+ }+ taken.add(candidate.toLowerCase())+ return candidate+ })+}++/** par2 files last, the small index file first among them: what is fetched first is what the+ * user can start on. Otherwise by name, numbers in order ("part2" before "part10"). */+function downloadOrder(a: NzbFileEntry, b: NzbFileEntry): number {+ const rank = (file: NzbFileEntry): number =>+ !/\.par2$/i.test(file.name) ? 0 : /\.vol\d+\+\d+\.par2$/i.test(file.name) ? 2 : 1+ return (+ rank(a) - rank(b) ||+ a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' })+ )+}++/** Reads an NZB. Throws when it isn't one, or lists nothing to download. */+export function parseNzb(source: Uint8Array): ParsedNzb {+ const xml = Buffer.from(source)+ .toString('utf8')+ .replace(/<!--[\s\S]*?-->/g, '')+ .replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, (_, text: string) =>+ text.replace(/&/g, '&').replace(/</g, '<')+ )+ if (!/<(?:[\w-]+:)?nzb\b/i.test(xml)) throw new Error(NOT_NZB)++ let password: string | undefined+ for (const meta of elements(xml, 'meta')) {+ if (meta.attrs.get('type')?.toLowerCase() === 'password') {+ password = decodeEntities(meta.body).trim() || undefined+ }+ }++ const raw: { name: string; segments: NzbSegment[] }[] = []+ let index = 0+ for (const file of elements(xml, 'file')) {+ index+++ const seen = new Set<number>()+ const segments: NzbSegment[] = []+ for (const segment of elements(file.body, 'segment')) {+ const number = Number(segment.attrs.get('number'))+ const bytes = Number(segment.attrs.get('bytes'))+ const messageId = decodeEntities(segment.body).trim().replace(/^<|>$/g, '')+ // A message ID can't hold whitespace or angle brackets: one that does would break the command.+ if (!Number.isSafeInteger(number) || number < 1 || seen.has(number)) continue+ if (!messageId || /[\s<>]/.test(messageId)) continue+ seen.add(number)+ segments.push({+ number,+ // Every article is at least one byte; a missing or bad size counts as that.+ bytes: Number.isSafeInteger(bytes) && bytes > 0 ? bytes : 1,+ messageId+ })+ }+ if (segments.length === 0) continue+ segments.sort((a, b) => a.number - b.number)+ raw.push({+ name: safeName(nameFromSubject(file.attrs.get('subject') ?? ''), `file-${index}`),+ segments+ })+ }+ if (raw.length === 0) throw new Error('This NZB lists no files to download')+ raw.sort(downloadOrder)+ const names = uniqueNames(raw.map((file) => file.name))+ return { files: raw.map((file, i) => ({ ...file, name: names[i] })), password }+}++/** Its articles as one download's segments, file after file, each as long as the NZB says. */+export function planSegments(nzb: ParsedNzb): NzbSegmentState[] {+ const segments: NzbSegmentState[] = []+ let rangeStart = 0+ for (const file of nzb.files) {+ for (const segment of file.segments) {+ segments.push({+ kind: 'nzb',+ index: segments.length,+ rangeStart,+ rangeEnd: rangeStart + segment.bytes - 1,+ status: 'pending',+ bytesDownloaded: 0,+ bytesByInterface: {}+ })+ rangeStart += segment.bytes+ }+ }+ return segments+}++/** For each segment, by index: its file's index and the article. */+export function segmentArticles(nzb: ParsedNzb): { file: number; segment: NzbSegment }[] {+ return nzb.files.flatMap((file, index) =>+ file.segments.map((segment) => ({ file: index, segment }))+ )+}++/** The NZB of each one probed lately, by hash: what a download of it starts from, so the window+ * never has to hold or send it. */+const recent = new Map<string, Uint8Array>()+const RECENT_LIMIT = 8++export function probedNzbFile(hash: string): Uint8Array | undefined {+ return recent.get(hash)+}++/** What an NZB holds, as a probe result. `name`: what to call the download, when the link or+ * file has a name of its own. */+export function describeNzb(nzbFile: Uint8Array, link: string, name: string): NzbProbeResult {+ const nzb = parseNzb(nzbFile)+ const hash = createHash('sha256').update(nzbFile).digest('hex').slice(0, 32)+ recent.delete(hash)+ recent.set(hash, nzbFile)+ if (recent.size > RECENT_LIMIT) recent.delete(recent.keys().next().value!)+ const files = nzb.files.map((file) => ({+ name: file.name,+ bytes: file.segments.reduce((sum, segment) => sum + segment.bytes, 0),+ segments: file.segments.length+ }))+ return {+ kind: 'nzb',+ requestedUrl: link,+ finalUrl: link,+ supportsRanges: true,+ totalBytes: files.reduce((sum, file) => sum + file.bytes, 0),+ suggestedFileName: safeName(name.replace(/\.nzb$/i, ''), 'Usenet download'),+ contentType: 'application/x-nzb',+ etag: null,+ lastModified: null,+ nzb: { hash, files }+ }+}++/** An NZB file on this computer. */+export async function readNzbFile(path: string): Promise<Uint8Array> {+ const handle = await open(path, 'r')+ try {+ if ((await handle.stat()).size > MAX_NZB_FILE_BYTES) throw new Error(TOO_LARGE)+ return await handle.readFile()+ } finally {+ await handle.close()+ }+}++/** How much of an XML answer is read to tell an NZB from an indexer's error. */+const SNIFF_BYTES = 64 * 1024++/** The start of what `url` answers, as text: enough to see what kind of XML it is. */+export async function sniffXml(+ url: string,+ timeoutMs: number,+ headers: Record<string, string>+): Promise<string> {+ const stop = new AbortController()+ const response = await fetch(url, {+ headers,+ signal: AbortSignal.any([stop.signal, AbortSignal.timeout(timeoutMs)])+ })+ if (!response.ok || !response.body) {+ throw new Error(`Server responded with status ${response.status}`)+ }+ const chunks: Uint8Array[] = []+ let total = 0+ try {+ for await (const chunk of response.body) {+ chunks.push(chunk)+ total += chunk.length+ if (total >= SNIFF_BYTES) break+ }+ } finally {+ stop.abort()+ }+ return Buffer.concat(chunks).subarray(0, SNIFF_BYTES).toString('utf8')+}++/** An indexer's refusal, as its (Newznab) API sends one in place of the NZB:+ * `<error code="100" description="Incorrect user credentials"/>`. Null when it isn't one. */+export function indexerError(xml: string): string | null {+ const error = /<error\b([^>]*)\/?>/i.exec(xml)+ if (!error || /<(?:[\w-]+:)?nzb\b/i.test(xml)) return null+ const attrs = attributes(error[1])+ const description = attrs.get('description')?.trim()+ const code = attrs.get('code')?.trim()+ return `The indexer refused this link: ${description || 'no reason given'}${code ? ` (error ${code})` : ''}`+}++/** An NZB file a link points at, as an indexer serves it. `headers` are for `url`. */+export async function downloadNzbFile(+ url: string,+ timeoutMs: number,+ headers: Record<string, string>+): Promise<Uint8Array> {+ const response = await fetch(url, { headers, signal: AbortSignal.timeout(timeoutMs) })+ if (!response.ok || !response.body) {+ throw new Error(`Server responded with status ${response.status}`)+ }+ const chunks: Uint8Array[] = []+ let total = 0+ for await (const chunk of response.body) {+ total += chunk.length+ if (total > MAX_NZB_FILE_BYTES) throw new Error(TOO_LARGE)+ chunks.push(chunk)+ }+ return Buffer.concat(chunks)+}diff --git a/src/main/download/nzb/nzbTransfer.ts b/src/main/download/nzb/nzbTransfer.tsnew file mode 100644index 0000000..972cdd0--- /dev/null+++ b/src/main/download/nzb/nzbTransfer.ts@@ -0,0 +1,602 @@+import type {+ DownloadStatus,+ HttpDownloadNetwork,+ HttpStreamState,+ NzbSegmentState+} from '../../../shared/types'+import { testKnobs } from '../../testKnobs'+import {+ asConnectionError,+ ConnectionError,+ connectThrough,+ NoCompatibleRouteError+} from '../../network/routes'+import { advanceBlock, retractBlock } from '../blockProgress'+import { interleave } from '../plan'+import {+ delay,+ recomputeAggregates,+ type NzbTransferTarget,+ type Transfer,+ type TransferHost+} from '../transfer'+import { ArticleMissingError, NntpAuthError, NntpConnection, NntpRefusedError } from './nntp'+import type { NzbSegment } from './nzbFile'+import type { UsenetServerConfig } from './usenetServer'+import { decodeYenc, YencError } from './yenc'++// Set PLEXO_DEBUG=1 to log every article's outcome.+const debug: (...args: unknown[]) => void = process.env['PLEXO_DEBUG']+ ? (...args) => console.debug('[plexo nzb]', ...args)+ : () => {}++const RETRY_BASE_DELAY_MS = testKnobs.retryBaseDelayMs+const RETRY_MAX_DELAY_MS = 15_000+const UNREACHABLE_RETRY_MS = 5_000+const SILENT_AFTER_MS = testKnobs.silentAfterMs+/** How often a connection with nothing to do looks for an article again. */+const IDLE_POLL_MS = 250+/** Answers that make no sense from a working server, in a row, before a connection gives up. */+const MAX_STRIKES = 5+/** A damaged article is fetched this many times before it counts as missing: a server sends the+ * same copy each time, but a connection can garble one in passing. */+const DAMAGED_TRIES = 2++/** Doubling, with ±20% jitter, as HTTP streams back off (see httpTransfer.ts). */+function retryDelayMs(attempt: number): number {+ const exact = Math.min(RETRY_BASE_DELAY_MS * 2 ** (attempt - 1), RETRY_MAX_DELAY_MS)+ return exact * (0.8 + 0.4 * Math.random())+}++const message = (error: unknown): string => (error instanceof Error ? error.message : String(error))++/** One article to fetch: its segment, where its data goes, and the NZB's record of it. */+export interface Article {+ /** The file it is part of, by index into the download's files. */+ file: number+ segment: NzbSegment+}++interface Connection {+ /** Stops the connection for good: a pause, a cancel, retiring it. */+ controller: AbortController+ /** Cuts its waits short: when it stops, or the run ends. */+ wait: AbortSignal+ /** Cuts a backoff short without stopping it (see wake). Replaced once used. */+ nudge: AbortController+ /** Its session with the server, signed in; null until it opens one, and after one fails. */+ session: NntpConnection | null+ /** What it is fetching: aborted to drop the socket under it (see wake). */+ fetching: AbortController | null+ failures: number+ strikes: number+ retiring: boolean+}++/**+ * Fetches an NZB's articles over connections to the user's news server, each one through one+ * network, as HTTP streams are. A connection signs in once and then takes article after article+ * — the lowest-numbered one no other connection holds — decodes it (yEnc) and writes it straight+ * into its file at the offset the article itself gives (see NzbDestination).+ *+ * The server allows its account only so many connections at once, for every network together:+ * they are shared out evenly over the networks in use. A server that turns one away (too many)+ * gets one fewer for the rest of the run.+ *+ * An article counts once it is decoded, checked (CRC-32) and written. One the server doesn't have,+ * or keeps sending damaged, is marked missing and the download carries on: a par2 repair+ * restores it, which Plexo leaves to the user's tools.+ */+export class NzbTransfer implements Transfer {+ private connections = new Map<number, Connection>()+ /** The running connections' workers, by stream id. */+ private workers = new Map<number, Promise<void>>()+ /** The server, as loaded for this run; null until it has been. */+ private server: UsenetServerConfig | null = null+ private loading: Promise<void> | null = null+ /** Connections the server lets this run keep: its setting, less what it turned away. */+ private allowed = Infinity+ /** Articles in flight, by segment index. */+ private inFlight = new Set<number>()+ /** Damaged copies received, by segment index. */+ private damaged = new Map<number, number>()+ /** By network: when it last showed it can reach the server. */+ private aliveAt = new Map<string, number>()++ constructor(+ private readonly runtime: NzbTransferTarget,+ private readonly host: TransferHost,+ /** By segment index. */+ private readonly articles: readonly Article[],+ private readonly loadServer: () => Promise<UsenetServerConfig | null>+ ) {}++ reconcile(): void {+ if (!this.server) {+ this.loading ??= this.loadServer().then(+ (server) => {+ if (this.runtime.stop.signal.aborted) return+ if (!server) {+ this.host.failDownload('Set up a news server in Plexo to download from Usenet.')+ return+ }+ this.server = server+ this.allowed = server.connections+ this.host.reconcile()+ },+ (error) => this.host.failDownload(message(error))+ )+ return+ }+ const { state } = this.runtime+ const usable = state.networks.filter(+ (network) => network.status === 'on' || network.status === 'unreachable'+ )+ const targets = shareConnections(usable, this.allowed)+ // New connections only for articles no connection is on yet: near the end, fewer are needed.+ let waiting = this.runtime.blocks.filter(+ (block) => block.status === 'pending' && !this.inFlight.has(block.index)+ ).length++ const now = Date.now()+ const toStart: HttpStreamState[] = []+ for (const network of state.networks) {+ // Idle ones first, so retiring one doesn't cut an article short.+ const live = this.liveStreams(network.id).sort(+ (a, b) =>+ Number(a.currentBlockIndex === undefined) - Number(b.currentBlockIndex === undefined)+ )+ const target = targets.get(network.id) ?? 0+ if (target > 0 && !this.aliveAt.has(network.id)) this.aliveAt.set(network.id, now)+ if (live.length > target) this.retire(live.slice(target))+ // Kept from before a pause: started again while there is work for them, dropped if not.+ for (const stream of live.slice(0, target)) {+ if (this.workers.has(stream.id)) continue+ if (waiting > 0) {+ toStart.push(stream)+ waiting--+ } else {+ this.removeStream(stream)+ }+ }+ const adding = Math.min(target - Math.min(live.length, target), waiting)+ if (adding > 0) {+ toStart.push(...this.addStreams(network.id, adding))+ waiting -= adding+ }+ }+ // Every network gets its first connection before any gets a second.+ this.start(interleave(toStart, (stream) => stream.interfaceId))+ }++ tick(): void {+ this.host.reconcile()+ }++ running(): Iterable<Promise<void>> {+ return this.workers.values()+ }++ abort(): void {+ for (const connection of this.connections.values()) connection.controller.abort()+ }++ reset(): void {+ this.server = null+ this.loading = null+ this.allowed = Infinity+ this.damaged.clear()+ this.aliveAt.clear()+ }++ wake(pick: (networkId: string) => boolean, reconnect: (networkId: string) => boolean): void {+ for (const stream of this.runtime.state.streams) {+ const connection = this.connections.get(stream.id)+ if (!connection || connection.retiring || !pick(stream.interfaceId)) continue+ connection.failures = 0+ if (reconnect(stream.interfaceId)) {+ connection.fetching?.abort()+ connection.session?.destroy()+ connection.session = null+ }+ connection.nudge.abort()+ }+ }++ systemResumed(now: number): void {+ for (const id of this.aliveAt.keys()) this.aliveAt.set(id, now)+ this.wake(+ () => true,+ () => true+ )+ }++ private network(id: string): HttpDownloadNetwork {+ const network = this.runtime.state.networks.find((entry) => entry.id === id)+ if (!network) throw new Error(`Unknown network ${id}`)+ return network+ }++ private liveStreams(networkId: string): HttpStreamState[] {+ return this.runtime.state.streams.filter(+ (stream) => stream.interfaceId === networkId && !this.connections.get(stream.id)?.retiring+ )+ }++ private addStreams(networkId: string, count: number): HttpStreamState[] {+ const { state } = this.runtime+ let id = Math.max(-1, ...state.streams.map((stream) => stream.id))+ const added = Array.from({ length: count }, (): HttpStreamState => ({+ id: ++id,+ interfaceId: networkId,+ rangeStart: 0,+ rangeEnd: null,+ bytesDownloaded: 0,+ speedBytesPerSec: 0,+ status: 'pending'+ }))+ state.streams.push(...added)+ state.peakStreams = Math.max(state.peakStreams, state.streams.length)+ this.host.scheduleUpdate()+ return added+ }++ /** Stops these streams for good; what one was fetching goes back for another to take. */+ private retire(streams: HttpStreamState[]): void {+ for (const stream of streams) {+ const connection = this.connections.get(stream.id)+ if (connection && this.workers.has(stream.id)) {+ connection.retiring = true+ connection.controller.abort()+ } else {+ this.removeStream(stream)+ }+ }+ }++ private removeStream(stream: HttpStreamState): void {+ this.connections.delete(stream.id)+ this.runtime.meters.connections.delete(stream.id)+ const index = this.runtime.state.streams.indexOf(stream)+ if (index >= 0) this.runtime.state.streams.splice(index, 1)+ this.host.scheduleUpdate()+ }++ private start(streams: HttpStreamState[]): void {+ for (const stream of streams) {+ this.workers.set(+ stream.id,+ this.runConnection(stream).finally(() => {+ this.workers.delete(stream.id)+ if (this.connections.get(stream.id)?.retiring) this.removeStream(stream)+ })+ )+ }+ if (streams.length > 0) this.host.scheduleUpdate()+ }++ /** The lowest-numbered article nothing holds, taken. */+ private claim(): NzbSegmentState | null {+ for (const block of this.runtime.blocks) {+ if (block.status === 'pending' && !this.inFlight.has(block.index)) {+ this.inFlight.add(block.index)+ return block+ }+ }+ return null+ }++ private goIdle(stream: HttpStreamState): void {+ stream.status = 'pending'+ stream.currentBlockIndex = undefined+ stream.speedBytesPerSec = 0+ }++ /** The article goes back, unfinished, for any connection to take. */+ private release(stream: HttpStreamState, block: NzbSegmentState): void {+ this.inFlight.delete(block.index)+ if (block.status === 'downloading') block.status = 'pending'+ if (retractBlock(block, 0, stream.interfaceId) > 0) {+ recomputeAggregates(this.runtime.state, this.runtime.blocks)+ }+ this.goIdle(stream)+ this.host.scheduleUpdate()+ }++ private stopped(connection: Connection): boolean {+ return (+ connection.controller.signal.aborted ||+ connection.retiring ||+ (this.runtime.state.status as DownloadStatus) !== 'downloading'+ )+ }++ private async runConnection(stream: HttpStreamState): Promise<void> {+ const controller = new AbortController()+ const connection: Connection = {+ controller,+ wait: AbortSignal.any([controller.signal, this.runtime.stop.signal]),+ nudge: new AbortController(),+ session: null,+ fetching: null,+ failures: 0,+ strikes: 0,+ retiring: false+ }+ this.connections.set(stream.id, connection)+ try {+ while (!this.stopped(connection)) {+ const block = this.claim()+ if (!block) {+ this.goIdle(stream)+ // Others still have articles in flight: one may come back for this connection to take.+ if (this.inFlight.size > 0) {+ await delay(IDLE_POLL_MS, connection.wait)+ continue+ }+ stream.status = 'completed'+ this.host.scheduleUpdate()+ break+ }+ const next = await this.fetch(stream, connection, block)+ if (next === 'stop') break+ }+ } finally {+ connection.session?.close()+ connection.session = null+ if (stream.status !== 'completed') {+ stream.status = this.runtime.state.status === 'paused' ? 'paused' : 'cancelled'+ }+ stream.currentBlockIndex = undefined+ stream.speedBytesPerSec = 0+ this.host.scheduleUpdate()+ }+ }++ /** Fetches, decodes and writes one article, and applies what became of it. */+ private async fetch(+ stream: HttpStreamState,+ connection: Connection,+ block: NzbSegmentState+ ): Promise<'continue' | 'stop'> {+ const { segment, file } = this.articles[block.index]+ const networkId = stream.interfaceId+ block.status = 'downloading'+ block.interfaceId = networkId+ stream.status = 'downloading'+ stream.currentBlockIndex = block.index+ stream.rangeStart = block.rangeStart+ stream.rangeEnd = block.rangeEnd+ this.host.scheduleUpdate()++ const fetching = new AbortController()+ connection.fetching = fetching+ const signal = AbortSignal.any([connection.controller.signal, fetching.signal])+ const length = block.rangeEnd! - block.rangeStart + 1+ let received = 0+ try {+ connection.session ??= await this.openSession(networkId, signal)+ const body = await connection.session.body(+ segment.messageId,+ (bytes) => {+ received += bytes+ this.onReceived(stream, block, Math.min(received, length - 1), bytes)+ return this.host.limits.take(networkId, bytes)+ },+ signal+ )+ const part = decodeYenc(body)+ await this.runtime.file.write(file, part.offset, part.data)+ const entry = this.runtime.state.nzbFiles[file]+ entry.size ??= part.fileSize+ this.complete(stream, block)+ connection.failures = 0+ connection.strikes = 0+ debug('article', { segment: block.index, network: networkId, bytes: received })+ return 'continue'+ } catch (error) {+ if (fetching.signal.aborted && !this.stopped(connection)) {+ // Dropped on purpose (see wake): not a failure, and the article is taken up again at once.+ this.release(stream, block)+ connection.session?.destroy()+ connection.session = null+ return 'continue'+ }+ return this.failed(stream, connection, block, error)+ } finally {+ connection.fetching = null+ }+ }++ private async openSession(networkId: string, signal: AbortSignal): Promise<NntpConnection> {+ const server = this.server!+ const iface = this.host.networks.find(networkId)+ if (!iface) throw new ConnectionError(new Error('The network is not connected'))+ // Refused, timed out, or a failed TLS handshake: the server can't be reached, as with a+ // dropped connection (see failed), rather than a strike against the server.+ const socket = await connectThrough(+ iface,+ server.host,+ server.port,+ server.tls,+ Math.min(testKnobs.connectTimeoutMs, testKnobs.stallTimeoutMs),+ signal,+ this.host.getProxyForInterface?.(networkId)+ ).catch((error: unknown) => {+ throw asConnectionError(error)+ })+ return NntpConnection.open(socket, {+ timeoutMs: testKnobs.stallTimeoutMs,+ username: server.username,+ password: server.password,+ signal+ })+ }++ /** Bytes of an article came in: it moves on screen, and the network is through. */+ private onReceived(+ stream: HttpStreamState,+ block: NzbSegmentState,+ position: number,+ bytes: number+ ): void {+ const networkId = stream.interfaceId+ this.aliveAt.set(networkId, Date.now())+ const network = this.network(networkId)+ if (network.status === 'unreachable') network.status = 'on'+ // Shown as it comes, short of its last byte: the article counts once it's written.+ const gained = advanceBlock(block, networkId, position)+ stream.bytesDownloaded += gained+ network.bytesDownloaded += gained+ this.runtime.state.bytesDownloaded += gained+ this.runtime.meters.add(stream.id, networkId, bytes)+ this.host.scheduleUpdate()+ }++ private complete(stream: HttpStreamState, block: NzbSegmentState): void {+ this.inFlight.delete(block.index)+ this.damaged.delete(block.index)+ const gained = advanceBlock(block, stream.interfaceId, block.rangeEnd! - block.rangeStart + 1)+ stream.bytesDownloaded += gained+ block.status = 'completed'+ recomputeAggregates(this.runtime.state, this.runtime.blocks)+ // Between articles, not idle: it keeps its speed, which only the download's clock reads (an+ // article takes about as long as one tick, so zeroing it here showed every connection at 0).+ stream.status = 'pending'+ stream.currentBlockIndex = undefined+ this.host.scheduleUpdate()+ }++ /** The article is gone from the server, or keeps coming damaged: left as zeros, for a par2+ * repair. Every article missing means the server has none of this NZB, which is no download. */+ private markMissing(stream: HttpStreamState, block: NzbSegmentState): void {+ this.release(stream, block)+ this.damaged.delete(block.index)+ block.status = 'missing'+ const { state } = this.runtime+ state.nzbFiles[this.articles[block.index].file].missing += 1+ state.missingBytes += block.rangeEnd! - block.rangeStart + 1+ this.host.scheduleUpdate()+ if (this.runtime.blocks.every((other) => other.status === 'missing')) {+ this.host.failDownload(+ 'The news server has none of this NZB’s articles. They may have expired, or been taken down.',+ true+ )+ }+ }++ private async failed(+ stream: HttpStreamState,+ connection: Connection,+ block: NzbSegmentState,+ error: unknown+ ): Promise<'continue' | 'stop'> {+ debug('article failed', {+ segment: block.index,+ network: stream.interfaceId,+ error: message(error)+ })+ if (this.stopped(connection)) {+ this.release(stream, block)+ return 'stop'+ }+ if (error instanceof ArticleMissingError) {+ this.markMissing(stream, block)+ return 'continue'+ }+ if (error instanceof YencError) {+ const tries = (this.damaged.get(block.index) ?? 0) + 1+ this.damaged.set(block.index, tries)+ if (tries >= DAMAGED_TRIES) this.markMissing(stream, block)+ else this.release(stream, block)+ return 'continue'+ }+ this.release(stream, block)+ // The session can't be trusted after anything else: a fresh one next time.+ connection.session?.destroy()+ connection.session = null+ const network = this.network(stream.interfaceId)++ if (error instanceof NntpAuthError) {+ this.host.failDownload(+ `The news server didn’t accept the username or password. Check them in the server’s settings, then try again. (${error.message})`+ )+ return 'stop'+ }+ if (error instanceof NoCompatibleRouteError) {+ this.host.failNetwork(network, error.message)+ return 'stop'+ }+ if (error instanceof NntpRefusedError) {+ // Over the account's limit: one connection fewer from now on. The last one keeps trying.+ const live = this.runtime.state.streams.filter(+ (other) => !this.connections.get(other.id)?.retiring+ ).length+ if (live > 1) {+ this.allowed = Math.min(this.allowed, live - 1)+ connection.retiring = true+ this.host.reconcile()+ return 'stop'+ }+ }++ const now = Date.now()+ if (+ error instanceof ConnectionError &&+ network.status === 'on' &&+ now - (this.aliveAt.get(network.id) ?? now) >= SILENT_AFTER_MS+ ) {+ // Nothing through this network for a while, and its connections failing: it can't reach+ // the server. It keeps one connection trying (see reconcile).+ network.status = 'unreachable'+ this.host.reconcile()+ }+ if (!(error instanceof ConnectionError) && !(error instanceof NntpRefusedError)) {+ if (++connection.strikes > MAX_STRIKES) {+ connection.retiring = true+ if (this.liveStreams(network.id).length === 0)+ this.host.failNetwork(network, message(error))+ return 'stop'+ }+ }+ if (network.status === 'on') network.retries += 1+ connection.failures += 1+ let wait = retryDelayMs(connection.failures)+ if (network.status === 'unreachable') wait = Math.min(wait, UNREACHABLE_RETRY_MS)+ stream.status = 'retrying'+ this.host.scheduleUpdate()+ await delay(wait, AbortSignal.any([connection.wait, connection.nudge.signal]))+ if (connection.nudge.signal.aborted) connection.nudge = new AbortController()+ if (this.stopped(connection)) return 'stop'+ this.goIdle(stream)+ return 'continue'+ }+}++/** `total` connections over `networks`: one for each that can't reach the server (it only+ * keeps trying), the rest evenly over those in use, the first ones taking what doesn't divide. */+export function shareConnections(+ networks: readonly HttpDownloadNetwork[],+ total: number+): Map<string, number> {+ const shares = new Map<string, number>()+ const trying = networks.filter((network) => network.status === 'unreachable')+ const inUse = networks.filter((network) => network.status === 'on')+ let left = total+ for (const network of trying) {+ if (left <= 0) break+ shares.set(network.id, 1)+ left--+ }+ inUse.forEach((network, index) => {+ const share = Math.floor(left / inUse.length) + (index < left % inUse.length ? 1 : 0)+ shares.set(network.id, share)+ })+ // Fewer connections than networks: the one that can't reach the server gives way first.+ if (inUse.length > 0 && inUse.every((network) => shares.get(network.id) === 0)) {+ shares.set(inUse[0].id, 1)+ const given = trying.find((network) => shares.get(network.id))+ if (given) shares.set(given.id, 0)+ }+ return shares+}diff --git a/src/main/download/nzb/usenetServer.ts b/src/main/download/nzb/usenetServer.tsnew file mode 100644index 0000000..8536309--- /dev/null+++ b/src/main/download/nzb/usenetServer.ts@@ -0,0 +1,103 @@+import { join } from 'node:path'+import { app, safeStorage } from 'electron'+import type { UsenetServer, UsenetServerInput } from '../../../shared/types'+import { readJson, updateJson } from '../../jsonFile'++/** The news server, with what only the main process may see: its password. */+export interface UsenetServerConfig extends Omit<UsenetServer, 'hasPassword' | 'passwordInMemory'> {+ password: string+}++/** What providers allow: rarely past 50 connections an account. */+export const MAX_CONNECTIONS = 100+export const DEFAULT_CONNECTIONS = 8++function serverPath(): string {+ return join(app.getPath('userData'), 'usenet-server.json')+}++/** The password, when this computer has no keychain to seal it with: kept until Plexo quits. */+let inMemory: string | undefined++/** As for the browser's cookies (see sealBrowser in downloadManager.ts): never written in the+ * clear, and on Linux without a keyring, not written at all. */+function canSeal(): boolean {+ if (!safeStorage.isEncryptionAvailable()) return false+ return !(process.platform === 'linux' && safeStorage.getSelectedStorageBackend() === 'basic_text')+}++const isRecord = (value: unknown): value is Record<string, unknown> =>+ typeof value === 'object' && value !== null && !Array.isArray(value)++/** Only fields of the right type and range: the file may be hand-edited, the input is the+ * window's. A host is all that's needed for the rest to make sense. */+export function checkedServer(value: unknown): Omit<UsenetServerConfig, 'password'> | null {+ if (!isRecord(value)) return null+ const host = typeof value.host === 'string' ? value.host.trim() : ''+ // A host name or an address, nothing a command line or URL could make more of.+ if (!host || !/^[\w.:[\]-]+$/.test(host) || host.length > 253) return null+ const tls = value.tls !== false+ const port = Number(value.port)+ const connections = Number(value.connections)+ const username = typeof value.username === 'string' ? value.username.trim() : ''+ if (/[\r\n]/.test(username)) return null+ return {+ host: host.replace(/^\[|\]$/g, ''),+ port: Number.isInteger(port) && port > 0 && port < 65536 ? port : tls ? 563 : 119,+ tls,+ username,+ connections:+ Number.isInteger(connections) && connections >= 1+ ? Math.min(connections, MAX_CONNECTIONS)+ : DEFAULT_CONNECTIONS+ }+}++/** The news server set up, password included; null when there's none. */+export async function loadUsenetServer(): Promise<UsenetServerConfig | null> {+ const saved = await readJson(serverPath()).catch(() => undefined)+ const server = checkedServer(saved)+ if (!server || !isRecord(saved)) return null+ let password = inMemory ?? ''+ if (typeof saved.password === 'string' && canSeal()) {+ try {+ password = safeStorage.decryptString(Buffer.from(saved.password, 'base64'))+ } catch {+ // Sealed by another keychain (a restored profile, say): it has to be entered again.+ }+ }+ return { ...server, password }+}++/** As the window may see it. */+export function viewOf(server: UsenetServerConfig): UsenetServer {+ const { password, ...rest } = server+ return { ...rest, hasPassword: password !== '', ...(!canSeal() && { passwordInMemory: true }) }+}++/** The server as `input` sets it: the password it gives, or the one saved when it gives none. */+export async function serverFromInput(input: UsenetServerInput): Promise<UsenetServerConfig> {+ const server = checkedServer(input)+ if (!server) throw new Error('Enter the news server’s address')+ if (input.password !== undefined && /[\r\n]/.test(input.password)) {+ throw new Error('A password can’t span lines')+ }+ const password = input.password ?? (await loadUsenetServer())?.password ?? ''+ return { ...server, password }+}++export async function saveUsenetServer(input: UsenetServerInput): Promise<UsenetServer> {+ const server = await serverFromInput(input)+ const { password, ...rest } = server+ let sealed: string | undefined+ if (canSeal() && password) {+ try {+ sealed = safeStorage.encryptString(password).toString('base64')+ } catch {+ // The keychain refused: kept for this session instead.+ }+ }+ inMemory = sealed === undefined ? password || undefined : undefined+ await updateJson(serverPath(), () => ({ ...rest, ...(sealed && { password: sealed }) }))+ return viewOf(server)+}diff --git a/src/main/download/nzb/yenc.ts b/src/main/download/nzb/yenc.tsnew file mode 100644index 0000000..cf935f5--- /dev/null+++ b/src/main/download/nzb/yenc.ts@@ -0,0 +1,122 @@+import { crc32 } from 'node:zlib'++/** An article's body that isn't the yEnc it should be, or whose bytes don't match its checksum:+ * the article is damaged, whichever server sent it. */+export class YencError extends Error {}++/** One decoded article: part of a file, and where in that file it goes. */+export interface YencPart {+ /** The file's name, as the poster's yEnc header gives it. */+ name: string+ /** The whole file's size. */+ fileSize: number+ /** Where `data` goes in the file, from its first byte. */+ offset: number+ data: Buffer+}++const CR = 0x0d+const LF = 0x0a+const ESCAPE = 0x3d // '='++/** A header line's `key=value` fields. `name` runs to the end of the line, spaces and all, so+ * it is taken apart from the rest. */+function fields(line: string): Map<string, string> {+ const found = new Map<string, string>()+ const nameAt = line.search(/\bname=/)+ const head = nameAt >= 0 ? line.slice(0, nameAt) : line+ if (nameAt >= 0) found.set('name', line.slice(nameAt + 5).trim())+ for (const match of head.matchAll(/(\w+)=(\S*)/g)) found.set(match[1], match[2])+ return found+}++function count(value: string | undefined, what: string): number {+ const parsed = Number(value)+ if (value === undefined || !Number.isSafeInteger(parsed) || parsed < 0) {+ throw new YencError(`This article's yEnc header has no valid ${what}`)+ }+ return parsed+}++/** The CRC-32 a trailer gives, if it gives one: some posters leave it out. */+function checksum(value: string | undefined): number | undefined {+ if (value === undefined || !/^[0-9a-f]{1,8}$/i.test(value)) return undefined+ return parseInt(value, 16)+}++/**+ * Decodes one article's body (already dot-unstuffed, see nntp.ts) as yEnc: the `=ybegin` line,+ * `=ypart` for an article that is part of a larger file, the data lines, and `=yend`. Anything+ * before `=ybegin` is ignored, as decoders do. The data's length and CRC-32 are checked against+ * the trailer, so a damaged article is never written as if it were good.+ */+export function decodeYenc(body: Buffer): YencPart {+ let position = 0+ const nextLine = (): Buffer | null => {+ if (position >= body.length) return null+ let end = body.indexOf(LF, position)+ if (end < 0) end = body.length+ const line = body.subarray(position, end > position && body[end - 1] === CR ? end - 1 : end)+ position = end + 1+ return line+ }++ let line = nextLine()+ while (line && !line.subarray(0, 8).equals(Buffer.from('=ybegin '))) line = nextLine()+ if (!line) throw new YencError('This article isn’t yEnc-encoded')+ // The name is the poster's bytes: UTF-8 when it's anything readable at all.+ const begin = fields(line.toString('utf8'))+ const fileSize = count(begin.get('size'), 'file size')+ const name = begin.get('name') ?? ''++ let offset = 0+ let expected = fileSize+ if (begin.has('part')) {+ const partLine = nextLine()+ if (!partLine?.subarray(0, 7).equals(Buffer.from('=ypart '))) {+ throw new YencError('This article’s yEnc part header is missing')+ }+ const part = fields(partLine.toString('latin1'))+ const first = count(part.get('begin'), 'part start')+ const last = count(part.get('end'), 'part end')+ if (first < 1 || last < first || last > fileSize) {+ throw new YencError('This article’s yEnc part lies outside its file')+ }+ offset = first - 1+ expected = last - first + 1+ }++ // Decoded data is never longer than what encodes it.+ const data = Buffer.allocUnsafe(Math.min(expected, body.length - position))+ let length = 0+ let trailer: Map<string, string> | null = null+ while ((line = nextLine()) !== null) {+ if (line.length >= 5 && line.subarray(0, 5).equals(Buffer.from('=yend'))) {+ trailer = fields(line.toString('latin1'))+ break+ }+ for (let i = 0; i < line.length; i++) {+ let byte = line[i]+ if (byte === ESCAPE) {+ // An escape as a line's last byte escapes nothing: posters' encoders shouldn't, but some do.+ if (++i >= line.length) break+ byte = line[i] - 64+ }+ if (length >= data.length) throw new YencError('This article holds more data than it says')+ data[length++] = (byte - 42) & 0xff+ }+ }+ if (!trailer) throw new YencError('This article ends before its yEnc trailer')+ if (+ length !== expected ||+ (trailer.has('size') && count(trailer.get('size'), 'size') !== length)+ ) {+ throw new YencError('This article’s data is shorter or longer than it says')+ }+ const decoded = data.subarray(0, length)+ const expectedCrc = checksum(begin.has('part') ? trailer.get('pcrc32') : trailer.get('crc32'))+ if (expectedCrc !== undefined && crc32(decoded) >>> 0 !== expectedCrc >>> 0) {+ throw new YencError('This article’s data doesn’t match its checksum')+ }+ return { name, fileSize, offset, data: decoded }+}diff --git a/src/main/download/probe.ts b/src/main/download/probe.tsindex f1103d2..064ba72 100644--- a/src/main/download/probe.ts+++ b/src/main/download/probe.ts@@ -1,11 +1,12 @@import { request as httpRequest } from 'node:http'import { request as httpsRequest } from 'node:https'-import { isAbsolute } from 'node:path'+import { basename, isAbsolute } from 'node:path'import { URL } from 'node:url'import type { BrowserContext, ProbeResult } from '../../shared/types'import { testKnobs } from '../testKnobs'import { requestHeaders } from './browserContext'import { parseContentDispositionFilename } from './contentDisposition'+import { describeNzb, downloadNzbFile, indexerError, readNzbFile, sniffXml } from './nzb/nzbFile'import { normalizeEtag } from './fileVersion'import {describeTorrent,@@ -159,14 +160,29 @@ function isTorrentLink(headers: Headers, url: URL): boolean {return /^application\/x-bittorrent\b/i.test(contentType) || /\.torrent$/i.test(url.pathname)}-/** What a link would download: a file over HTTP(S), or a torrent — a magnet link, a link to a- * .torrent, or the path of a .torrent on this computer (opened or dropped on the window).- * `browser`: what the browser it came from sends with it (see BrowserContext). */+/** A link the server says is an NZB, or that is named like one. Indexers serve them from API+ * links that say nothing in their path, so the type is what usually tells. */+function isNzbLink(headers: Headers, url: URL, fileName: string): boolean {+ const contentType = headerValue(headers, 'content-type') ?? ''+ return (+ /^application\/x-nzb\b/i.test(contentType) ||+ /\.nzb$/i.test(url.pathname) ||+ /\.nzb$/i.test(fileName)+ )+}++/** What a link would download: a file over HTTP(S), a torrent — a magnet link, a link to a+ * .torrent, or the path of a .torrent on this computer (opened or dropped on the window) — or an+ * NZB, by link or path, the same way. `browser`: what the browser it came from sends with it (see+ * BrowserContext). */export async function probeUrl(rawUrl: string, browser?: BrowserContext): Promise<ProbeResult> {if (/^magnet:/i.test(rawUrl)) return describeTorrent(await fetchMagnetMetadata(rawUrl), rawUrl)if (isAbsolute(rawUrl) && /\.torrent$/i.test(rawUrl)) {return describeTorrent(await readTorrentFile(rawUrl), rawUrl)}+ if (isAbsolute(rawUrl) && /\.nzb$/i.test(rawUrl)) {+ return describeNzb(await readNzbFile(rawUrl), rawUrl, basename(rawUrl))+ }const { current, response } = await requestFollowingRedirects(rawUrl, browser)@@ -204,6 +220,30 @@ export async function probeUrl(rawUrl: string, browser?: BrowserContext): Promisreturn describeTorrent(torrentFile, finalUrl)}+ const suggestedFileName = fileNameFromHeaders(response.headers, current)+ if (isNzbLink(response.headers, current, suggestedFileName)) {+ const finalUrl = current.toString()+ const nzbFile = await downloadNzbFile(+ finalUrl,+ PROBE_TIMEOUT_MS,+ requestHeaders(current, browser)+ )+ return describeNzb(nzbFile, finalUrl, suggestedFileName)+ }+ // An indexer's API answers in XML either way: an NZB it doesn't label as one, or an error (a+ // wrong API key, its daily limit reached) where the NZB would be. Its start tells which.+ if (/xml/i.test(headerValue(response.headers, 'content-type') ?? '')) {+ const finalUrl = current.toString()+ const headers = requestHeaders(current, browser)+ const start = await sniffXml(finalUrl, PROBE_TIMEOUT_MS, headers)+ const refusal = indexerError(start)+ if (refusal) throw new Error(refusal)+ if (/<(?:[\w-]+:)?nzb\b/i.test(start)) {+ const nzbFile = await downloadNzbFile(finalUrl, PROBE_TIMEOUT_MS, headers)+ return describeNzb(nzbFile, finalUrl, suggestedFileName)+ }+ }+const contentRange = headerValue(response.headers, 'content-range')// A server that supports range requests must answer our range GET with 206 Partial Content.// If it returned 200 OK, it ignored the Range header and sent the whole file — even if its headers@@ -230,7 +270,7 @@ export async function probeUrl(rawUrl: string, browser?: BrowserContext): PromisfinalUrl: current.toString(),supportsRanges,totalBytes,- suggestedFileName: fileNameFromHeaders(response.headers, current),+ suggestedFileName,contentType: headerValue(response.headers, 'content-type') ?? null,etag: perRange ? null : (headerValue(response.headers, 'etag') ?? null),lastModified: headerValue(response.headers, 'last-modified') ?? nulldiff --git a/src/main/download/savedProgress.ts b/src/main/download/savedProgress.tsindex ac991b5..63c3566 100644--- a/src/main/download/savedProgress.ts+++ b/src/main/download/savedProgress.ts@@ -1,4 +1,9 @@-import type { DownloadUnitState, HttpBlockState, TorrentPieceState } from '../../shared/types'+import type {+ DownloadUnitState,+ HttpBlockState,+ NzbSegmentState,+ TorrentPieceState+} from '../../shared/types'import { planBlocks, planPieces } from './plan'/** How a download's HTTP blocks or torrent pieces are checkpointed. Their ranges follow from the@@ -95,3 +100,50 @@ export function restorePieces(}return pieces}++/**+ * An NZB download's articles as planned from its NZB (`planned`), with the progress saved for+ * them. An article only ever counts whole: one that was coming in when the download stopped+ * starts over. `missing`: the articles the server didn't have, by index. Undefined when the NZB+ * doesn't match the download saved.+ */+export function restoreSegments(+ planned: NzbSegmentState[],+ state: { totalBytes: number; totalBlocks: number },+ saved: SavedBlocks,+ missing: unknown+): NzbSegmentState[] | undefined {+ const length = (segment: NzbSegmentState): number => segment.rangeEnd! - segment.rangeStart + 1+ const total = planned.reduce((sum, segment) => sum + length(segment), 0)+ if (planned.length !== state.totalBlocks || total !== state.totalBytes) return undefined++ const columns =+ typeof saved.progress === 'object' && saved.progress !== null+ ? Object.entries(saved.progress)+ : []+ for (const [networkId, column] of columns) {+ if (!Array.isArray(column) || column.length !== planned.length || !column.every(isCount)) {+ continue+ }+ column.forEach((bytes, index) => {+ if (bytes === 0) return+ planned[index].bytesByInterface[networkId] = bytes+ planned[index].bytesDownloaded += bytes+ })+ }+ for (const segment of planned) {+ if (segment.bytesDownloaded === length(segment)) {+ segment.status = 'completed'+ } else {+ segment.bytesDownloaded = 0+ segment.bytesByInterface = {}+ }+ }+ if (Array.isArray(missing)) {+ for (const index of missing) {+ const segment = isCount(index) ? planned[index] : undefined+ if (segment?.status === 'pending') segment.status = 'missing'+ }+ }+ return planned+}diff --git a/src/main/download/transfer.ts b/src/main/download/transfer.tsindex 974a375..6a91b54 100644--- a/src/main/download/transfer.ts+++ b/src/main/download/transfer.ts@@ -4,8 +4,11 @@ import type {DownloadUnitState,HttpBlockState,HttpDownloadState,+ NzbDownloadState,+ NzbSegmentState,ProxyConfig,StartHttpDownloadRequest,+ StartNzbDownloadRequest,StartTorrentDownloadRequest,TorrentDownloadState,TorrentPieceState@@ -13,10 +16,12 @@ import type {import type { NetworkMonitor } from '../network/interfaces'import type { Limits } from '../network/limits'import type { DownloadFile } from './downloadFile'+import type { NzbDestination } from './nzb/nzbDestination'import type { TorrentDestination } from './torrent/torrentDestination'/**- * How a download's bytes get fetched: HTTP streams today (see httpTransfer.ts). The manager runs+ * How a download's bytes get fetched: HTTP streams (see httpTransfer.ts), torrent peers+ * (torrent/torrentTransfer.ts) or connections to a news server (nzb/nzbTransfer.ts). The manager runs* everything a download does whatever fetches it — its networks, pause and resume, saving it,* telling the window — and hands the fetching to its transfer.*/@@ -66,7 +71,16 @@ export interface TorrentTransferTarget extends TransferTargetBase {uploadMeters: Meters}-export type TransferTarget = HttpTransferTarget | TorrentTransferTarget+export interface NzbTransferTarget extends TransferTargetBase {+ file: NzbDestination+ state: NzbDownloadState+ requestPayload: StartNzbDownloadRequest+ blocks: NzbSegmentState[]+ /** What each connection, and each network, has received (see updateSpeeds). */+ meters: Meters+}++export type TransferTarget = HttpTransferTarget | TorrentTransferTarget | NzbTransferTarget/** What a transfer asks of the manager. */export interface TransferHost {@@ -150,7 +164,7 @@ export function updateSpeeds(runtime: TransferTarget, now: number): boolean {target[key] = value}const connections: Array<{ id: number; status: string; speedBytesPerSec: number }> =- state.kind === 'http' ? state.streams : state.peers+ state.kind === 'torrent' ? state.peers : state.streamsfor (const connection of connections) {// Read every tick, moving or not, so its window is never stale when it moves again.const speed = runtime.meters.connections.get(connection.id)?.read(now) ?? 0@@ -202,10 +216,20 @@ export function smoothTimeLeft(return expected + (estimate - expected) * (1 - (1 - share) ** elapsedSeconds)}+/** Of a download's bytes, those it won't fetch: a torrent's pieces no chosen file needs, an+ * NZB's articles the server doesn't have. */+export function unwantedBytes(state: DownloadState): number {+ return state.kind === 'torrent'+ ? state.skippedBytes+ : state.kind === 'nzb'+ ? state.missingBytes+ : 0+}+/** Brings time left up to date from the speed, `elapsedSeconds` after it last was. Read on the* download's clock, with the speeds. Says whether it changed. */export function updateTimeLeft(state: DownloadState, elapsedSeconds: number): boolean {- const wanted = state.totalBytes - (state.kind === 'torrent' ? state.skippedBytes : 0)+ const wanted = state.totalBytes - unwantedBytes(state)const before = state.timeLeftSecondsstate.timeLeftSeconds =state.totalBytes > 0 && state.speedBytesPerSec > 0@@ -222,7 +246,7 @@ export function updateTimeLeft(state: DownloadState, elapsedSeconds: number): boexport function clearSpeeds(state: DownloadState): void {state.speedBytesPerSec = 0state.timeLeftSeconds = undefined- const connections = state.kind === 'http' ? state.streams : state.peers+ const connections = state.kind === 'torrent' ? state.peers : state.streamsfor (const connection of connections) connection.speedBytesPerSec = 0for (const network of state.networks) network.speedBytesPerSec = 0if (state.kind === 'http') state.diskLimited = undefineddiff --git a/src/main/download/trashDownload.ts b/src/main/download/trashDownload.tsindex 93ffeeb..a0744af 100644--- a/src/main/download/trashDownload.ts+++ b/src/main/download/trashDownload.ts@@ -3,19 +3,20 @@ import { shell } from 'electron'import type { DownloadState, FinishedDownload } from '../../shared/types'import { ownedTorrentFiles } from './torrent/ownedFiles'-/** Trash only owned torrent files; never move unrelated files added to its folder. */+/** Trash only a torrent's (or an NZB's) own files; never move unrelated files added to its+ * folder. */export async function trashDownload(download: DownloadState | FinishedDownload,ownedFiles?: readonly string[]): Promise<void> {- if (download.kind !== 'torrent' || !download.folder) {+ if (download.kind === 'http' || (download.kind === 'torrent' && !download.folder)) {await shell.trashItem(download.destinationPath)return}const files = ownedFiles ?? ('downloadedFiles' in download ? download.downloadedFiles : undefined)if (!files?.length) {throw new Error(- 'Plexo can’t identify the files in this older torrent. Remove it from the list or manage its files in your file browser.'+ 'Plexo can’t identify this download’s files. Remove it from the list or manage its files in your file browser.')}const { targets, folders } = await ownedTorrentFiles(download.destinationPath, files)diff --git a/src/main/ipc/handlers.ts b/src/main/ipc/handlers.tsindex 7fe3515..ac2163d 100644--- a/src/main/ipc/handlers.ts+++ b/src/main/ipc/handlers.ts@@ -21,6 +21,13 @@ import { DownloadManager } from '../download/downloadManager'import { getDefaultDownloadsDir, getHomeDir } from '../download/paths'import { listHistory } from '../download/history'import { probeUrl } from '../download/probe'+import { testUsenetServer } from '../download/nzb/nntp'+import {+ loadUsenetServer,+ saveUsenetServer,+ serverFromInput,+ viewOf+} from '../download/nzb/usenetServer'import { onMagnetCheckStatus } from '../download/torrent/metadata'import { deviceBindingSupported } from '../network/deviceBinding'import { pendingLink, pendingLinks, releaseLink } from '../openLinks'@@ -175,12 +182,16 @@ export function registerIpcHandlers(getWindow: () => BrowserWindow | null): Downreturn result.filePaths[0]})- handle('chooseTorrentFile', async () => {+ handle('chooseDownloadFile', async () => {const window = getWindow()if (!window) return nullconst result = await dialog.showOpenDialog(window, {properties: ['openFile'],- filters: [{ name: 'Torrent', extensions: ['torrent'] }]+ filters: [+ { name: 'Torrent or NZB', extensions: ['torrent', 'nzb'] },+ { name: 'Torrent', extensions: ['torrent'] },+ { name: 'NZB', extensions: ['nzb'] }+ ]})if (result.canceled || result.filePaths.length === 0) return nullreturn result.filePaths[0]@@ -250,6 +261,15 @@ export function registerIpcHandlers(getWindow: () => BrowserWindow | null): Downmanager.remove(id, options?.trashFile === true))+ handle('usenetServer', async () => {+ const server = await loadUsenetServer()+ return server && viewOf(server)+ })+ handle('saveUsenetServer', async (_event, input) => saveUsenetServer(input))+ handle('testUsenetServer', async (_event, input) =>+ testUsenetServer(await serverFromInput(input))+ )+handle('updateState', () => updateState())handle('showUpdateMenu', () => showUpdateMenu())diff --git a/src/main/network/proxyTunnel.ts b/src/main/network/proxyTunnel.tsindex 5ee444a..3a0fd23 100644--- a/src/main/network/proxyTunnel.ts+++ b/src/main/network/proxyTunnel.ts@@ -17,23 +17,30 @@ function readExactBytes(let buffer = Buffer.alloc(0)const cleanup = (): void => {- socket.removeListener('data', onData)+ socket.removeListener('readable', onReadable)socket.removeListener('error', onError)socket.removeListener('close', onClose)socket.removeListener('timeout', onTimeout)signal?.removeEventListener('abort', onAbort)}- const onData = (chunk: Buffer): void => {- buffer = Buffer.concat([buffer, chunk])- if (buffer.length >= length) {- cleanup()- const result = buffer.subarray(0, length)- const remainder = buffer.subarray(length)- if (remainder.length > 0) {- socket.unshift(remainder)+ // Paused-mode reads: once the listener is gone, what follows stays buffered for whoever reads+ // the tunnel next. A 'data' listener would leave the socket flowing with no one listening, and+ // drop what a server sends first (an NNTP greeting).+ const onReadable = (): void => {+ let chunk: Buffer | null+ while ((chunk = socket.read() as Buffer | null) !== null) {+ buffer = Buffer.concat([buffer, chunk])+ if (buffer.length >= length) {+ cleanup()+ const result = buffer.subarray(0, length)+ const remainder = buffer.subarray(length)+ if (remainder.length > 0) {+ socket.unshift(remainder)+ }+ resolve(result)+ return}- resolve(result)}}@@ -65,7 +72,7 @@ function readExactBytes(}signal?.addEventListener('abort', onAbort, { once: true })- socket.on('data', onData)+ socket.on('readable', onReadable)socket.once('error', onError)socket.once('close', onClose)socket.once('timeout', onTimeout)@@ -81,26 +88,31 @@ function readHttpHeaders(let buffer = Buffer.alloc(0)const cleanup = (): void => {- socket.removeListener('data', onData)+ socket.removeListener('readable', onReadable)socket.removeListener('error', onError)socket.removeListener('close', onClose)socket.removeListener('timeout', onTimeout)signal?.removeEventListener('abort', onAbort)}- const onData = (chunk: Buffer): void => {- buffer = Buffer.concat([buffer, chunk])- const delimiterIndex = buffer.indexOf('\r\n\r\n')- if (delimiterIndex !== -1) {- cleanup()- const headerText = buffer.subarray(0, delimiterIndex).toString('utf-8')- const remainder = buffer.subarray(delimiterIndex + 4)- if (remainder.length > 0) {- socket.unshift(remainder)+ // Paused-mode reads, as in readExactBytes.+ const onReadable = (): void => {+ let chunk: Buffer | null+ while ((chunk = socket.read() as Buffer | null) !== null) {+ buffer = Buffer.concat([buffer, chunk])+ const delimiterIndex = buffer.indexOf('\r\n\r\n')+ if (delimiterIndex !== -1) {+ cleanup()+ const headerText = buffer.subarray(0, delimiterIndex).toString('utf-8')+ const remainder = buffer.subarray(delimiterIndex + 4)+ if (remainder.length > 0) {+ socket.unshift(remainder)+ }+ const statusMatch = /^HTTP\/\d\.\d\s+(\d+)/i.exec(headerText)+ const status = statusMatch ? parseInt(statusMatch[1], 10) : 0+ resolve({ headers: headerText, status })+ return}- const statusMatch = /^HTTP\/\d\.\d\s+(\d+)/i.exec(headerText)- const status = statusMatch ? parseInt(statusMatch[1], 10) : 0- resolve({ headers: headerText, status })}}@@ -132,7 +144,7 @@ function readHttpHeaders(}signal?.addEventListener('abort', onAbort, { once: true })- socket.on('data', onData)+ socket.on('readable', onReadable)socket.once('error', onError)socket.once('close', onClose)socket.once('timeout', onTimeout)diff --git a/src/main/network/routes.ts b/src/main/network/routes.tsindex 7efc6f8..764babe 100644--- a/src/main/network/routes.ts+++ b/src/main/network/routes.ts@@ -8,6 +8,7 @@ import {} from 'node:http'import { Agent as HttpsAgent, request as httpsRequest } from 'node:https'import { isIP, type Socket } from 'node:net'+import { connect as tlsConnect } from 'node:tls'import type { Duplex } from 'node:stream'import type { IpFamily, NetworkInterfaceInfo, ProxyConfig } from '../../shared/types'import { connectRoute } from './deviceBinding'@@ -346,6 +347,27 @@ async function connectOnInterface(})}+/**+ * A socket to `host` through `iface` for a protocol other than HTTP (NNTP, see nzb/nntp.ts):+ * raced over the network's routes as an HTTP stream's is, with TLS on top when `tls` is set,+ * verified against `host`, and through the network's `proxy` when one is set.+ */+export function connectThrough(+ iface: NetworkInterfaceInfo,+ host: string,+ port: number,+ tls: boolean,+ timeoutMs: number,+ signal: AbortSignal,+ proxy?: ProxyConfig+): Promise<Socket> {+ const secure = tls+ ? (socket: Socket): Socket =>+ tlsConnect({ socket, servername: isIP(host) ? undefined : host, host })+ : null+ return connectOnInterface(iface, host, port, secure, timeoutMs, signal, undefined, proxy)+}+/** What a request passes its agent: Node hands a request's options to the agent's* createConnection, so a connection opened for a request can be dropped with it. */type ConnectOptions = ClientRequestArgs & { connectSignal?: AbortSignal }diff --git a/src/main/openLinks.ts b/src/main/openLinks.tsindex 99424f4..4ca09a4 100644--- a/src/main/openLinks.ts+++ b/src/main/openLinks.ts@@ -5,8 +5,8 @@ import { app, type BrowserWindow } from 'electron'import { IpcChannels } from '../shared/ipc-channels'import type { BrowserContext, PendingLink } from '../shared/types'-// Links the OS hands Plexo (a magnet link clicked in a browser, a .torrent opened from the file-// manager) or the browser extension does. They're never started on their own: the user still sees+// Links the OS hands Plexo (a magnet link clicked in a browser, a .torrent or .nzb opened from the+// file manager) or the browser extension does. They're never started on their own: the user still sees// what the link is and presses Start./** The browser's sign-in stays in main: the window never sees it. */@@ -18,10 +18,11 @@ export interface Offer extends Omit<PendingLink, 'id'> {// already dropped its copy, and the window's own list is lost when the window closes or reloads.const links = new Map<string, Offer>()-/** A magnet link, or the path of a .torrent file that exists; anything else isn't taken. */+/** A magnet link, or the path of a .torrent or .nzb file that exists; anything else isn't+ * taken. */export function acceptedLink(candidate: string): string | null {if (/^magnet:\?/i.test(candidate)) return candidate- if (!isAbsolute(candidate) || !/\.torrent$/i.test(candidate)) return null+ if (!isAbsolute(candidate) || !/\.(torrent|nzb)$/i.test(candidate)) return nulltry {return statSync(candidate).isFile() ? candidate : null} catch {diff --git a/src/preload/index.ts b/src/preload/index.tsindex 504f47e..fa5b176 100644--- a/src/preload/index.ts+++ b/src/preload/index.ts@@ -7,7 +7,8 @@ import type {InitialState,MagnetCheckStatus,NetworkInterfaceInfo,- UpdateState+ UpdateState,+ UsenetServerInput} from '../shared/types'/** Typed wrapper around ipcRenderer.invoke — the channel name picks its args/result shape out of@@ -31,7 +32,7 @@ const plexoApi = {updateSettings: (patch: AppSettings) => invoke('updateSettings', patch),probeUrl: (url: string) => invoke('probeUrl', url),chooseDestinationFolder: (defaultPath: string) => invoke('chooseDestinationFolder', defaultPath),- chooseTorrentFile: () => invoke('chooseTorrentFile'),+ chooseDownloadFile: () => invoke('chooseDownloadFile'),/** Where a file dropped on the window is on disk ('' for one that isn't a file). */pathForFile: (file: File) => webUtils.getPathForFile(file),readClipboardText: () => invoke('readClipboardText'),@@ -62,6 +63,9 @@ const plexoApi = {resumeFromLink: (linkId: string, downloadId: string) =>invoke('resumeFromLink', linkId, downloadId),dismissLink: (id: string) => invoke('dismissLink', id),+ usenetServer: () => invoke('usenetServer'),+ saveUsenetServer: (server: UsenetServerInput) => invoke('saveUsenetServer', server),+ testUsenetServer: (server: UsenetServerInput) => invoke('testUsenetServer', server),/** pendingLinks() has the new link. */onLinkReceived: (callback: () => void): (() => void) => {diff --git a/src/renderer/src/components/BlockGrid.tsx b/src/renderer/src/components/BlockGrid.tsxindex 88c0725..77376d3 100644--- a/src/renderer/src/components/BlockGrid.tsx+++ b/src/renderer/src/components/BlockGrid.tsx@@ -1,4 +1,4 @@-import type { DownloadUnitState, TorrentPieceStatus } from '@shared/types'+import type { DownloadUnitState } from '@shared/types'import { cn } from 'cn'import { ArrowDown, ArrowUp } from 'lucide-react'import { useCallback, useRef, useState } from 'react'@@ -48,7 +48,7 @@ interface CellSegment {}interface DisplayCell {- status: TorrentPieceStatus+ status: DownloadUnitState['status']/** The network that delivered the most bytes here — what the cell reads out as, and what* colors it when its share is drawn as a single block. */interfaceId?: string@@ -162,6 +162,8 @@ interface BlockGridProps {isPaused?: boolean/** A torrent's grid: its blocks are the torrent's pieces. */pieces?: boolean+ /** An NZB's grid: its blocks are Usenet articles, of varying size. */+ articles?: boolean}export function BlockGrid({@@ -171,9 +173,10 @@ export function BlockGrid({knownSize,remainingBytes,isPaused = false,- pieces = false+ pieces = false,+ articles = false}: BlockGridProps): React.JSX.Element {- const unit = pieces ? 'Piece' : 'Chunk'+ const unit = pieces ? 'Piece' : articles ? 'Article' : 'Chunk'const [gridWidth, setGridWidth] = useState(0)const [hoveredIndex, setHoveredIndex] = useState<number | null>(null)const observerRef = useRef<ResizeObserver | null>(null)@@ -207,8 +210,14 @@ export function BlockGrid({})const orderedInterfaceIds = groups.map((g) => g.id)+ const lastEnd = blocks.at(-1)?.rangeEnd ?? null+ // An NZB's articles differ in size (each file's last is shorter): what one is on average.const chunkBytes =- blocks[0].rangeEnd !== null ? blocks[0].rangeEnd - blocks[0].rangeStart + 1 : 0+ articles && lastEnd !== null+ ? Math.round((lastEnd + 1) / blocks.length)+ : blocks[0].rangeEnd !== null+ ? blocks[0].rangeEnd - blocks[0].rangeStart + 1+ : 0const rows = Math.ceil(blocks.length / cols)const visibleRows = Math.min(rows, MAX_VISIBLE_ROWS)// Only the rows in view (and a few either side) are drawn: a big torrent has hundreds of@@ -268,14 +277,16 @@ export function BlockGrid({? 'queued': hoveredCell.status === 'skipped'? 'skipped: in no file chosen'- : '—')+ : hoveredCell.status === 'missing'+ ? 'missing on the server: needs a par2 repair'+ : '—')const provisional =hoveredCell.provisionalBytes > 0? ` · ${formatBytes(hoveredCell.provisionalBytes)} received, awaiting verification`: ''readout = `${unit} #${hoveredCell.chunkNumber} · ${formatBytes(hoveredCell.bytesDownloaded)} verified${provisional} / ${formatBytes(hoveredCell.totalBytes)} · ${where}`} else {- readout = `${blocks.length} ${unit.toLowerCase()}s · ${formatBytes(chunkBytes)} each`+ readout = `${blocks.length} ${unit.toLowerCase()}s · ${articles ? 'about ' : ''}${formatBytes(chunkBytes)} each`}return (@@ -382,6 +393,10 @@ export function BlockGrid({// None of the files chosen needs it: there, but not this download's.border = '0.5px dashed var(--border-strong)'opacity = 0.3+ } else if (cell.status === 'missing') {+ // The server didn't have it: a hole in its file, for a par2 repair to fill.+ background = 'color-mix(in srgb, var(--color-danger) 22%, transparent)'+ border = '1px dashed var(--color-danger)'}const rawFillPercent = Math.min(1, Math.max(0, cell.fillRatio)) * 100diff --git a/src/renderer/src/components/NetworkRow.tsx b/src/renderer/src/components/NetworkRow.tsxindex 5d3a501..c325f6b 100644--- a/src/renderer/src/components/NetworkRow.tsx+++ b/src/renderer/src/components/NetworkRow.tsx@@ -1,4 +1,10 @@-import type { HttpBlockState, HttpStreamState, NetworkStatus, SpeedUnit } from '@shared/types'+import type {+ HttpBlockState,+ HttpStreamState,+ NetworkStatus,+ NzbSegmentState,+ SpeedUnit+} from '@shared/types'import { useState } from 'react'import { useAppStore } from '../store/useAppStore'import { DANGER, type NetworkVisual } from '../theme'@@ -17,7 +23,9 @@ interface NetworkRowProps {visual: NetworkVisualsharePercent: numbertotalBytes?: number | null- blocks?: HttpBlockState[]+ blocks?: (HttpBlockState | NzbSegmentState)[]+ /** An NZB download's: its streams are connections to the news server, its blocks articles. */+ usenet?: booleanonSwitch: (enabled: boolean) => void}@@ -77,9 +85,9 @@ function ProgressBar({function streamProgress(stream: HttpStreamState,- blocks?: HttpBlockState[]+ blocks?: (HttpBlockState | NzbSegmentState)[]): {- block: HttpBlockState | undefined+ block: HttpBlockState | NzbSegmentState | undefineddone: booleansize: numberdownloaded: number@@ -101,13 +109,16 @@ function streamProgress(function HttpStreamRows({group,visual,- blocks+ blocks,+ usenet = false}: {group: HttpNetworkGroupvisual: NetworkVisual- blocks?: HttpBlockState[]+ blocks?: (HttpBlockState | NzbSegmentState)[]+ usenet?: boolean}): React.JSX.Element {const formatSpeed = useFormatSpeed()+ const [name, unit] = usenet ? ['Connection', 'Article'] : ['Stream', 'Chunk']return (<>{group.streams.map((stream, index) => {@@ -137,11 +148,11 @@ function HttpStreamRows({</div><div role="cell" className="flex min-w-0 items-center gap-[6px]"><span className="font-medium whitespace-nowrap text-foreground">- Stream #{index + 1}+ {name} #{index + 1}</span>{progress.block && (<span className="rounded-[3px] border-[0.5px] border-border bg-secondary px-[4.5px] py-[1.5px] text-[9px] leading-none whitespace-nowrap text-muted-foreground">- Chunk #{progress.block.index + 1}+ {unit} #{progress.block.index + 1}</span>)}{/* A lit dot and a speed say it's fetching; only what isn't the usual gets a word. */}@@ -178,7 +189,7 @@ function HttpStreamRows({</div><ProgressBarclassName="h-[5px]"- label={`Stream #${index + 1} progress`}+ label={`${name} #${index + 1} progress`}percent={progress.percent}color={visual.solid}/>@@ -373,6 +384,7 @@ export function NetworkRow({sharePercent,totalBytes,blocks,+ usenet = false,onSwitch}: NetworkRowProps): React.JSX.Element {const formatSpeed = useFormatSpeed()@@ -387,7 +399,7 @@ export function NetworkRow({const isProxyActive = Boolean(proxy?.enabled && proxy.host)const rounded = Math.round(sharePercent)const shareLabel = group.bytesDownloaded === 0 ? '0%' : rounded === 0 ? '<1%' : `${rounded}%`- const noun = group.transfer === 'http' ? 'stream' : 'peer'+ const noun = group.transfer === 'torrent' ? 'peer' : usenet ? 'connection' : 'stream'return (<><div@@ -526,7 +538,7 @@ export function NetworkRow({</div>{expanded &&(group.transfer === 'http' ? (- <HttpStreamRows group={group} visual={visual} blocks={blocks} />+ <HttpStreamRows group={group} visual={visual} blocks={blocks} usenet={usenet} />) : (<PeerRows group={group} visual={visual} />))}diff --git a/src/renderer/src/components/NewDownloadDialog.tsx b/src/renderer/src/components/NewDownloadDialog.tsxindex 8de891f..fc56baf 100644--- a/src/renderer/src/components/NewDownloadDialog.tsx+++ b/src/renderer/src/components/NewDownloadDialog.tsx@@ -1,4 +1,4 @@-import type { ProbeResult } from '@shared/types'+import type { ProbeResult, UsenetServer } from '@shared/types'import { cn } from 'cn'import {AlertTriangle,@@ -28,6 +28,7 @@ import { Button } from './ui/button'import { Dialog, DialogContent, DialogTitle } from './ui/dialog'import { ToggleGroup, ToggleGroupItem } from './ui/toggle-group'import { TorrentFileList } from './TorrentFiles'+import { UsenetServerDialog } from './UsenetServerDialog'import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip'import { NetworkEditPopover } from './NetworkEditPopover'@@ -141,6 +142,9 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Elementconst [skippedFiles, setSkippedFiles] = useState<number[]>([])// For this download only: the next one starts on Auto again.const [streamsChoice, setStreamsChoice] = useState<StreamsChoice>('auto')+ // The news server, for an NZB: undefined until it has been asked for, null when there's none.+ const [server, setServer] = useState<UsenetServer | null | undefined>(undefined)+ const [serverOpen, setServerOpen] = useState(false)// Opened with nothing in it: a link on the clipboard is most likely what it's for, as download// managers have long assumed. Anything else on the clipboard is left alone.@@ -208,6 +212,17 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Elementconst ready = probe.status === 'ready' ? probe.result : nullconst torrent = ready?.kind === 'torrent' ? ready.torrent : null+ const nzb = ready?.kind === 'nzb' ? ready.nzb : null++ // Asked for once an NZB turns up: no other download needs it.+ const wantsServer = nzb !== null && server === undefined+ useEffect(() => {+ if (!wantsServer) return+ void window.plexo+ .usenetServer()+ .then(setServer)+ .catch(() => setServer(null))+ }, [wantsServer])const chosenFiles = torrent? torrent.files.flatMap((_, index) => (skippedFiles.includes(index) ? [] : [index])): []@@ -241,6 +256,7 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Elementconst canStart =probe.status === 'ready' &&(!torrent || chosenFiles.length > 0) &&+ (!nzb || !!server) &&selectedInterfaceIds.length > 0 &&Boolean(destinationDir) &&!starting@@ -295,9 +311,9 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Elementif (chosen) setDestinationDir(chosen)}- // A .torrent file goes in the link field as its path; the probe reads it from there.- const handleOpenTorrent = async (): Promise<void> => {- const path = await window.plexo.chooseTorrentFile()+ // A .torrent or .nzb file goes in the link field as its path; the probe reads it from there.+ const handleOpenFile = async (): Promise<void> => {+ const path = await window.plexo.chooseDownloadFile()if (path) setUrl(path)}@@ -343,11 +359,13 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.ElementinfoHash: probe.result.torrent.infoHash,selectedFiles: skippedFiles.length > 0 ? chosenFiles : undefined}- : {- ...common,- kind: 'http',- streamsPerNetwork: streamsChoice === 'auto' ? undefined : streamsChoice- },+ : probe.result.kind === 'nzb'+ ? { ...common, kind: 'nzb', nzbHash: probe.result.nzb.hash }+ : {+ ...common,+ kind: 'http',+ streamsPerNetwork: streamsChoice === 'auto' ? undefined : streamsChoice+ },linkId)// Remembered for the next download, like the folder. Not a single-stream pick: that's one@@ -460,10 +478,17 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Element</Tooltip>)}</div>- <Button type="button" variant="secondary" onClick={handleOpenTorrent} className="h-9">- <FolderOpen data-icon="inline-start" />- Open .torrent- </Button>+ <Tooltip>+ <TooltipTrigger+ render={+ <Button type="button" variant="secondary" onClick={handleOpenFile} className="h-9">+ <FolderOpen data-icon="inline-start" />+ Open file+ </Button>+ }+ />+ <TooltipContent>Open a .torrent or .nzb file</TooltipContent>+ </Tooltip></div>{shownLink?.from && (@@ -515,7 +540,7 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Element{ready && (<div className="flex items-center gap-3"><div className="flex size-10 shrink-0 items-center justify-center rounded-lg border-[0.5px] border-border bg-card font-mono text-[10px] font-semibold text-muted-foreground">- {torrent && torrent.files.length > 1 ? 'DIR' : fileExtensionBadge(fileName)}+ {(torrent && torrent.files.length > 1) || nzb ? 'DIR' : fileExtensionBadge(fileName)}</div><div className="flex min-w-0 flex-1 flex-col gap-1">{torrent ? (@@ -533,8 +558,11 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Element<div className="font-mono text-[11.5px] text-muted-foreground">{[torrent && describeFileCount(chosenFiles.length, torrent.files.length),+ nzb && describeFileCount(nzb.files.length, nzb.files.length),sizeToFetch !== null ? formatBytes(sizeToFetch) : 'size unknown',+ nzb && 'from Usenet',!torrent &&+ !nzb &&(isSingleStreamOnly? ready.supportsRanges? 'Uses one connection. The file size is unknown.'@@ -556,6 +584,29 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Element/>)}+ {nzb && server !== undefined && (+ <div className="flex items-center gap-3">+ <div className={labelClass}>Server</div>+ {server ? (+ <div className="min-w-0 flex-1 truncate font-mono text-[12.5px]">+ {server.host}+ <span className="text-muted-foreground">+ {' '}+ · {server.connections} {server.connections === 1 ? 'connection' : 'connections'}+ </span>+ </div>+ ) : (+ <div className="flex min-w-0 flex-1 items-center gap-2 text-[12.5px] text-[var(--color-usb-text)]">+ <AlertTriangle aria-hidden className="size-3.5 shrink-0" />+ Set up your news server to download this NZB.+ </div>+ )}+ <Button type="button" variant="secondary" size="sm" onClick={() => setServerOpen(true)}>+ {server ? 'Change' : 'Set up'}+ </Button>+ </div>+ )}+<div className="flex items-center gap-3"><div className={labelClass}>Save to</div><div className="min-w-0 flex-1 truncate font-mono text-[12.5px]">@@ -687,8 +738,9 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Element</div></div>- {/* A torrent's speed comes from its peers, and an unsplittable file has one stream. */}- {ready && !torrent && !isSingleStreamOnly && (+ {/* A torrent's speed comes from its peers, an NZB's connections are the server's, and an+ unsplittable file has one stream. */}+ {ready && !torrent && !nzb && !isSingleStreamOnly && (<div className="flex items-center gap-3"><div className={labelClass} id="new-download-streams">Streams@@ -761,6 +813,13 @@ function NewDownloadForm({ onDone }: { onDone: () => void }): React.JSX.Element{startLabel}</Button></div>+ {/* Its form keeps its submit to itself: a portal's events still bubble up the React tree. */}+ <UsenetServerDialog+ open={serverOpen}+ server={server ?? null}+ onOpenChange={setServerOpen}+ onSaved={setServer}+ /></form>)}diff --git a/src/renderer/src/components/UsenetBadge.tsx b/src/renderer/src/components/UsenetBadge.tsxnew file mode 100644index 0000000..30ec7ff--- /dev/null+++ b/src/renderer/src/components/UsenetBadge.tsx@@ -0,0 +1,13 @@+import { Badge } from './ui/badge'++/** Next to an NZB download's name, wherever it's listed, as TorrentBadge is beside a torrent's. */+export function UsenetBadge(): React.JSX.Element {+ return (+ <Badge+ variant="outline"+ className="h-4 rounded-[3.5px] px-[6px] font-mono text-[9.5px] leading-none font-semibold tracking-[0.08em] text-muted-foreground"+ >+ USENET+ </Badge>+ )+}diff --git a/src/renderer/src/components/UsenetServerDialog.tsx b/src/renderer/src/components/UsenetServerDialog.tsxnew file mode 100644index 0000000..b74838c--- /dev/null+++ b/src/renderer/src/components/UsenetServerDialog.tsx@@ -0,0 +1,271 @@+import type { UsenetServer, UsenetServerInput } from '@shared/types'+import { AlertTriangle, Check, Server } from 'lucide-react'+import { useId, useState } from 'react'+import { describeError } from '../utils/format'+import { Button } from './ui/button'+import { Dialog, DialogContent, DialogTitle } from './ui/dialog'+import { Input } from './ui/input'+import { Switch } from './ui/switch'++const labelClass = 'w-24 shrink-0 text-[12.5px] text-[var(--text-secondary)]'+const hintClass = 'text-[12px] leading-snug text-muted-foreground'++/** The news server NZB downloads come from: where it is, the account, and how many connections+ * the account allows. Set once, for every NZB; the password never comes back to the window. */+export function UsenetServerDialog({+ open,+ server,+ onOpenChange,+ onSaved+}: {+ open: boolean+ /** As saved; null when there's none yet. */+ server: UsenetServer | null+ onOpenChange: (open: boolean) => void+ onSaved: (server: UsenetServer) => void+}): React.JSX.Element {+ return (+ <Dialog open={open} onOpenChange={onOpenChange}>+ <DialogContent showCloseButton={false} className="flex flex-col gap-0 p-0 sm:max-w-[460px]">+ {/* Mounted only while open, so each opening starts from what's saved. */}+ {open && (+ <UsenetServerForm+ server={server}+ onCancel={() => onOpenChange(false)}+ onSaved={(saved) => {+ onSaved(saved)+ onOpenChange(false)+ }}+ />+ )}+ </DialogContent>+ </Dialog>+ )+}++/** An NZB download's news server, by name, opening its settings: where a wrong password is put+ * right without an NZB to hand. */+export function UsenetServerButton({ host }: { host: string }): React.JSX.Element {+ const [server, setServer] = useState<UsenetServer | null>(null)+ const [open, setOpen] = useState(false)+ return (+ <>+ <Button+ type="button"+ variant="outline"+ size="xs"+ aria-label={`Usenet server ${host}: change settings`}+ onClick={() => {+ void window.plexo+ .usenetServer()+ .then(setServer)+ .catch(() => setServer(null))+ .finally(() => setOpen(true))+ }}+ className="h-auto cursor-default gap-1 rounded-[4px] border-[0.5px] bg-card px-[7px] py-[3px] font-mono text-[10.5px] leading-none font-medium text-[var(--text-secondary)] dark:bg-card"+ >+ <Server aria-hidden className="size-3" />+ {host}+ </Button>+ <UsenetServerDialog open={open} server={server} onOpenChange={setOpen} onSaved={setServer} />+ </>+ )+}++function UsenetServerForm({+ server,+ onCancel,+ onSaved+}: {+ server: UsenetServer | null+ onCancel: () => void+ onSaved: (server: UsenetServer) => void+}): React.JSX.Element {+ const id = useId()+ const [host, setHost] = useState(server?.host ?? '')+ const [tls, setTls] = useState(server?.tls ?? true)+ const [port, setPort] = useState(String(server?.port ?? 563))+ const [username, setUsername] = useState(server?.username ?? '')+ // Left empty, the saved password stays.+ const [password, setPassword] = useState('')+ const [connections, setConnections] = useState(String(server?.connections ?? 8))+ const [busy, setBusy] = useState<'test' | 'save' | null>(null)+ const [result, setResult] = useState<{ ok: boolean; text: string } | null>(null)++ const input = (): UsenetServerInput => ({+ host: host.trim(),+ port: Number(port),+ tls,+ username: username.trim(),+ connections: Number(connections),+ password: password === '' && server?.hasPassword ? undefined : password+ })+ const valid =+ host.trim() !== '' &&+ Number.isInteger(Number(port)) &&+ Number(port) > 0 &&+ Number(port) < 65536 &&+ Number.isInteger(Number(connections)) &&+ Number(connections) >= 1++ const test = async (): Promise<void> => {+ setBusy('test')+ setResult(null)+ try {+ await window.plexo.testUsenetServer(input())+ setResult({ ok: true, text: 'Connected and signed in.' })+ } catch (error) {+ setResult({ ok: false, text: describeError(error) })+ } finally {+ setBusy(null)+ }+ }++ const save = async (): Promise<void> => {+ setBusy('save')+ setResult(null)+ try {+ onSaved(await window.plexo.saveUsenetServer(input()))+ } catch (error) {+ setResult({ ok: false, text: describeError(error) })+ setBusy(null)+ }+ }++ return (+ <form+ className="flex flex-col"+ onSubmit={(event) => {+ event.preventDefault()+ // Opened from another form (New download): its submit is not this one's.+ event.stopPropagation()+ if (valid && !busy) void save()+ }}+ >+ <div className="border-b-[0.5px] border-border px-5 py-4">+ <DialogTitle className="text-[16px] font-semibold">Usenet server</DialogTitle>+ <p className={`${hintClass} mt-1.5`}>+ The news server your NZB downloads come from, as your Usenet provider gives it.+ </p>+ </div>++ <div className="flex flex-col gap-3 px-5 py-4">+ <div className="flex items-center gap-3">+ <label htmlFor={`${id}-host`} className={labelClass}>+ Server+ </label>+ <Input+ id={`${id}-host`}+ autoFocus+ value={host}+ onChange={(event) => setHost(event.target.value)}+ placeholder="news.example.com"+ spellCheck={false}+ className="font-mono"+ />+ </div>+ <div className="flex items-center gap-3">+ <label htmlFor={`${id}-port`} className={labelClass}>+ Port+ </label>+ <Input+ id={`${id}-port`}+ inputMode="numeric"+ value={port}+ onChange={(event) => setPort(event.target.value.replace(/\D/g, ''))}+ className="w-24 font-mono"+ />+ <label className="ml-auto flex items-center gap-2 text-[13px]">+ SSL+ <Switch+ checked={tls}+ onCheckedChange={(on) => {+ setTls(on)+ // The usual ports go with it; one set by hand stays.+ if (port === (on ? '119' : '563')) setPort(on ? '563' : '119')+ }}+ />+ </label>+ </div>+ {!tls && <p className={hintClass}>Without SSL your password is sent unencrypted.</p>}+ <div className="flex items-center gap-3">+ <label htmlFor={`${id}-username`} className={labelClass}>+ Username+ </label>+ <Input+ id={`${id}-username`}+ value={username}+ onChange={(event) => setUsername(event.target.value)}+ autoComplete="off"+ spellCheck={false}+ />+ </div>+ <div className="flex items-center gap-3">+ <label htmlFor={`${id}-password`} className={labelClass}>+ Password+ </label>+ <Input+ id={`${id}-password`}+ type="password"+ value={password}+ onChange={(event) => setPassword(event.target.value)}+ placeholder={server?.hasPassword ? 'Saved' : ''}+ autoComplete="off"+ />+ </div>+ <div className="flex items-center gap-3">+ <label htmlFor={`${id}-connections`} className={labelClass}>+ Connections+ </label>+ <Input+ id={`${id}-connections`}+ inputMode="numeric"+ value={connections}+ onChange={(event) => setConnections(event.target.value.replace(/\D/g, ''))}+ className="w-24 font-mono"+ />+ <span className={hintClass}>+ What your account allows, shared by every network a download uses.+ </span>+ </div>+ {server?.passwordInMemory && (+ <p className={hintClass}>+ This computer has no keychain Plexo can use, so your password is kept only until Plexo+ quits.+ </p>+ )}+ </div>++ <div className="flex items-center gap-2 border-t-[0.5px] border-border px-4 py-2">+ <div+ role="status"+ className={`flex min-w-0 flex-1 items-center gap-1.5 text-[12.5px] ${result?.ok ? 'text-[var(--color-wifi-text)]' : 'text-[var(--color-danger)]'}`}+ >+ {result &&+ (result.ok ? (+ <Check aria-hidden className="size-3.5 shrink-0" />+ ) : (+ <AlertTriangle aria-hidden className="size-3.5 shrink-0" />+ ))}+ <span className="truncate" title={result?.text}>+ {result?.text}+ </span>+ </div>+ <Button+ type="button"+ variant="secondary"+ disabled={!valid || busy !== null}+ onClick={() => void test()}+ >+ {busy === 'test' ? 'Testing' : 'Test'}+ </Button>+ <Button type="button" variant="secondary" onClick={onCancel}>+ Cancel+ </Button>+ <Button type="submit" disabled={!valid || busy !== null}>+ Save+ </Button>+ </div>+ </form>+ )+}diff --git a/src/renderer/src/hooks/useOpenedLinks.ts b/src/renderer/src/hooks/useOpenedLinks.tsindex 80e9fee..d87d477 100644--- a/src/renderer/src/hooks/useOpenedLinks.ts+++ b/src/renderer/src/hooks/useOpenedLinks.ts@@ -17,7 +17,8 @@ export function useOpenedLinks(): void {const isMac = window.plexo.platform === 'darwin'/** The ways into New download from anywhere in the window, as download managers have them:- * pasting a link (outside a text field), ⌘N / Ctrl+N, and dropping a link or a .torrent. */+ * pasting a link (outside a text field), ⌘N / Ctrl+N, and dropping a link, a .torrent or an+ * .nzb. */export function useNewDownloadShortcuts(): void {const openNewDownload = useAppStore((store) => store.openNewDownload)@@ -44,7 +45,7 @@ export function useNewDownloadShortcuts(): void {event.preventDefault()const file = event.dataTransfer?.files[0]const link = file- ? /\.torrent$/i.test(file.name)+ ? /\.(torrent|nzb)$/i.test(file.name)? window.plexo.pathForFile(file): null: acceptedLink(event.dataTransfer?.getData('text') ?? '')diff --git a/src/renderer/src/screens/CompleteScreen.tsx b/src/renderer/src/screens/CompleteScreen.tsxindex 4d9275b..2493dae 100644--- a/src/renderer/src/screens/CompleteScreen.tsx+++ b/src/renderer/src/screens/CompleteScreen.tsx@@ -1,4 +1,5 @@-import type { DownloadState, FinishedDownload } from '@shared/types'+import type { DownloadState, FinishedDownload, NzbFileState } from '@shared/types'+import { AlertTriangle, Info } from 'lucide-react'import { HeroBand } from '../components/HeroBand'import { DetailHeader } from '../components/DetailHeader'import { ThroughputChart } from '../components/ThroughputChart'@@ -17,6 +18,30 @@ import { useFormatSpeed } from '../hooks/useFormatSpeed'const sectionHeaderClass ='font-mono text-[10px] leading-none tracking-[0.16em] text-muted-foreground uppercase'+/** What's left to do with an NZB download, which Plexo leaves to the user's tools: a repair+ * where articles were missing, unpacking where it's an archive. Null when it's ready as it is. */+function usenetNote(files: NzbFileState[]): { warning: boolean; text: string } | null {+ const missing = files.reduce((sum, file) => sum + file.missing, 0)+ const par2 = files.some((file) => /\.par2$/i.test(file.name))+ const archive = files.some((file) => /\.(rar|r\d{2,3}|7z|zip|\d{3})$/i.test(file.name))+ const articles = missing === 1 ? 'article wasn’t' : 'articles weren’t'+ if (missing > 0) {+ return {+ warning: true,+ text: par2+ ? `${missing} ${articles} on the news server, so some files have gaps. Repair them with the download’s .par2 files${archive ? ' before unpacking' : ''}.`+ : `${missing} ${articles} on the news server, so some files have gaps, and this download has no .par2 files to repair them with.`+ }+ }+ if (archive) {+ return {+ warning: false,+ text: 'The files are as posted: unpack the archive to get at what’s inside.'+ }+ }+ return null+}+export function CompleteScreen({download}: {@@ -52,8 +77,16 @@ export function CompleteScreen({? download.unitsWritten: download.kind === 'torrent'? download.pieces.filter((piece) => piece.status !== 'skipped').length- : download.totalBlocks- const files = isTorrent && download.files.total > 1 ? download.files : null+ : download.kind === 'nzb'+ ? download.blocks.filter((block) => block.status === 'completed').length+ : download.totalBlocks+ const files =+ isTorrent && download.files.total > 1+ ? download.files+ : download.kind === 'nzb'+ ? { chosen: download.nzbFiles.length, total: download.nzbFiles.length }+ : null+ const note = download.kind === 'nzb' ? usenetNote(download.nzbFiles) : nullreturn (<div className="flex h-full flex-col bg-background">@@ -103,6 +136,20 @@ export function CompleteScreen({</div></HeroBand>+ {note && (+ <div+ role={note.warning ? 'alert' : undefined}+ className={`mx-5 mt-[18px] flex items-start gap-2 rounded-[9px] border-[0.5px] border-border bg-card px-3 py-2.5 text-[12.5px] leading-snug ${note.warning ? 'text-[var(--color-danger)]' : 'text-[var(--text-secondary)]'}`}+ >+ {note.warning ? (+ <AlertTriangle aria-hidden className="mt-0.5 size-3.5 shrink-0" />+ ) : (+ <Info aria-hidden className="mt-0.5 size-3.5 shrink-0" />+ )}+ {note.text}+ </div>+ )}+<div className="mx-5 my-[18px] grid grid-cols-5 overflow-hidden rounded-[10px] border-[0.5px] border-border bg-card">{[{ label: 'Size', value: formatBytes(finalSize) },@@ -117,7 +164,7 @@ export function CompleteScreen({{ label: 'Networks', value: String(groups.length) },// The most it ran at once: streams that didn't make it faster were closed along the way.{- label: isTorrent ? 'Peers' : 'Streams',+ label: isTorrent ? 'Peers' : download.kind === 'nzb' ? 'Connections' : 'Streams',value: String(isTorrent ? download.peakPeers : download.peakStreams)}].map((stat, index) => (@@ -180,7 +227,7 @@ export function CompleteScreen({</div><div className="mt-auto pt-3 font-mono text-[11px] leading-[1.4] text-muted-foreground">{[- `written in ${totalChunkCount} ${isTorrent ? 'pieces' : 'chunks'}`,+ `written in ${totalChunkCount} ${isTorrent ? 'pieces' : download.kind === 'nzb' ? 'articles' : 'chunks'}`,// What its peers got from it while it downloaded.isTorrent && `uploaded ${formatBytes(download.bytesUploaded ?? 0)}`,`${totalRetries} ${totalRetries === 1 ? 'retry' : 'retries'}`diff --git a/src/renderer/src/screens/DownloadingScreen.tsx b/src/renderer/src/screens/DownloadingScreen.tsxindex f4daa0a..f26f697 100644--- a/src/renderer/src/screens/DownloadingScreen.tsx+++ b/src/renderer/src/screens/DownloadingScreen.tsx@@ -12,6 +12,8 @@ import { ThroughputChart } from '../components/ThroughputChart'import { TorrentFiles } from '../components/TorrentFiles'import { TorrentBadge } from '../components/TorrentBadge'import { TruncatedText } from '../components/TruncatedText'+import { UsenetBadge } from '../components/UsenetBadge'+import { UsenetServerButton } from '../components/UsenetServerDialog'import { Button } from '../components/ui/button'import { Tooltip, TooltipContent, TooltipTrigger } from '../components/ui/tooltip'import { useNetworkVisuals } from '../hooks/useNetworkVisuals'@@ -94,7 +96,9 @@ function waitingFor(download: DownloadState): string | null {if (enabled.some((network) => network.status === 'unreachable')) {return download.kind === 'torrent'? 'Can’t reach peers. Retrying…'- : 'Can’t reach the server. Retrying…'+ : download.kind === 'nzb'+ ? 'Can’t reach the news server. Retrying…'+ : 'Can’t reach the server. Retrying…'}return enabled.length > 0 && enabled.every((network) => network.status === 'limit')? 'Every network in use has reached its data limit. Raise one in Speed & data limits.'@@ -124,6 +128,8 @@ export const DownloadingScreen = memo(function DownloadingScreen({const isTorrent = download.kind === 'torrent'const percent = formatPercent(download.bytesDownloaded, wantedBytes(download))const knownSize = download.totalBytes > 0+ const missingArticles =+ download.kind === 'nzb' ? download.nzbFiles.reduce((sum, file) => sum + file.missing, 0) : 0const [now, setNow] = useState(() => Date.now())useEffect(() => {@@ -333,6 +339,7 @@ export const DownloadingScreen = memo(function DownloadingScreen({className="font-sans text-[15px] leading-[1.3] font-semibold tracking-[-0.01em] text-foreground"/>{isTorrent && <TorrentBadge />}+ {download.kind === 'nzb' && <UsenetBadge />}</div><div className="flex min-w-0 items-center gap-[7px] font-mono text-[12.5px] leading-[1.2] tabular-nums text-[var(--text-secondary)]"><span>@@ -353,6 +360,35 @@ export const DownloadingScreen = memo(function DownloadingScreen({</span></>)}+ {download.kind === 'nzb' && (+ <>+ <Dot />+ <span>+ {describeFileCount(download.nzbFiles.length, download.nzbFiles.length)}+ </span>+ <Dot />+ <UsenetServerButton host={download.server} />+ </>+ )}+ {missingArticles > 0 && (+ <>+ <Dot />+ <Tooltip>+ <TooltipTrigger+ render={+ <span tabIndex={0} className="text-destructive outline-none">+ {missingArticles} missing+ </span>+ }+ />+ <TooltipContent>+ The news server doesn’t have{' '}+ {missingArticles === 1 ? 'this article' : 'these articles'}. Once the download+ is done, repair its files with their .par2 files.+ </TooltipContent>+ </Tooltip>+ </>+ )}{/* Several files: they open below. One is the name above. */}{download.kind === 'torrent' && download.files.total > 1 && (<>@@ -432,13 +468,14 @@ export const DownloadingScreen = memo(function DownloadingScreen({{/* Block grid: pinned with the file info. It's capped in height and scrolls itself. */}<div className="shrink-0 px-5 pt-3"><BlockGrid- blocks={download.kind === 'http' ? download.blocks : download.pieces}+ blocks={download.kind === 'torrent' ? download.pieces : download.blocks}groups={groups}visuals={visuals}knownSize={knownSize}remainingBytes={remainingBytes}isPaused={isPaused}pieces={isTorrent}+ articles={download.kind === 'nzb'}/></div>@@ -500,7 +537,8 @@ export const DownloadingScreen = memo(function DownloadingScreen({: 0}totalBytes={wantedBytes(download)}- blocks={download.kind === 'http' ? download.blocks : undefined}+ blocks={download.kind === 'torrent' ? undefined : download.blocks}+ usenet={download.kind === 'nzb'}onSwitch={(enabled) =>void window.plexo.setDownloadNetwork(download.id, row.id, enabled)}diff --git a/src/renderer/src/screens/DownloadsScreen.tsx b/src/renderer/src/screens/DownloadsScreen.tsxindex e5d942b..7f23071 100644--- a/src/renderer/src/screens/DownloadsScreen.tsx+++ b/src/renderer/src/screens/DownloadsScreen.tsx@@ -14,6 +14,7 @@ import { networkStatusText } from '../components/NetworkRow'import { scheduleMessage, scheduleWindow } from '@shared/downloadSchedule'import { NetworksMenu } from '../components/NetworksMenu'import { TorrentBadge } from '../components/TorrentBadge'+import { UsenetBadge } from '../components/UsenetBadge'import { Button } from '../components/ui/button'import { Checkbox } from '../components/ui/checkbox'import { Tooltip, TooltipContent, TooltipTrigger } from '../components/ui/tooltip'@@ -396,7 +397,7 @@ function EmptyState(): React.JSX.Element {<><div className="font-sans text-[16px] leading-[1.2] font-bold">No downloads yet</div><div className="max-w-[380px] text-[12.5px] leading-[1.6] text-[var(--text-secondary)]">- Paste a link ({PASTE_SHORTCUT}) or drop a .torrent anywhere in this window.+ Paste a link ({PASTE_SHORTCUT}) or drop a .torrent or .nzb anywhere in this window.</div><Button type="button" className="mt-1" onClick={() => openNewDownload()}><Plus data-icon="inline-start" />@@ -574,6 +575,7 @@ const DownloadRow = memo(function DownloadRow({{item.fileName}</span>{item.kind === 'torrent' && <TorrentBadge />}+ {item.kind === 'nzb' && <UsenetBadge />}{(dots || speed) && (<div className="ml-auto flex shrink-0 items-center gap-2.5 pl-2">{dots && (diff --git a/src/renderer/src/screens/ErrorScreen.tsx b/src/renderer/src/screens/ErrorScreen.tsxindex ee9dba2..0563c12 100644--- a/src/renderer/src/screens/ErrorScreen.tsx+++ b/src/renderer/src/screens/ErrorScreen.tsx@@ -3,6 +3,7 @@ import { Folder } from 'lucide-react'import { useState } from 'react'import { DetailHeader } from '../components/DetailHeader'import { TruncatedText } from '../components/TruncatedText'+import { UsenetServerButton } from '../components/UsenetServerDialog'import {describeError,fileExtensionBadge,@@ -101,6 +102,14 @@ export function ErrorScreen({ download }: { download: DownloadState }): React.JS</div></div>+ {/* What an NZB download most often fails on is the server's settings: they open here. */}+ {download.kind === 'nzb' && !cancelled && (+ <div className="flex items-center gap-2 font-sans text-[12px] text-[var(--text-secondary)]">+ Usenet server+ <UsenetServerButton host={download.server} />+ </div>+ )}+{/* File capsule */}<div className="flex w-full items-center gap-[11px] rounded-[9px] border-[0.5px] border-border bg-background p-[10px_12px] text-left"><div className="flex size-[34px] shrink-0 items-center justify-center rounded-[7px] border-[0.5px] border-[var(--border-strong)] bg-card font-mono text-[8.5px] leading-none font-semibold text-[var(--text-secondary)]">diff --git a/src/renderer/src/utils/format.ts b/src/renderer/src/utils/format.tsindex 3353ba8..ad0ec5b 100644--- a/src/renderer/src/utils/format.ts+++ b/src/renderer/src/utils/format.ts@@ -136,9 +136,17 @@ export function pathInTorrent(path: string): string {return path.split(/[\\/]/).slice(1).join('/') || path}-/** What a download fetches: all of it, bar a torrent's pieces no chosen file needs. */+/** What a download fetches: all of it, bar a torrent's pieces no chosen file needs and an NZB's+ * articles the server doesn't have. */export function wantedBytes(download: DownloadState | FinishedDownload): number {- return download.totalBytes - (download.kind === 'torrent' ? download.skippedBytes : 0)+ return (+ download.totalBytes -+ (download.kind === 'torrent'+ ? download.skippedBytes+ : download.kind === 'nzb'+ ? download.missingBytes+ : 0)+ )}export function formatPercent(bytesDownloaded: number, totalBytes: number): number {@@ -154,9 +162,9 @@ export function fileNameFromPath(path: string): string {}/** Short uppercase file-type badge from a name's extension, e.g. "Xcode_16.2.xip" -> "XIP", "photo.jpeg" -> "JPEG". */-/** A download that is a folder: a torrent's, of its files. */+/** A download that is a folder: a torrent's, or an NZB's, of its files. */export function isFolder(download: DownloadState | FinishedDownload): boolean {- return download.kind === 'torrent' && download.folder+ return (download.kind === 'torrent' && download.folder) || download.kind === 'nzb'}export function fileExtensionBadge(fileName: string): string {@@ -181,11 +189,12 @@ export function formatWhen(time: number, now: number): string {return date.toLocaleDateString(undefined, { day: 'numeric', month: 'short', year: 'numeric' })}-/** What Plexo can download: a web link, a magnet link, or a .torrent file on this computer. */+/** What Plexo can download: a web link, a magnet link, or a .torrent or .nzb file on this+ * computer. */export function acceptedLink(text: string): string | null {const link = text.trim()if (/^(https?:\/\/|magnet:\?)/i.test(link)) return link- if (/^(\/|[a-z]:\\).*\.torrent$/i.test(link)) return link+ if (/^(\/|[a-z]:\\).*\.(torrent|nzb)$/i.test(link)) return linkreturn null}@@ -227,7 +236,7 @@ export interface TorrentNetworkGroup extends TorrentDownloadNetwork {export type NetworkGroup = HttpNetworkGroup | TorrentNetworkGroupexport function groupByNetwork(download: DownloadState): NetworkGroup[] {- if (download.kind === 'http') {+ if (download.kind !== 'torrent') {return download.networks.map((network) => ({...network,streams: download.streams.filter((stream) => stream.interfaceId === network.id)diff --git a/src/shared/downloadUpdate.ts b/src/shared/downloadUpdate.tsindex 8e40de7..d51479f 100644--- a/src/shared/downloadUpdate.ts+++ b/src/shared/downloadUpdate.ts@@ -1,35 +1,58 @@-import type { DownloadState, DownloadUpdate } from './types'+import type {+ DownloadState,+ DownloadUpdate,+ HttpDownloadUpdate,+ NzbDownloadUpdate,+ TorrentDownloadUpdate+} from './types'++/** `units` with the `changed` ones put in; without `units` to start from, only a snapshot (every+ * one of the `total` units) will do, and null says this update isn't one. */+function withChanged<T extends { index: number }>(+ units: T[] | undefined,+ changed: T[],+ total: number+): T[] | null {+ if (!units) return changed.length < total ? null : changed+ if (changed.length === 0) return units+ const next = [...units]+ for (const unit of changed) next[unit.index] = unit+ return next+}/** The download as `current` plus `update`: what anything watching a download keeps. */export function applyDownloadUpdate(current: DownloadState | null,update: DownloadUpdate): DownloadState | null {- if ('blocks' in update) {- const { state, blocks: changed, seq } = update- if (current?.id === state.id && current.kind === 'http') {- if ((current.seq ?? 0) >= seq) return current- let blocks = current.blocks- if (changed.length > 0) {- blocks = [...blocks]- for (const block of changed) blocks[block.index] = block- }- return { ...state, blocks, seq }- }- if (changed.length < state.totalBlocks) return current- return { ...state, blocks: changed, seq }- }+ const same =+ current?.id === update.state.id && current.kind === update.state.kind ? current : null+ if (same && (same.seq ?? 0) >= update.seq) return current+ const { seq } = update- const { state, pieces: changed, seq } = update- if (current?.id === state.id && current.kind === 'torrent') {- if ((current.seq ?? 0) >= seq) return current- let pieces = current.pieces- if (changed.length > 0) {- pieces = [...pieces]- for (const piece of changed) pieces[piece.index] = piece- }- return { ...state, pieces, seq }+ if ('pieces' in update) {+ const { state, pieces: changed } = update as TorrentDownloadUpdate+ const pieces = withChanged(+ same?.kind === 'torrent' ? same.pieces : undefined,+ changed,+ state.totalPieces+ )+ return pieces ? { ...state, pieces, seq } : current+ }+ if (update.state.kind === 'nzb') {+ const { state, blocks: changed } = update as NzbDownloadUpdate+ const blocks = withChanged(+ same?.kind === 'nzb' ? same.blocks : undefined,+ changed,+ state.totalBlocks+ )+ return blocks ? { ...state, blocks, seq } : current}- if (changed.length < state.totalPieces) return current- return { ...state, pieces: changed, seq }+ const { state, blocks: changed } = update as HttpDownloadUpdate+ const blocks = withChanged(+ same?.kind === 'http' ? same.blocks : undefined,+ changed,+ state.totalBlocks+ )+ return blocks ? { ...state, blocks, seq } : current}diff --git a/src/shared/ipc-channels.ts b/src/shared/ipc-channels.tsindex b9b4529..8bbed01 100644--- a/src/shared/ipc-channels.ts+++ b/src/shared/ipc-channels.ts@@ -6,7 +6,7 @@ export const IpcChannels = {getInitialState: 'app:get-initial-state',updateSettings: 'app:update-settings',chooseDestinationFolder: 'dialog:choose-destination-folder',- chooseTorrentFile: 'dialog:choose-torrent-file',+ chooseDownloadFile: 'dialog:choose-download-file',readClipboardText: 'clipboard:read-text',revealDownload: 'shell:reveal-download',openDownload: 'shell:open-download',@@ -35,5 +35,8 @@ export const IpcChannels = {dismissLink: 'app:dismiss-link',updateState: 'update:state',updateStateChanged: 'update:state-changed',- showUpdateMenu: 'update:show-menu'+ showUpdateMenu: 'update:show-menu',+ usenetServer: 'usenet:server',+ saveUsenetServer: 'usenet:save-server',+ testUsenetServer: 'usenet:test-server'} as constdiff --git a/src/shared/ipc-contract.ts b/src/shared/ipc-contract.tsindex d8f186f..c18330a 100644--- a/src/shared/ipc-contract.ts+++ b/src/shared/ipc-contract.ts@@ -7,7 +7,9 @@ import type {ProbeResult,StartDownloadRequest,TorrentFileEntry,- UpdateState+ UpdateState,+ UsenetServer,+ UsenetServerInput} from './types'/** The request/response half of the IPC surface (every IpcChannels entry except the@@ -22,7 +24,8 @@ export interface IpcContract {updateSettings: { args: [patch: AppSettings]; result: void }probeUrl: { args: [url: string]; result: ProbeResult }chooseDestinationFolder: { args: [defaultPath: string]; result: string | null }- chooseTorrentFile: { args: []; result: string | null }+ /** A .torrent or .nzb file to download from, picked in the OS's dialog. */+ chooseDownloadFile: { args: []; result: string | null }readClipboardText: { args: []; result: string }/** Shows a download's file in its folder, by the download's own path. False when nothing is* there any more: history is re-sent, with it marked missing. */@@ -66,4 +69,9 @@ export interface IpcContract {resumeFromLink: { args: [linkId: string, downloadId: string]; result: void }/** Forgets the sign-in of a link that won't be started. */dismissLink: { args: [id: string]; result: void }+ /** The news server NZB downloads come from; null until one is set up. */+ usenetServer: { args: []; result: UsenetServer | null }+ saveUsenetServer: { args: [server: UsenetServerInput]; result: UsenetServer }+ /** Connects and signs in, over the computer's default route; throws what went wrong. */+ testUsenetServer: { args: [server: UsenetServerInput]; result: void }}diff --git a/src/shared/taskbarProgress.ts b/src/shared/taskbarProgress.tsindex ec18715..921f300 100644--- a/src/shared/taskbarProgress.ts+++ b/src/shared/taskbarProgress.ts@@ -11,15 +11,24 @@ export interface TaskbarDownload {status: DownloadStatustotalBytes: numberbytesDownloaded: number- kind?: 'http' | 'torrent'+ kind?: 'http' | 'torrent' | 'nzb'skippedBytes?: number+ missingBytes?: number}-/** What a download fetches: all of it, bar a torrent's pieces no chosen file needs. */+/** What a download fetches: all of it, bar a torrent's pieces no chosen file needs and an NZB's+ * articles the server doesn't have. */export function wantedBytes(- download: Pick<TaskbarDownload, 'totalBytes' | 'kind' | 'skippedBytes'>+ download: Pick<TaskbarDownload, 'totalBytes' | 'kind' | 'skippedBytes' | 'missingBytes'>): number {- return download.totalBytes - (download.kind === 'torrent' ? (download.skippedBytes ?? 0) : 0)+ return (+ download.totalBytes -+ (download.kind === 'torrent'+ ? (download.skippedBytes ?? 0)+ : download.kind === 'nzb'+ ? (download.missingBytes ?? 0)+ : 0)+ )}/**diff --git a/src/shared/types.ts b/src/shared/types.tsindex d9b8117..08592f7 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -44,7 +44,12 @@ export interface TorrentProbeResult extends ProbeResultBase {torrent: TorrentInfo}-export type ProbeResult = HttpProbeResult | TorrentProbeResult+export interface NzbProbeResult extends ProbeResultBase {+ kind: 'nzb'+ nzb: NzbInfo+}++export type ProbeResult = HttpProbeResult | TorrentProbeResult | NzbProbeResult/** How the check of a magnet link is going, while it's still going: `noPeersYet` once its first* search came back empty and it's looking a little longer. */@@ -62,6 +67,15 @@ export interface TorrentInfo {files: { path: string; length: number }[]}+/** What an NZB holds: Usenet articles, grouped into the files they make up once decoded. */+export interface NzbInfo {+ /** Identifies the NZB among those probed lately (see main/download/nzb/nzbFile.ts). */+ hash: string+ /** Each file as it will be saved in the download's folder, and what its articles add up to:+ * a little more than the file itself, since yEnc encodes it with some overhead. */+ files: { name: string; bytes: number; segments: number }[]+}+/** One of a torrent download's files, for listing them while it runs. */export interface TorrentFileEntry {/** As TorrentInfo's: relative to the destination folder, the torrent's own folder first. */@@ -154,7 +168,18 @@ export interface TorrentPieceState extends WorkUnitState {provisionalBytes: number}-export type DownloadUnitState = HttpBlockState | TorrentPieceState+/** `missing`: the news server no longer has the article, or sent it damaged. What the article+ * held is left as zeros in its file, for a par2 repair to restore. */+export type NzbSegmentStatus = HttpBlockStatus | 'missing'++/** One Usenet article of an NZB download. Its range is in the NZB's own byte count, the articles+ * as posted, one after the other: the files' real sizes are only known once they come in. */+export interface NzbSegmentState extends WorkUnitState {+ kind: 'nzb'+ status: NzbSegmentStatus+}++export type DownloadUnitState = HttpBlockState | TorrentPieceState | NzbSegmentState/*** - on: in use.@@ -265,18 +290,51 @@ export interface TorrentDownloadState extends DownloadStateBase {uploadSpeedBytesPerSec: number}-export type DownloadState = HttpDownloadState | TorrentDownloadState+/** One file of an NZB download. */+export interface NzbFileState {+ /** As it is saved, in the download's folder. */+ name: string+ /** What its articles add up to, as the NZB counts them. */+ bytes: number+ /** Its real size, from its articles' yEnc headers; unset until one has come in. */+ size?: number+ segments: number+ /** Articles the server didn't have, or sent damaged (see NzbSegmentStatus). */+ missing: number+}++/** A Usenet download: an NZB's articles, fetched over connections to the user's news server. Its+ * streams are those connections, its blocks the articles. */+export interface NzbDownloadState extends DownloadStateBase {+ kind: 'nzb'+ networks: HttpDownloadNetwork[]+ streams: HttpStreamState[]+ peakStreams: number+ blocks: NzbSegmentState[]+ totalBlocks: number+ nzbFiles: NzbFileState[]+ /** Of `totalBytes`, the bytes of articles that are missing. */+ missingBytes: number+ /** The news server it downloads from, by host name. */+ server: string+}++export type DownloadState = HttpDownloadState | TorrentDownloadState | NzbDownloadState/** A finished download as history keeps it: its state without the work units and connections,* which only a running download needs. */export type FinishedDownload = (- Omit<HttpDownloadState, 'blocks' | 'streams'> | Omit<TorrentDownloadState, 'pieces' | 'peers'>+ | Omit<HttpDownloadState, 'blocks' | 'streams'>+ | Omit<TorrentDownloadState, 'pieces' | 'peers'>+ | Omit<NzbDownloadState, 'blocks' | 'streams'>) & {- /** The blocks or pieces it was written in (a torrent's: those its chosen files needed). */+ /** The blocks or pieces it was written in (a torrent's: those its chosen files needed; an+ * NZB's: its articles that came in). */unitsWritten: number/** Set when listed: its file or folder is no longer where it was saved. */missing?: boolean- /** Chosen torrent files relative to its destination folder, retained for safe file removal. */+ /** Chosen torrent files (or an NZB's files) relative to its destination folder, retained for+ * safe file removal. */downloadedFiles?: string[]}@@ -296,7 +354,13 @@ export interface TorrentDownloadUpdate {pieces: TorrentPieceState[]}-export type DownloadUpdate = HttpDownloadUpdate | TorrentDownloadUpdate+export interface NzbDownloadUpdate {+ seq: number+ state: Omit<NzbDownloadState, 'blocks'>+ blocks: NzbSegmentState[]+}++export type DownloadUpdate = HttpDownloadUpdate | TorrentDownloadUpdate | NzbDownloadUpdate/** User customization for one physical network, keyed by NetworkInterfaceInfo.id — lets a* cryptic OS device name (e.g. "feth0") get a real label, and a color distinct from its@@ -463,4 +527,37 @@ export interface StartTorrentDownloadRequest extends StartDownloadRequestBase {selectedFiles?: number[]}-export type StartDownloadRequest = StartHttpDownloadRequest | StartTorrentDownloadRequest+export interface StartNzbDownloadRequest extends StartDownloadRequestBase {+ kind: 'nzb'+ /** The download starts from the NZB retained when it was probed (see NzbInfo.hash). */+ nzbHash: string+}++export type StartDownloadRequest =+ StartHttpDownloadRequest | StartTorrentDownloadRequest | StartNzbDownloadRequest++/** The news server NZB downloads come from, as the window sees it: never its password. */+export interface UsenetServer {+ host: string+ port: number+ tls: boolean+ username: string+ /** How many connections the server allows at once, for every network together. */+ connections: number+ /** A password is set. */+ hasPassword: boolean+ /** Its password is kept only until Plexo quits: this computer offers no keychain to seal it+ * with (see main/download/nzb/usenetServer.ts). */+ passwordInMemory?: boolean+}++/** What the window sends to set the news server, or to test one. `password` left out keeps the+ * one already saved. */+export interface UsenetServerInput {+ host: string+ port: number+ tls: boolean+ username: string+ connections: number+ password?: string+}