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

Add ru lang to android app, fix disconnect in android app #169

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<intent-filter>
<action android:name="com.genymobile.gnirehtet.START" />
<action android:name="com.genymobile.gnirehtet.STOP" />
<action android:name="android.hardware.usb.action.USB_STATE" />
</intent-filter>
</receiver>
<service
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/genymobile/gnirehtet/Forwarder.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public void stop() {
wakeUpReadWorkaround();
}

@SuppressWarnings("checkstyle:MagicNumber")
private void forwardDeviceToTunnel(Tunnel tunnel) throws IOException {
Log.d(TAG, "Device to tunnel forwarding started");
FileInputStream vpnInput = new FileInputStream(vpnFileDescriptor);
Expand All @@ -100,6 +99,7 @@ private void forwardDeviceToTunnel(Tunnel tunnel) throws IOException {
break;
}
if (r > 0) {
// blocking send
int version = buffer[0] >> 4;
if (version == 4) {
// blocking send
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public void onReceive(Context context, Intent intent) {
startGnirehtet(context, config);
} else if (ACTION_GNIREHTET_STOP.equals(action)) {
stopGnirehtet(context);
} else if ("android.hardware.usb.action.USB_STATE".equals(action)){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it should rely on whether the socket is connected or not, whatever the USB state is (the socket can be disconnected while the USB cable is still connected).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, i not replace intent "ACTION_GNIREHTET_STOP"("socket can be disconnected while the USB cable is still connected"), i add intent USB_STATE with disconnected (if cabel unplug, then socket dont work too).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not rely on USB state, but on the socket connection state.

if(intent.getExtras().getBoolean("connected")) {
// USB was connected
} else {
// USB was disconnected
stopGnirehtet(context);
}
}
}

Expand Down
25 changes: 18 additions & 7 deletions app/src/main/java/com/genymobile/gnirehtet/GnirehtetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class GnirehtetService extends VpnService {
// magic value: higher (like 0x8000 or 0xffff) or lower (like 1500) values show poorer performances
private static final int MTU = 0x4000;


private final Notifier notifier = new Notifier(this);
private final Handler handler = new RelayTunnelConnectionStateHandler(this);

Expand Down Expand Up @@ -109,6 +110,19 @@ private void startVpn(VpnConfiguration config) {
}
}

@Override
public void onRevoke() {
try {
forwarder.stop();
forwarder = null;
vpnInterface.close();
vpnInterface = null;
} catch (IOException e) {
Log.w(TAG, "Cannot close VPN file descriptor", e);
}
stopSelf();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be possible to just call close()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopSelf() - method from android api, iron destroy service.
I put close() method, service not stop immedly.


@SuppressWarnings("checkstyle:MagicNumber")
private boolean setupVpn(VpnConfiguration config) {
Builder builder = new Builder();
Expand Down Expand Up @@ -189,9 +203,7 @@ private void close() {
// already closed
return;
}

notifier.stop();

notifier.removeFailure();
try {
forwarder.stop();
forwarder = null;
Expand All @@ -200,9 +212,8 @@ private void close() {
} catch (IOException e) {
Log.w(TAG, "Cannot close VPN file descriptor", e);
}
stopSelf();
}


private static final class RelayTunnelConnectionStateHandler extends Handler {

private final GnirehtetService vpnService;
Expand All @@ -220,11 +231,11 @@ public void handleMessage(Message message) {
switch (message.what) {
case RelayTunnelListener.MSG_RELAY_TUNNEL_CONNECTED:
Log.d(TAG, "Relay tunnel connected");
vpnService.notifier.setFailure(false);
vpnService.notifier.setFailure();
break;
case RelayTunnelListener.MSG_RELAY_TUNNEL_DISCONNECTED:
Log.d(TAG, "Relay tunnel disconnected");
vpnService.notifier.setFailure(true);
vpnService.notifier.removeFailure();
break;
default:
}
Expand Down
28 changes: 10 additions & 18 deletions app/src/main/java/com/genymobile/gnirehtet/Notifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,16 @@ public class Notifier {
private static final String CHANNEL_ID = "Gnirehtet";

private final Service context;
private boolean failure;

public Notifier(Service context) {
this.context = context;
}

private Notification createNotification(boolean failure) {
private Notification createNotification() {
Notification.Builder notificationBuilder = createNotificationBuilder();
notificationBuilder.setContentTitle(context.getString(R.string.app_name));
if (failure) {
notificationBuilder.setContentText(context.getString(R.string.relay_disconnected));
notificationBuilder.setSmallIcon(R.drawable.ic_report_problem_24dp);
} else {
notificationBuilder.setContentText(context.getString(R.string.relay_connected));
notificationBuilder.setSmallIcon(R.drawable.ic_usb_24dp);
}
notificationBuilder.setContentText(context.getString(R.string.relay_connected));
notificationBuilder.setSmallIcon(R.drawable.ic_usb_24dp);
notificationBuilder.addAction(createStopAction());
return notificationBuilder.build();
}
Expand All @@ -60,11 +54,10 @@ private void deleteNotificationChannel() {
}

public void start() {
failure = false; // reset failure flag
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
context.startForeground(NOTIFICATION_ID, createNotification(false));
context.startForeground(NOTIFICATION_ID, createNotification());
}

public void stop() {
Expand All @@ -74,14 +67,13 @@ public void stop() {
}
}

public void setFailure(boolean failure) {
if (this.failure != failure) {
this.failure = failure;
Notification notification = createNotification(failure);
public void setFailure() {
Notification notification = createNotification();
getNotificationManager().notify(NOTIFICATION_ID, notification);
}
}

public void removeFailure() {
getNotificationManager().cancel(NOTIFICATION_ID);
}
private Notification.Action createStopAction() {
Intent stopIntent = GnirehtetService.createStopIntent(context);
PendingIntent stopPendingIntent = PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_ONE_SHOT);
Expand All @@ -95,4 +87,4 @@ private Notification.Action createStopAction() {
private NotificationManager getNotificationManager() {
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
}
1 change: 0 additions & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="relay_connected">Reverse tethering activé</string>
<string name="relay_disconnected">Déconnecté du serveur relais</string>
<string name="stop_vpn">Arrêter Gnirehtet</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="relay_connected">Подключение через Gnirehtet активно</string>
<string name="stop_vpn">Остановить Gnirehtet</string>
</resources>
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<resources>
<string name="app_name" translatable="false">Gnirehtet</string>
<string name="relay_connected">Reverse tethering enabled</string>
<string name="relay_disconnected">Disconnected from the relay server</string>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like the close-on-disconnect behavior in addition to the current behavior (via a command-line option). See #51

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, dude dont find this thread. Think, when device disconnected from relay server, out service and notif only angry users (my users). Notif can not close.

Sorry my english, bro, i from russian,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A -k option to keep the current behavior would be perfect :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can work on this option when I have some time, if you prefer.

<string name="stop_vpn">Stop Gnirehtet</string>
</resources>