diff --git a/AGENTS.md b/AGENTS.md
index 93271b652..76b5ccb5f 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -667,7 +667,8 @@ docker/ # Docker image of `ketch server` (Dockerfile, entrypoint, compose
- HTTP requests can be spread round-robin over selected network interfaces
(`KetchApi.selectNetworkInterfaces`, `MultiNetworkHttpEngine`); see
[multiple networks](docs/multiple-networks.md)
-- Proxies for HTTP(S), HLS and DASH downloads (not FTP or BitTorrent; Discover never proxies):
+- Proxies for HTTP(S), HLS and DASH downloads (not FTP; BitTorrent only with
+ `[torrent] useProxy`, see the torrent section; Discover never proxies):
`DownloadConfig.proxy` (`[download.proxy]`), replaced per task by `DownloadRequest.proxy`, which
`DownloadCoordinator` folds into the run's `DownloadContext.config` (`forRequest`); `resolve`
uses the global one. `Proxy` is sealed: `System` (default), `Direct` or `Manual` with a
@@ -1418,6 +1419,22 @@ docker/ # Docker image of `ketch server` (Dockerfile, entrypoint, compose
`sequential`, `TorrentSnapshot.sequential`), saved in `TorrentSourceState.sequential`, read live
by `TorrentPieceScheduler` and the v2 loop (`TorrentV2RarityPicker.lowest`); the inspector's
`SequentialControl` switches it. It orders pieces; file priorities choose which files first
+- Proxy: `TorrentConfig.proxy` (a `ProxyAddress`, read as the engine starts) sends every
+ torrent through a SOCKS5 or HTTP proxy. `TorrentDownloadSource`'s engine factory then builds
+ the engine over `ProxiedTorrentNetwork` (`net`), whose `connect` tunnels through the proxy
+ (SOCKS5 `CONNECT` with user name and password, host names left to the proxy; or HTTP
+ `CONNECT` with Basic credentials), whose `bindUdp` sockets send and receive nothing and whose
+ `listen` binds loopback, with DHT, uTP, port mapping and local discovery off in its config
+ whatever the settings say, and no port mapper or LSD transport at all. The engine is built
+ `proxied`: a peer named by host (`x.pe`, a tracker's list) is dialed by that name
+ (`resolveEndpoint(resolve = false)`, `TorrentPeerEncryption(resolveNames = false)`, so the IP
+ filter checks addresses only), and UDP trackers fail at once instead of timing out; trackers, web seeds,
+ metainfo and tracker lists go through `HttpEngine.withProxy` (an engine that cannot use one
+ fails them, `TorrentHttp.default(proxy)` without an injected engine). Hosts pass
+ `KetchConfig.torrentProxy`: the manual proxy of `[download.proxy]` while `[torrent] useProxy`
+ is on (off by default; Settings → BitTorrent → Connections → Use the proxy, this device
+ only), in the desktop, Android and iOS apps and the CLI; a change applies at the next start.
+ Tests: `ProxiedTorrentNetworkTest` (scripted SOCKS5 and HTTP proxies on loopback)
- IP filter (capability `ip-filter`): `TorrentConfig.blockedAddresses` / `[torrent] blocklist`
/ `TorrentEngineSettings.blockedAddresses` (live through `setBlockedAddresses` and
`updateSettings`, which refuses entries that are none with `INVALID_INPUT`) lists addresses,
diff --git a/app/android/src/main/kotlin/com/linroid/ketch/app/android/KetchService.kt b/app/android/src/main/kotlin/com/linroid/ketch/app/android/KetchService.kt
index b37c2e29c..8836ca9de 100644
--- a/app/android/src/main/kotlin/com/linroid/ketch/app/android/KetchService.kt
+++ b/app/android/src/main/kotlin/com/linroid/ketch/app/android/KetchService.kt
@@ -189,7 +189,8 @@ class KetchService : Service() {
val instanceId = DeviceStore(File(filesDir, DEVICE_FILE_NAME).path).instanceId()
val torrentSource = TorrentDownloadSource(
TorrentConfig(stateDirectory = filesDir.resolve("torrent-state").absolutePath)
- .withSettings(config.torrent.engineSettings),
+ .withSettings(config.torrent.engineSettings)
+ .copy(proxy = config.torrentProxy),
// Wi-Fi drivers drop multicast to save power unless an app holds a lock; LAN discovery
// holds it only while a public torrent runs.
multicastLock = {
diff --git a/app/desktop/src/main/kotlin/com/linroid/ketch/app/desktop/main.kt b/app/desktop/src/main/kotlin/com/linroid/ketch/app/desktop/main.kt
index ff26ffb0e..4fd130c7f 100644
--- a/app/desktop/src/main/kotlin/com/linroid/ketch/app/desktop/main.kt
+++ b/app/desktop/src/main/kotlin/com/linroid/ketch/app/desktop/main.kt
@@ -709,7 +709,8 @@ private fun createInstanceManager(
val taskStore = createSqliteTaskStore(driverFactory)
val torrentSource = TorrentDownloadSource(
TorrentConfig(stateDirectory = File(configDir, "torrent-state").path)
- .withSettings(config.torrent.engineSettings),
+ .withSettings(config.torrent.engineSettings)
+ .copy(proxy = config.torrentProxy),
)
return InstanceManager(
factory = InstanceFactory(
diff --git a/app/shared/src/commonMain/composeResources/values-de/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-de/strings_settings.xml
index 8d90449ed..80f496811 100644
--- a/app/shared/src/commonMain/composeResources/values-de/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-de/strings_settings.xml
@@ -604,6 +604,8 @@
Bittet deinen Router (UPnP oder NAT-PMP), einen Port weiterzuleiten, damit sich mehr Peers mit diesem Gerät verbinden können.
Lokale Peer-Suche
Findet Geräte in deinem lokalen Netzwerk, die dieselben öffentlichen Torrents herunterladen oder teilen, damit sie Teile direkt austauschen.
+ Proxy verwenden
+ Erreicht Peers, HTTP-Tracker und Web-Seeds über den unter „Netzwerk“ eingestellten Proxy. DHT, uTP, UDP-Tracker und Portweiterleitung sind über einen Proxy aus, und Peers können keine Verbindung zu dir aufbauen. Gilt nach einem Neustart von Ketch.
Verschlüsselung verbirgt, welche Art von Datenverkehr es ist, nicht, mit wem du dich verbindest.
Zusätzliche Tracker
Keine zusätzlichen Tracker
diff --git a/app/shared/src/commonMain/composeResources/values-es/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-es/strings_settings.xml
index 4dbf443e8..76c75e168 100644
--- a/app/shared/src/commonMain/composeResources/values-es/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-es/strings_settings.xml
@@ -604,6 +604,8 @@
Pide a tu router (UPnP o NAT-PMP) que reenvíe un puerto, para que más pares puedan conectarse a este dispositivo.
Descubrimiento de pares locales
Encuentra dispositivos de tu red local que descargan o comparten los mismos torrents públicos, para que intercambien partes directamente.
+ Usar el proxy
+ Llega a los pares, los trackers HTTP y las semillas web a través del proxy configurado en Red. DHT, uTP, los trackers UDP y el reenvío de puertos se desactivan con un proxy, y los pares no pueden conectarse a ti. Se aplica al reiniciar Ketch.
El cifrado oculta qué tipo de tráfico es, no con quién te conectas.
Trackers adicionales
Sin trackers adicionales
diff --git a/app/shared/src/commonMain/composeResources/values-fr/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-fr/strings_settings.xml
index 1690ea5b9..8594cc056 100644
--- a/app/shared/src/commonMain/composeResources/values-fr/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-fr/strings_settings.xml
@@ -604,6 +604,8 @@
Demande à votre routeur (UPnP ou NAT-PMP) de rediriger un port, pour que davantage de pairs puissent se connecter à cet appareil.
Découverte de pairs locaux
Trouve les appareils de votre réseau local qui téléchargent ou partagent les mêmes torrents publics, pour qu'ils échangent des morceaux directement.
+ Utiliser le proxy
+ Joint les pairs, les trackers HTTP et les sources web via le proxy défini dans Réseau. DHT, uTP, trackers UDP et redirection de port sont désactivés via un proxy, et les pairs ne peuvent pas se connecter à vous. Prend effet au redémarrage de Ketch.
Le chiffrement cache la nature du trafic, pas les pairs auxquels vous vous connectez.
Trackers supplémentaires
Aucun tracker supplémentaire
diff --git a/app/shared/src/commonMain/composeResources/values-ja/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-ja/strings_settings.xml
index cf6ddaf2e..9eeabfe2d 100644
--- a/app/shared/src/commonMain/composeResources/values-ja/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-ja/strings_settings.xml
@@ -592,6 +592,8 @@
ルーター(UPnPまたはNAT-PMP)にポート転送を依頼し、より多くのピアがこのデバイスに接続できるようにします。
ローカルピア検出
同じ公開トレントをダウンロードまたは共有しているローカルネットワーク上のデバイスを見つけ、ピースを直接やり取りします。
+ プロキシを使用
+ 「ネットワーク」で設定したプロキシ経由でピア、HTTP トラッカー、ウェブシードに接続します。プロキシ経由では DHT、uTP、UDP トラッカー、ポート転送は無効になり、ピアからの接続も受けられません。Ketch の再起動後に有効になります。
暗号化で隠れるのは通信の種類で、接続相手は隠れません。
追加トラッカー
追加トラッカーはありません
diff --git a/app/shared/src/commonMain/composeResources/values-ko/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-ko/strings_settings.xml
index c64c0aee9..bd4c1ce2d 100644
--- a/app/shared/src/commonMain/composeResources/values-ko/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-ko/strings_settings.xml
@@ -592,6 +592,8 @@
라우터(UPnP 또는 NAT-PMP)에 포트 포워딩을 요청해 더 많은 피어가 이 기기에 연결할 수 있게 합니다.
로컬 피어 검색
같은 공개 토렌트를 다운로드하거나 공유하는 로컬 네트워크의 기기를 찾아 조각을 직접 주고받습니다.
+ 프록시 사용
+ 네트워크에서 설정한 프록시를 통해 피어, HTTP 트래커, 웹 시드에 연결합니다. 프록시를 사용하면 DHT, uTP, UDP 트래커, 포트 포워딩이 꺼지고 피어가 연결해 올 수 없습니다. Ketch를 다시 시작하면 적용됩니다.
암호화는 트래픽의 종류를 숨길 뿐, 누구와 연결하는지는 숨기지 않습니다.
추가 트래커
추가 트래커 없음
diff --git a/app/shared/src/commonMain/composeResources/values-pt/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-pt/strings_settings.xml
index 5e77ee84b..5493f2b79 100644
--- a/app/shared/src/commonMain/composeResources/values-pt/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-pt/strings_settings.xml
@@ -604,6 +604,8 @@
Pede ao seu roteador (UPnP ou NAT-PMP) para redirecionar uma porta, para que mais peers possam se conectar a este dispositivo.
Descoberta de peers locais
Encontra dispositivos na sua rede local que baixam ou compartilham os mesmos torrents públicos, para que troquem partes diretamente.
+ Usar o proxy
+ Alcança pares, trackers HTTP e sementes web pelo proxy definido em Rede. DHT, uTP, trackers UDP e encaminhamento de portas ficam desativados com um proxy, e os pares não conseguem se conectar a você. Vale depois de reiniciar o Ketch.
A criptografia esconde que tipo de tráfego é, não com quem você se conecta.
Trackers extras
Nenhum tracker extra
diff --git a/app/shared/src/commonMain/composeResources/values-ru/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-ru/strings_settings.xml
index 3b2d3db7f..ef5a75a83 100644
--- a/app/shared/src/commonMain/composeResources/values-ru/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-ru/strings_settings.xml
@@ -628,6 +628,8 @@
Просит роутер (UPnP или NAT-PMP) пробросить порт, чтобы к этому устройству могло подключиться больше пиров.
Поиск пиров в локальной сети
Находит устройства в вашей локальной сети, которые скачивают или раздают те же публичные торренты, чтобы они обменивались частями напрямую.
+ Использовать прокси
+ Пиры, HTTP-трекеры и веб-сиды доступны через прокси, заданный в разделе «Сеть». Через прокси не работают DHT, uTP, UDP-трекеры и проброс порта, а пиры не могут подключаться к вам. Вступает в силу после перезапуска Ketch.
Шифрование скрывает, что это за трафик, но не то, с кем вы соединяетесь.
Дополнительные трекеры
Нет дополнительных трекеров
diff --git a/app/shared/src/commonMain/composeResources/values-zh-rTW/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-zh-rTW/strings_settings.xml
index e52b2243b..11bb7030b 100644
--- a/app/shared/src/commonMain/composeResources/values-zh-rTW/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-zh-rTW/strings_settings.xml
@@ -592,6 +592,8 @@
請求路由器(UPnP 或 NAT-PMP)轉送一個連接埠,讓更多對等端能連線至此裝置。
本機對等端探索
尋找區域網路中下載或分享相同公開種子的裝置,讓它們直接交換資料塊。
+ 使用代理
+ 透過「網路」中設定的代理連線節點、HTTP Tracker 和網頁種子。使用代理時 DHT、uTP、UDP Tracker 和連接埠轉送皆關閉,其他節點也無法連入。重新啟動 Ketch 後生效。
加密隱藏的是流量類型,而不是連線的對象。
額外的 Tracker
沒有額外的 Tracker
diff --git a/app/shared/src/commonMain/composeResources/values-zh/strings_settings.xml b/app/shared/src/commonMain/composeResources/values-zh/strings_settings.xml
index 540a55452..34f7ecc6a 100644
--- a/app/shared/src/commonMain/composeResources/values-zh/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values-zh/strings_settings.xml
@@ -592,6 +592,8 @@
请求路由器(UPnP 或 NAT-PMP)转发一个端口,让更多节点能连接到此设备。
本地节点发现
查找本地网络中下载或分享相同公开种子的设备,让它们直接交换数据块。
+ 使用代理
+ 通过“网络”中设置的代理连接节点、HTTP Tracker 和网页种子。使用代理时 DHT、uTP、UDP Tracker 和端口转发均关闭,其他节点也无法连入。重启 Ketch 后生效。
加密隐藏的是流量类型,而不是连接的对象。
额外的 Tracker
没有额外的 Tracker
diff --git a/app/shared/src/commonMain/composeResources/values/strings_settings.xml b/app/shared/src/commonMain/composeResources/values/strings_settings.xml
index 49c21b5d8..64f238443 100644
--- a/app/shared/src/commonMain/composeResources/values/strings_settings.xml
+++ b/app/shared/src/commonMain/composeResources/values/strings_settings.xml
@@ -796,6 +796,8 @@
Asks your router (UPnP or NAT-PMP) to forward a port, so more peers can connect to this device.
Local peer discovery
Finds devices on your local network that download or share the same public torrents, so they exchange pieces directly.
+ Use the proxy
+ Reaches peers, HTTP trackers and web seeds through the proxy set under Network. DHT, uTP, UDP trackers and port forwarding are off through a proxy, and peers can't connect in. Takes effect when Ketch restarts.
Encryption hides what the traffic is, not who you connect to.
Extra trackers
No extra trackers
diff --git a/app/shared/src/commonMain/kotlin/com/linroid/ketch/app/ui/settings/BitTorrentSettings.kt b/app/shared/src/commonMain/kotlin/com/linroid/ketch/app/ui/settings/BitTorrentSettings.kt
index fb75bf258..59758f8e5 100644
--- a/app/shared/src/commonMain/kotlin/com/linroid/ketch/app/ui/settings/BitTorrentSettings.kt
+++ b/app/shared/src/commonMain/kotlin/com/linroid/ketch/app/ui/settings/BitTorrentSettings.kt
@@ -121,6 +121,8 @@ import ketch.app.shared.generated.resources.settings_torrent_upload_while_downlo
import ketch.app.shared.generated.resources.settings_torrent_upload_while_downloading_hint
import ketch.app.shared.generated.resources.settings_torrent_upload_wifi_only
import ketch.app.shared.generated.resources.settings_torrent_upload_wifi_only_hint
+import ketch.app.shared.generated.resources.settings_torrent_use_proxy
+import ketch.app.shared.generated.resources.settings_torrent_use_proxy_hint
import ketch.app.shared.generated.resources.settings_torrent_utp
import ketch.app.shared.generated.resources.settings_torrent_utp_hint
import org.jetbrains.compose.resources.StringResource
@@ -502,6 +504,15 @@ private fun ConnectionsGroup(
},
)
}
+ // The host builds the engine with it, so only this device's, and only from its next start.
+ if (onThisDevice) {
+ SettingsSwitchRow(
+ title = stringResource(Res.string.settings_torrent_use_proxy),
+ description = stringResource(Res.string.settings_torrent_use_proxy_hint),
+ checked = settings.useProxy,
+ onCheckedChange = { onChange(settings.copy(useProxy = it)) },
+ )
+ }
}
}
diff --git a/app/shared/src/iosMain/kotlin/com/linroid/ketch/app/MainViewController.kt b/app/shared/src/iosMain/kotlin/com/linroid/ketch/app/MainViewController.kt
index 834010c4b..4f4398e5e 100644
--- a/app/shared/src/iosMain/kotlin/com/linroid/ketch/app/MainViewController.kt
+++ b/app/shared/src/iosMain/kotlin/com/linroid/ketch/app/MainViewController.kt
@@ -127,7 +127,8 @@ private fun createInstanceManager(unreadableFiles: UnreadableFiles): InstanceMan
val instanceId = DeviceStore("$supportDir/$DEVICE_FILE_NAME").instanceId()
val torrentSource = TorrentDownloadSource(
TorrentConfig(stateDirectory = "$supportDir/torrent-state")
- .withSettings(config.torrent.engineSettings),
+ .withSettings(config.torrent.engineSettings)
+ .copy(proxy = config.torrentProxy),
// Paused until the path monitor reports the network, moments after launch.
uploadsPaused = config.torrent.uploadWifiOnly,
)
diff --git a/cli/README.md b/cli/README.md
index 71537cb9b..51d771679 100644
--- a/cli/README.md
+++ b/cli/README.md
@@ -1098,6 +1098,7 @@ on a server, changes made there, like the other download settings, last until it
| `trackers` | string[] | `[]` | Extra `http`, `https` or `udp` trackers that public torrents also announce to |
| `trackerList` | bool | `true` | Subscribe to the tracker lists at `trackerListUrls`, downloaded daily; their trackers are used after `trackers` |
| `trackerListUrls` | string[] | ngosang's [`trackers_best.txt`](https://github.com/ngosang/trackerslist) and XIU2's [`best.txt`](https://github.com/XIU2/TrackersListCollection) | `http` or `https` URLs of plain-text lists, one announce URL per line |
+| `useProxy` | bool | `false` | Send torrents through the manual proxy of `[download.proxy]`; without DHT, uTP, UDP trackers, local discovery or port forwarding. Read at start |
| `listenPort` | int | `0` | Port `ketch server` accepts peers on (TCP) and runs DHT on (UDP), 0-65535; `0` picks a free one at every start. The apps and the other commands always pick one, so they never compete for it |
| `upload` | string | `"while-downloading"` | What torrents upload to peers: `while-downloading` (verified pieces, until each torrent finishes), `off` or `seed` (finished torrents too, also after `ketch server` restarts). Peers you upload to see your IP address. Unknown values load as `off`, so a typo never turns uploading on. `ketch server` also takes it from `--torrent-upload` and `KETCH_TORRENT_UPLOAD` |
| `uploadLimit` | string | `"unlimited"` | Upload cap shared by all torrents, in the [speed limit format](#speed-limit-format); a value that cannot be read loads as unlimited |
diff --git a/cli/src/main/kotlin/com/linroid/ketch/cli/Main.kt b/cli/src/main/kotlin/com/linroid/ketch/cli/Main.kt
index 1ff48d437..930c51f41 100644
--- a/cli/src/main/kotlin/com/linroid/ketch/cli/Main.kt
+++ b/cli/src/main/kotlin/com/linroid/ketch/cli/Main.kt
@@ -13,6 +13,7 @@ import com.linroid.ketch.api.DownloadConfig
import com.linroid.ketch.api.DownloadPriority
import com.linroid.ketch.api.KetchApi
import com.linroid.ketch.api.Proxy
+import com.linroid.ketch.api.ProxyAddress
import com.linroid.ketch.api.SpeedLimit
import com.linroid.ketch.api.SpeedLimitMode
import com.linroid.ketch.api.log.KetchLogger
@@ -185,7 +186,7 @@ private fun runDownload(args: DownloadArgs.Download): Int {
config = config,
logger = Logger.console(quietLogLevel(ketchLogLevel)).above(progress),
additionalSources = listOf(
- FtpDownloadSource(), torrentSource(fileConfig.torrent),
+ FtpDownloadSource(), torrentSource(fileConfig.torrent, proxy = fileConfig.torrentProxy),
HlsDownloadSource(httpEngine), DashDownloadSource(httpEngine),
MetalinkDownloadSource(httpEngine), WebDavDownloadSource(httpEngine),
),
@@ -213,7 +214,7 @@ private fun runListFiles(args: DownloadArgs.Download): Int {
config = DownloadConfig(proxy = args.proxy ?: fileConfig.download.proxy),
logger = Logger.console(quietLogLevel(ketchLogLevel)),
additionalSources = listOf(
- FtpDownloadSource(), torrentSource(fileConfig.torrent),
+ FtpDownloadSource(), torrentSource(fileConfig.torrent, proxy = fileConfig.torrentProxy),
HlsDownloadSource(httpEngine), DashDownloadSource(httpEngine),
MetalinkDownloadSource(httpEngine), WebDavDownloadSource(httpEngine),
),
@@ -520,7 +521,8 @@ private fun runServer(args: Array): Int {
val instanceId = deviceInstanceId()
val httpEngine = KtorHttpEngine.withNetworkInterfaces()
- val torrents = torrentSource(mergedConfig.torrent, listenPort = mergedConfig.torrent.listenPort)
+ val torrents = torrentSource(mergedConfig.torrent, listenPort = mergedConfig.torrent.listenPort,
+ proxy = mergedConfig.torrentProxy)
val ketch = Ketch(
httpEngine = httpEngine,
taskStore = taskStore,
@@ -1124,7 +1126,8 @@ private fun runStandaloneMcp(
val httpEngine = KtorHttpEngine.withNetworkInterfaces()
// It serves an agent while the client runs, so it does not share finished torrents again when
// it starts; MCP offers no seeding tool to stop them.
- val torrents = torrentSource(fileConfig.torrent, restoreSeeding = false)
+ val torrents = torrentSource(fileConfig.torrent, restoreSeeding = false,
+ proxy = fileConfig.torrentProxy)
val ketch = Ketch(
httpEngine = httpEngine,
taskStore = taskStore,
@@ -1274,17 +1277,19 @@ private fun printMcpUsage() {
* and uploads as `[torrent] upload` and `uploadLimit` say. Only `ketch server` passes a
* [listenPort], [TorrentSettings.listenPort]: the other commands may run beside it, so they let
* the system pick a free port. [restoreSeeding] shares finished torrents again when the engine
- * starts, as `[torrent] upload = "seed"` asks; `ketch mcp --standalone` turns it off.
+ * starts, as `[torrent] upload = "seed"` asks; `ketch mcp --standalone` turns it off. Torrents
+ * go through [proxy], `[download.proxy]` while `[torrent] useProxy` is on.
*/
private fun torrentSource(
settings: TorrentSettings,
listenPort: Int = 0,
restoreSeeding: Boolean = true,
+ proxy: ProxyAddress? = null,
) = TorrentDownloadSource(
config = TorrentConfig(
listenPort = listenPort,
stateDirectory = File(defaultConfigDir(), "torrent-state").path,
- ).withSettings(settings.engineSettings),
+ ).withSettings(settings.engineSettings).copy(proxy = proxy),
restoreSeeding = restoreSeeding,
)
diff --git a/config/src/commonMain/kotlin/com/linroid/ketch/config/KetchConfig.kt b/config/src/commonMain/kotlin/com/linroid/ketch/config/KetchConfig.kt
index 5bf980b05..30af265ca 100644
--- a/config/src/commonMain/kotlin/com/linroid/ketch/config/KetchConfig.kt
+++ b/config/src/commonMain/kotlin/com/linroid/ketch/config/KetchConfig.kt
@@ -1,6 +1,8 @@
package com.linroid.ketch.config
import com.linroid.ketch.api.DownloadConfig
+import com.linroid.ketch.api.Proxy
+import com.linroid.ketch.api.ProxyAddress
import com.linroid.ketch.api.SpeedSettings
import kotlinx.serialization.Serializable
@@ -41,6 +43,13 @@ data class KetchConfig(
val power: PowerSettings = PowerSettings(),
val hooks: List = emptyList(),
) {
+ /**
+ * The proxy torrents go through: the manual one of `[download.proxy]` while
+ * `[torrent] useProxy` is on, else `null`, for direct connections.
+ */
+ val torrentProxy: ProxyAddress?
+ get() = (download.proxy as? Proxy.Manual)?.address?.takeIf { torrent.useProxy }
+
companion object {
/**
* What runs in place of a config file that cannot be read: the defaults, except that
diff --git a/config/src/commonMain/kotlin/com/linroid/ketch/config/TorrentSettings.kt b/config/src/commonMain/kotlin/com/linroid/ketch/config/TorrentSettings.kt
index 9f0d0a9d9..8b3790a88 100644
--- a/config/src/commonMain/kotlin/com/linroid/ketch/config/TorrentSettings.kt
+++ b/config/src/commonMain/kotlin/com/linroid/ketch/config/TorrentSettings.kt
@@ -154,6 +154,9 @@ internal object LenientSpeedLimitSerializer : KSerializer {
* less sets no such goal, the default.
* @property blocklist peers never connected to and never accepted: addresses, CIDR blocks
* (`203.0.113.0/24`) and ranges (`203.0.113.1-203.0.113.99`); none by default.
+ * @property useProxy whether torrents go through the manual proxy of `[download.proxy]`, read
+ * when the engine starts; off by default, and without effect while that proxy is not manual.
+ * Through a proxy there is no DHT, uTP, UDP tracker, local discovery or port mapping.
*/
@Serializable
data class TorrentSettings(
@@ -173,6 +176,7 @@ data class TorrentSettings(
val seedRatioPercent: Int = 0,
val seedTimeMinutes: Int = 0,
val blocklist: List = emptyList(),
+ val useProxy: Boolean = false,
) {
init {
require(listenPort in 0..65535) { "listenPort must be between 0 and 65535" }
diff --git a/config/src/commonTest/kotlin/com/linroid/ketch/config/TorrentSettingsTest.kt b/config/src/commonTest/kotlin/com/linroid/ketch/config/TorrentSettingsTest.kt
index 571dafee2..0a558130c 100644
--- a/config/src/commonTest/kotlin/com/linroid/ketch/config/TorrentSettingsTest.kt
+++ b/config/src/commonTest/kotlin/com/linroid/ketch/config/TorrentSettingsTest.kt
@@ -7,6 +7,8 @@ import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
+import kotlin.test.assertNotNull
+import kotlin.test.assertNull
class TorrentSettingsTest {
@@ -295,4 +297,29 @@ class TorrentSettingsTest {
assertEquals(null, replaced.trackerListUrl)
assertEquals(listOf("https://n/t"), replaced.trackerListAddresses)
}
+
+ @Test
+ fun `torrents take the manual download proxy only when asked to`() {
+ fun config(torrent: String) = ConfigStore.toml.decodeFromString(
+ KetchConfig.serializer(),
+ """
+ |[download.proxy]
+ |mode = "manual"
+ |url = "socks5://127.0.0.1:1080"
+ |username = "ketch"
+ |password = "secret"
+ |
+ |[torrent]
+ |$torrent
+ """.trimMargin(),
+ )
+
+ // Off unless the file says so: a proxy set for downloads does not move torrents onto it.
+ assertNull(config("utp = true").torrentProxy)
+ val proxy = assertNotNull(config("useProxy = true").torrentProxy)
+ assertEquals("socks5://127.0.0.1:1080", proxy.url)
+ assertEquals("ketch", proxy.username)
+ // Nor is there one to use while downloads follow the system or connect directly.
+ assertNull(KetchConfig(torrent = TorrentSettings(useProxy = true)).torrentProxy)
+ }
}
diff --git a/docs/proxy.md b/docs/proxy.md
index b24c4eb6f..4e472588d 100644
--- a/docs/proxy.md
+++ b/docs/proxy.md
@@ -56,13 +56,33 @@ it adds to. `ketch add` refuses these options for a device that does not support
| Traffic | Proxy |
| --- | --- |
| HTTP(S) downloads, HLS and DASH streams, and previews (`resolve`) | The proxy setting |
-| FTP downloads, BitTorrent peers | Always direct |
-| BitTorrent trackers over HTTP, `.torrent` files and web seeds | The system's settings |
+| FTP downloads | Always direct |
+| BitTorrent peers | Direct, or the manual proxy with `[torrent] useProxy` (below) |
+| BitTorrent trackers over HTTP, `.torrent` files and web seeds | The system's settings, or the manual proxy with `[torrent] useProxy` |
| AI discovery's page fetches | Never proxied |
A download reads the setting when it starts or resumes, so a change applies to downloads started
afterwards.
+## Torrents through the proxy
+
+Torrents connect directly unless you ask otherwise: with a manual proxy set, turn on
+**Settings → BitTorrent → Connections → Use the proxy** (`[torrent] useProxy = true`) and
+restart Ketch. Peers are then reached through the proxy, SOCKS5 or HTTP `CONNECT`, and so are
+HTTP trackers, web seeds, `.torrent` files and the tracker lists. The bypass list does not apply
+to torrents.
+
+A proxy carries TCP connections Ketch opens and nothing else, so through one Ketch switches off
+what would otherwise go around it and reveal your address: DHT, uTP, UDP trackers, local peer
+discovery and port forwarding, and other peers cannot connect to you. Expect fewer peers,
+especially for torrents that rely on DHT or UDP trackers; adding `http` or `https` trackers
+helps. UDP trackers are skipped at once, so they do not hold up the `http` ones listed after
+them. Peers given by host name are resolved by the proxy, not by your system, and the blocklist
+then applies to peers given by address only. Host names of HTTP trackers are resolved by a
+SOCKS5 proxy; with an HTTP proxy your system may still look them up. The setting is read
+when the torrent engine starts, so changing it, or the proxy itself, takes effect after a
+restart.
+
## Proxy URLs
- `http://host:port` is an HTTP proxy. Plain HTTP requests go to it with absolute URLs, and HTTPS
diff --git a/docs/torrent.md b/docs/torrent.md
index ce36e8b8b..dd9e35ef3 100644
--- a/docs/torrent.md
+++ b/docs/torrent.md
@@ -405,6 +405,7 @@ listenPort = 16881 # read by `ketch server` only; 0 (default) picks a
| `uploadLimit` | unlimited | Upload cap shared by all torrents. |
| `seedRatioPercent` | 0 | A finished torrent stops seeding once it uploaded this percentage of its size, in all; 0 sets no goal. |
| `seedTimeMinutes` | 0 | A finished torrent stops seeding after this many minutes since it last began; 0 sets no goal. |
+| `useProxy` | `false` | Sends torrents through the manual proxy of `[download.proxy]`, without DHT, uTP, UDP trackers, local discovery or port forwarding; read at start. See [torrents through the proxy](proxy.md#torrents-through-the-proxy). |
| `blocklist` | `[]` | Peers never connected to or accepted, by address, CIDR block or range. See [blocking peers by address](#blocking-peers-by-address). |
| `uploadWifiOnly` | `true` | Pause uploads off Wi-Fi; read by the Android and iOS apps only. See [only on Wi-Fi](#only-on-wi-fi). |
| `encryption` | `preferred` | Protocol encryption; see [encryption](#encryption). An unknown value loads as `preferred`. |
diff --git a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentConfig.kt b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentConfig.kt
index 5fcbde3e2..193f6eaaa 100644
--- a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentConfig.kt
+++ b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentConfig.kt
@@ -1,5 +1,6 @@
package com.linroid.ketch.torrent
+import com.linroid.ketch.api.ProxyAddress
import com.linroid.ketch.torrent.tracker.TRACKER_SCRAPE_WORKSPACE_BYTES
/**
@@ -52,6 +53,13 @@ data class TorrentConfig(
* share pieces directly. Private and tracker-only torrents never take part. Android, JVM only.
*/
val localDiscovery: Boolean = true,
+ /**
+ * A SOCKS5 or HTTP proxy every torrent reaches its peers, HTTP trackers and web seeds through,
+ * read as the engine starts; `null` connects directly. While set, what a proxy cannot carry is
+ * off rather than sent around it: DHT, uTP, UDP trackers, local discovery and port mapping,
+ * and peers cannot connect in.
+ */
+ val proxy: ProxyAddress? = null,
val maxActiveTorrents: Int = 5,
val connectionsPerTorrent: Int = 100,
val enableUpload: Boolean = false,
diff --git a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentDownloadSource.kt b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentDownloadSource.kt
index a0f0c183e..e74c3cdbd 100644
--- a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentDownloadSource.kt
+++ b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentDownloadSource.kt
@@ -4,6 +4,7 @@ import com.linroid.ketch.api.DownloadConfig
import com.linroid.ketch.api.FileConflict
import com.linroid.ketch.api.FileSelectionMode
import com.linroid.ketch.api.KetchError
+import com.linroid.ketch.api.Proxy
import com.linroid.ketch.api.KetchFeatures
import com.linroid.ketch.api.ResolveInput
import com.linroid.ketch.api.ResolvedSource
@@ -62,8 +63,10 @@ import com.linroid.ketch.torrent.magnet.MagnetUri
import com.linroid.ketch.torrent.metainfo.TorrentMetadata
import com.linroid.ketch.torrent.metainfo.TorrentV2Document
import com.linroid.ketch.torrent.metainfo.V2InfoHash
+import com.linroid.ketch.torrent.net.ProxiedTorrentNetwork
import com.linroid.ketch.torrent.net.ResponseTooLargeException
import com.linroid.ketch.torrent.net.TorrentHttp
+import com.linroid.ketch.torrent.net.createTorrentNetwork
import com.linroid.ketch.torrent.storage.TorrentCheckpoint
import com.linroid.ketch.torrent.storage.moveTorrentTree
import com.linroid.ketch.torrent.storage.TorrentPieceStore
@@ -157,7 +160,16 @@ class TorrentDownloadSource(
private val multicastLock: TorrentMulticastLock? = null,
uploadsPaused: Boolean = false,
) : DownloadSource {
- private val httpDelegate = lazy { httpEngine?.let { TorrentHttp(it) } ?: TorrentHttp.default() }
+ private val httpDelegate = lazy {
+ // Trackers, web seeds and tracker lists go through the proxy too; an engine that cannot use
+ // one fails them rather than connect around it.
+ val proxied = config.proxy?.let { Proxy.Manual(it) }
+ when {
+ proxied == null -> httpEngine?.let { TorrentHttp(it) } ?: TorrentHttp.default()
+ httpEngine != null -> TorrentHttp(httpEngine.withProxy(proxied))
+ else -> TorrentHttp.default(proxied)
+ }
+ }
private val http by httpDelegate
private val additionalTrackers = AtomicReference(config.additionalTrackers)
private val listedTrackers = AtomicReference(emptyList())
@@ -198,9 +210,19 @@ class TorrentDownloadSource(
internal var engineFactory: (TorrentConfig) -> TorrentEngine = {
// May map ports and join the LAN's discovery group; TorrentConfig.portMapping and
// localDiscovery, which their setters change, decide if it does.
- KotlinTorrentEngine(it, http = http, portMapping = true,
- localDiscovery = ::openLsdMulticast, multicastLock = multicastLock,
- magnetLookupTime = magnetLookupTime)
+ val proxy = it.proxy
+ if (proxy == null) {
+ KotlinTorrentEngine(it, http = http, portMapping = true,
+ localDiscovery = ::openLsdMulticast, multicastLock = multicastLock,
+ magnetLookupTime = magnetLookupTime)
+ } else {
+ // Nothing that a proxy cannot carry: no DHT, uTP, port mapping or LAN discovery, whatever
+ // the settings say, and a network that sends no datagram at all.
+ log.i { "Torrents use a ${proxy.type} proxy: DHT, uTP, UDP trackers and port mapping off" }
+ KotlinTorrentEngine(it.copy(dhtEnabled = false, utpEnabled = false, portMapping = false,
+ localDiscovery = false), ProxiedTorrentNetwork(createTorrentNetwork(), proxy),
+ http = http, magnetLookupTime = magnetLookupTime, proxied = true)
+ }
}
private val scope = CoroutineScope(SupervisorJob() + TorrentDispatchers.disk)
private val trackerListSubscription = TrackerLists(
diff --git a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/engine/KotlinTorrentEngine.kt b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/engine/KotlinTorrentEngine.kt
index 408a83022..6ff018d8e 100644
--- a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/engine/KotlinTorrentEngine.kt
+++ b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/engine/KotlinTorrentEngine.kt
@@ -224,6 +224,12 @@ internal class KotlinTorrentEngine(
multicastLock: TorrentMulticastLock? = null,
/** Test hook: how long one magnet metadata lookup runs. */
private val magnetLookupTime: Duration = MAGNET_LOOKUP_TIME,
+ /**
+ * The network reaches peers through a proxy: a peer's host name is left to the proxy to
+ * resolve, never looked up here, and UDP trackers, which a proxy cannot carry, are refused at
+ * once rather than waited for.
+ */
+ private val proxied: Boolean = false,
/** Test hook: how long a magnet metadata lookup goes on once nothing waits for it. */
magnetLookupGrace: Duration = ABANDONED_LOOKUP_GRACE,
/** Test hook: the file system v1 stores keep their payload in. */
@@ -274,7 +280,8 @@ internal class KotlinTorrentEngine(
utpEndpoints.load()[host]
}
private val connectionBudget = TorrentConnectionBudget(utpFallback, config.maxConnections)
- private val network = TorrentPeerEncryption(connectionBudget, config.encryption)
+ private val network = TorrentPeerEncryption(connectionBudget, config.encryption,
+ resolveNames = !proxied)
private val ipFilter = AtomicReference(TorrentIpFilter.NONE)
init {
@@ -315,7 +322,13 @@ internal class KotlinTorrentEngine(
exchangeBudgets.cache, magnetLookupGrace)
/** Magnet metadata lookups running, those nothing waits for in their grace too; for tests. */
internal val pendingMetadataLookups: Int get() = cache.pendingFetches
- private val tracker = TorrentTracker(http, network)
+ private val tracker = if (proxied) {
+ TorrentTracker(http, network, resolve = {
+ throw okio.IOException("UDP trackers cannot be reached through a proxy")
+ })
+ } else {
+ TorrentTracker(http, network)
+ }
// One peer ID for every peer, tracker and metadata lookup (BEP 20), as private trackers check.
private val peerId = TorrentClient.peerId()
private val mutex = Mutex()
@@ -965,7 +978,7 @@ internal class KotlinTorrentEngine(
supervisorScope {
launch {
for (text in magnet.explicitPeers) {
- attempt { resolveEndpoint(text) }?.forEach { output.send(it) }
+ attempt { resolveEndpoint(text, !proxied) }?.forEach { output.send(it) }
}
}
suspend fun announce(tiers: TrackerTiers) {
@@ -1363,7 +1376,7 @@ internal class KotlinTorrentEngine(
magnetUri?.let { uri -> launch {
MagnetUri.parse(uri).explicitPeers.forEach { endpoint ->
attempt {
- resolveEndpoint(endpoint).forEach {
+ resolveEndpoint(endpoint, !proxied).forEach {
sink.offer(it, explicitTopic, PeerOrigin.EXPLICIT)
}
}
@@ -1473,7 +1486,7 @@ internal class KotlinTorrentEngine(
}
magnetUri?.let { uri -> launch {
MagnetUri.parse(uri).explicitPeers.forEach { text ->
- attempt { resolveEndpoint(text) }?.forEach { output.send(it) }
+ attempt { resolveEndpoint(text, !proxied) }?.forEach { output.send(it) }
}
} }
// The peers that sent its metadata.
@@ -1985,10 +1998,15 @@ private suspend fun attempt(label: String? = null, block: suspend () -> T):
null
}
-internal suspend fun resolveEndpoint(value: String): List {
+/**
+ * The endpoints `host:port` in [value] names: every address its host resolves to, or with
+ * [resolve] off the host as it is, for a proxy to resolve.
+ */
+internal suspend fun resolveEndpoint(value: String, resolve: Boolean = true): List {
val port = value.substringAfterLast(':').toIntOrNull()
require(port != null && port in 1..65535) { "Invalid peer endpoint" }
val host = value.substringBeforeLast(':').removeSurrounding("[", "]")
+ if (!resolve) return listOf(PeerEndpoint(numericAddress(host)?.let(::numericHost) ?: host, port))
return resolveTorrentHost(host).map { PeerEndpoint(numericHost(it), port) }
}
diff --git a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/mse/TorrentPeerEncryption.kt b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/mse/TorrentPeerEncryption.kt
index c57e03413..410d702c9 100644
--- a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/mse/TorrentPeerEncryption.kt
+++ b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/mse/TorrentPeerEncryption.kt
@@ -37,6 +37,11 @@ internal class TorrentPeerEncryption(
private val keyPairs: () -> MseKeyPair = ::MseKeyPair,
/** Looks a peer's host name up when a filter is set, so the filter sees its address. */
private val resolve: suspend (String) -> List = ::resolveTorrentHost,
+ /**
+ * Off when a proxy resolves host names: a peer named by host is then dialed by that name,
+ * unfiltered, rather than looked up here, which would tell the local resolver of it.
+ */
+ private val resolveNames: Boolean = true,
) : TorrentNetwork by delegate {
private val log = KetchLogger("TorrentEngine")
private val current = AtomicReference(policy)
@@ -62,6 +67,7 @@ internal class TorrentPeerEncryption(
if (filter.blocks(remote.host)) throw IOException("The peer's address is blocked")
return remote
}
+ if (!resolveNames) return remote
val address = resolve(remote.host).map(::numericHost).firstOrNull { !filter.blocks(it) }
?: throw IOException("The peer's address is blocked")
return PeerEndpoint(address, remote.port)
diff --git a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/net/ProxiedTorrentNetwork.kt b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/net/ProxiedTorrentNetwork.kt
new file mode 100644
index 000000000..0cff8c75c
--- /dev/null
+++ b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/net/ProxiedTorrentNetwork.kt
@@ -0,0 +1,136 @@
+package com.linroid.ketch.torrent.net
+
+import com.linroid.ketch.api.ProxyAddress
+import kotlinx.coroutines.awaitCancellation
+import okio.IOException
+import kotlin.io.encoding.Base64
+
+/**
+ * A network that reaches peers only through [proxy], so no peer, tracker or router learns this
+ * machine's address from it: TCP connections are tunneled (SOCKS5 `CONNECT`, RFC 1928 with
+ * RFC 1929 passwords, or HTTP `CONNECT`), with host names left to the proxy to resolve. What a
+ * proxy cannot carry goes nowhere rather than around it: UDP sockets ([bindUdp]) send nothing
+ * and receive nothing, which silences DHT, uTP, UDP trackers and port mapping, and the peer
+ * listener ([listen]) is bound to loopback, as peers could only reach it at the address the
+ * proxy hides.
+ */
+internal class ProxiedTorrentNetwork(
+ private val delegate: TorrentNetwork,
+ private val proxy: ProxyAddress,
+) : TorrentNetwork {
+ override suspend fun connect(remote: PeerEndpoint): TorrentConnection {
+ val tunnel = delegate.connect(PeerEndpoint(proxy.host, proxy.port))
+ try {
+ when (proxy.type) {
+ ProxyAddress.Type.SOCKS5 -> socks5(tunnel, remote)
+ ProxyAddress.Type.HTTP -> httpConnect(tunnel, remote)
+ }
+ } catch (e: Throwable) {
+ tunnel.close()
+ throw e
+ }
+ return Tunneled(tunnel, remote)
+ }
+
+ override suspend fun listen(local: PeerEndpoint): TorrentListener =
+ delegate.listen(PeerEndpoint(if (':' in local.host) "::1" else "127.0.0.1", local.port))
+
+ override suspend fun bindUdp(local: PeerEndpoint): TorrentDatagramSocket = Silent(local)
+
+ override fun close() = delegate.close()
+
+ private suspend fun socks5(tunnel: TorrentConnection, remote: PeerEndpoint) {
+ val username = proxy.username
+ val methods = if (username != null) byteArrayOf(NO_AUTH, PASSWORD) else byteArrayOf(NO_AUTH)
+ tunnel.write(byteArrayOf(VERSION, methods.size.toByte()) + methods)
+ val chosen = tunnel.readExactly(2)
+ if (chosen[0] != VERSION) throw IOException("The proxy does not speak SOCKS5")
+ when (chosen[1]) {
+ NO_AUTH -> Unit
+ PASSWORD -> {
+ val name = username?.encodeToByteArray()
+ val secret = proxy.password.orEmpty().encodeToByteArray()
+ if (name == null || name.size !in 1..255 || secret.size > 255) {
+ throw IOException("The proxy asks for a user name and password it can take")
+ }
+ tunnel.write(byteArrayOf(1, name.size.toByte()) + name +
+ byteArrayOf(secret.size.toByte()) + secret)
+ if (tunnel.readExactly(2)[1] != 0.toByte()) {
+ throw IOException("The proxy refused the user name or password")
+ }
+ }
+ else -> throw IOException("The proxy accepts no sign-in this client offers")
+ }
+ val address = numericAddress(remote.host)
+ val target = when {
+ address != null && address.size == 4 -> byteArrayOf(IPV4) + address
+ address != null -> byteArrayOf(IPV6) + address
+ else -> {
+ val name = remote.host.encodeToByteArray()
+ if (name.size > 255) throw IOException("The host name is too long for the proxy")
+ byteArrayOf(DOMAIN, name.size.toByte()) + name
+ }
+ }
+ tunnel.write(byteArrayOf(VERSION, CONNECT, 0) + target +
+ byteArrayOf((remote.port shr 8).toByte(), remote.port.toByte()))
+ val reply = tunnel.readExactly(4)
+ if (reply[0] != VERSION || reply[1] != 0.toByte()) {
+ throw IOException("The proxy could not connect (SOCKS5 reply ${reply[1].toInt() and 0xff})")
+ }
+ // The address the proxy bound, which nothing here uses.
+ val bound = when (reply[3]) {
+ IPV4 -> 4
+ IPV6 -> 16
+ DOMAIN -> tunnel.readExactly(1)[0].toInt() and 0xff
+ else -> throw IOException("The proxy sent an address of an unknown kind")
+ }
+ tunnel.readExactly(bound + 2)
+ }
+
+ private suspend fun httpConnect(tunnel: TorrentConnection, remote: PeerEndpoint) {
+ val host = if (':' in remote.host) "[${remote.host}]" else remote.host
+ val request = StringBuilder("CONNECT $host:${remote.port} HTTP/1.1\r\n")
+ .append("Host: $host:${remote.port}\r\n")
+ proxy.username?.let { name ->
+ val credentials = "$name:${proxy.password.orEmpty()}".encodeToByteArray()
+ request.append("Proxy-Authorization: Basic ${Base64.Default.encode(credentials)}\r\n")
+ }
+ tunnel.write(request.append("\r\n").toString().encodeToByteArray())
+ // One byte at a time: what follows the blank line already belongs to the peer.
+ val head = StringBuilder()
+ while (!head.endsWith("\r\n\r\n")) {
+ if (head.length >= MAX_HTTP_HEAD) throw IOException("The proxy's answer is too long")
+ head.append((tunnel.readExactly(1)[0].toInt() and 0xff).toChar())
+ }
+ val status = head.lineSequence().first().split(' ').getOrNull(1)?.toIntOrNull()
+ if (status == null || status !in 200..299) {
+ throw IOException("The proxy could not connect (HTTP ${status ?: "answer unreadable"})")
+ }
+ }
+
+ /** The tunnel [connection], named after the peer it reaches. */
+ private class Tunneled(
+ private val connection: TorrentConnection,
+ override val remote: PeerEndpoint,
+ ) : TorrentConnection by connection
+
+ /** A UDP socket that sends nothing and hears nothing. */
+ private class Silent(override val local: PeerEndpoint) : TorrentDatagramSocket {
+ override suspend fun send(remote: PeerEndpoint, bytes: ByteArray) = Unit
+
+ override suspend fun receive(): TorrentDatagram = awaitCancellation()
+
+ override fun close() = Unit
+ }
+
+ private companion object {
+ const val VERSION: Byte = 5
+ const val NO_AUTH: Byte = 0
+ const val PASSWORD: Byte = 2
+ const val CONNECT: Byte = 1
+ const val IPV4: Byte = 1
+ const val DOMAIN: Byte = 3
+ const val IPV6: Byte = 4
+ const val MAX_HTTP_HEAD = 8192
+ }
+}
diff --git a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/net/TorrentHttp.kt b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/net/TorrentHttp.kt
index 0f4419c52..edbd1d17c 100644
--- a/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/net/TorrentHttp.kt
+++ b/library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/net/TorrentHttp.kt
@@ -1,6 +1,7 @@
package com.linroid.ketch.torrent.net
import com.linroid.ketch.api.KetchError
+import com.linroid.ketch.api.Proxy
import com.linroid.ketch.core.engine.HttpEngine
import com.linroid.ketch.engine.KtorHttpEngine
import com.linroid.ketch.torrent.monotonicClock
@@ -97,6 +98,16 @@ internal class TorrentHttp(
companion object {
fun default(): TorrentHttp = TorrentHttp(KtorHttpEngine(logRequests = false), ownsEngine = true)
+
+ /** [default], with every request sent through [proxy]. */
+ fun default(proxy: Proxy): TorrentHttp {
+ val engine = KtorHttpEngine(logRequests = false)
+ // The proxied view shares the engine's clients and closes nothing: close the engine.
+ val proxied = engine.withProxy(proxy)
+ return TorrentHttp(object : HttpEngine by proxied {
+ override fun close() = engine.close()
+ }, ownsEngine = true)
+ }
}
}
diff --git a/library/torrent/src/commonTest/kotlin/com/linroid/ketch/torrent/mse/TorrentPeerFilterTest.kt b/library/torrent/src/commonTest/kotlin/com/linroid/ketch/torrent/mse/TorrentPeerFilterTest.kt
index 4e7398d1e..da6570b55 100644
--- a/library/torrent/src/commonTest/kotlin/com/linroid/ketch/torrent/mse/TorrentPeerFilterTest.kt
+++ b/library/torrent/src/commonTest/kotlin/com/linroid/ketch/torrent/mse/TorrentPeerFilterTest.kt
@@ -57,6 +57,26 @@ class TorrentPeerFilterTest {
assertEquals(listOf(PeerEndpoint("198.51.100.9", 6881)), delegate.dialed)
}
+ @Test
+ fun `through a proxy a peer listed by name is dialed by that name and never looked up`() =
+ runTest {
+ val delegate = Refusing()
+ val lookups = mutableListOf()
+ val network = TorrentPeerEncryption(delegate, TorrentEncryption.DISABLED,
+ resolve = { host -> lookups += host; emptyList() }, resolveNames = false)
+ network.filter = { TorrentIpFilter.parse(listOf("203.0.113.0/24")) }
+
+ assertFailsWith { network.dial(PeerEndpoint("peer.example", 6881), tag) }
+ val blocked = assertFailsWith {
+ network.dial(PeerEndpoint("203.0.113.7", 6881), tag)
+ }
+
+ // The proxy resolves the name; addresses are still filtered.
+ assertEquals(listOf(PeerEndpoint("peer.example", 6881)), delegate.dialed)
+ assertTrue(lookups.isEmpty())
+ assertTrue("blocked" in blocked.message.orEmpty())
+ }
+
@Test
fun `a peer listed by name is dialed at an address the filter allows`() = runTest {
val delegate = Refusing()
diff --git a/library/torrent/src/commonTest/kotlin/com/linroid/ketch/torrent/net/ProxiedTorrentNetworkTest.kt b/library/torrent/src/commonTest/kotlin/com/linroid/ketch/torrent/net/ProxiedTorrentNetworkTest.kt
new file mode 100644
index 000000000..f8195a7ab
--- /dev/null
+++ b/library/torrent/src/commonTest/kotlin/com/linroid/ketch/torrent/net/ProxiedTorrentNetworkTest.kt
@@ -0,0 +1,234 @@
+package com.linroid.ketch.torrent.net
+
+import com.linroid.ketch.api.ProxyAddress
+import com.linroid.ketch.torrent.engine.resolveEndpoint
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.cancelAndJoin
+import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.isActive
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.withContext
+import kotlinx.coroutines.withTimeout
+import kotlinx.coroutines.withTimeoutOrNull
+import okio.IOException
+import kotlin.io.encoding.Base64
+import kotlin.test.Test
+import kotlin.test.assertContentEquals
+import kotlin.test.assertEquals
+import kotlin.test.assertFailsWith
+import kotlin.test.assertNull
+import kotlin.test.assertTrue
+
+/** Peer connections through a SOCKS5 or HTTP proxy on loopback, and what must not leave. */
+class ProxiedTorrentNetworkTest {
+ /** What a scripted proxy saw of the tunnels it was asked for. */
+ private class Seen {
+ val targets = mutableListOf()
+ val credentials = mutableListOf()
+ }
+
+ private class Fixture(val network: TorrentNetwork, val peer: PeerEndpoint, val seen: Seen) {
+ lateinit var proxy: PeerEndpoint
+
+ fun through(type: ProxyAddress.Type, user: String? = null, password: String? = null) =
+ ProxiedTorrentNetwork(network, ProxyAddress(type, proxy.host, proxy.port, user, password))
+ }
+
+ /** Runs [block] beside a peer that echoes what it is sent, 5 bytes at a time. */
+ private fun fixture(
+ proxy: suspend CoroutineScope.(Fixture, TorrentConnection) -> Unit,
+ block: suspend (Fixture) -> Unit,
+ ) = runTest {
+ withContext(Dispatchers.Default) {
+ withTimeout(20_000) {
+ coroutineScope {
+ val network = createTorrentNetwork()
+ val peers = network.listen(PeerEndpoint("127.0.0.1", 0))
+ val proxies = network.listen(PeerEndpoint("127.0.0.1", 0))
+ val fixture = Fixture(network, peers.local, Seen())
+ fixture.proxy = proxies.local
+ val serving = launch {
+ launch {
+ while (isActive) {
+ val connection = peers.accept()
+ launch {
+ try {
+ while (isActive) connection.write(connection.readExactly(5))
+ } catch (_: IOException) {
+ connection.close()
+ }
+ }
+ }
+ }
+ while (isActive) {
+ val connection = proxies.accept()
+ launch {
+ try {
+ proxy(fixture, connection)
+ } catch (_: IOException) {
+ connection.close()
+ }
+ }
+ }
+ }
+ try {
+ block(fixture)
+ } finally {
+ serving.cancelAndJoin()
+ peers.close()
+ proxies.close()
+ network.close()
+ }
+ }
+ }
+ }
+ }
+
+ /** Copies both ways between [client] and the test's peer until either side ends. */
+ private suspend fun CoroutineScope.relay(fixture: Fixture, client: TorrentConnection) {
+ val peer = fixture.network.connect(fixture.peer)
+ for ((from, to) in listOf(client to peer, peer to client)) {
+ launch {
+ try {
+ while (isActive) to.write(from.readExactly(5))
+ } catch (_: IOException) {
+ // Either end hung up: so does the other.
+ from.close()
+ to.close()
+ }
+ }
+ }
+ }
+
+ /** A SOCKS5 proxy asking for [password] when set, tunneling every request to the test's peer. */
+ private fun socks5(password: String? = null):
+ suspend CoroutineScope.(Fixture, TorrentConnection) -> Unit = { fixture, client ->
+ val greeting = client.readExactly(2)
+ val methods = client.readExactly(greeting[1].toInt())
+ if (password == null) {
+ client.write(byteArrayOf(5, 0))
+ } else {
+ assertTrue(2.toByte() in methods)
+ client.write(byteArrayOf(5, 2))
+ val name = client.readExactly(client.readExactly(2)[1].toInt()).decodeToString()
+ val secret = client.readExactly(client.readExactly(1)[0].toInt()).decodeToString()
+ fixture.seen.credentials += "$name:$secret"
+ client.write(byteArrayOf(1, if (secret == password) 0 else 1))
+ }
+ val request = client.readExactly(4)
+ val host = when (request[3].toInt()) {
+ 1 -> numericHost(client.readExactly(4))
+ 4 -> numericHost(client.readExactly(16))
+ else -> client.readExactly(client.readExactly(1)[0].toInt()).decodeToString()
+ }
+ val port = client.readExactly(2)
+ .let { (it[0].toInt() and 0xff shl 8) or (it[1].toInt() and 0xff) }
+ fixture.seen.targets += "$host:$port"
+ if (host == "refused.example") {
+ client.write(byteArrayOf(5, 5, 0, 1, 0, 0, 0, 0, 0, 0))
+ } else {
+ client.write(byteArrayOf(5, 0, 0, 1, 127, 0, 0, 1, 0, 80))
+ relay(fixture, client)
+ }
+ }
+
+ /** An HTTP proxy that tunnels `CONNECT` requests carrying [authorization], when it asks one. */
+ private fun http(authorization: String? = null):
+ suspend CoroutineScope.(Fixture, TorrentConnection) -> Unit = { fixture, client ->
+ val head = StringBuilder()
+ while (!head.endsWith("\r\n\r\n")) head.append(client.readExactly(1)[0].toInt().toChar())
+ val lines = head.lines()
+ fixture.seen.targets += lines.first().split(' ')[1]
+ val sent = lines.firstOrNull { it.startsWith("Proxy-Authorization: ") }?.substringAfter(": ")
+ sent?.let { fixture.seen.credentials += it }
+ if (authorization != null && sent != authorization) {
+ client.write("HTTP/1.1 407 Proxy Authentication Required\r\n\r\n".encodeToByteArray())
+ } else {
+ client.write("HTTP/1.1 200 Connection established\r\n\r\n".encodeToByteArray())
+ relay(fixture, client)
+ }
+ }
+
+ private suspend fun TorrentConnection.echoes(text: String) {
+ write(text.encodeToByteArray())
+ assertContentEquals(text.encodeToByteArray(), readExactly(text.length))
+ }
+
+ @Test
+ fun socks5_tunnelsToThePeer_byAddressAndByName() = fixture(socks5()) { fixture ->
+ val network = fixture.through(ProxyAddress.Type.SOCKS5)
+
+ val connection = network.connect(fixture.peer)
+ assertEquals(fixture.peer, connection.remote)
+ connection.echoes("hello")
+ connection.echoes("again")
+ connection.close()
+ // A host name is the proxy's to resolve: no lookup happens here.
+ val named = PeerEndpoint("peer.example", 6881)
+ network.connect(named).apply {
+ assertEquals(named, remote)
+ echoes("named")
+ close()
+ }
+
+ assertEquals(listOf("127.0.0.1:${fixture.peer.port}", "peer.example:6881"),
+ fixture.seen.targets)
+ assertFailsWith { network.connect(PeerEndpoint("refused.example", 1)) }
+ }
+
+ @Test
+ fun socks5_signsInWithItsPassword_andFailsWhenItIsRefused() = fixture(socks5("s3cret")) { f ->
+ f.through(ProxyAddress.Type.SOCKS5, "ketch", "s3cret").connect(f.peer).apply {
+ echoes("hello")
+ close()
+ }
+ assertFailsWith {
+ f.through(ProxyAddress.Type.SOCKS5, "ketch", "wrong").connect(f.peer)
+ }
+ assertEquals(listOf("ketch:s3cret", "ketch:wrong"), f.seen.credentials)
+ // Refused before any tunnel was asked for.
+ assertEquals(1, f.seen.targets.size)
+ }
+
+ @Test
+ fun httpConnect_tunnelsToThePeer_withItsCredentials() {
+ val basic = "Basic " + Base64.Default.encode("ketch:s3cret".encodeToByteArray())
+ fixture(http(basic)) { f ->
+ f.through(ProxyAddress.Type.HTTP, "ketch", "s3cret").connect(f.peer).apply {
+ echoes("hello")
+ close()
+ }
+ assertEquals(listOf("127.0.0.1:${f.peer.port}"), f.seen.targets)
+ assertEquals(listOf(basic), f.seen.credentials)
+ assertFailsWith { f.through(ProxyAddress.Type.HTTP).connect(f.peer) }
+ }
+ }
+
+ @Test
+ fun endpointsOfAProxiedEngine_keepHostNamesForTheProxy() = runTest {
+ assertEquals(listOf(PeerEndpoint("peer.example", 6881)),
+ resolveEndpoint("peer.example:6881", resolve = false))
+ assertEquals(listOf(PeerEndpoint("203.0.113.7", 51413)),
+ resolveEndpoint("203.0.113.7:51413", resolve = false))
+ assertEquals(listOf(PeerEndpoint("2001:db8:0:0:0:0:0:1", 6881)),
+ resolveEndpoint("[2001:db8::1]:6881", resolve = false))
+ }
+
+ @Test
+ fun nothingGoesAroundTheProxy() = fixture(socks5()) { fixture ->
+ val network = fixture.through(ProxyAddress.Type.SOCKS5)
+
+ // Datagrams go nowhere and none arrives: DHT, uTP, UDP trackers and port mapping are silent.
+ val udp = network.bindUdp(PeerEndpoint("0.0.0.0", 0))
+ udp.send(fixture.peer, byteArrayOf(1, 2, 3))
+ assertNull(withTimeoutOrNull(200) { udp.receive() })
+ udp.close()
+ // Peers could only reach the listener at the address the proxy hides.
+ val listener = network.listen(PeerEndpoint("0.0.0.0", 0))
+ assertEquals("127.0.0.1", listener.local.host)
+ listener.close()
+ assertTrue(fixture.seen.targets.isEmpty())
+ }
+}