启动VPN连接

时间:2017-08-28 11:07:03

标签: android vpn

我想为一些内部公司开发一个应用程序。但是,这需要使用.p12证书进行VPN连接。

有没有办法,我可以使用此证书在应用程序中打开VPN连接?或者更容易自动打开VPN应用程序并连接到所选的VPN。

1 个答案:

答案 0 :(得分:0)

是的,您可以通过以下方式完成:

 Intent intent = VpnService.prepare(StatePermissionActivity.this);
if (intent != null) {
                startActivityForResult(intent, 0);
            }

建立VPN服务:

public class UtilityVpnService extends VpnService {

    //https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/VpnService.java
    // http://stackoverflow.com/questions/32173374/packet-sent-but-cannot-received-packets

    Builder builder;
    private ParcelFileDescriptor mInterface;
    private Thread mThread;
    private IBinder mBinder = new MyBinder();
    public static String VPN_STATE = "VPN_STATE";
    public static int VPN_SERVICE_STOPPED = 1;
    public static int VPN_SERVICE_STARTED = 2;
    public static int VPN_SERVICE_RESUMED = 3;
    public static int BLOCK_SINGLE_APP = 4;
    public static int ALLOW_SINGLE_APP = 5;
    public static int PACKAGE_NAME = 6;

    public UtilityVpnService() {
    }

    @Override
    public void onCreate() {
        Context context = this;
        Notification notification;
        Intent notificationIntent = new Intent();
        notificationIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        NotificationCompat.Builder bBuilder = new NotificationCompat.Builder(context);

        Intent vpnIntent = new Intent(context, UtilityVpnService.class);
        context.startService(vpnIntent);

        notification = bBuilder.build();
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        startForeground(54312, notification);
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return true;
    }


    public class MyBinder extends Binder {
        public UtilityVpnService getService() {
            return UtilityVpnService.this;
        }
    }

    public void closeVpnInterface() {
        try {
            if (mInterface != null) {
                mInterface.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        stopSelf();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Intent restartService = new Intent(getApplicationContext(), this.getClass());
        restartService.setPackage(getPackageName());
        PendingIntent restartServicePI = PendingIntent.getService(getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);

        //Restart the service once it has been killed android
        AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 100, restartServicePI);
        alarmService.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, restartServicePI);
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Context context = this;

        initilizeVpnBuilder();
        if (!CheckServiceIsRunning.isMyServiceRunning(RunningAppsIdentify.class, this)) {
            Intent vpnIntent = new Intent(context, RunningAppsIdentify.class);
            context.startService(vpnIntent);
        }

        return START_STICKY;
    }


    private void initilizeVpnBuilder() {
        mThread = new Thread(new Thread() {
            @Override
            public void run() {
                try {
//                    try {
//                        if (builder != null) {
//                            mInterface.close();
//                        }
//                    } catch (Exception ex) {
//                        ex.printStackTrace();
//                    }

                    // Configure the tunnel and get the interface.
                    // Build VPN service
                    builder = new Builder();
                    builder.setSession(getString(R.string.app_name));

                    // VPN address
                    builder.addAddress("10.1.10.1", 32);
                    builder.addAddress("fd00:1:fd00:1:fd00:1:fd00:1", 128);

                    // DNS address
                    builder.addDnsServer("8.8.8.8");

                    // Add Route
                    builder.addRoute("0.0.0.0", 0);
                    builder.addRoute("0:0:0:0:0:0:0:0", 0);

                    //Setting Applications which are allowed Internet
                    // and isInternetAllowed = True
                    List<AppsModel> list = AppsModel.getAppsAllowedForInternet();
                    for (int i = 0; i < list.size(); i++) {
                        builder.addDisallowedApplication(list.get(i).getAppPackage());
                    }

                    //Establish interface
                    mInterface = builder.establish();

                    // Add list of allowed applications
                    // Packets to be sent are queued in this input stream.
                    FileInputStream inputStream = new FileInputStream(mInterface.getFileDescriptor());

                    // Packets recieved need to be written to this output stream.
                    FileOutputStream outputStream = new FileOutputStream(mInterface.getFileDescriptor());

                    DatagramChannel tunnel = DatagramChannel.open();
                    tunnel.connect(new InetSocketAddress("127.0.0.1", 8087));
                    protect(tunnel.socket());

                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    reader.mark(123);
                    reader.markSupported();

                    ByteBuffer packet = ByteBuffer.allocate(32767);
                    //byte[] bytes = extract(inputStream);
                    //byteBuffer.put(bytes,0,bytes.length);

                    // We keep forwarding packets till something goes wrong.
                    while (true) {
                        // Assume that we did not make any progress in this iteration.
                        try {
                            //Utils.getWifiSpeed(UtilityVpnService.this);
                            // Read the outgoing packet from the input stream.
                            int length = inputStream.read(packet.array());
                            if (length > 0) {
                                //Log.d("packet received", "" + length);
                                packet.limit(length);
                                tunnel.write(packet);
                                // Getting info about the packet..
                                //debugPacket(packet);
                                packet.clear();

                                // reading incoming packet from tunnel.
                                length = tunnel.read(packet);
                                if (length > 0) {
                                    outputStream.write(packet.array());
                                    packet.clear();
                                }
                            }
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
//                        String line = reader.readLine();
//                        if (line != null)
//                            Log.d("line", "" + line);
                        // Instructions
                        //get packet with in
                        //put packet to tunnel
                        //get packet from tunnel
                        // return packet with out
                        Thread.sleep(100);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        mThread.start();
    }

    @Override
    public void onRevoke() {
        super.onRevoke();
        stopSelf();
    }

    private void debugPacket(ByteBuffer packet) {
        int buffer = packet.get();
        int version;
        int headerlength;
        version = buffer >> 4;
        headerlength = buffer & 0x0F;
        headerlength *= 4;
        Log.d("Tag", "IP Version:" + version);
        Log.d("Tag", "Header Length:" + headerlength);

        String status = "";
        status += "Header Length:" + headerlength;

        buffer = packet.get();      //DSCP + EN
        buffer = packet.getChar();  //Total Length

        Log.d("Tag", "Total Length:" + buffer);

        buffer = packet.getChar();  //Identification
        buffer = packet.getChar();  //Flags + Fragment Offset
        buffer = packet.get();      //Time to Live
        buffer = packet.get();      //Protocol

        Log.d("Tag", "Protocol:" + buffer);
        status += "  Protocol:" + buffer;

        buffer = packet.getChar();  //Header checksum

        String sourceIP = "";
        buffer = packet.get();  //Source IP 1st Octet
        sourceIP += ((int) buffer) & 0xFF;
        sourceIP += ".";

        buffer = packet.get();  //Source IP 2nd Octet
        sourceIP += ((int) buffer) & 0xFF;
        sourceIP += ".";

        buffer = packet.get();  //Source IP 3rd Octet
        sourceIP += ((int) buffer) & 0xFF;
        sourceIP += ".";

        buffer = packet.get();  //Source IP 4th Octet
        sourceIP += ((int) buffer) & 0xFF;

        Log.d("Tag", "Source IP:" + sourceIP);
        status += "   Source IP:" + sourceIP;

        String destIP = "";
        buffer = packet.get();  //Destination IP 1st Octet
        destIP += ((int) buffer) & 0xFF;
        destIP += ".";

        buffer = packet.get();  //Destination IP 2nd Octet
        destIP += ((int) buffer) & 0xFF;
        destIP += ".";

        buffer = packet.get();  //Destination IP 3rd Octet
        destIP += ((int) buffer) & 0xFF;
        destIP += ".";

        buffer = packet.get();  //Destination IP 4th Octet
        destIP += ((int) buffer) & 0xFF;

        Log.d("Tag", "Destination IP:" + destIP);
        status += "   Destination IP:" + destIP;

        try {
            InetAddress addr = InetAddress.getByName(destIP);
            String host = addr.getHostName();
            Log.d("Tag", "Hostname:" + host);
        } catch (UnknownHostException e) {
            Log.d("Tag", "Unknown host");
        }
    }

    @Override
    public void onDestroy() {
        if (mThread != null) {
            mThread.interrupt();
        }

        super.onDestroy();
    }

}
startService

上的

onActivityResult()