mirror of
https://github.com/amnezia-vpn/amneziawg-android.git
synced 2026-07-03 14:05:13 +02:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9245e9536a | |||
| 292df12175 | |||
| f988306c17 | |||
| 64ce6cd5a2 | |||
| 5476fc2228 | |||
| 7120ef9ffd |
+2
-2
@@ -20,8 +20,8 @@ android {
|
||||
applicationId 'com.wireguard.android'
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 27
|
||||
versionCode 416
|
||||
versionName '0.0.20180524'
|
||||
versionCode 418
|
||||
versionName '0.0.20180527'
|
||||
}
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
|
||||
@@ -62,9 +62,6 @@
|
||||
<intent-filter>
|
||||
<action android:name="android.net.VpnService" />
|
||||
</intent-filter>
|
||||
<meta-data
|
||||
android:name="android.net.VpnService.SUPPORTS_ALWAYS_ON"
|
||||
android:value="false" />
|
||||
</service>
|
||||
|
||||
<service
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import com.wireguard.android.backend.WgQuickBackend;
|
||||
import com.wireguard.android.model.TunnelManager;
|
||||
import com.wireguard.android.util.ExceptionLoggers;
|
||||
|
||||
@@ -19,13 +20,16 @@ public class BootShutdownReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(final Context context, final Intent intent) {
|
||||
if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
|
||||
return;
|
||||
}
|
||||
final String action = intent.getAction();
|
||||
if (action == null)
|
||||
return;
|
||||
final TunnelManager tunnelManager = Application.getComponent().getTunnelManager();
|
||||
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
|
||||
Log.i(TAG, "Broadcast receiver restoring state (boot)");
|
||||
tunnelManager.restoreState().whenComplete(ExceptionLoggers.D);
|
||||
tunnelManager.restoreState(false).whenComplete(ExceptionLoggers.D);
|
||||
} else if (Intent.ACTION_SHUTDOWN.equals(action)) {
|
||||
Log.i(TAG, "Broadcast receiver saving state (shutdown)");
|
||||
tunnelManager.saveState();
|
||||
|
||||
@@ -94,9 +94,10 @@ public class SettingsActivity extends AppCompatActivity {
|
||||
public void onCreatePreferences(final Bundle savedInstanceState, final String key) {
|
||||
addPreferencesFromResource(R.xml.preferences);
|
||||
if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
|
||||
final Preference toolsInstaller =
|
||||
getPreferenceManager().findPreference("tools_installer");
|
||||
getPreferenceScreen().removePreference(toolsInstaller);
|
||||
Preference pref = getPreferenceManager().findPreference("tools_installer");
|
||||
getPreferenceScreen().removePreference(pref);
|
||||
pref = getPreferenceManager().findPreference("restore_on_boot");
|
||||
getPreferenceScreen().removePreference(pref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package com.wireguard.android.backend;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
@@ -13,9 +14,11 @@ import android.support.v4.util.ArraySet;
|
||||
import android.util.Log;
|
||||
|
||||
import com.wireguard.android.Application;
|
||||
import com.wireguard.android.activity.MainActivity;
|
||||
import com.wireguard.android.model.Tunnel;
|
||||
import com.wireguard.android.model.Tunnel.State;
|
||||
import com.wireguard.android.model.Tunnel.Statistics;
|
||||
import com.wireguard.android.util.ExceptionLoggers;
|
||||
import com.wireguard.config.Config;
|
||||
import com.wireguard.config.IPCidr;
|
||||
import com.wireguard.config.Interface;
|
||||
@@ -51,10 +54,6 @@ public final class GoBackend implements Backend {
|
||||
|
||||
private static native int wgGetSocketV6(int handle);
|
||||
|
||||
private static native void wgPutSocketV4(int handle);
|
||||
|
||||
private static native void wgPutSocketV6(int handle);
|
||||
|
||||
private static native void wgTurnOff(int handle);
|
||||
|
||||
private static native int wgTurnOn(String ifName, int tunFd, String settings);
|
||||
@@ -162,6 +161,10 @@ public final class GoBackend implements Backend {
|
||||
final VpnService.Builder builder = service.getBuilder();
|
||||
builder.setSession(tunnel.getName());
|
||||
|
||||
final Intent configureIntent = new Intent(context, MainActivity.class);
|
||||
configureIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
builder.setConfigureIntent(PendingIntent.getActivity(context, 0, configureIntent, 0));
|
||||
|
||||
for (final IPCidr addr : config.getInterface().getAddresses())
|
||||
builder.addAddress(addr.getAddress(), addr.getCidr());
|
||||
|
||||
@@ -190,9 +193,7 @@ public final class GoBackend implements Backend {
|
||||
currentTunnel = tunnel;
|
||||
|
||||
service.protect(wgGetSocketV4(currentTunnelHandle));
|
||||
wgPutSocketV4(currentTunnelHandle);
|
||||
service.protect(wgGetSocketV6(currentTunnelHandle));
|
||||
wgPutSocketV6(currentTunnelHandle);
|
||||
} else {
|
||||
Log.i(TAG, "Bringing tunnel down");
|
||||
|
||||
@@ -208,6 +209,7 @@ public final class GoBackend implements Backend {
|
||||
}
|
||||
|
||||
private void startVpnService() {
|
||||
Log.d(TAG, "Requesting to start VpnService");
|
||||
context.startService(new Intent(context, VpnService.class));
|
||||
}
|
||||
|
||||
@@ -231,5 +233,15 @@ public final class GoBackend implements Backend {
|
||||
vpnService = vpnService.newIncompleteFuture();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
vpnService.complete(this);
|
||||
if (intent == null || intent.getComponent() == null || !intent.getComponent().getPackageName().equals(getPackageName())) {
|
||||
Log.d(TAG, "Service started by Always-on VPN feature");
|
||||
Application.getComponent().getTunnelManager().restoreState(true).whenComplete(ExceptionLoggers.D);
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,6 +169,7 @@ public class TunnelEditorFragment extends BaseFragment {
|
||||
tunnel = newTunnel;
|
||||
message = getString(R.string.tunnel_create_success, tunnel.getName());
|
||||
Log.d(TAG, message);
|
||||
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
|
||||
onFinished();
|
||||
} else {
|
||||
final String error = ExceptionLoggers.unwrapMessage(throwable);
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.wireguard.android.util.ObservableSortedKeyedArrayList;
|
||||
import com.wireguard.android.util.ObservableSortedKeyedList;
|
||||
import com.wireguard.config.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Set;
|
||||
@@ -55,6 +56,8 @@ public final class TunnelManager extends BaseObservable {
|
||||
private final ObservableSortedKeyedList<String, Tunnel> tunnels =
|
||||
new ObservableSortedKeyedArrayList<>(COMPARATOR);
|
||||
private Tunnel lastUsedTunnel;
|
||||
private boolean haveLoaded;
|
||||
private ArrayList<CompletableFuture<Void>> delayedLoadRestoreTunnels = new ArrayList<>();
|
||||
|
||||
@Inject
|
||||
public TunnelManager(final AsyncWorker asyncWorker, final Backend backend,
|
||||
@@ -140,12 +143,27 @@ public final class TunnelManager extends BaseObservable {
|
||||
.whenComplete(ExceptionLoggers.E);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void onTunnelsLoaded(final Iterable<String> present, final Collection<String> running) {
|
||||
for (final String name : present)
|
||||
addToList(name, null, running.contains(name) ? State.UP : State.DOWN);
|
||||
final String lastUsedName = preferences.getString(KEY_LAST_USED_TUNNEL, null);
|
||||
if (lastUsedName != null)
|
||||
setLastUsedTunnel(tunnels.get(lastUsedName));
|
||||
CompletableFuture<Void> toComplete[];
|
||||
synchronized (delayedLoadRestoreTunnels) {
|
||||
haveLoaded = true;
|
||||
toComplete = delayedLoadRestoreTunnels.toArray(new CompletableFuture[delayedLoadRestoreTunnels.size()]);
|
||||
delayedLoadRestoreTunnels.clear();
|
||||
}
|
||||
restoreState(true).whenComplete((v, t) -> {
|
||||
for (final CompletableFuture<Void> f : toComplete) {
|
||||
if (t == null)
|
||||
f.complete(v);
|
||||
else
|
||||
f.completeExceptionally(t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void refreshTunnelStates() {
|
||||
@@ -157,9 +175,16 @@ public final class TunnelManager extends BaseObservable {
|
||||
.whenComplete(ExceptionLoggers.E);
|
||||
}
|
||||
|
||||
public CompletionStage<Void> restoreState() {
|
||||
if (!preferences.getBoolean(KEY_RESTORE_ON_BOOT, false))
|
||||
public CompletionStage<Void> restoreState(boolean force) {
|
||||
if (!force && !preferences.getBoolean(KEY_RESTORE_ON_BOOT, false))
|
||||
return CompletableFuture.completedFuture(null);
|
||||
synchronized (delayedLoadRestoreTunnels) {
|
||||
if (!haveLoaded) {
|
||||
CompletableFuture<Void> f = new CompletableFuture<>();
|
||||
delayedLoadRestoreTunnels.add(f);
|
||||
return f;
|
||||
}
|
||||
}
|
||||
final Set<String> previouslyRunning = preferences.getStringSet(KEY_RUNNING_TUNNELS, null);
|
||||
if (previouslyRunning == null)
|
||||
return CompletableFuture.completedFuture(null);
|
||||
@@ -236,6 +261,7 @@ public final class TunnelManager extends BaseObservable {
|
||||
tunnel.onStateChanged(e == null ? newState : tunnel.getState());
|
||||
if (e == null && newState == State.UP)
|
||||
setLastUsedTunnel(tunnel);
|
||||
saveState();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ func (l AndroidLogger) Write(p []byte) (int, error) {
|
||||
var tunnelHandles map[int32]*Device
|
||||
|
||||
func init() {
|
||||
roamingDisabled = true
|
||||
tunnelHandles = make(map[int32]*Device)
|
||||
signals := make(chan os.Signal)
|
||||
signal.Notify(signals, unix.SIGUSR2)
|
||||
@@ -85,7 +86,6 @@ func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 {
|
||||
}
|
||||
|
||||
device.Up()
|
||||
device.net.bind.(*NativeBind).clearSourceOnAllRouteChanges = true
|
||||
logger.Info.Println("Device started")
|
||||
|
||||
var i int32
|
||||
@@ -122,7 +122,18 @@ func wgGetSocketV4(tunnelHandle int32) int32 {
|
||||
if !ok {
|
||||
return -1
|
||||
}
|
||||
return int32(native.sock4)
|
||||
fd := int32(-1)
|
||||
conn, err := native.ipv4.SyscallConn()
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
err = conn.Control(func(f uintptr) {
|
||||
fd = int32(f)
|
||||
})
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
return fd
|
||||
}
|
||||
|
||||
//export wgGetSocketV6
|
||||
@@ -135,41 +146,18 @@ func wgGetSocketV6(tunnelHandle int32) int32 {
|
||||
if !ok {
|
||||
return -1
|
||||
}
|
||||
return int32(native.sock6)
|
||||
}
|
||||
|
||||
//export wgPutSocketV4
|
||||
func wgPutSocketV4(tunnelHandle int32) {
|
||||
device, ok := tunnelHandles[tunnelHandle]
|
||||
if !ok {
|
||||
return
|
||||
fd := int32(-1)
|
||||
conn, err := native.ipv6.SyscallConn()
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
native, ok := device.net.bind.(*NativeBind)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
fwmark, err := unix.GetsockoptInt(native.sock6, unix.SOL_SOCKET, unix.SO_MARK)
|
||||
if err == nil {
|
||||
native.lastMark = uint32(fwmark)
|
||||
device.net.fwmark = uint32(fwmark)
|
||||
}
|
||||
}
|
||||
|
||||
//export wgPutSocketV6
|
||||
func wgPutSocketV6(tunnelHandle int32) {
|
||||
device, ok := tunnelHandles[tunnelHandle]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
native, ok := device.net.bind.(*NativeBind)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
fwmark, err := unix.GetsockoptInt(native.sock6, unix.SOL_SOCKET, unix.SO_MARK)
|
||||
if err == nil {
|
||||
native.lastMark = uint32(fwmark)
|
||||
device.net.fwmark = uint32(fwmark)
|
||||
err = conn.Control(func(f uintptr) {
|
||||
fd = int32(f)
|
||||
})
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
return fd
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
@@ -10,8 +10,6 @@ extern int wgTurnOn(struct go_string ifname, int tun_fd, struct go_string settin
|
||||
extern void wgTurnOff(int handle);
|
||||
extern int wgGetSocketV4(int handle);
|
||||
extern int wgGetSocketV6(int handle);
|
||||
extern void wgPutSocketV4(int handle);
|
||||
extern void wgPutSocketV6(int handle);
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOn(JNIEnv *env, jclass c, jstring ifname, jint tun_fd, jstring settings)
|
||||
{
|
||||
@@ -45,13 +43,3 @@ JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgGetSocketV
|
||||
{
|
||||
return wgGetSocketV6(handle);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_com_wireguard_android_backend_GoBackend_wgPutSocketV4(JNIEnv *env, jclass c, jint handle)
|
||||
{
|
||||
wgPutSocketV4(handle);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_com_wireguard_android_backend_GoBackend_wgPutSocketV6(JNIEnv *env, jclass c, jint handle)
|
||||
{
|
||||
wgPutSocketV6(handle);
|
||||
}
|
||||
|
||||
+1
-1
Submodule app/tools/wireguard-go updated: 4365b4583f...1794f1f485
Reference in New Issue
Block a user