Android: Sample Code to connect to VPN in Android 4.0 using VPNService API [closed]
Asked Answered
S

3

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?

Smokedry answered 1/11, 2012 at 12:54 Comment(0)
M
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.

Megillah answered 9/4, 2016 at 15:26 Comment(5)
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?Frankenstein
@Frankenstein 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.Megillah
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.Frankenstein
Does it support openvpn as well?Adversity
@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)Megillah
M
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

Mooring answered 1/11, 2012 at 13:15 Comment(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 advanceSmokedry
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.Smokedry
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 usefulSmokedry
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.Voodooism
@Voodooism u r meaning to say the source which i compiled is wrong? actually i need a demo credetials for the above apk for testing purposeSmokedry
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.Voodooism
@Voodooism oh k...Can u tell how much ur project will help my scenario that i mentioned in the question.. can u share how to acheive that, as iam totally new to this concepts.Smokedry
You have not stated if your own VPN is based on OpenVPN. If it is it should work out of the box. If not my project won't help you thereVoodooism
@Voodooism is there any test values for ur project to check for the connection.. U mentioned "You have not stated if your own VPN is based on OpenVPN" is this means there are some criteria/spec for a VPN to be a OpenVPN?Smokedry
@Deepak you may need to create the cerficate for openvpn and then you need to load that certificate to make OpenVPN working.But see i tried to install the certificate but that was not worked for me.so if its worked for you then let me knowMooring
@juned i too installed one certificate it didnt work.. iam wondering there will be public vpn site if we can use their ip and certificate then we can test. but i couldnt find anythng, if u can share info on this i ll check and let u knw..Smokedry
@Deepak we need some guidance of plaisthos, its his project so definitely he has idea about thatMooring
@juned he is saying the project which u shared is not his project..i think u can able to see his commnts..Smokedry
Ohh i see but we need a single working project which able to establish a VPN connection,so it doesn't matter who is owner of project. if his project is working then that will be greatMooring
R
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.

Rebuke answered 28/10, 2014 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.