Skip to content

Commit 1cf17d8

Browse files
committed
fix: improve network connectivity and simplify proxy handling on JVM
- Set JVM DNS cache TTL to 30 seconds for positive lookups and 5 seconds for negative lookups to ensure network changes (like VPN toggles) are detected quickly. - Refactor `createPlatformHttpClient` to simplify proxy configuration using OkHttp's native capabilities. - Replace manual system proxy parsing with `proxySelector(ProxySelector.getDefault())` for `ProxyConfig.System`. - Explicitly set `Proxy.NO_PROXY` when no proxy is configured. - Remove redundant URI parsing and manual address mapping logic for system-level proxy detection.
1 parent 725ffd8 commit 1cf17d8

2 files changed

Lines changed: 22 additions & 40 deletions

File tree

composeApp/src/jvmMain/kotlin/zed/rainxch/githubstore/DesktopApp.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ import java.awt.Desktop
2424
import kotlin.system.exitProcess
2525

2626
fun main(args: Array<String>) {
27+
// Reduce JVM DNS cache TTL so network changes (VPN on/off) are picked up quickly.
28+
// Default JVM caches positive lookups for 30s and negative lookups forever,
29+
// which breaks connectivity when a VPN changes DNS/routing mid-session.
30+
java.security.Security.setProperty("networkaddress.cache.ttl", "30")
31+
java.security.Security.setProperty("networkaddress.cache.negative.ttl", "5")
32+
2733
initKoin()
2834

2935
val deepLinkArg = args.firstOrNull()

core/data/src/jvmMain/kotlin/zed/rainxch/core/data/network/HttpClientFactory.jvm.kt

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,32 @@ import io.ktor.client.engine.ProxyBuilder
55
import io.ktor.client.engine.okhttp.OkHttp
66
import io.ktor.http.Url
77
import zed.rainxch.core.domain.model.ProxyConfig
8+
import java.net.Proxy
89
import java.net.ProxySelector
9-
import java.net.URI
1010

1111
actual fun createPlatformHttpClient(proxyConfig: ProxyConfig): HttpClient =
1212
HttpClient(OkHttp) {
1313
engine {
14-
proxy =
15-
when (proxyConfig) {
16-
is ProxyConfig.None -> {
17-
null
14+
when (proxyConfig) {
15+
is ProxyConfig.None -> {
16+
config {
17+
proxy(Proxy.NO_PROXY)
1818
}
19+
}
1920

20-
is ProxyConfig.System -> {
21-
val systemProxy =
22-
ProxySelector
23-
.getDefault()
24-
?.select(URI("https://api.github.com"))
25-
?.firstOrNull { it.type() != java.net.Proxy.Type.DIRECT }
26-
27-
if (systemProxy != null) {
28-
val addr = systemProxy.address() as? java.net.InetSocketAddress
29-
if (addr != null) {
30-
when (systemProxy.type()) {
31-
java.net.Proxy.Type.HTTP -> {
32-
ProxyBuilder.http(Url("http://${addr.hostString}:${addr.port}"))
33-
}
34-
35-
java.net.Proxy.Type.SOCKS -> {
36-
ProxyBuilder.socks(addr.hostString, addr.port)
37-
}
38-
39-
else -> {
40-
null
41-
}
42-
}
43-
} else {
44-
null
45-
}
46-
} else {
47-
null
48-
}
21+
is ProxyConfig.System -> {
22+
config {
23+
proxySelector(ProxySelector.getDefault())
4924
}
25+
}
5026

51-
is ProxyConfig.Http -> {
52-
ProxyBuilder.http(Url("http://${proxyConfig.host}:${proxyConfig.port}"))
53-
}
27+
is ProxyConfig.Http -> {
28+
proxy = ProxyBuilder.http(Url("http://${proxyConfig.host}:${proxyConfig.port}"))
29+
}
5430

55-
is ProxyConfig.Socks -> {
56-
ProxyBuilder.socks(proxyConfig.host, proxyConfig.port)
57-
}
31+
is ProxyConfig.Socks -> {
32+
proxy = ProxyBuilder.socks(proxyConfig.host, proxyConfig.port)
5833
}
34+
}
5935
}
6036
}

0 commit comments

Comments
 (0)