Skip to content

Commit 1e0e935

Browse files
authored
Merge pull request #61 from anonvector/dev
v2.3.1: Fix HTTP proxy reliability issues
2 parents d848edd + 11661cb commit 1e0e935

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ plugins {
1414
}
1515

1616
val minSdkVersion = 24
17-
val appVersionName = "2.3"
18-
val appVersionCode = 39
17+
val appVersionName = "2.3.1"
18+
val appVersionCode = 40
1919
val cargoProfile = (findProperty("CARGO_PROFILE") as String?) ?: run {
2020
val isRelease = gradle.startParameter.taskNames.any { it.contains("Release", ignoreCase = true) }
2121
if (isRelease) "release" else "debug"

app/src/main/java/app/slipnet/tunnel/HttpProxyServer.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ object HttpProxyServer {
2929
private const val BIND_RETRY_DELAY_MS = 200L
3030
private const val BUFFER_SIZE = 32768
3131
private const val TCP_CONNECT_TIMEOUT_MS = 30000
32+
private const val RELAY_IDLE_TIMEOUT_MS = 300000
3233

3334
private var socksHost: String = "127.0.0.1"
3435
private var socksPort: Int = 1080
@@ -210,7 +211,7 @@ object HttpProxyServer {
210211
clientOutput.write("HTTP/1.1 200 Connection Established\r\n\r\n".toByteArray())
211212
clientOutput.flush()
212213

213-
clientSocket.soTimeout = 0
214+
clientSocket.soTimeout = RELAY_IDLE_TIMEOUT_MS
214215

215216
// Bridge bidirectionally
216217
remoteSocket.use { remote ->
@@ -222,7 +223,7 @@ object HttpProxyServer {
222223
copyStream(clientInput, remoteOutput)
223224
} catch (_: Exception) {
224225
} finally {
225-
try { remoteOutput.close() } catch (_: Exception) {}
226+
try { remote.shutdownOutput() } catch (_: Exception) {}
226227
}
227228
}, "http-proxy-c2s")
228229
t1.isDaemon = true
@@ -331,7 +332,7 @@ object HttpProxyServer {
331332
remoteOutput.write("\r\n".toByteArray())
332333
remoteOutput.flush()
333334

334-
clientSocket.soTimeout = 0
335+
clientSocket.soTimeout = RELAY_IDLE_TIMEOUT_MS
335336

336337
// Bidirectional bridge: forwards request body (if any) and response.
337338
// Server will close after response due to Connection: close.
@@ -420,17 +421,13 @@ object HttpProxyServer {
420421
}
421422

422423
private fun copyStream(input: InputStream, output: OutputStream) {
423-
val buffered = BufferedOutputStream(output, BUFFER_SIZE)
424424
val buffer = ByteArray(BUFFER_SIZE)
425425
while (!Thread.currentThread().isInterrupted) {
426426
val bytesRead = input.read(buffer)
427427
if (bytesRead == -1) break
428-
buffered.write(buffer, 0, bytesRead)
429-
if (bytesRead < BUFFER_SIZE || input.available() == 0) {
430-
buffered.flush()
431-
}
428+
output.write(buffer, 0, bytesRead)
429+
output.flush()
432430
}
433-
buffered.flush()
434431
}
435432

436433
}

0 commit comments

Comments
 (0)