Skip to content

Commit

Permalink
Ability to get IPv4 and IPv6 addresses from the service
Browse files Browse the repository at this point in the history
  • Loading branch information
Dániel Zolnai committed Nov 8, 2016
1 parent 5d1778a commit ec9097d
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Collection;
import java.util.Locale;
import java.util.Vector;
import java.util.regex.Pattern;

import de.blinkt.openvpn.BuildConfig;
import de.blinkt.openvpn.R;
Expand Down Expand Up @@ -87,6 +88,9 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac
private Handler guiHandler;
private Toast mlastToast;
private Runnable mOpenVPNThread;
private String mLastLocalIp;
private String mLastLocalIpV6;

private static Class mNotificationActivityClass;

// From: http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
Expand Down Expand Up @@ -489,16 +493,16 @@ private void startOpenVPN() {
}

new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (mDeviceStateReceiver != null)
unregisterDeviceStateReceiver();
@Override
public void run() {
if (mDeviceStateReceiver != null)
unregisterDeviceStateReceiver();

registerDeviceStateReceiver(mManagement);
}
}
registerDeviceStateReceiver(mManagement);
}
}

);
);
}

private void stopOldOpenVPNProcess() {
Expand Down Expand Up @@ -704,6 +708,10 @@ else if (mLocalIP != null)

mLastTunCfg = getTunConfigString();

// Store the IPs
mLastLocalIp = mLocalIP == null ? null : mLocalIP.mIp;
mLastLocalIpV6 = (mLocalIPv6 != null && mLocalIPv6.contains("/")) ? mLocalIPv6.split(Pattern.quote("/"))[0] : mLocalIPv6;

// Reset information
mDnslist.clear();
mRoutes.clear();
Expand Down Expand Up @@ -929,6 +937,21 @@ public void setLocalIPv6(String ipv6addr) {
mLocalIPv6 = ipv6addr;
}

/**
* Returns the lastly used IPv4 address used with the VPN.
* @return The lastly used IPv4 address. This property is not cleared on disconnection.
*/
public String getLastLocalIpV4Address() {
return mLastLocalIp;
}
/**
* Returns the lastly used IPv6 address used with the VPN.
* @return The lastly used IPv6 address. This property is not cleared on disconnection.
*/
public String getLastLocalIpV6Address() {
return mLastLocalIpV6;
}

@Override
public void updateState(String state, String logmessage, int resid, ConnectionStatus level) {
// If the process is not running, ignore any state,
Expand Down Expand Up @@ -1025,4 +1048,4 @@ public OpenVPNService getService() {
return OpenVPNService.this;
}
}
}
}

0 comments on commit ec9097d

Please sign in to comment.