summaryrefslogtreecommitdiff
path: root/src/se/leap/leapclient/EIP.java
blob: 867805bd04e60766274f616d36ffa1c339157a52 (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package se.leap.leapclient;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import se.leap.openvpn.ConfigParser;
import se.leap.openvpn.ConfigParser.ConfigParseError;
import se.leap.openvpn.LaunchVPN;
import se.leap.openvpn.OpenVpnService;
import se.leap.openvpn.OpenVpnService.LocalBinder;
import se.leap.openvpn.ProfileManager;
import se.leap.openvpn.VpnProfile;

import android.app.Activity;
import android.app.IntentService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.util.Log;

/**
 * @author Sean Leonard <meanderingcode@aetherislands.net>
 *
 */
public final class EIP extends IntentService {
	
	public final static String ACTION_START_EIP = "se.leap.leapclient.START_EIP";
	public final static String ACTION_STOP_EIP = "se.leap.leapclient.STOP_EIP";
	public final static String ACTION_UPDATE_EIP_SERVICE = "se.leap.leapclient.UPDATE_EIP_SERVICE";
	
	public final static String ACTION_IS_EIP_RUNNING = "se.leap.leapclient.IS_RUNNING";
	
	public final static String EIP_NOTIFICATION = "EIP_NOTIFICATION";
	
	private static Context context;
	private static ResultReceiver mReceiver;
	// Binder to OpenVpnService for comm ops
	private static OpenVpnService mVpnService;
	private static boolean mBound = false;
	// Used to store actions to "resume" onServiceConnection
	private static String mPending = null;
	
	// Represents our Provider's eip-service.json
	private static JSONObject eipDefinition = null;
	
	// Our active gateway
	private static OVPNGateway activeGateway = null;

	public EIP(){
		super("LEAPEIP");
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		context = getApplicationContext();
		
		// Inflate our eip-service.json data
		try {
			eipDefinition = ConfigHelper.getJsonFromSharedPref(ConfigHelper.EIP_SERVICE_KEY);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.retreiveVpnService();
	}
	
	@Override
	public void onDestroy() {
		unbindService(mVpnServiceConn);
		mBound = false;

		super.onDestroy();
	}
	
	@Override
	protected void onHandleIntent(Intent intent) {
		// Get our action from the Intent
		String action = intent.getAction();
		// Get the ResultReceiver, if any
		mReceiver = intent.getParcelableExtra(ConfigHelper.RECEIVER_TAG);
		
		if ( action == ACTION_IS_EIP_RUNNING )
			this.isRunning();
		if ( action == ACTION_UPDATE_EIP_SERVICE )
			this.updateEIPService();
		else if ( action == ACTION_START_EIP )
			this.startEIP();
		else if ( action == ACTION_STOP_EIP )
			this.stopEIP();
	}
	
	private void retreiveVpnService() {
		Intent bindIntent = new Intent(this,OpenVpnService.class);
		bindIntent.setAction(OpenVpnService.RETRIEVE_SERVICE);
		bindService(bindIntent, mVpnServiceConn, 0);
	}
	
	private static ServiceConnection mVpnServiceConn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			mVpnService = binder.getService();
			mBound = true;

			if (mReceiver != null && mPending != null) {
				
				boolean running = mVpnService.isRunning();
				int resultCode = Activity.RESULT_CANCELED;
				
				if (mPending.equals(ACTION_IS_EIP_RUNNING))
					resultCode = (running) ? Activity.RESULT_OK : Activity.RESULT_CANCELED;
				if (mPending.equals(ACTION_START_EIP))
					resultCode = (running) ? Activity.RESULT_OK : Activity.RESULT_CANCELED;
				else if (mPending.equals(ACTION_STOP_EIP))
					resultCode = (running) ? Activity.RESULT_CANCELED
							: Activity.RESULT_OK;
				Bundle resultData = new Bundle();
				resultData.putString(ConfigHelper.REQUEST_TAG, EIP_NOTIFICATION);
				mReceiver.send(resultCode, resultData);
				
				mPending = null;
			}
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// XXX tell mReceiver!!
			mBound = false;
			
			if (mReceiver != null){
				Bundle resultData = new Bundle();
				resultData.putString(ConfigHelper.REQUEST_TAG, EIP_NOTIFICATION);
				mReceiver.send(Activity.RESULT_CANCELED, resultData);
			}
		}
	
	};
	
	private void isRunning() {
		// TODO I don't like that whatever requested this never receives a response
		//		if OpenVpnService has not been START_SERVICE, though the one place this is used that's okay
		if (mBound) {
			if (mReceiver != null){
				Bundle resultData = new Bundle();
				resultData.putString(ConfigHelper.REQUEST_TAG, ACTION_IS_EIP_RUNNING);
				int resultCode = (mVpnService.isRunning()) ? Activity.RESULT_OK : Activity.RESULT_CANCELED;
				mReceiver.send(resultCode, resultData);
			}
		} else {
			mPending = ACTION_IS_EIP_RUNNING;
			this.retreiveVpnService();
		}
	}

	private void startEIP() {
		if (activeGateway==null)
			activeGateway = selectGateway();
		
		Intent intent = new Intent(this,LaunchVPN.class);
		intent.setAction(Intent.ACTION_MAIN);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(LaunchVPN.EXTRA_KEY, activeGateway.mVpnProfile.getUUID().toString() );
		intent.putExtra(LaunchVPN.EXTRA_NAME, activeGateway.mVpnProfile.getName() );
		intent.putExtra(ConfigHelper.RECEIVER_TAG, mReceiver);
		startActivity(intent);
		// Let's give it 2s to get rolling... TODO there really should be a better way to do this, or it's not needed.
		// Read more code in .openvpn package
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Bind OpenVpnService for comm ops
		if (!mBound){
			mPending = ACTION_START_EIP;
			this.retreiveVpnService();
		} else {
			if (mReceiver != null) {
				Bundle resultData = new Bundle();
				resultData.putString(ConfigHelper.REQUEST_TAG, ACTION_START_EIP);
				mReceiver.send(Activity.RESULT_OK, resultData);
			}
		}
	}
	
	private void stopEIP() {
		if (mBound){
			mVpnService.onRevoke();

			/*if (mReceiver != null){
				Bundle resultData = new Bundle();
				resultData.putString(ConfigHelper.REQUEST_TAG, ACTION_STOP_EIP);
				mReceiver.send(Activity.RESULT_OK, resultData);
			}*/
		} else {
			// TODO If OpenVpnService isn't bound, does that really always mean it's not running?
			//		If it's not running, bindService doesn't work w/o START_SERVICE action, so...
			/*mPending = ACTION_STOP_EIP;
			this.retreiveVpnService();*/
		}
		// Remove this if above comes back
		if (mReceiver != null){
			Bundle resultData = new Bundle();
			resultData.putString(ConfigHelper.REQUEST_TAG, ACTION_STOP_EIP);
			mReceiver.send(Activity.RESULT_OK, resultData);
		}
	}

	private void updateEIPService() {
		// TODO this will also fetch new eip-service.json
		try {
			eipDefinition = ConfigHelper.getJsonFromSharedPref(ConfigHelper.EIP_SERVICE_KEY);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		updateGateways();
	}
	
	private OVPNGateway selectGateway() {
		// TODO Logic, yay!
		return new OVPNGateway("first");
	}
	
	private void updateGateways(){
		JSONArray gatewaysDefined = null;
		
		// Get our list of gateways
		try {
			gatewaysDefined = eipDefinition.getJSONArray("gateways");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		// Walk the list of gateways and inflate them to VPNProfiles
		for ( int i=0 ; i < gatewaysDefined.length(); i++ ){
			
			JSONObject gw = null;
			
			try {
				gw = gatewaysDefined.getJSONObject(i);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				if ( gw.getJSONObject("capabilities").getJSONArray("transport").toString().contains("openvpn") ){
					// We have an openvpn gateway!
					// Now build VPNProfiles and save their UUIDs
					// TODO create multiple profiles for each gateway to allow trying e.g. different ports when connections don't complete
					new OVPNGateway(gw);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private class OVPNGateway {
		
		// Log tag
		private String TAG = "OVPNGateway";
		
		// The actual VPN Profile object
		private VpnProfile mVpnProfile;
		// Our gateway definition from eip-service.json
		private JSONObject gateway;
		// This holds our OpenVPN options for creating the VPNProfile
		// Options get put here in the form that se.leap.openvpn.ConfigParser wants TODO will be gone w/ rewrite
		private HashMap<String,Vector<Vector<String>>> options = new HashMap<String, Vector<Vector<String>>>();

		
		// Constructor to load a gateway by name
		private OVPNGateway(String name){
			ProfileManager vpl = ProfileManager.getInstance(context);
			
			try {

				// FIXME ha, this got funny..it will get smart once i'm further...
				if ( name == "first" ) {
					name = vpl.getProfiles().iterator().next().mName;
				}
				
				mVpnProfile = vpl.getProfileByName(name);
				
			} catch (NoSuchElementException e) {
				
				// The gateway we were looking for is not in ProfileList!
				updateEIPService();
				
				// TODO prompt user to fix config error
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		// Constructor to create a gateway by definition
		protected OVPNGateway(JSONObject gw){
			// TODO We're going to build 1 profile per gateway, but eventually several

			gateway = gw;
			
			// Delete VpnProfile for host, if there already is one
			// FIXME There is a better way to check settings and update them, instead of destroy/rebuild
			// Also, this allows one gateway per hostname entry, so that had better be true from the server!
			// TODO Will we define multiple gateways per host, for variable options?  or change how .openvpn.VpnProfile works?
			ProfileManager vpl = ProfileManager.getInstance(context);
			Collection<VpnProfile> profiles = vpl.getProfiles();
			for (VpnProfile p : profiles){
				try {
					if ( p.mName.contains( gateway.getString("host") ) )
						vpl.removeProfile(context, p);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			// Create options HashMap for se.leap.openvpn.ConfigParser
			this.parseOptions();
			// Now create the VPNProfile
			this.createVPNProfile();
			// Now let's save it in the .openvpn package way
			
			setUniqueProfileName(vpl);
			vpl.addProfile(mVpnProfile);
			vpl.saveProfile(context, mVpnProfile);
			vpl.saveProfileList(context);
		}
		
		private void setUniqueProfileName(ProfileManager vpl) {
			int i=0;

			String newname;
			try {
				newname = gateway.getString("host");
				while(vpl.getProfileByName(newname)!=null) {
					i++;
					if(i==1)
						newname = getString(R.string.converted_profile);
					else
						newname = getString(R.string.converted_profile_i,i);
				}

				mVpnProfile.mName=newname;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				Log.v(TAG,"Couldn't read gateway name for profile creation!");
				e.printStackTrace();
			}
		}

		// FIXME this whole thing will get rewritten when we modify ConfigParser
		// 			in fact, don't even bother looking, except to debug
		private void parseOptions(){
			// TODO we will want to rewrite se.leap.openvpn.ConfigParser soon to be targeted at our use
			
			// FIXME move these to a common API (& version) definition place, like ProviderAPI or ConfigHelper
			String common_options = "openvpn_configuration";
			String remote = "ip_address";
			String ports = "ports";
			String protos = "protocols";
			String capabilities = "capabilities";
			
			// FIXME Our gateway definition has keys that are not OpenVPN options...
			// We need a hard spec for the eip-service.json and better handling in this class
			// Then we can stop dumping all the "capabilities" key:values into our options for parsing
			
			// Put our common options in
			// FIXME quite ugly.  We don't need the nested vectors, as we're not byte-reading file input, but we haven't rewritten the parser, yet

			Vector<String> arg = new Vector<String>();
			Vector<Vector<String>> args = new Vector<Vector<String>>();
			
			try {
				JSONObject def = (JSONObject) eipDefinition.get(common_options);
				Iterator keys = def.keys();
				Vector<Vector<String>> value = new Vector<Vector<String>>();
				while ( keys.hasNext() ){
					String key = keys.next().toString();
					
					arg.add(key);
					for ( String word : def.getString(key).split(" ") )
						arg.add(word);
					value.add( (Vector<String>) arg.clone() );
					options.put(key, (Vector<Vector<String>>) value.clone());

					value.clear();
					arg.clear();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// Now our gateway-specific options
			// to hold 'em the way they're wanted for parsing

			// remote:ip_address
			try {
				arg.add("remote");
				arg.add(gateway.getString(remote));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			args.add((Vector<String>) arg.clone());
			options.put("remote", (Vector<Vector<String>>) args.clone() );
			arg.clear();
			args.clear();
			
			// proto:udp||tcp
			JSONArray protocolsJSON = null;
			arg.add("proto");
			try {
				protocolsJSON = gateway.getJSONObject(capabilities).getJSONArray(protos);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Vector<String> protocols = new Vector<String>();
			for ( int i=0; i<protocolsJSON.length(); i++ )
				protocols.add(protocolsJSON.optString(i));
			if ( protocols.contains("udp"))
				arg.add("udp");
			else if ( protocols.contains("tcp"))
				arg.add("tcp");
			args.add((Vector<String>) arg.clone());
			options.put("proto", (Vector<Vector<String>>) args.clone());
			arg.clear();
			args.clear();
			
			
			// Now ports...picking one 'cause i say so'... TODO we should have multiple profiles?...
			String port = null;
			arg.add("port");
			try {
				port = gateway.getJSONObject(capabilities).getJSONArray(ports).optString(0);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			arg.add(port);
			args.add((Vector<String>) arg.clone());
			options.put("port", (Vector<Vector<String>>) args.clone());
			args.clear();
			arg.clear();
		}
		
		protected void createVPNProfile(){
			// TODO take data from eip-service.json for openvpn gateway definitions and create VPNProfile for each
			try {
				ConfigParser cp = new ConfigParser();
				cp.setDefinition(options);
				VpnProfile vp = cp.convertProfile();
				mVpnProfile = vp;
				Log.v(TAG,"Created VPNProfile");
			} catch (ConfigParseError e) {
				// FIXME Being here is bad because we didn't get a VpnProfile!
				Log.v(TAG,"Error createing VPNProfile");
				e.printStackTrace();
			}
		}
	}

}