Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android: implement VpnService #577

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions android/yass/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".YassVpnService"
android:directBootAware="true"
android:exported="false"
android:label="@string/app_name"
android:permission="android.permission.BIND_VPN_SERVICE"
android:process=":bg">

<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
</service>
</application>

</manifest>
85 changes: 67 additions & 18 deletions android/yass/src/main/java/it/gui/yass/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -50,6 +53,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onDestroy() {
stopRefreshPoll();
tun2ProxyStop();
onNativeDestroy();
super.onDestroy();
}
Expand Down Expand Up @@ -142,37 +146,82 @@ private void saveSettingsIntoNative() {
setTimeout(Integer.parseInt(timeoutEditText.getText().toString()));
}

private final YassVpnService vpnService = new YassVpnService();

private ParcelFileDescriptor tunFd = null;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VPN_SERVICE_CODE && resultCode == RESULT_OK) {
onStartVpn();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}

private final int VPN_SERVICE_CODE = 10000;
private Thread tun2proxyThread;
private void onStartVpn() {
tunFd = vpnService.connect(getApplicationContext());
if (tunFd == null) {
return;
}
tun2proxyThread = new Thread(){
public void run() {
tun2ProxyStart("socks5://127.0.0.1:3000", tunFd.getFd(), vpnService.DEFAULT_MTU, true, true);
}
};
tun2proxyThread.start();

Button startButton = findViewById(R.id.startButton);
startButton.setEnabled(false);

TextView statusTextView = findViewById(R.id.statusTextView);
statusTextView.setText(R.string.status_starting);
state = NativeMachineState.STARTING;
nativeStart();
}

public void onStartClicked(View view) {
if (state == NativeMachineState.STOPPED) {
saveSettingsIntoNative();

if (tun2ProxyStart("socks5://127.0.0.1:3000", -1, 1500, true, true) != 0) {
return;
Intent intent = YassVpnService.prepare(getApplicationContext());
if (intent == null) {
onActivityResult(VPN_SERVICE_CODE, RESULT_OK, null);
} else {
startActivityForResult(intent, VPN_SERVICE_CODE);
}
}
}

Button startButton = findViewById(R.id.startButton);
startButton.setEnabled(false);
private void onStopVpn() {
try {
tunFd.close();
} catch (IOException e) {
//nop
}
tunFd = null;

TextView statusTextView = findViewById(R.id.statusTextView);
statusTextView.setText(R.string.status_starting);
state = NativeMachineState.STARTING;
nativeStart();
tun2ProxyStop();
try {
tun2proxyThread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
nativeStop();

}
Button stopButton = findViewById(R.id.stopButton);
stopButton.setEnabled(false);

TextView statusTextView = findViewById(R.id.statusTextView);
statusTextView.setText(R.string.status_stopping);
state = NativeMachineState.STOPPING;
}
public void onStopClicked(View view) {
if (state == NativeMachineState.STARTED) {
stopRefreshPoll();
tun2ProxyStop();

Button stopButton = findViewById(R.id.stopButton);
stopButton.setEnabled(false);

TextView statusTextView = findViewById(R.id.statusTextView);
statusTextView.setText(R.string.status_stopping);
state = NativeMachineState.STOPPING;
nativeStop();
onStopVpn();
}
}

Expand Down
46 changes: 46 additions & 0 deletions android/yass/src/main/java/it/gui/yass/YassVpnService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package it.gui.yass;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ProxyInfo;
import android.net.VpnService;
import android.os.Build;
import android.os.ParcelFileDescriptor;

public class YassVpnService extends VpnService {
public int DEFAULT_MTU = 1500;
private String PRIVATE_VLAN4_CLIENT = "172.19.0.1";
private String PRIVATE_VLAN4_GATEWAY = "172.19.0.2";
private String PRIVATE_VLAN6_CLIENT = "fdfe:dcba:9876::1";
private String PRIVATE_VLAN6_GATEWAY = "fdfe:dcba:9876::2";

public ParcelFileDescriptor connect(Context context) {
Builder builder = new Builder();

builder.setConfigureIntent(PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_IMMUTABLE));
builder.setSession("session");
builder.setMtu(DEFAULT_MTU);
builder.addAddress(PRIVATE_VLAN4_CLIENT, 30);
builder.addAddress(PRIVATE_VLAN6_CLIENT, 126);
builder.addRoute("0.0.0.0", 0);
builder.addRoute("::", 0);
// builder.setUnderlyingNetworks(underlyingNetworks);
// builder.setMetered(metered);
builder.addDnsServer(PRIVATE_VLAN4_GATEWAY);
builder.addDnsServer(PRIVATE_VLAN6_GATEWAY);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
builder.setHttpProxy(ProxyInfo.buildDirectProxy("127.0.0.1", 3000));
}
try {
builder.addDisallowedApplication("it.gui.yass");
} catch (PackageManager.NameNotFoundException e) {
// nop
}

return builder.establish();
}
}
2 changes: 2 additions & 0 deletions src/android/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ CIPHER_METHOD_VALID_MAP(XX)
};
DCHECK_LT((uint32_t)value, methods_idxes.size());
absl::SetFlag(&FLAGS_method, methods_idxes[value]);
absl::SetFlag(&FLAGS_local_host, "0.0.0.0");
absl::SetFlag(&FLAGS_local_port, 3000);
}

JNIEXPORT void JNICALL Java_it_gui_yass_MainActivity_setTimeout(JNIEnv *env, jobject obj, jint value) {
Expand Down
Loading