heiher / hev-socks5-tunnel

A high-performance tun2socks for Linux/Android/FreeBSD/macOS/iOS/WSL2 (IPv4/IPv6/TCP/UDP)
MIT License
636 stars 130 forks source link

crash in v 2.6.5 #103

Closed itsDantes closed 3 months ago

itsDantes commented 3 months ago

there is an error happens sometimes and my app crashing:

2024-01-23 17:08:26.538  5180-5881  libc                    com.myvapp                A  Fatal signal 6 (SIGABRT), code -6 in tid 5881 (Thread-264)
2024-01-23 17:08:26.680  5890-5890  DEBUG                   pid-5890                             A  pid: 5180, tid: 5881, name: Thread-264  >>> com.myvapp <<<
2024-01-23 17:08:26.687  5890-5890  DEBUG                   pid-5890                             A      #03 pc 00042021  /data/app/com.myvapp-NmJ98TzCG94QTFCKqEedSA==/lib/x86/libhev-socks5-tunnel.so (netif_add+545)
2024-01-23 17:08:26.687  5890-5890  DEBUG                   pid-5890                             A      #04 pc 00041ded  /data/app/com.myvapp-NmJ98TzCG94QTFCKqEedSA==/lib/x86/libhev-socks5-tunnel.so (netif_add_noaddr+45)
2024-01-23 17:08:26.688  5890-5890  DEBUG                   pid-5890                             A      #05 pc 00022dcc  /data/app/com.myvapp-NmJ98TzCG94QTFCKqEedSA==/lib/x86/libhev-socks5-tunnel.so (hev_socks5_tunnel_init+92)
2024-01-23 17:08:26.688  5890-5890  DEBUG                   pid-5890                             A      #06 pc 00021a0d  /data/app/com.myvapp-NmJ98TzCG94QTFCKqEedSA==/lib/x86/libhev-socks5-tunnel.so (hev_socks5_tunnel_main+173)
2024-01-23 17:08:26.688  5890-5890  DEBUG                   pid-5890                             A      #07 pc 00021945  /data/app/com.myvapp-NmJ98TzCG94QTFCKqEedSA==/lib/x86/libhev-socks5-tunnel.so
2024-01-23 17:08:29.440  1753-1829  InputDispatcher         system_process                       E  channel '6ddbfb8 com.myvapp/com.myvapp.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2024-01-23 17:08:29.466  1422-1470  SurfaceFlinger          surfaceflinger                       E  Failed to find layer (com.myvapp/com.myvapp.MainActivity#0) in layer parent (no-parent).
heiher commented 3 months ago

Does this happen on first boot or reboot? If rebooting, you need to ensure that the previous thread running hev-socks5-tunnel has completely exited.

itsDantes commented 3 months ago

@heiher i call TProxyStopService() always before start TProxyStartService() like this:

cals

it happens when i destroy VPN via an button and ordering my app to connect VPN service again.

as i said it just happens sometimes.

heiher commented 3 months ago

Does this patch fix it? https://github.com/heiher/hev-socks5-tunnel/commit/f7506158dc151a97af761b36097c9e5ec87075d6

itsDantes commented 3 months ago

well, let me to update project to the least version and test it.

itsDantes commented 3 months ago

@heiher warning: the connection completely not working on this commit.

but in the old hev-jni.c version, it works fine.

itsDantes commented 3 months ago

i just replaced hev-jni.c with the old version and it working fine. but in the last version that you added pthread_mutex_t parameters, the connection is not working at all.

fast

and i updated project to 2.6.6 with the below command

git clone --recursive --depth 1 https://github.com/heiher/hev-socks5-tunnel

itsDantes commented 3 months ago

also suddenly i got another crash again. (it happened after i updated to 2.6.6)

2024-01-23 22:42:10.982 17048-17445 libc                    com.myvapp                A  fdsan: attempted to close file descriptor 156, expected to be unowned, actually owned by SocketImpl 0xbb261df
2024-01-23 22:42:10.983 17048-17445 libc                    com.myvapp                A  Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 17445 (Thread-117), pid 17048 (hrooz.lagshecan)
2024-01-23 22:42:11.105 17576-17576 DEBUG                   pid-17576                            A  pid: 17048, tid: 17445, name: Thread-117  >>> com.myvapp <<<
2024-01-23 22:42:11.105 17576-17576 DEBUG                   pid-17576                            A        #03 pc 000000000001d008  /data/app/~~wIlb95NgTWRSxCRSu0bvqQ==/com.myvapp-Ad0Kivi4WUjtTrUGDlvNaw==/lib/arm64/libhev-socks5-tunnel.so (BuildId: 95e03b6e010a1e20e0c213afd59b3a41b7ed46a2)
2024-01-23 22:42:11.105 17576-17576 DEBUG                   pid-17576                            A        #04 pc 0000000000049644  /data/app/~~wIlb95NgTWRSxCRSu0bvqQ==/com.myvapp-Ad0Kivi4WUjtTrUGDlvNaw==/lib/arm64/libhev-socks5-tunnel.so (BuildId: 95e03b6e010a1e20e0c213afd59b3a41b7ed46a2)
2024-01-23 22:42:11.105 17576-17576 DEBUG                   pid-17576                            A        #05 pc 0000000000049614  /data/app/~~wIlb95NgTWRSxCRSu0bvqQ==/com.myvapp-Ad0Kivi4WUjtTrUGDlvNaw==/lib/arm64/libhev-socks5-tunnel.so (hev_task_execute+16) (BuildId: 95e03b6e010a1e20e0c213afd59b3a41b7ed46a2)
heiher commented 3 months ago

I can't reproduce it here. Could you make an example based on sockstun? Thanks.

itsDantes commented 3 months ago

@heiher this is my VPN code file :

package com.myvapp

class TProxyService : VpnService() {

    companion object {
        init {
            System.loadLibrary("hev-socks5-tunnel")
        }
    }

    private external fun TProxyStartService(configPath: String, fd: Int)
    private external fun TProxyStopService()

    private fun tun2socksConfig(): File = File(context.filesDir, "tun2socks.yml")

    override fun onCreate() {
        super.onCreate()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return START_NOT_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
        try {
            Thread {
                stopVPN()
            }.start()
        } catch (_: Exception) {
        }
    }

    fun startVPN() {
        try {

            val primaryDns: String = "8.8.8.8"
            val secondaryDns: String = "8.8.4.4"

            val builder = Builder()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                builder.setMetered(false)
            }

            builder.setMtu(1500)
            builder.setSession(tunName)

            builder.addRoute("0.0.0.0", 0)

            builder.addAddress("26.26.26.1", 30)

            builder.addDnsServer(primaryDns)

            builder.addDnsServer(secondaryDns)

            builder.addDisallowedApplication(context.packageName)

            tunDevice = builder.establish()

            reSelectServer()

        } catch (e: Exception) {
            println(e.message.toString())
        }
    }

    private fun runTun2socks() {
        try {

            val tun2socksConfig = arrayOf(
                "tunnel:",
                "  name: tun0",
                "  mtu: 1500",
                "  multi-queue: true",
                "  ipv4:",
                "    gateway: 26.26.26.1",
                "    address: 26.26.26.2",
                "    prefix: 30",
                "  ipv6:",
                "    gateway: da26:2626::1",
                "    address: da26:2626::2",
                "    prefix: 30",
                "socks5:",
                "  address: 127.0.0.1",
                "  port: 10808",
                "  udp: udp",
                ""
            )
            tun2socksConfig().writeText(tun2socksConfig.joinToString("\n"))

            Thread {
                try {
                    if (tunDevice != null && utils.vpnProfileActive()) {
                       TProxyStopService()
                       TProxyStartService(tun2socksConfig().absolutePath, tunDevice!!.fd)
                    }
                } catch (_: Exception) {
                    onDestroy()
                    utils.startMainActivity("serviceCrashed")
                }
            }.start()

        } catch (_: Exception) {
        }
    }

    @Suppress("DEPRECATION")
    private fun stopVPN() {
        try {

            v2RayPoint.stopLoop()

            TProxyStopService()

            context.stopForeground(true)
            context.stopForeground(STOP_FOREGROUND_REMOVE)

            stopSelf()

            tunDevice!!.close() 
            tunDevice = null
        } catch (_: Exception) {
        }
    }

    private fun reSelectServer(
        title: String,
        body: String,
        setLargeIcon: Int
    ) {
        try {

            val inUseConfig = "jhquxg";
            val configIP: String = utils.parseV2rayJsonFile(inUseConfig, "SERVER_ADDRESS")
            val configPort: String = utils.parseV2rayJsonFile(inUseConfig, "SERVER_PORT")

            Thread {
                v2RayPoint.configureFileContent = inUseConfig
                v2RayPoint.domainName = "$configIP:$configPort"
                v2RayPoint.runLoop(false)
            }.start()

            runTun2socks()

        } catch (_: Exception) {
        }
    }

    private val v2RayPoint: V2RayPoint =
        Liblagshecan.newV2RayPoint(object : V2RayVPNServiceSupportsSet {
            override fun shutdown(): Long {
                try {
                    onDestroy()
                    dataProcessor.setData("serviceCrashed", "true", -1)
                } catch (_: Exception) {
                }
                return 0
            }

            override fun prepare(): Long {
                return 0
            }

            override fun protect(l: Long): Boolean {
                return true
            }

            override fun onEmitStatus(l: Long, s: String?): Long {
                return 0
            }

            override fun setup(s: String?): Long {
                return 0
            }
        }, Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1)
}
itsDantes commented 3 months ago

@heiher i get back to 2.6.5 version. because 2.6.6 throw some more errors.

this is error in version 2.6.6

FATAL EXCEPTION: main
 Process: com.myvapp, PID: 22526
 java.lang.NoSuchMethodError: no static or non-static method "Lir/myvapp/service/TProxyService;.TProxyGetStats()[J"
 at java.lang.Runtime.nativeLoad(Native Method)
 at java.lang.Runtime.nativeLoad(Runtime.java:1131)
 at java.lang.Runtime.loadLibrary0(Runtime.java:1085)
 at java.lang.Runtime.loadLibrary0(Runtime.java:1008)
 at java.lang.System.loadLibrary(System.java:1664)
 at ir.myvapp.service.TProxyService.<clinit>(Unknown Source:2)
 at java.lang.Class.newInstance(Native Method)
 at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:129)
 at androidx.core.app.CoreComponentFactory.instantiateService(Unknown Source:0)
 at android.app.ActivityThread.handleCreateService(ActivityThread.java:4330)
 at android.app.ActivityThread.access$1800(ActivityThread.java:263)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2053)
 at android.os.Handler.dispatchMessage(Handler.java:106)
 at android.os.Looper.loop(Looper.java:236)
 at android.app.ActivityThread.main(ActivityThread.java:8057)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)

i also added below code to top of my VPN file code;

private external fun TProxyGetStats(): LongArray

but still getting this error and app crashing in release mode. this error only happens in release mode. but in debug its works.

heiher commented 3 months ago

java.lang.NoSuchMethodError: no static or non-static method

class TProxyService {
    companion object {
        @JvmStatic
        external fun TProxyGetStats(): LongArray
    }
}
itsDantes commented 3 months ago

@heiher thanks man. now it works and i using latest version (2.6.6) of your library.

th

but still there is a problem that i solved by replace this patch with the old . the tunnel not working when i use f750615 patch. i mean you added static pthread_mutex_t mutex; and i think it makes stop the tunnel connection. i dont know about C language. but i just replaced this patch with its prev version and now it works fine.

heiher commented 3 months ago

@itsDantes I don't see any issues with this commit working with sockstun, which leaves me with no way around this issue. This is why I hope you can reproduce the problem based on sockstun.

itsDantes commented 3 months ago

@heiher well, i don't know then

kabo00os commented 1 month ago

hello everyone

i have same problem with new version but working fine with old version

how to fix this issue

heiher commented 1 month ago

hello everyone

i have same problem with new version but working fine with old version

how to fix this issue

Is this line missing from your project?

https://github.com/heiher/sockstun/blob/master/app/src/main/java/hev/sockstun/TProxyService.java#L33

kabo00os commented 1 month ago

hello everyone i have same problem with new version but working fine with old version how to fix this issue

Is this line missing from your project?

https://github.com/heiher/sockstun/blob/master/app/src/main/java/hev/sockstun/TProxyService.java#L33

yes i have this line

and (java.lang.NoSuchMethodError: no static or non-static method) after disable "shrinkResources" and "minifyEnabled" It worked without problems

but the problem is with pthread_mutex_t after use old version (without pthread_mutex_t) working fine