Skip to content

Commit

Permalink
proxymode enhancement: show ip
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielcoderX committed Aug 17, 2024
1 parent 26c2584 commit 08cf04a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-feature android:name="android.hardware.wifi" android:required="false" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/org/bepass/oblivion/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,18 @@ private void updateUIForConnectingState() {
private void updateUIForConnectedState() {
binding.switchButton.setEnabled(true);
if (FileManager.getBoolean("USERSETTING_proxymode")) {
binding.stateText.setText(String.format(Locale.getDefault(), "%s\nsocks5 on 127.0.0.1:%s", getString(R.string.connected), FileManager.getString("USERSETTING_port")));
} else {
if (FileManager.getBoolean("USERSETTING_lan")) {
String lanIP;
try {
lanIP = NetworkUtils.getLocalIpAddress(this);
binding.stateText.setText(String.format(Locale.getDefault(), "%s\n socks5 over LAN on\n %s:%s", getString(R.string.connected), lanIP, FileManager.getString("USERSETTING_port")));
} catch (Exception e) {
binding.stateText.setText(String.format(Locale.getDefault(), "%s\n socks5 over LAN on\n 127.0.0.1:%s", getString(R.string.connected), FileManager.getString("USERSETTING_port")));
}
} else {
binding.stateText.setText(String.format(Locale.getDefault(), "%s\nsocks5 on 127.0.0.1:%s", getString(R.string.connected), FileManager.getString("USERSETTING_port")));
}
}else {
binding.stateText.setText(R.string.connected);
}
binding.switchButton.setChecked(true, false);
Expand Down
55 changes: 55 additions & 0 deletions app/src/main/java/org/bepass/oblivion/utils/NetworkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler;

import org.bepass.oblivion.enums.ConnectionState;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;

public class NetworkUtils {

private static final Handler handler = new Handler();
Expand Down Expand Up @@ -53,4 +58,54 @@ private static boolean isConnectedToInternet(Context context) {
}
return false;
}
public static String getLocalIpAddress(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();

if (activeNetwork != null && activeNetwork.isConnected()) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// Get IP Address from Wi-Fi
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
return String.format(Locale.US,"%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// Get IP Address from Mobile Data using InetAddress
try {
InetAddress ip = InetAddress.getByName("google.com");
return ip.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// For devices running Android Marshmallow or higher
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
// Get IP Address from Wi-Fi
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
return String.format(Locale.US,"%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));
} else if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
// Get IP Address from Mobile Data using InetAddress
try {
InetAddress ip = InetAddress.getByName("google.com");
return ip.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

return null; // Return null if no connection is available
}


}

0 comments on commit 08cf04a

Please sign in to comment.