I am new to android and I am trying to establish and connect to our own vpn
(Not the default vpn
providers i.e, PPTP
, L2TP
etc which is present in the Android Setting -> Wireless and Networks
) programatically.
My scenario is, I have button and when I click the button I need to configure my own vpn
and a link (say www.google.com) should go through that VPN
which I had configured.
Is there any sample code?
I don't know why I googled thousand times, but did not github's search. Looking for ages now, I finally found one written beautifully. The code is literally beautiful which is rare:
https://github.com/hexene/LocalVPN
The project does not use NDK or any native code, purely java, which makes it a perfect start for a simple project (not that it wont be good for a complex one). And the workflow is quite simple:
LocalVPNService.class
LocalVPNService
the TCP/UDP packet is investigated and source and destination IP's are extracted (Packet.class
is used for that).LocalVPNService
makes a connection on behalf of the app starting the request, and calls VpnService.protect()
on newly created connection to avoid loops. As you can see hexene has done all the hard work and heavy lifting already.
All the classes referenced above are found in the projects directory. I had a quick look into the source code from github, the workflow discussed here might not be accurate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With