package chat.vojo.proxy.core

/**
 * Kotlin facade over the native hev-socks5-tunnel engine.
 *
 * The engine reads IP packets from the VPN tun fd and converts every TCP/UDP
 * flow into a SOCKS5 connection towards a local address (our [chat.vojo.proxy.core.socks.LocalSocks5Server]).
 * [nativeStart] blocks for the lifetime of the tunnel, so callers must run it on
 * a dedicated thread and stop it via [nativeStop].
 */
object Tun2Socks {
    init {
        // libtun2socks depends on libhev-socks5-tunnel; load the dependency first.
        // libhev's JNI_OnLoad fires on load and RegisterNatives onto the stub
        // [hev.htproxy.TProxyService] — which must exist or the process aborts.
        // We then drive the engine through our own C bridge below.
        System.loadLibrary("hev-socks5-tunnel")
        System.loadLibrary("tun2socks")
    }

    /** Blocks until [nativeStop] is called or the engine fails. Returns 0 on clean exit. */
    external fun nativeStart(config: String, tunFd: Int): Int

    external fun nativeStop()

    /** @return [txPackets, txBytes, rxPackets, rxBytes] as observed by the engine. */
    external fun nativeStats(): LongArray
}
