Android: Sample Code to use own VPN connection in Android 4.0 using VPNService API [closed]

泪湿孤枕 提交于 2019-11-26 11:56:14

问题


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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/13177522/android-sample-code-to-use-own-vpn-connection-in-android-4-0-using-vpnservice-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!