@@ -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