这段时间项目中使用到了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连接的一般步骤:

  1. When the user presses the button to connect, call prepare(Context) and launch the returned intent, if non-null.
  2. When the application becomes prepared, start the service.
  3. Create a tunnel to the remote server and negotiate the network parameters for the VPN connection.
  4. Supply those parameters to a VpnService.Builder and create a VPN interface by calling establish().
  5. Process and exchange packets between the tunnel and the returned file descriptor.
  6. When onRevoke() is invoked, close the file descriptor and shut down the tunnel gracefully.
  1. 当用户按下Button去连接时,调用prepare(Context)返回一个intent,如果这个intent不为空,就启动它。
  2. 当应用准备好后,启动这个服务。
  3. 创建一个到远程服务器的通道,为VPN连接协商网络参数。
  4. 提供这些参数给VpnService.Builder,通过调用它establish()创建一个VPN接口。
  5. 在通道与返回的文件描述之间,处理和交换数据包。
  6. 当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的更多相关文章

  1. Android英文文档翻译系列(5)——VPNService

      API14位于android.net.VpnService 类概述|Class OverviewVpnService is a base class for applications to ext ...

  2. Android N开发 你需要知道的一切

    title: Android N开发 你需要知道的一切 tags: Android N,Android7.0,Android --- 转载请注明出处:http://www.cnblogs.com/yi ...

  3. Neutron 理解(10):虚拟专用网(VPN)虚拟化 [How Neutron implements VPN Virtualization]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  4. android权限permission大全

    1.Android.permission.WRITE_USER_DICTIONARY允许应用程序向用户词典中写入新词 2.android.permission.WRITE_SYNC_SETTINGS写 ...

  5. 安卓开发_深入学习ViewPager控件

    一.概述 ViewPager是android扩展包v4包(android.support.v4.view.ViewPager)中的类,这个类可以让用户左右切换当前的view. ViewPager特点: ...

  6. Android 服务类Service 的详细学习

    http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建   目录(?)[+] 什么是服务 服务有 ...

  7. Android_使用 OpenVPN

    1. 需要 root http://www.doc88.com/p-847633362717.html http://bbs.gfan.com/android-3679297-1-1.html 需要 ...

  8. Android开发----权限大全

    一.添加权限格式:     示例:      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STOR ...

  9. Android permission访问权限大全

    1.android.permission.WRITE_USER_DICTIONARY 允许应用程序向用户词典中写入新词 2.android.permission.WRITE_SYNC_SETTINGS ...

随机推荐

  1. PostgreSQL停止动作观察

    实验过程如下: 启动一个客户端: [postgres@cnrd56 bin]$ ./psql psql () Type "help" for help. postgres=# be ...

  2. C++中C/C++格式化输出

    对于不同的机器,一此格式化输出的函数经常会得不到正确的输出,比方小端上的程序在大端上执行等,另外,在日常程序开发时,也会经常被这种小问题而困扰非常久.终于发现是她的问题.不免有点叹息,以下对print ...

  3. 【M30】代理类

    1.考虑二维数组,在栈上分配,必须在编译时确定大小,也就是大小是常量.另外一点,C++不支持在堆上分配二维数组.怎么解决这个问题? 二维数组可以看成,一维数组的数组.因此,可以使用代理类,Array2 ...

  4. UITableViewCell重用机制

    UITableView是iOS开发中使用频率非常高的一个控件,它常被用来展示信息列表,尽管信息数据可能非常多,但UITableView消耗的资源却并不会随着展示信息的增多而变大,这都要得益于UITab ...

  5. poj 1845(等比数列前n项和及高速幂)

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13959   Accepted: 3433 Descripti ...

  6. GridView 编辑修改

    //点击gridview控件自带的编辑按钮时执行的事件    protected void gvNewsList_RowEditing(object sender, GridViewEditEvent ...

  7. dsPIC33EP timer1 初始化设置及应用

    //文件 p33timer1.h #ifndef _P33TIMER1_H_ #define _P33TIMER1_H_ //#include "p33timer1.h" #def ...

  8. JRebel_修改class后无法正确调试问题解决【2014-03-12】

    原文地址:http://www.cnblogs.com/hbbbs/p/3596179.html 现象 修改了class,编译后,JRebel自动装载到Web容器中.但此时调试这个类会发现无法正常调试 ...

  9. Viewport解决分辨率适配问题

    Viewport :   字面意思为视图窗口,在移动 web 开发中使用.表示将设备浏览器宽度虚拟成一个特定的值(或计算得出),这样利于移动 web 站点跨设备显示效果基本一致.   基本写法: &l ...

  10. php.ini的配置

    一.user_agent ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)') user_agent,再 ...