Skip to content

Latest commit

 

History

History
201 lines (161 loc) · 5.02 KB

ENGLISH.md

File metadata and controls

201 lines (161 loc) · 5.02 KB

SimpleOpenVpn

Based on OpenVPN encapsulated VPN connection library, support account password connection support all features of OpenVPN.

Applicable scenarios

  1. Network switching and area switching.
  2. Remove advertising (self-configuring net dns).
  3. Wait....

Conditions of Use

Need a server with OpenVpn~

Ready

Step 1. Clone,Input Module...

Step 2. Configuration permissions

    <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" />

Usage

Step 1.Initialization

Activity extends VPNActivity:

public class MainActivity extends VPNActivity{
    ...
}

Step 2. Setting ovpn configuration information

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";
         
// Set up the configuration for this connection 
 loadVpnProfile(data);

Step 3. More configuration information settings

   // Here you can set up your account password or other more information.
   getVpnProfile().mName = "This is VPN Title";
   .....

Step 4. Everything is ready. Let's start connecting.

    connectVpn();

Step 5. Status monitoring

implements 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, "未知错误");
        }
   }

or

    @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();
    }

Step 6. Notice Bar Click Jump

    @Override
    public Intent getJumpIntent() {
        Intent intent = new Intent();
        intent.setClassName(getPackageName(), MainActivity.class.getName());
        intent.setFlags(FLAG_ACTIVITY_SINGLE_TOP);
        return intent;
    }

Step 7. Disconnect

 stopVpn();

If the project helps you, please star!

License

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

   http: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.