Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Switch to local UDP DNS resolver" #2650

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ jobs:
environment:
GRADLE_OPTS: -Dorg.gradle.workers.max=1 -Dorg.gradle.daemon=false -Dkotlin.compiler.execution.strategy="in-process"
steps:
- run: rustup toolchain install nightly-2020-12-20
- run: rustup override set nightly-2020-12-20
- run: rustup target install armv7-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android
- checkout
- run: git submodule update --init --recursive
- run: rustup update
- run: cd core/src/main/rust/shadowsocks-rust && rustup target add armv7-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android
- restore_cache:
key: jars-{{ checksum "build.gradle.kts" }}
- run:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ for Android TV ([beta](https://play.google.com/apps/testing/com.github.shadowsoc
* Rust with Android targets installed

```bash
$ cd core/src/main/rust/shadowsocks-rust
$ rustup target add armv7-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android
$ rustup toolchain install nightly-2020-12-20
$ rustup override set nightly-2020-12-20
$ rustup target install armv7-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android
```

### BUILD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ object BaseService {
File(Core.deviceStorage.noBackupFilesDir, "stat_udp"),
File(configRoot, CONFIG_FILE_UDP),
"-u", false)
data.localDns = LocalDnsWorker(this::rawResolver, DataStore.portLocalDns + 1).apply { start() }
data.localDns = LocalDnsWorker(this::rawResolver).apply { start() }
}

fun startRunner() {
Expand Down
74 changes: 39 additions & 35 deletions core/src/main/java/com/github/shadowsocks/bg/LocalDnsWorker.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.shadowsocks.bg

import com.github.shadowsocks.net.ConcurrentUdpSocketListener
import android.net.LocalSocket
import com.github.shadowsocks.Core
import com.github.shadowsocks.net.ConcurrentLocalSocketListener
import com.github.shadowsocks.net.DnsResolverCompat
import com.github.shadowsocks.utils.readableMessage
import kotlinx.coroutines.CancellationException
Expand All @@ -10,45 +12,47 @@ import kotlinx.coroutines.launch
import org.xbill.DNS.Message
import org.xbill.DNS.Rcode
import timber.log.Timber
import java.io.DataInputStream
import java.io.DataOutputStream
import java.io.File
import java.io.IOException
import java.net.SocketAddress
import java.nio.ByteBuffer
import java.nio.channels.DatagramChannel

class LocalDnsWorker(private val resolver: suspend (ByteArray) -> ByteArray, port: Int) : ConcurrentUdpSocketListener(
"LocalDnsThread", port), CoroutineScope {

override fun handle(channel: DatagramChannel, sender: SocketAddress, query: ByteBuffer) {
class LocalDnsWorker(private val resolver: suspend (ByteArray) -> ByteArray) : ConcurrentLocalSocketListener(
"LocalDnsThread", File(Core.deviceStorage.noBackupFilesDir, "local_dns_path")), CoroutineScope {
override fun acceptInternal(socket: LocalSocket) = error("big no no")
override fun accept(socket: LocalSocket) {
launch {
query.flip()
val data = ByteArray(query.remaining())
query.get(data)
try {
resolver(data)
} catch (e: Exception) {
when (e) {
is TimeoutCancellationException -> Timber.w("Resolving timed out")
is CancellationException -> {
} // ignore
is IOException -> Timber.d(e)
else -> Timber.w(e)
}
try {
DnsResolverCompat.prepareDnsResponse(Message(data)).apply {
header.rcode = Rcode.SERVFAIL
}.toWire()
} catch (_: IOException) {
byteArrayOf() // return empty if cannot parse packet
socket.use {
val input = DataInputStream(socket.inputStream)
val query = try {
ByteArray(input.readUnsignedShort()).also { input.read(it) }
} catch (e: IOException) { // connection early close possibly due to resolving timeout
return@use Timber.d(e)
}
}?.let { r ->
try {
val response = ByteBuffer.allocate(1024)
response.clear()
response.put(r)
response.flip()
channel.send(response, sender)
} catch (e: IOException) {
Timber.d(e.readableMessage)
resolver(query)
} catch (e: Exception) {
when (e) {
is TimeoutCancellationException -> Timber.w("Resolving timed out")
is CancellationException -> { } // ignore
is IOException -> Timber.d(e)
else -> Timber.w(e)
}
try {
DnsResolverCompat.prepareDnsResponse(Message(query)).apply {
header.rcode = Rcode.SERVFAIL
}.toWire()
} catch (_: IOException) {
byteArrayOf() // return empty if cannot parse packet
}
}?.let { response ->
try {
val output = DataOutputStream(socket.outputStream)
output.writeShort(response.size)
output.write(response)
} catch (e: IOException) {
Timber.d(e.readableMessage)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
}.let { dns ->
cmd += arrayListOf(
"--dns-addr", "${DataStore.listenAddress}:${DataStore.portLocalDns}",
"--local-dns-addr", "127.0.0.1:${DataStore.portLocalDns + 1}",
"--local-dns-addr", "local_dns_path",
"--remote-dns-addr", "${dns.host ?: "0.0.0.0"}:${if (dns.port < 0) 53 else dns.port}")
}

Expand Down

This file was deleted.

72 changes: 0 additions & 72 deletions core/src/main/java/com/github/shadowsocks/net/UdpSocketListener.kt

This file was deleted.

2 changes: 2 additions & 0 deletions core/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<item>AES-128-GCM</item>
<item>AES-256-GCM</item>
<item>CHACHA20-IETF-POLY1305</item>
<item>XCHACHA20-IETF-POLY1305</item>
</string-array>

<string-array name="enc_method_value" translatable="false">
Expand All @@ -34,6 +35,7 @@
<item>aes-128-gcm</item>
<item>aes-256-gcm</item>
<item>chacha20-ietf-poly1305</item>
<item>xchacha20-ietf-poly1305</item>
</string-array>

<string-array name="bypass_private_route" translatable="false">
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/rust/shadowsocks-rust
Submodule shadowsocks-rust updated 58 files
+6 −3 .github/workflows/build-and-test.yml
+4 −3 .github/workflows/build-release.yml
+0 −1 .gitignore
+136 −348 Cargo.lock
+4 −3 Cargo.toml
+0 −1 README.md
+14 −3 bin/common/monitor/windows.rs
+8 −0 bin/common/validator/mod.rs
+9 −21 bin/sslocal.rs
+0 −18 bin/ssmanager.rs
+0 −18 bin/ssserver.rs
+0 −25 configs/log4rs.yaml
+15 −12 crates/shadowsocks-service/Cargo.toml
+39 −73 crates/shadowsocks-service/src/config.rs
+0 −12 crates/shadowsocks-service/src/lib.rs
+1 −13 crates/shadowsocks-service/src/local/context.rs
+1 −2 crates/shadowsocks-service/src/local/dns/client_cache.rs
+67 −0 crates/shadowsocks-service/src/local/dns/config.rs
+220 −0 crates/shadowsocks-service/src/local/dns/dns_resolver.rs
+2 −0 crates/shadowsocks-service/src/local/dns/mod.rs
+74 −41 crates/shadowsocks-service/src/local/dns/server.rs
+1 −2 crates/shadowsocks-service/src/local/dns/upstream.rs
+5 −12 crates/shadowsocks-service/src/local/http/http_stream.rs
+29 −53 crates/shadowsocks-service/src/local/loadbalancing/ping_balancer.rs
+2 −2 crates/shadowsocks-service/src/local/loadbalancing/server_stat.rs
+28 −60 crates/shadowsocks-service/src/local/mod.rs
+1 −3 crates/shadowsocks-service/src/local/redir/tcprelay/mod.rs
+117 −224 crates/shadowsocks-service/src/local/redir/udprelay/mod.rs
+1 −2 crates/shadowsocks-service/src/local/socks/server/mod.rs
+117 −223 crates/shadowsocks-service/src/local/socks/server/socks5/udprelay.rs
+1 −2 crates/shadowsocks-service/src/local/tunnel/tcprelay.rs
+66 −223 crates/shadowsocks-service/src/local/tunnel/udprelay.rs
+3 −17 crates/shadowsocks-service/src/manager/mod.rs
+5 −9 crates/shadowsocks-service/src/manager/server.rs
+7 −26 crates/shadowsocks-service/src/server/mod.rs
+9 −9 crates/shadowsocks-service/src/server/server.rs
+27 −16 crates/shadowsocks-service/src/server/tcprelay.rs
+87 −113 crates/shadowsocks-service/src/server/udprelay.rs
+3 −3 crates/shadowsocks/Cargo.toml
+46 −111 crates/shadowsocks/src/dns_resolver/resolver.rs
+38 −34 crates/shadowsocks/src/dns_resolver/trust_dns_resolver.rs
+9 −8 crates/shadowsocks/src/manager/datagram.rs
+0 −42 crates/shadowsocks/src/net/connect_opt.rs
+2 −35 crates/shadowsocks/src/net/mod.rs
+8 −106 crates/shadowsocks/src/net/tcp.rs
+8 −8 crates/shadowsocks/src/net/udp.rs
+10 −26 crates/shadowsocks/src/relay/sys/unix/mod.rs
+32 −38 crates/shadowsocks/src/relay/sys/unix/uds.rs
+10 −16 crates/shadowsocks/src/relay/sys/windows/mod.rs
+3 −3 crates/shadowsocks/src/relay/tcprelay/aead.rs
+3 −20 crates/shadowsocks/src/relay/tcprelay/proxy_listener.rs
+1 −1 crates/shadowsocks/src/relay/tcprelay/stream.rs
+0 −1 rust-toolchain
+1 −1 tests/dns.rs
+1 −5 tests/http.rs
+1 −1 tests/socks4.rs
+1 −1 tests/socks5.rs
+1 −1 tests/tunnel.rs