Skip to content

Commit

Permalink
dns2socks integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Nov 22, 2024
1 parent 1adca99 commit df0e725
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@
path = core/src/main/jni/fake-dlfcn
url = https://github.com/ssrlive/fake-dlfcn.git
branch = master
[submodule "rust/dns2socks"]
path = rust/dns2socks
url = https://github.com/tun2proxy/dns2socks.git
branch = master
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cargo {
profile = gradle.startParameter.taskNames.any{it.toLowerCase().contains("release")} ? "release" : "debug"
prebuiltToolchains = true
apiLevel = 24
targetIncludes = ['libovertls.so', 'libtun2proxy.so']
targetIncludes = ['libovertls.so', 'libtun2proxy.so', 'libdns2socks.so']
}

preBuild.dependsOn "cargoBuild"
Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/com/github/shadowsocks/bg/Dns2socks.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.shadowsocks.bg

object Dns2socks {
init {
System.loadLibrary("dns2socks")
}

@JvmStatic
external fun start(listen_addr: String?,
dns_remote_server: String?,
socks5_server: String?,
username: String?,
password: String?,
force_tcp: Boolean,
cache_records: Boolean,
verbosity: Int,
timeout: Int): Int

@JvmStatic
external fun stop(): Int
}
60 changes: 60 additions & 0 deletions core/src/main/java/com/github/shadowsocks/bg/SsrVpnService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class SsrVpnService : VpnService(), BaseService.Interface {
override fun onRevoke() = stopRunner()

override fun killProcesses(scope: CoroutineScope) {
dns2socksThread?.terminate()
dns2socksThread?.join()
dns2socksThread = null

tunThread?.terminate()
tunThread?.join()
tunThread = null
Expand Down Expand Up @@ -186,6 +190,30 @@ class SsrVpnService : VpnService(), BaseService.Interface {
tunThread = Tun2proxyThread(proxyUrl, tunFd, tunMtu, verbose, dnsOverTcp)
tunThread?.isDaemon = true
tunThread?.start()

val listenAddr = "${PRIVATE_VLAN4_ROUTER}:53"
val dnsRemoteServer = profile.remoteDns
val socks5Server = "${DataStore.listenAddress}:${DataStore.portProxy}"
val username = null
val password = null
val forceTcp = true
val cacheRecords = false
val verbosity = if (verbose) 5 else 3
val timeout = 10

dns2socksThread = Dns2socksThread(
listenAddr,
dnsRemoteServer,
socks5Server,
username,
password,
forceTcp,
cacheRecords,
verbosity,
timeout
)
dns2socksThread?.isDaemon = true
dns2socksThread?.start()
}

override fun onDestroy() {
Expand All @@ -212,4 +240,36 @@ class SsrVpnService : VpnService(), BaseService.Interface {
Tun2proxy.stop()
}
}

private var dns2socksThread: Dns2socksThread? = null

internal class Dns2socksThread(
private val listenAddr: String?,
private val dnsRemoteServer: String?,
private val socks5Server: String?,
private val username: String?,
private val password: String?,
private val forceTcp: Boolean,
private val cacheRecords: Boolean,
private val verbosity: Int,
private val timeout: Int
) : Thread() {
override fun run() {
Dns2socks.start(
listenAddr,
dnsRemoteServer,
socks5Server,
username,
password,
forceTcp,
cacheRecords,
verbosity,
timeout
)
}

fun terminate() {
Dns2socks.stop()
}
}
}
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
resolver = "2"
members = ["./overtls", "./tun2proxy"]
members = ["./overtls", "./tun2proxy", "./dns2socks"]
1 change: 1 addition & 0 deletions rust/dns2socks
Submodule dns2socks added at f40b80

0 comments on commit df0e725

Please sign in to comment.