简体   繁体   中英

Socket creation in VpnService freezes thread

I'm trying to implement no root firewall for android. I've gone through all related questions on SO, but haven't find an answer:

Here I am configuring my 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();
}

And then I am trying to send data to destination address

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)) { 
        ...

But thread is freezing on constructor of Socket and then rasinig IOException like this:

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

Maybe try builder.setMtu(1492); to be on a safer side. See http://en.wikipedia.org/wiki/Maximum_transmission_unit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM