Home | History | Annotate | Download | only in toyvpn
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.toyvpn;
     18 
     19 import android.app.Notification;
     20 import android.app.PendingIntent;
     21 import android.app.Service;
     22 import android.content.Intent;
     23 import android.content.SharedPreferences;
     24 import android.net.VpnService;
     25 import android.os.Handler;
     26 import android.os.Message;
     27 import android.os.ParcelFileDescriptor;
     28 import android.util.Log;
     29 import android.util.Pair;
     30 import android.widget.Toast;
     31 
     32 import java.io.IOException;
     33 import java.util.concurrent.atomic.AtomicInteger;
     34 import java.util.concurrent.atomic.AtomicReference;
     35 
     36 public class ToyVpnService extends VpnService implements Handler.Callback {
     37     private static final String TAG = ToyVpnService.class.getSimpleName();
     38 
     39     public static final String ACTION_CONNECT = "com.example.android.toyvpn.START";
     40     public static final String ACTION_DISCONNECT = "com.example.android.toyvpn.STOP";
     41 
     42     private Handler mHandler;
     43 
     44     private static class Connection extends Pair<Thread, ParcelFileDescriptor> {
     45         public Connection(Thread thread, ParcelFileDescriptor pfd) {
     46             super(thread, pfd);
     47         }
     48     }
     49 
     50     private final AtomicReference<Thread> mConnectingThread = new AtomicReference<>();
     51     private final AtomicReference<Connection> mConnection = new AtomicReference<>();
     52 
     53     private AtomicInteger mNextConnectionId = new AtomicInteger(1);
     54 
     55     private PendingIntent mConfigureIntent;
     56 
     57     @Override
     58     public void onCreate() {
     59         // The handler is only used to show messages.
     60         if (mHandler == null) {
     61             mHandler = new Handler(this);
     62         }
     63 
     64         // Create the intent to "configure" the connection (just start ToyVpnClient).
     65         mConfigureIntent = PendingIntent.getActivity(this, 0, new Intent(this, ToyVpnClient.class),
     66                 PendingIntent.FLAG_UPDATE_CURRENT);
     67     }
     68 
     69     @Override
     70     public int onStartCommand(Intent intent, int flags, int startId) {
     71         if (intent != null && ACTION_DISCONNECT.equals(intent.getAction())) {
     72             disconnect();
     73             return START_NOT_STICKY;
     74         } else {
     75             connect();
     76             return START_STICKY;
     77         }
     78     }
     79 
     80     @Override
     81     public void onDestroy() {
     82         disconnect();
     83     }
     84 
     85     @Override
     86     public boolean handleMessage(Message message) {
     87         Toast.makeText(this, message.what, Toast.LENGTH_SHORT).show();
     88         if (message.what != R.string.disconnected) {
     89             updateForegroundNotification(message.what);
     90         }
     91         return true;
     92     }
     93 
     94     private void connect() {
     95         // Become a foreground service. Background services can be VPN services too, but they can
     96         // be killed by background check before getting a chance to receive onRevoke().
     97         updateForegroundNotification(R.string.connecting);
     98         mHandler.sendEmptyMessage(R.string.connecting);
     99 
    100         // Extract information from the shared preferences.
    101         final SharedPreferences prefs = getSharedPreferences(ToyVpnClient.Prefs.NAME, MODE_PRIVATE);
    102         final String server = prefs.getString(ToyVpnClient.Prefs.SERVER_ADDRESS, "");
    103         final byte[] secret = prefs.getString(ToyVpnClient.Prefs.SHARED_SECRET, "").getBytes();
    104         final int port;
    105         try {
    106             port = Integer.parseInt(prefs.getString(ToyVpnClient.Prefs.SERVER_PORT, ""));
    107         } catch (NumberFormatException e) {
    108             Log.e(TAG, "Bad port: " + prefs.getString(ToyVpnClient.Prefs.SERVER_PORT, null), e);
    109             return;
    110         }
    111 
    112         // Kick off a connection.
    113         startConnection(new ToyVpnConnection(
    114                 this, mNextConnectionId.getAndIncrement(), server, port, secret));
    115     }
    116 
    117     private void startConnection(final ToyVpnConnection connection) {
    118         // Replace any existing connecting thread with the  new one.
    119         final Thread thread = new Thread(connection, "ToyVpnThread");
    120         setConnectingThread(thread);
    121 
    122         // Handler to mark as connected once onEstablish is called.
    123         connection.setConfigureIntent(mConfigureIntent);
    124         connection.setOnEstablishListener(new ToyVpnConnection.OnEstablishListener() {
    125             public void onEstablish(ParcelFileDescriptor tunInterface) {
    126                 mHandler.sendEmptyMessage(R.string.connected);
    127 
    128                 mConnectingThread.compareAndSet(thread, null);
    129                 setConnection(new Connection(thread, tunInterface));
    130             }
    131         });
    132         thread.start();
    133     }
    134 
    135     private void setConnectingThread(final Thread thread) {
    136         final Thread oldThread = mConnectingThread.getAndSet(thread);
    137         if (oldThread != null) {
    138             oldThread.interrupt();
    139         }
    140     }
    141 
    142     private void setConnection(final Connection connection) {
    143         final Connection oldConnection = mConnection.getAndSet(connection);
    144         if (oldConnection != null) {
    145             try {
    146                 oldConnection.first.interrupt();
    147                 oldConnection.second.close();
    148             } catch (IOException e) {
    149                 Log.e(TAG, "Closing VPN interface", e);
    150             }
    151         }
    152     }
    153 
    154     private void disconnect() {
    155         mHandler.sendEmptyMessage(R.string.disconnected);
    156         setConnectingThread(null);
    157         setConnection(null);
    158         stopForeground(true);
    159     }
    160 
    161     private void updateForegroundNotification(final int message) {
    162         startForeground(1, new Notification.Builder(this)
    163                 .setSmallIcon(R.drawable.ic_vpn)
    164                 .setContentText(getString(message))
    165                 .setContentIntent(mConfigureIntent)
    166                 .build());
    167     }
    168 }
    169