基于OpenVPN封装的VPN连接库,支持账号密码连接 支持OpenVPN所有特性。
- 网络切换、地区切换。
- 去除广告(自行配置净网dns)。
- 等等....
需要一台搭建有OpenVpn的服务器~
Step 1. Clone,Input Module...
Step 2. 配置权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
在您的Activity中 extends VPNActivity:
public class MainActivity extends VPNActivity{
...
}
String data = "###############################################################################\n" +
"# OpenVPN 2.0 Sample Configuration File\n" +
"# for PacketiX VPN / SoftEther VPN Server\n" +
"# \n" +
"# !!! AUTO-GENERATED BY SOFTETHER VPN SERVER MANAGEMENT TOOL" +
" skip ........." +
"E2d5K+skR65kZjHIJ8TpFYb7ONSNhfkqFq0gP3JsqnKsbugjmiO4Lo0CgYEA5E5s\n" +
"62Q7dQ4kP+y5SBAPAtO2pxybyi/EiDFd9U1bSI1ln861yFIzKWKiirbwtbUhqeZR\n" +
"RzfBZ4TkMXQBZ86iquhSkkW6jCZktVpHhP5xFwdRSVxnNn8vQd2v73jijG154CCQ\n" +
"k6lpittssoMqRtmaE1MvB8Oe2x7Nnd3ll0egEt5AyVq6nAtq+6Ev\n" +
"-----END RSA PRIVATE KEY-----\n" +
"\n" +
"</key>\n";
// 设置此次连接的配置
loadVpnProfile(data);
// 在此可以设置账号密码或者其他更多信息
getVpnProfile().mName = "This is VPN Title";
.....
connectVpn();
实现 VPNActivity.VPNStatusListener 接口
public class MainActivity extends VPNActivity implements VPNActivity.VPNStatusListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// bind VPNStatusListener
addVPNStatusListener(this);
}
@Override
public void onConnectStart() {
Log.d(TAG, "开始连接");
}
@Override
public void onConnected() {
Log.d(TAG, "已连接");
}
@Override
public void onPaused() {
Log.d(TAG, "已暂停");
}
@Override
public void onNoNetwork() {
Log.d(TAG, "无网络");
}
@Override
public void onConnectClose() {
Log.d(TAG, "连接关闭");
}
@Override
public void onAuthFailed() {
Log.d(TAG, "认证失败");
}
@Override
public void onUnknown() {
Log.d(TAG, "未知错误");
}
}
或者
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// bindStatus
BindUtils.bind(this);
}
@BindStatus
public void onStatus(StatusInfo statusInfo) {
switch (statusInfo.getLevel()) {
case LEVEL_START:
// 开始连接
break;
case LEVEL_CONNECTED:
// 已连接
break;
case LEVEL_VPNPAUSED:
// 暂停
break;
.....
}
@Override
protected void onDestroy() {
BindUtils.unBind(this);
super.onDestroy();
}
@Override
public Intent getJumpIntent() {
Intent intent = new Intent();
intent.setClassName(getPackageName(), MainActivity.class.getName());
intent.setFlags(FLAG_ACTIVITY_SINGLE_TOP);
return intent;
}
stopVpn();
Copyright 2017 Milk Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.