aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/service/TincVpnService.java
blob: 1a6f58ffdf36c90ed6fa435fb817f8adc3bbf1b7 (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
package org.pacien.tincapp.service;

import android.app.Service;
import android.content.Intent;
import android.net.VpnService;

import org.pacien.tincapp.BuildConfig;
import org.pacien.tincapp.commands.Tinc;
import org.pacien.tincapp.commands.Tincd;
import org.pacien.tincapp.context.AppPaths;

import java.io.IOException;

import static org.pacien.tincapp.util.Function.applyIgnoringExcept;

/**
 * @author pacien
 */
public class TincVpnService extends VpnService {

	static final public String INTENT_EXTRA_NET_NAME = "netName";

	private String netName;

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME);

		Builder net = new Builder().setSession(this.netName);
		VpnInterfaceConfigurator.applyConfiguration(net, AppPaths.netConfFile(this, this.netName));
		applyIgnoringExcept(net::addDisallowedApplication, BuildConfig.APPLICATION_ID);

		try {
			Tincd.start(this, this.netName, net.establish().detachFd());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return Service.START_STICKY;
	}

	@Override
	public void onDestroy() {
		try {
			Tinc.stop(this, this.netName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}