blob: a459dba526a2b1ff5b59294d4aa37f2e0506c7df (
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
|
package com.wireguard.android;
import android.app.Service;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.wireguard.config.Profile;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/**
* Service that handles profile state coordination and all background processing for the app.
*/
public class ProfileService extends Service {
private static final String TAG = "ProfileService";
private final IBinder binder = new ProfileServiceBinder();
private final ObservableList<Profile> profiles = new ObservableArrayList<>();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
new ProfileLoader().execute(getFilesDir().listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".conf");
}
}));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private class ProfileLoader extends AsyncTask<File, Void, List<Profile>> {
@Override
protected List<Profile> doInBackground(File... files) {
final List<String> interfaceNames = new LinkedList<>();
final List<Profile> loadedProfiles = new LinkedList<>();
final String command = "ip -br link show type wireguard | cut -d' ' -f1";
if (RootShell.run(interfaceNames, command) != 0) {
interfaceNames.clear();
Log.w(TAG, "Can't enumerate network interfaces. All profiles will appear down.");
}
for (File file : files) {
if (isCancelled())
return null;
final String fileName = file.getName();
final String profileName = fileName.substring(0, fileName.length() - 5);
final Profile profile = new Profile(profileName);
Log.v(TAG, "Attempting to load profile " + profileName);
try {
profile.parseFrom(openFileInput(fileName));
profile.setIsConnected(interfaceNames.contains(profileName));
loadedProfiles.add(profile);
} catch (IOException e) {
Log.w(TAG, "Failed to load profile from " + fileName, e);
}
}
return loadedProfiles;
}
@Override
protected void onPostExecute(List<Profile> loadedProfiles) {
if (loadedProfiles == null)
return;
profiles.addAll(loadedProfiles);
}
}
private class ProfileServiceBinder extends Binder implements ProfileServiceInterface {
@Override
public void connectProfile(Profile profile) {
}
@Override
public Profile copyProfileForEditing(Profile profile) {
return null;
}
@Override
public void disconnectProfile(Profile profile) {
}
@Override
public ObservableList<Profile> getProfiles() {
return profiles;
}
@Override
public void removeProfile(Profile profile) {
}
@Override
public void saveProfile(Profile newProfile) {
}
}
}
|