【问题标题】:Socket creation in VpnService freezes thread在 VpnService 中创建套接字会冻结线程
【发布时间】:2015-04-26 06:32:36
【问题描述】:

我正在尝试为 android 实施无根防火墙。我已经解决了关于 SO 的所有相关问题,但没有找到答案:

在这里我正在配置我的 vpn。

private void configure() {
    // If the old interface has exactly the same parameters, use it!
    if (mInterface != null) {
        Log.i(TAG, "Using the previous interface");
        return;
    }

    // Configure a builder while parsing the parameters.
    Builder builder = new Builder();
    builder.setMtu(1500);
    builder.addAddress("192.168.178.90", 24);
    //builder.addAddress("10.0.2.0", 32);
    //builder.addDnsServer("8.8.8.8");
    builder.addRoute("0.0.0.0", 0);  // to intercept packets
    try {
        mInterface.close();
    } catch (Exception e) {
        // ignore
    }
    mInterface = builder.establish();
}

然后我尝试将数据发送到目标地址

byte[] data= new byte[pdata.data.capacity()];
pdata.data.get(data);


Socket s = new Socket(pdata.destAddr,pdata.destPort);

if(shouldBeBlocked(pdata.destAddr)) {
    sendResult("blocked: "+ pdata.destAddr.toString()+":"+pdata.destPort);
} else {
    sendResult(pdata.destAddr.toString()+":"+pdata.destPort);
    if (protect(s)) { 
        ...

但线程在 Socket 的构造函数上冻结,然后像这样 rasinig IOException:

java.net.ConnectException: failed to connect to /173.194.71.100 (port 443): connect failed: ETIMEDOUT (Connection timed out)

【问题讨论】:

  • 您是否检查过您是否在目标端接收到任何数据?你能用wireshark之​​类的东西来查看数据包丢失的地方或超时的来源吗?
  • @MattKo ConnectException 在我可以发送任何数据之前被抛出。这是Socket s = new Socket(pdata.destAddr,pdata.destPort);

标签: java android sockets vpn firewall


【解决方案1】:

也许尝试builder.setMtu(1492); 更安全。见http://en.wikipedia.org/wiki/Maximum_transmission_unit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2010-10-05
    相关资源
    最近更新 更多