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 ...
随机推荐
- ThinkPHP模板(一)
如何关闭ThinkPHP的模板缓存 ThinkPHP的模板缓存是无奈关闭的,因为内置的模板引擎是一个编译型的模板引擎,必须经过编译后生成一个可执行的缓存文件才能被执行.但是可以设置缓存的有效期,例如设 ...
- Codeforces Round #180 (Div. 2) D. Fish Weight 贪心
D. Fish Weight 题目连接: http://www.codeforces.com/contest/298/problem/D Description It is known that th ...
- Shadow Mapping 的原理与实践(一)
早在上世纪七十年代末,Williams在他的“Casting Curved Shadows on Curved Surface”一文中提出了名为Shadow Map的阴影生成技术.之后,他人在此基础上 ...
- Kubuntu(14.04)共享wifi(热点)
笔记本(kubuntu14.04)通过有线上网,共享本机无线给手机.平板及其他通过wifi上网的设备. 曾经在网上找过各种方法.下载了非常多软件都不能在本机上实现wifi共享,以下这样的方法眼下相对简 ...
- Linux内存管理原理 与文件读写 图 相当详细
http://www.cnblogs.com/zhaoyl/p/3695517.html http://www.cnblogs.com/huxiao-tee/p/4657851.html#_label ...
- [转]使用 PIVOT 和 UNPIVOT
可以使用 PIVOT 和 UNPIVOT 关系运算符将表值表达式更改为另一个表.PIVOT 通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执 ...
- 项目源码--Android聚合视频类播放器
下载源码 技术要点: 1.高效支持主流的视音频格式 2.本地视频的播放与管理 3.聚合电视在线直播 4.聚合优酷.搜狐.乐视.爱奇艺等多种在线视频 5.优质播放,包含播放.暂停,声音.亮度调整等功能 ...
- 最少javascript代码完成一个2048游戏
原生javascript代码写的2048游戏.建议在谷歌浏览器下跑.'WASD'控制方向.演示地址请移步:http://runjs.cn/detail/bp8baf8b 直接贴代码~ html: &l ...
- mysql记录sql执行时间
1.开启和关闭mysql> set profiling=1;mysql> set profiling=0; information_schema 的 database 会建立一个PROFI ...
- Java SortedSet接口
该SortedSet的接口扩展集Set并声明了一组按升序排序的行为.除了通过集定义的那些方法中,SortedSet接口声明总结于下表的方法: 有几种方法没有任何条目包含在调用Set的时候抛出一个NoS ...