VpnService
这段时间项目中使用到了VpnService,整理了一下官方文档的资料
VpnService is a base class for applications to extend and build their own VPN solutions. In general, it creates a virtual network interface, configures addresses and routing rules, and returns a file descriptor to the application. Each read from the descriptor retrieves an outgoing packet which was routed to the interface. Each write to the descriptor injects an incoming packet just like it was received from the interface. The interface is running on Internet Protocol (IP), so packets are always started with IP headers. The application then completes a VPN connection by processing and exchanging packets with the remote server over a tunnel.
VpnService是一个基类,用于应用扩展和构建自己的VPN解决方案。通常,它会创建一个虚拟网络接口、配置地址和路由规则,并返回一个文件描述给应用。每次读取描述都会去检索一个路由到接口的输出数据包。每次写入描述都会注入一个输入数据包,就像从接口收到的一样。这个接口运行在IP协议上,所以这些包总是以IP头开始。应用利用通道上的远程服务器处理和交换数据包,实现VPN连接。
Letting applications intercept packets raises huge security concerns. A VPN application can easily break the network. Besides, two of them may conflict with each other. The system takes several actions to address these issues. Here are some key points:
让应用程序截获数据包会引起巨大的安全问题。一个VPN应用能够轻易地破坏网络。此外,两者之间可能会互相冲突。系统采取了一系列措施来解决这些问题。以下是一些要点:
- User action is required the first time an application creates a VPN connection.
- There can be only one VPN connection running at the same time. The existing interface is deactivated when a new one is created.
- A system-managed notification is shown during the lifetime of a VPN connection.
- A system-managed dialog gives the information of the current VPN connection. It also provides a button to disconnect.
- The network is restored automatically when the file descriptor is closed. It also covers the cases when a VPN application is crashed or killed by the system.
- 应用第一次创建VPN连接需要用户操作。
- 同一时刻只能运行一个VPN连接。当一个新的VPN被创建时,已经存在的会失效。
- 在VPN连接的生命周期中,会显示一个系统管理级通知。
- 一个系统管理级的对话框提供当前VPN连接的信息。也提供了一个按钮去关闭连接。
- 当文件描述关闭时,网络会自动恢复。当一个VPN应用崩溃或者被系统杀死后,它的配置仍然有效。
There are two primary methods in this class: prepare(Context) and establish(). The former deals with user action and stops the VPN connection created by another application. The latter creates a VPN interface using the parameters supplied to the VpnService.Builder. An application must call prepare(Context) to grant the right to use other methods in this class, and the right can be revoked at any time. Here are the general steps to create a VPN connection:
这个类中有两个主要的方法:prepare(Context)和establish()。前者用于用户操作和停止在其他应用中创建的VPN连接。后者使用参数创建一个VPN接口提供给VpnService.Builder。应用必须调用prepare(Context)授权,才能使用本类中的其他方法,并且权限可以随时撤销。以下是创建一个VPN连接的一般步骤:
- When the user presses the button to connect, call
prepare(Context)and launch the returned intent, if non-null. - When the application becomes prepared, start the service.
- Create a tunnel to the remote server and negotiate the network parameters for the VPN connection.
- Supply those parameters to a
VpnService.Builderand create a VPN interface by callingestablish(). - Process and exchange packets between the tunnel and the returned file descriptor.
- When
onRevoke()is invoked, close the file descriptor and shut down the tunnel gracefully.
- 当用户按下Button去连接时,调用prepare(Context)返回一个intent,如果这个intent不为空,就启动它。
- 当应用准备好后,启动这个服务。
- 创建一个到远程服务器的通道,为VPN连接协商网络参数。
- 提供这些参数给VpnService.Builder,通过调用它establish()创建一个VPN接口。
- 在通道与返回的文件描述之间,处理和交换数据包。
- 当onRevoke()被调用时,优雅地关闭文件描述与通道。
Services extended this class need to be declared with appropriate permission and intent filter. Their access must be secured by BIND_VPN_SERVICE permission, and their intent filter must match SERVICE_INTERFACE action. Here is an example of declaring a VPN service in AndroidManifest.xml:
<service android:name=".ExampleVpnService"
android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter>
<action android:name="android.net.VpnService"/>
</intent-filter>
</service>
继承自此类的Services需要声明权限和intent filter。它们必须通过BIND_VPN_SERVICE权限才能安全访问,并且它们的intent filter必须匹配SERVICE_INTERFACE的action。以下是在AndroidManifest.xml中声明一个VPN service的案例:
<service android:name=".ExampleVpnService"
android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter>
<action android:name="android.net.VpnService"/>
</intent-filter>
</service>
VpnService的更多相关文章
- Android英文文档翻译系列(5)——VPNService
API14位于android.net.VpnService 类概述|Class OverviewVpnService is a base class for applications to ext ...
- Android N开发 你需要知道的一切
title: Android N开发 你需要知道的一切 tags: Android N,Android7.0,Android --- 转载请注明出处:http://www.cnblogs.com/yi ...
- Neutron 理解(10):虚拟专用网(VPN)虚拟化 [How Neutron implements VPN Virtualization]
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- android权限permission大全
1.Android.permission.WRITE_USER_DICTIONARY允许应用程序向用户词典中写入新词 2.android.permission.WRITE_SYNC_SETTINGS写 ...
- 安卓开发_深入学习ViewPager控件
一.概述 ViewPager是android扩展包v4包(android.support.v4.view.ViewPager)中的类,这个类可以让用户左右切换当前的view. ViewPager特点: ...
- Android 服务类Service 的详细学习
http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建 目录(?)[+] 什么是服务 服务有 ...
- Android_使用 OpenVPN
1. 需要 root http://www.doc88.com/p-847633362717.html http://bbs.gfan.com/android-3679297-1-1.html 需要 ...
- Android开发----权限大全
一.添加权限格式: 示例: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STOR ...
- Android permission访问权限大全
1.android.permission.WRITE_USER_DICTIONARY 允许应用程序向用户词典中写入新词 2.android.permission.WRITE_SYNC_SETTINGS ...
随机推荐
- Android开发之SQLite的使用方法
前言 SQLite是一种轻量级的小型数据库,虽然比较小,但是功能相对比较完善,一些常见的数据库基本功能也具有,在现在的嵌入式系统中使用该数据库的比较多,因为它占用系统资源很少.Android系统中也不 ...
- TMS3705A PCF7991AT 线路图
- httpclient发起https请求以及获取https返回内容
工作中的需要,使用Apache httpclient发起获取通过https返回的内容,试了很多网上的解决办法都不行,查阅了Apache httpclient的官方文档,最后终于找出解决方法,分享给需要 ...
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- android安全:forceStopPackage对android的Alarm的影响
也许一些使用alarmmanager做定时任务的同学遇到过这样的问题:设定alarm后,进入设置-->应用程序管理-->强行停止app后,定时任务就失效了. 简单的讲就是:force st ...
- 总结下cocopods的安装
一:CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库 ...
- python字符串操作(连接、比较、格式化等)(转)
字符串连接 方法一: Python代码 >>> str1 = 'hello' >>> str2 = 'world' >>> str1_2 = st ...
- MySQL优化---DBA对MySQL优化的一些总结
非原创, 来自梦嘉朋友, 非常好的总结, 一起学习. ------------------------------------------------- 1. 要确保有足够的内存数据库能够高效的运 ...
- valgrind 内存检测与调用图生成
http://blog.csdn.net/destina/article/details/6198443 感谢作者的分享! 一 valgrind是什么? Valgrind是一套Linux下,开放源 ...
- LINUX 逻辑地址、线性地址、物理地址和虚拟地址 转
一.概念物理地址(physical address)用于内存芯片级的单元寻址,与处理器和CPU连接的地址总线相对应.——这个概念应该是这几个概念中最好理解的一个,但是值得一提的是,虽然可以直接把物理地 ...