shadowsocks-rust icon indicating copy to clipboard operation
shadowsocks-rust copied to clipboard

在android下运行tun遇到的问题

Open dev4u opened this issue 3 years ago • 22 comments
trafficstars

本来想试试能不能用tun来替换ss android下的tun2socks,但是没办法编译通过。

cargo {
    module = "src/main/rust/shadowsocks-rust"
    libname = "sslocal"
    targets = listOf("arm64")
    profile = findProperty("CARGO_PROFILE")?.toString() ?: currentFlavor
    extraCargoBuildArguments = listOf("--bin", libname!!)
    featureSpec.noDefaultBut(arrayOf(
        "stream-cipher",
        "aead-cipher-extra",
        "logging",
        "local-flow-stat",
        "local-dns",
        "local-tun",
        "armv8",
        "neon",
        "aead-cipher-2022",
    ))
    exec = { spec, toolchain ->
        spec.environment("RUST_ANDROID_GRADLE_PYTHON_COMMAND", "python3")
        spec.environment("RUST_ANDROID_GRADLE_LINKER_WRAPPER_PY", "$projectDir/$module/../linker-wrapper.py")
        spec.environment("RUST_ANDROID_GRADLE_TARGET", "target/${toolchain.target}/$profile/lib$libname.so")
    }
}
   Compiling tun v0.5.3
error[E0433]: failed to resolve: use of undeclared crate or module `tokio`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:21:5
   |
21 | use tokio::io::unix::AsyncFd;
   |     ^^^^^ use of undeclared crate or module `tokio`

error[E0433]: failed to resolve: use of undeclared crate or module `tokio`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:22:5
   |
22 | use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
   |     ^^^^^ use of undeclared crate or module `tokio`

error[E0433]: failed to resolve: use of undeclared crate or module `tokio_util`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:23:5
   |
23 | use tokio_util::codec::Framed;
   |     ^^^^^^^^^^ use of undeclared crate or module `tokio_util`

error[E0432]: unresolved import `futures_core`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:20:5
   |
20 | use futures_core::ready;
   |     ^^^^^^^^^^^^ use of undeclared crate or module `futures_core`

error[E0432]: unresolved import `byteorder`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/codec.rs:17:5
   |
17 | use byteorder::{NativeEndian, NetworkEndian, WriteBytesExt};
   |     ^^^^^^^^^ use of undeclared crate or module `byteorder`

error[E0433]: failed to resolve: use of undeclared crate or module `tokio_util`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/codec.rs:19:5
   |
19 | use tokio_util::codec::{Decoder, Encoder};
   |     ^^^^^^^^^^ use of undeclared crate or module `tokio_util`

error[E0432]: unresolved import `bytes`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/codec.rs:18:5
   |
18 | use bytes::{BufMut, Bytes, BytesMut};
   |     ^^^^^ use of undeclared crate or module `bytes`

error: cannot determine resolution for the macro `ready`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:67:29
   |
cannot determine resolution for the macro `ready`

67 |             let mut guard = ready!(self.inner.poll_read_ready_mut(cx))?;
   |                             ^^^^^
   |
   = note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the macro `ready`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:84:29
   |
84 |             let mut guard = ready!(self.inner.poll_write_ready_mut(cx))?;
   |                             ^^^^^
   |
   = note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the macro `ready`
  --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:94:29
   |
94 |             let mut guard = ready!(self.inner.poll_write_ready_mut(cx))?;
   |                             ^^^^^
   |
   = note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the macro `ready`
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:112:29
    |
112 |             let mut guard = ready!(self.inner.poll_write_ready_mut(cx))?;
    |                             ^^^^^
    |
    = note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the macro `ready`
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:163:29
    |
163 |             let mut guard = ready!(self.inner.poll_read_ready_mut(cx))?;
    |                             ^^^^^
    |
    = note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the macro `ready`
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:180:29
    |
180 |             let mut guard = ready!(self.inner.poll_write_ready_mut(cx))?;
    |                             ^^^^^
    |
    = note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the macro `ready`
   --> /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tun-0.5.3/src/async/device.rs:190:29
    |
190 |             let mut guard = ready!(self.inner.poll_write_ready_mut(cx))?;
    |                             ^^^^^
    |
    = note: import resolution is stuck, try simplifying macro imports

dev4u avatar Jun 03 '22 16:06 dev4u

https://github.com/meh/rust-tun/blob/b60d4c2ce9aa0b7518fc259c914a0459a5320888/Cargo.toml#L32-L33

It seems that it requires to explicitly enable the async feature. But we have already enabled it in shadowsocks-service:

https://github.com/shadowsocks/shadowsocks-rust/blob/0137258e0312d436fe9a494dac5059a684e984e4/crates/shadowsocks-service/Cargo.toml#L124

zonyitoo avatar Jun 04 '22 07:06 zonyitoo

意思是要等依赖的模块修复才能继续?

dev4u avatar Jun 05 '22 04:06 dev4u

y.

zonyitoo avatar Jun 05 '22 04:06 zonyitoo

The PR was merged. Please try to use the code from master and see if it fixes your issue.

zonyitoo avatar Jun 08 '22 01:06 zonyitoo

好,今晚回去试试,谢谢!

dev4u avatar Jun 08 '22 02:06 dev4u

cargo update后,项目还是编译不通过。

dev4u avatar Jun 08 '22 15:06 dev4u

You have to modify the Tun dependency to git

zonyitoo avatar Jun 09 '22 00:06 zonyitoo

噢,是我漏做这一步,回去再试试。

dev4u avatar Jun 09 '22 11:06 dev4u

我尝试整合到android,启动的时候报错。

2022-06-09 23:37:00.155 6188-6540/com.github.shadowsocks E/libsslocal: thread 'main' panicked at 'create local: Custom { kind: Other, error: InvalidConfig }', src/service/local.rs:781:51

我把在android下启动sslocal的配置打印出来,大概是这样的:

{
  "server": "****",
  "server_port": ****,
  "password": "****",
  "method": "2022-blake3-aes-128-gcm",
  "timeout": 30,
  "mode": "tcp_and_udp",
  "dns": "unix://local_dns_path",
  "locals": [
    {
      "protocol": "tun",
      "tun_device_fd_from_path": "sock_path",
      "tun_interface_name": "eth0", //也试过tun0
      "tun_interface_address": "172.19.0.1/24"
    },
    {
      "local_address": "127.0.0.1",
      "local_port": 5450,
      "local_dns_address": "local_dns_path",
      "remote_dns_address": "****",
      "remote_dns_port": 53,
      "protocol": "dns"
    }
  ]
}

dev4u avatar Jun 09 '22 16:06 dev4u

Remove tun_interface_name and tun_interface_address

zonyitoo avatar Jun 10 '22 00:06 zonyitoo

Remove tun_interface_name and tun_interface_address

好的,回去再试试。谢谢!

dev4u avatar Jun 10 '22 00:06 dev4u

@zonyitoo 按照你的建议修改配置,还是报InvalidConfig异常

{
  "server": "*******",
  "server_port": 9999,
  "password": "******",
  "method": "2022-blake3-aes-128-gcm",
  "timeout": 30,
  "mode": "tcp_and_udp",
  "dns": "unix://local_dns_path",
  "locals": [
    {
      "local_address": "127.0.0.1",
      "local_port": 1080,
      "local_udp_address": "127.0.0.1",
      "local_udp_port": 1080,
      "mode": "tcp_and_udp"
    },
    {
      "protocol": "tun",
      "tun_device_fd_from_path": "unix://sock_path" //也试过sock_path
    },
    {
      "local_address": "127.0.0.1",
      "local_port": 5450,
      "local_dns_address": "local_dns_path",
      "remote_dns_address": "*****",
      "remote_dns_port": 53,
      "protocol": "dns"
    }
  ]
}

dev4u avatar Jun 10 '22 14:06 dev4u

https://github.com/meh/rust-tun/blob/aeca1a64e9ded75ca256d920e45ffdb868a24c6f/src/platform/android/device.rs#L36

zonyitoo avatar Jun 10 '22 14:06 zonyitoo

Actually it should be set from command line argument tun-device-fd-from-path

https://github.com/shadowsocks/shadowsocks-rust/blob/c14b1c591cd8d9e0fe4b748281b51e4809543928/src/service/local.rs#L335-L343

zonyitoo avatar Jun 10 '22 14:06 zonyitoo

@zonyitoo 需要设置mtu参数,

2022-06-10 23:42:56.555 31826-2334/com.github.shadowsocks E/libsslocal: 2022-06-10T23:42:56.554187627+08:00 INFO  [2332:528444605688] [shadowsocks_rust::service::local] shadowsocks local 1.15.0-alpha.5 build 2022-06-10T14:43:13.312917+00:00
2022-06-10 23:42:56.557 31826-2334/com.github.shadowsocks E/libsslocal: 2022-06-10T23:42:56.556869242+08:00 INFO  [2332:528444605688] [shadowsocks_service::local] waiting tun's file descriptor from sock_path
2022-06-10 23:42:56.557 31826-2334/com.github.shadowsocks E/libsslocal: 2022-06-10T23:42:56.557573669+08:00 INFO  [2332:526236155136] [shadowsocks_service::local::socks::server] shadowsocks socks TCP listening on 127.0.0.1:1080
2022-06-10 23:42:56.557 31826-2334/com.github.shadowsocks E/libsslocal: 2022-06-10T23:42:56.557687107+08:00 INFO  [2332:526236155136] [shadowsocks_service::local::socks::server::socks5::udprelay] shadowsocks socks5 UDP listening on 127.0.0.1:1080
2022-06-10 23:42:56.795 31826-2334/com.github.shadowsocks E/libsslocal: 2022-06-10T23:42:56.794287888+08:00 INFO  [2332:528444605688] [shadowsocks_service::local] got file descriptor 13 for tun from (unnamed)
2022-06-10 23:42:56.801 31826-2334/com.github.shadowsocks E/libsslocal: thread 'tokio-runtime-worker' panicked at 'mtu: NotImplemented', /Users/develop/workspaces/shadowsocks-android/core/src/main/rust/shadowsocks-rust/crates/shadowsocks-service/src/local/tun/mod.rs:137:47
2022-06-10 23:42:56.801 31826-2334/com.github.shadowsocks E/libsslocal: note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2022-06-10 23:42:56.801 31826-2334/com.github.shadowsocks E/libsslocal: 2022-06-10T23:42:56.801038669+08:00 INFO  [2332:526114581760] [shadowsocks_service::local::dns::server] shadowsocks dns TCP listening on 127.0.0.1:5450, local: local_dns_path, remote: ****:53
2022-06-10 23:42:56.802 31826-2334/com.github.shadowsocks E/libsslocal: 2022-06-10T23:42:56.801148148+08:00 INFO  [2332:526114581760] [shadowsocks_service::local::dns::server] shadowsocks dns UDP listening on 127.0.0.1:5450, local: local_dns_path, remote: ****:53

dev4u avatar Jun 10 '22 15:06 dev4u

直接在代码里写死mtu值,启动不断报处理异常:

2022-06-11 00:16:07.748 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.747748639+08:00 INFO  [24547:526159967480] [shadowsocks_rust::service::local] shadowsocks local 1.15.0-alpha.5 build 2022-06-10T16:00:44.260081+00:00
2022-06-11 00:16:07.749 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.749230826+08:00 INFO  [24547:526159967480] [shadowsocks_service::local] waiting tun's file descriptor from sock_path
2022-06-11 00:16:07.752 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.750265670+08:00 INFO  [24547:523953691904] [shadowsocks_service::local::socks::server] shadowsocks socks TCP listening on 127.0.0.1:1080
2022-06-11 00:16:07.755 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.753332285+08:00 INFO  [24547:523953691904] [shadowsocks_service::local::socks::server::socks5::udprelay] shadowsocks socks5 UDP listening on 127.0.0.1:1080
2022-06-11 00:16:07.957 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.956960722+08:00 INFO  [24547:526159967480] [shadowsocks_service::local] got file descriptor 13 for tun from (unnamed)
2022-06-11 00:16:07.958 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957451920+08:00 INFO  [24547:523960044800] [shadowsocks_service::local::tun] shadowsocks tun device , mtu 1480, mode tcp_and_udp
2022-06-11 00:16:07.958 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957534837+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.958 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957556764+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.958 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957572389+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.958 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957587545+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.958 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957604680+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: truncated packet
2022-06-11 00:16:07.958 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957620149+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.958 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957634784+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957663170+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957679628+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957695357+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957710357+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957725618+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: truncated packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957740201+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: truncated packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957755618+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957771920+08:00 ERROR [24547:523960044800] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957893587+08:00 INFO  [24547:523960044800] [shadowsocks_service::local::dns::server] shadowsocks dns TCP listening on 127.0.0.1:5450, local: local_dns_path, remote: ****:53
2022-06-11 00:16:07.959 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.957964889+08:00 INFO  [24547:523960044800] [shadowsocks_service::local::dns::server] shadowsocks dns UDP listening on 127.0.0.1:5450, local: local_dns_path, remote: ****:53
2022-06-11 00:16:07.979 24363-24549/com.github.shadowsocks E/libsslocal: 2022-06-11T00:16:07.979763430+08:00 ERROR [24547:523949456640] [shadowsocks_service::local::tun] [TUN] handle IP frame failed, error: unrecognized packet

dev4u avatar Jun 10 '22 16:06 dev4u

Try again with this commit, Android's tun packet doesn't have packet informations.

zonyitoo avatar Jun 11 '22 13:06 zonyitoo

终于可以在android下跑起来了。 但是初步试用了一下,发现还存在一些问题。不确定是软件的问题?还是配置问题?

2022-06-11 23:01:31.626 15539-15754/com.github.shadowsocks E/libsslocal: 2022-06-11T23:01:31.625382896+08:00 ERROR [15751:527072939264] [shadowsocks_service::local::tun::tcp] TCP tunnel failure, 10.35.244.188:43140 <-> 172.19.0.2:853, error: Connection timed out (os error 110)

这条日志,是github官方客户端在打开的时候,发起的dot dns查询,对应的dns服务器地址是dns.google。 dns.google,这个域名,我已经配置在代理名单中,但走代理失败了导致查询失败。

除了这个,还有访问YouTube客户端(客户端走udp的),很久才加载到内容,换回旧程序,加载内容明显要快上许多。这个我怀疑是dns查询走的路由导致的。

dev4u avatar Jun 11 '22 15:06 dev4u

No idea. It requires further inspection.

zonyitoo avatar Jun 12 '22 11:06 zonyitoo

@zonyitoo 我想了解一下,TUN服务会按照配置文件中,dns的设置信息来处理请求吗?

我配置的远端dns是127.0.0.53,并且在acl配置127.0.0.53/32在代理列表中,TUN会怎么处理dns请求?

dev4u avatar Jun 12 '22 12:06 dev4u

The dns key is for trust-dns to resolve servers' name.

The remote_dns_addr is completely irrevelent to dns, and it doesn't matter what you put in acl. The Remote DNS will always send requests to "Remote".

zonyitoo avatar Jun 12 '22 12:06 zonyitoo

那真的是不知怎么回事。 我观察到,TUN模式下,只有TG是运行比较良好,除此之外其他的,例如用浏览器打开google、youtube客户端……都存在不同程序的不可用。我观察android后台,发起的请求很少是走代理的,都走直连。个别走代理的,也存在一定几率连接超时。

dev4u avatar Jun 12 '22 13:06 dev4u