summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/LaunchVPN.java
blob: bc0a4cf203082b7beace1722c8d60636529d7047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * 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://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.
 */

package de.blinkt.openvpn;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.VpnService;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * This Activity actually handles two stages of a launcher shortcut's life cycle.
 * 
 * 1. Your application offers to provide shortcuts to the launcher.  When
 *    the user installs a shortcut, an activity within your application
 *    generates the actual shortcut and returns it to the launcher, where it
 *    is shown to the user as an icon.
 *
 * 2. Any time the user clicks on an installed shortcut, an intent is sent.
 *    Typically this would then be handled as necessary by an activity within
 *    your application.
 *    
 * We handle stage 1 (creating a shortcut) by simply sending back the information (in the form
 * of an {@link android.content.Intent} that the launcher will use to create the shortcut.
 * 
 * You can also implement this in an interactive way, by having your activity actually present
 * UI for the user to select the specific nature of the shortcut, such as a contact, picture, URL,
 * media item, or action.
 * 
 * We handle stage 2 (responding to a shortcut) in this sample by simply displaying the contents
 * of the incoming {@link android.content.Intent}.
 * 
 * In a real application, you would probably use the shortcut intent to display specific content
 * or start a particular operation.
 */
public class LaunchVPN extends ListActivity implements OnItemClickListener {

	public static final String EXTRA_KEY = "de.blinkt.openvpn.shortcutProfileUUID";
	public static final String EXTRA_NAME = "de.blinkt.openvpn.shortcutProfileName";
	public static final String EXTRA_HIDELOG =  "de.blinkt.openvpn.showNoLogWindow";;

	private static final int START_VPN_PROFILE= 70;
	

	private ProfileManager mPM;
	private VpnProfile mSelectedProfile;
	private boolean mhideLog=false;

	private boolean mCmfixed=false;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		mPM =ProfileManager.getInstance(this);
	
	}	

	@Override
	protected void onStart() {
		super.onStart();
		// Resolve the intent

		final Intent intent = getIntent();
		final String action = intent.getAction();
		
		// If the intent is a request to create a shortcut, we'll do that and exit

		
		if(Intent.ACTION_MAIN.equals(action)) {
			// we got called to be the starting point, most likely a shortcut
			String shortcutUUID = intent.getStringExtra( EXTRA_KEY);
			String shortcutName = intent.getStringExtra( EXTRA_NAME);
			mhideLog = intent.getBooleanExtra(EXTRA_HIDELOG, false);

			VpnProfile profileToConnect = ProfileManager.get(shortcutUUID);
			if(shortcutName != null && profileToConnect ==null)
				profileToConnect = ProfileManager.getInstance(this).getProfileByName(shortcutName);

			if(profileToConnect ==null) {
				OpenVPN.logError(R.string.shortcut_profile_notfound);
				// show Log window to display error
				showLogWindow();
				finish();
				return;
			}

			mSelectedProfile = profileToConnect;
			launchVPN();

		} else if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
			createListView();
		}
	}

	private void createListView() {
		ListView lv = getListView();
		//lv.setTextFilterEnabled(true);

		Collection<VpnProfile> vpnlist = mPM.getProfiles();

		Vector<String> vpnnames=new Vector<String>();
		for (VpnProfile vpnProfile : vpnlist) {
			vpnnames.add(vpnProfile.mName);
		}



		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,vpnnames);
		lv.setAdapter(adapter);

		lv.setOnItemClickListener(this);
	}

	/**
	 * This function creates a shortcut and returns it to the caller.  There are actually two 
	 * intents that you will send back.
	 * 
	 * The first intent serves as a container for the shortcut and is returned to the launcher by 
	 * setResult().  This intent must contain three fields:
	 * 
	 * <ul>
	 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li>
	 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with
	 * the shortcut.</li>
	 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a
	 * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as
	 * a drawable resource.</li>
	 * </ul>
	 * 
	 * If you use a simple drawable resource, note that you must wrapper it using
	 * {@link android.content.Intent.ShortcutIconResource}, as shown below.  This is required so
	 * that the launcher can access resources that are stored in your application's .apk file.  If 
	 * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras 
	 * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}.
	 * 
	 * The shortcut intent can be any intent that you wish the launcher to send, when the user 
	 * clicks on the shortcut.  Typically this will be {@link android.content.Intent#ACTION_VIEW} 
	 * with an appropriate Uri for your content, but any Intent will work here as long as it 
	 * triggers the desired action within your Activity.
	 * @param profile 
	 */
	private void setupShortcut(VpnProfile profile) {
		// First, set up the shortcut intent.  For this example, we simply create an intent that
		// will bring us directly back to this activity.  A more typical implementation would use a 
		// data Uri in order to display a more specific result, or a custom action in order to 
		// launch a specific operation.

		Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
		shortcutIntent.setClass(this, LaunchVPN.class);
		shortcutIntent.putExtra(EXTRA_KEY,profile.getUUID().toString());

		// Then, set up the container intent (the response to the caller)

		Intent intent = new Intent();
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, profile.getName());
		Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
				this,  R.drawable.icon);
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

		// Now, return the result to the launcher

		setResult(RESULT_OK, intent);
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		String profilename = ((TextView) view).getText().toString();

		VpnProfile profile = mPM.getProfileByName(profilename);

		// if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {	
		setupShortcut(profile);
		finish();
		return;
		//    }

	}

	private boolean writeMiniVPN() {
		File mvpnout = new File(getCacheDir(),VpnProfile.MINIVPN);
		if (mvpnout.exists() && mvpnout.canExecute())
			return true;

		IOException e2 = null;

		try {
			
			
			InputStream mvpn;
			
			try {
				mvpn = getAssets().open("minivpn." + Build.CPU_ABI);
			}
			catch (IOException errabi) {
				OpenVPN.logInfo("Failed getting assets for archicture " + Build.CPU_ABI);
				e2=errabi;
				mvpn = getAssets().open("minivpn." + Build.CPU_ABI2);
				
			}


			FileOutputStream fout = new FileOutputStream(mvpnout);

			byte buf[]= new byte[4096];

			int lenread = mvpn.read(buf);
			while(lenread> 0) {
				fout.write(buf, 0, lenread);
				lenread = mvpn.read(buf);
			}
			fout.close();

			if(!mvpnout.setExecutable(true)) {
				OpenVPN.logMessage(0, "","Failed to set minivpn executable");
				return false;
			}
				
			
			return true;
		} catch (IOException e) {
			if(e2!=null)
				OpenVPN.logMessage(0, "",e2.getLocalizedMessage());
			OpenVPN.logMessage(0, "",e.getLocalizedMessage());
			e.printStackTrace();
			return false;
		}
	}

	private void askForPW(final int type) {

		final EditText entry = new EditText(this);
		entry.setSingleLine();
		entry.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		entry.setTransformationMethod(new PasswordTransformationMethod());

		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("Need " + getString(type));
		dialog.setMessage("Enter the password for profile " + mSelectedProfile.mName);
		dialog.setView(entry);

		dialog.setPositiveButton(android.R.string.ok,
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String pw = entry.getText().toString();
				if(type == R.string.password) {
					mSelectedProfile.mTransientPW = pw;
				} else {
					mSelectedProfile.mTransientPCKS12PW = pw;
				}
				onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null);

			}

		});
		dialog.setNegativeButton(android.R.string.cancel,
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});

		dialog.create().show();

	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if(requestCode==START_VPN_PROFILE) {
			if(resultCode == Activity.RESULT_OK) {
				int needpw = mSelectedProfile.needUserPWInput();
				if(needpw !=0) {
					askForPW(needpw);
				} else {
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);        
					boolean showlogwindow = prefs.getBoolean("showlogwindow", true);
					
					if(!mhideLog && showlogwindow)
						showLogWindow();
					new startOpenVpnThread().start();
				}
			} else if (resultCode == Activity.RESULT_CANCELED) {
				// User does not want us to start, so we just vanish
				finish();
			}
		}
	}
	void showLogWindow() {

		Intent startLW = new Intent(getBaseContext(),LogWindow.class);
		startLW.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(startLW);

	}

	void showConfigErrorDialog(int vpnok) {
		AlertDialog.Builder d = new AlertDialog.Builder(this);
		d.setTitle(R.string.config_error_found);
		d.setMessage(vpnok);
		d.setPositiveButton(android.R.string.ok, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();

			}
		});
		d.show();
	}

	void launchVPN () {
		int vpnok = mSelectedProfile.checkProfile(this);
		if(vpnok!= R.string.no_error_found) {
			showConfigErrorDialog(vpnok);
			return;
		}

		Intent intent = VpnService.prepare(this);
		// Check if we want to fix /dev/tun
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);        
		boolean usecm9fix = prefs.getBoolean("useCM9Fix", false);
		boolean loadTunModule = prefs.getBoolean("loadTunModule", false);

		if(loadTunModule)
			execeuteSUcmd("insmod /system/lib/modules/tun.ko");

		if(usecm9fix && !mCmfixed ) {
			execeuteSUcmd("chown system /dev/tun");
		}


		if (intent != null) {
			// Start the query
			try {
				startActivityForResult(intent, START_VPN_PROFILE);
			} catch (ActivityNotFoundException ane) {
				// Shame on you Sony! At least one user reported that 
				// an official Sony Xperia Arc S image triggers this exception
				OpenVPN.logError(R.string.no_vpn_support_image);
				showLogWindow();
			}
		} else {
			onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null);
		}

	}

	private void execeuteSUcmd(String command) {
		ProcessBuilder pb = new ProcessBuilder(new String[] {"su","-c",command});
		try {
			Process p = pb.start();
			int ret = p.waitFor();
			if(ret ==0)
				mCmfixed=true;
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private class startOpenVpnThread extends Thread {

		@Override
		public void run() {
			startOpenVpn();
		}

		void startOpenVpn() {
			if(!writeMiniVPN()) {
				OpenVPN.logMessage(0, "", "Error writing minivpn binary");
				return;
			}
			OpenVPN.logMessage(0, "", getString(R.string.building_configration));

			Intent startVPN = mSelectedProfile.prepareIntent(getBaseContext());
			if(startVPN!=null)
				startService(startVPN);
			finish();

		}
	}


}