package com.sshproxy.vpn.ssh

import java.net.InetSocketAddress
import java.net.Socket

object SocksHealthCheck {

    fun verifyLocalSocks(port: Int, timeoutMs: Int = 5_000): Boolean {
        return runCatching {
            Socket().use { socket ->
                socket.connect(InetSocketAddress("127.0.0.1", port), timeoutMs)
                val output = socket.getOutputStream()
                output.write(byteArrayOf(0x05, 0x01, 0x00))
                output.flush()
                val reply = ByteArray(2)
                val read = socket.getInputStream().read(reply)
                read == 2 && reply[0] == 0x05.toByte() && reply[1] == 0x00.toByte()
            }
        }.getOrDefault(false)
    }
}
