How to disconnect/close VPN Tunnel connection via VpnService class in android -
i working on android app in have created vpn connection using vpnservice class. have close vpn connection @ point, thought stopping vpnservice class vpn-connection closed fails. want know how can close vpn-connection created vpnservice. have used following code establish vpn-connection
activate.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = vpnservice.prepare(getapplicationcontext()); toast.maketext(homescreen.this, "intent "+intent.tostring(), toast.length_short).show(); if (intent != null) { log.d("vpnconnection","intent not null"); startactivityforresult(intent, 0); } else { log.d("vpnconnection","intent null"); onactivityresult(0, result_ok, null); } } }); protected void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode == result_ok) { log.d("vpnconnection","onactivityresult"); intent intent = new intent(this, myvpnservice.class); startservice(intent); } }
i have used vpnservice class
import android.content.intent; import android.net.vpnservice; import android.os.build; import android.os.parcelfiledescriptor; import android.support.annotation.requiresapi; import android.util.log; import android.widget.toast; import java.io.fileinputstream; import java.io.fileoutputstream; import java.net.inetsocketaddress; import java.nio.channels.datagramchannel; public class myvpnservice extends vpnservice { private thread mthread; private parcelfiledescriptor minterface; //a.configure builder interface. builder builder = new builder(); // services interface @override public int onstartcommand(intent intent, int flags, int startid) { // start new session creating new thread. mthread = new thread(new runnable() { @override public void run() { try { log.d("vpnconnection","running thread"); //a. configure tun , interface. minterface = builder.setsession("myvpnservice") .addaddress("75.102.22.210", 24) //it means "75.102.22.210" ip address py jo b request krega wo vpn-tunnel k through process hongi.. let suppose meri app 75.102.22.210 ip address py network request krti hy r meri app ny vpn implement b kia hoa hy 75.102.22.210 ip address meri app ki saari request vpn-tunnel k through process hongi.. .adddnsserver("8.8.8.8") .addroute("10.8.0.1", 24).establish(); //b. packets sent queued in input stream. fileinputstream in = new fileinputstream( minterface.getfiledescriptor()); //b. packets received need written output stream. fileoutputstream out = new fileoutputstream( minterface.getfiledescriptor()); //c. udp channel can used pass/get ip package to/from server datagramchannel tunnel = datagramchannel.open(); // connect server, localhost used demonstration only. tunnel.connect(new inetsocketaddress("159.203.96.153", 1194)); //d. protect socket, package send not feedback vpn service. protect(tunnel.socket()); //e. use loop pass packets. while (true) { } } catch (exception e) { log.d("vpnconnection","exception raise while connection: "+e); // catch exception e.printstacktrace(); } { try { if (minterface != null) { minterface.close(); minterface = null; log.d("vpnconnection","finally keywork running "); } else { log.d("vpnconnection", "interface null"); } } catch (exception e) { log.d("vpnconnection","finally excetion "+e); } } } }, "myvpnrunnable"); //start service mthread.start(); return start_sticky; } @override public void ondestroy() { // todo auto-generated method stub if (mthread != null) { mthread.interrupt(); } super.ondestroy(); } }
Comments
Post a Comment