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 option to stop the service upon disconnect #399

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class GnirehtetActivity extends Activity {

public static final String EXTRA_DNS_SERVERS = "dnsServers";
public static final String EXTRA_ROUTES = "routes";
public static final String EXTRA_STOP_ON_DISCONNECT = "stopOnDisconnect";

private static final int VPN_REQUEST_CODE = 0;

Expand Down Expand Up @@ -59,7 +60,8 @@ private static VpnConfiguration createConfig(Intent intent) {
if (routes == null) {
routes = new String[0];
}
return new VpnConfiguration(Net.toInetAddresses(dnsServers), Net.toCIDRs(routes));
boolean stopOnDisconnect = intent.getBooleanExtra(EXTRA_STOP_ON_DISCONNECT, false);
return new VpnConfiguration(Net.toInetAddresses(dnsServers), Net.toCIDRs(routes), stopOnDisconnect);
}

private boolean startGnirehtet(VpnConfiguration config) {
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/genymobile/gnirehtet/GnirehtetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class GnirehtetService extends VpnService {
private static final int MTU = 0x4000;

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

private ParcelFileDescriptor vpnInterface = null;
private Forwarder forwarder;
Expand Down Expand Up @@ -111,6 +111,7 @@ private void startVpn(VpnConfiguration config) {

@SuppressWarnings("checkstyle:MagicNumber")
private boolean setupVpn(VpnConfiguration config) {
handler.setStopOnDisconnect(config.getIsStopOnDisconnect());
Builder builder = new Builder();
builder.addAddress(VPN_ADDRESS, 32);
builder.setSession(getString(R.string.app_name));
Expand Down Expand Up @@ -206,6 +207,7 @@ private void close() {
private static final class RelayTunnelConnectionStateHandler extends Handler {

private final GnirehtetService vpnService;
private boolean stopOnDisconnect = false;

private RelayTunnelConnectionStateHandler(GnirehtetService vpnService) {
this.vpnService = vpnService;
Expand All @@ -224,10 +226,18 @@ public void handleMessage(Message message) {
break;
case RelayTunnelListener.MSG_RELAY_TUNNEL_DISCONNECTED:
Log.d(TAG, "Relay tunnel disconnected");
vpnService.notifier.setFailure(true);
if (stopOnDisconnect) {
stop(vpnService);
} else {
vpnService.notifier.setFailure(true);
}
break;
default:
}
}

public void setStopOnDisconnect(boolean stopOnDisconnect) {
this.stopOnDisconnect = stopOnDisconnect;
}
}
}
11 changes: 10 additions & 1 deletion app/src/main/java/com/genymobile/gnirehtet/VpnConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ public class VpnConfiguration implements Parcelable {

private final InetAddress[] dnsServers;
private final CIDR[] routes;
private final boolean stopOnDisconnect;

public VpnConfiguration() {
this.dnsServers = new InetAddress[0];
this.routes = new CIDR[0];
this.stopOnDisconnect = false;
}

public VpnConfiguration(InetAddress[] dnsServers, CIDR[] routes) {
public VpnConfiguration(InetAddress[] dnsServers, CIDR[] routes, boolean stopOnDisconnect) {
this.dnsServers = dnsServers;
this.routes = routes;
this.stopOnDisconnect = stopOnDisconnect;
}

private VpnConfiguration(Parcel source) {
Expand All @@ -48,6 +51,7 @@ private VpnConfiguration(Parcel source) {
throw new AssertionError("Invalid address", e);
}
routes = source.createTypedArray(CIDR.CREATOR);
stopOnDisconnect = source.readByte() == 1;
}

public InetAddress[] getDnsServers() {
Expand All @@ -58,13 +62,18 @@ public CIDR[] getRoutes() {
return routes;
}

public boolean getIsStopOnDisconnect() {
return stopOnDisconnect;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(dnsServers.length);
for (InetAddress addr : dnsServers) {
dest.writeByteArray(addr.getAddress());
}
dest.writeTypedArray(routes, 0);
dest.writeByte(stopOnDisconnect ? (byte) 1 : (byte) 0);
}

@Override
Expand Down
30 changes: 29 additions & 1 deletion relay-rust/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub const PARAM_SERIAL: u8 = 1;
pub const PARAM_DNS_SERVERS: u8 = 1 << 1;
pub const PARAM_ROUTES: u8 = 1 << 2;
pub const PARAM_PORT: u8 = 1 << 3;
pub const PARAM_STOP_ON_DISCONNECT: u8 = 1 << 4;

pub const DEFAULT_PORT: u16 = 31416;

Expand All @@ -27,6 +28,7 @@ pub struct CommandLineArguments {
dns_servers: Option<String>,
routes: Option<String>,
port: u16,
stop_on_disconnect: bool,
}

impl CommandLineArguments {
Expand All @@ -36,6 +38,7 @@ impl CommandLineArguments {
let mut dns_servers = None;
let mut routes = None;
let mut port = 0;
let mut stop_on_disconnect = false;

let mut iter = args.into_iter();
while let Some(arg) = iter.next() {
Expand Down Expand Up @@ -70,6 +73,11 @@ impl CommandLineArguments {
} else {
return Err(String::from("Missing -p parameter"));
}
} else if (accepted_parameters & PARAM_STOP_ON_DISCONNECT) != 0 && "-s" == arg {
if stop_on_disconnect {
return Err(String::from("Stop on disconnect already set"));
}
stop_on_disconnect = true;
} else if (accepted_parameters & PARAM_SERIAL) != 0 && serial.is_none() {
serial = Some(arg);
} else {
Expand All @@ -84,6 +92,7 @@ impl CommandLineArguments {
dns_servers,
routes,
port,
stop_on_disconnect,
})
}

Expand All @@ -102,13 +111,18 @@ impl CommandLineArguments {
pub fn port(&self) -> u16 {
self.port
}

pub fn stop_on_disconnect(&self) -> bool {
self.stop_on_disconnect
}
}

#[cfg(test)]
mod tests {
use super::*;

const ACCEPT_ALL: u8 = PARAM_SERIAL | PARAM_DNS_SERVERS | PARAM_ROUTES;
const ACCEPT_ALL: u8 =
PARAM_SERIAL | PARAM_DNS_SERVERS | PARAM_ROUTES | PARAM_STOP_ON_DISCONNECT;

#[test]
fn test_no_args() {
Expand Down Expand Up @@ -178,4 +192,18 @@ mod tests {
let raw_args = vec!["-r"];
assert!(CommandLineArguments::parse(ACCEPT_ALL, raw_args).is_err());
}

#[test]
fn test_stop_on_disconnect_parameter() {
let raw_args = vec!["-s"];
let args = CommandLineArguments::parse(ACCEPT_ALL, raw_args).unwrap();
assert!(args.stop_on_disconnect())
}

#[test]
fn test_no_stop_on_disconnect_parameter() {
let raw_args = Vec::<&str>::new();
let args = CommandLineArguments::parse(ACCEPT_ALL, raw_args).unwrap();
assert!(!args.stop_on_disconnect())
}
}
Loading