24

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?

3 Answers 3

43

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:

  • When an app makes an outbound request (request from android OS to some server on internet) the request arrives LocalVPNService.class
  • At LocalVPNService the TCP/UDP packet is investigated and source and destination IP's are extracted (Packet.class is used for that).
  • The LocalVPNService makes a connection on behalf of the app starting the request, and calls VpnService.protect() on newly created connection to avoid loops.
  • The connections are then passed to handlers who simply pass bytes from / to the two connection in a loop. The threads for UDP/TCP In/Out are managed in different worker classes and threads.

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.

5
  • 1
    Does that mean you need a true vpn server out on the internet? If I understand it right because android provides the tunnel the localvpn sees all the traffic going out? what if I want to simply parse the incoming tcp connections?
    – narb
    Commented May 1, 2017 at 20:17
  • @narb if I remember correctly, no you won't be needing a vpn server, I used it for packet parsing too. When packets are arrived they are simply sent to their destination.
    – hkoosha
    Commented May 2, 2017 at 6:12
  • It's almost a year ago :) thanks for answering. I'll give it a try with the loadvpn code but it is strange to have to use a vpn api to parse pakets. it would be nicer to have a pcap like api.
    – narb
    Commented May 2, 2017 at 19:13
  • Does it support openvpn as well? Commented Mar 1, 2018 at 12:27
  • @AhmadShahwaiz it doesn't support anything. It just provides a better and easier, java-ish interface into android VPN's interface. What you get in the end is some raw packets of bytes. You do anything you like, for instance sending them to OpenVPN (after implementing OpenVPN's interface)
    – hkoosha
    Commented May 23, 2018 at 16:48
8

You can refer the sample application ToyVpn which is bundled with your android SDK. so just go to samples and refer that application.Also see this

In additional to this you can download the source code of Openvpn. Here is the link to for that

https://github.com/kghost/ics-openvpn

Thanks

14
  • Thanks juned for ur reply. I am confused whether openvpn is an android project. I downloaded openvpn source, but t has only c files, i dont knw how to use t.. can u clear my doubt.. Thanks in advance
    – Sudarshan
    Commented Nov 2, 2012 at 5:03
  • it ll be helpful if u can share a android project which uses VpnServices and OpenVpn. This ll help me to understand and work on it.
    – Sudarshan
    Commented Nov 2, 2012 at 5:07
  • i juned i had compiled the code and ran it in my android device.. The app launched well but i dont knw how to test the app.. Is there any public test server and credential to test this app. can u share the link.. i searched but i couldnt find anything useful
    – Sudarshan
    Commented Nov 2, 2012 at 11:21
  • Please not that you changed the link of openvpn to another version of openvpn for android that is maintained anymore. The author of that software and I published our software around the same time.
    – plaisthos
    Commented Nov 2, 2012 at 11:21
  • 1
    I am just saying that github.com/kghost/ics-openvpn and code.google.com/p/ics-openvpn are different project. The latter is my project.
    – plaisthos
    Commented Nov 2, 2012 at 12:06
4

OpenSwan Solution: https://wiki.strongswan.org/projects/strongswan/wiki/AndroidVPNClient

OpenVPN Solution: https://code.google.com/p/ics-openvpn/

Both of these require the Android SDK and NDK.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.