Android Q 中访问 /proc/net/tcp

Ste*_*lla 7 ip networking android linux-kernel android-10.0

在 Android-9 之前的我的 VPN 应用程序中,可以/proc/net/tcp从针对 API 级别 < 28 的应用程序读取文件。在 Android Q 中,从应用程序进行访问似乎不再有效。我 /proc/net/tcp: open failed: EACCES (Permission denied)在尝试读取文件时遇到错误。

在 Android-Q 隐私变更中,谷歌解决了访问 /proc/net 文件系统的限制

我认为ConnectivityManager.getConnectionOwnerUid()如果应用程序的compileSDK版本是29就可以使用。但不幸的是,目前我无法更改我的compileSDK版本,但我将targetSDK版本更新到最新的IE,29。

还有其他可能的方式来读取 Android-10 中的文件吗?贴出我的代码供参考

public static final int INDEX_UID_COL = 7;
public static final int INDEX_LOCAL_ADDRESS_COL = 1;
public static final String PROC_FILE = "/proc/net/tcp";

 public static String getPackageName(Context context, int srcPort) {
        String packageName = "";
        try {

            BufferedReader br = new BufferedReader(new FileReader(PROC_FILE));

            //Ignore first line
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                /**
                 * Proc file table column sequence
                 * sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
                 */
                String[] parts = line.trim().split("\\s+");
                if (parts.length >= 8) {
                    String localAddress = parts[INDEX_LOCAL_ADDRESS_COL];
                    if (!localAddress.isEmpty()) {
                        String[] localAddressParts = localAddress.split(":");
                        if (localAddressParts.length == 2) {
                            int port = Integer.parseInt(localAddressParts[1], 16);
                            if (port == srcPort) {
                                int uid = Integer.parseInt(parts[INDEX_UID_COL]);
                                packageName = context.getPackageManager().getNameForUid(uid);
                                break;
                            }
                        }
                    }
                }
            }
            br.close();
        } catch (Exception ex) {
            Log.e("ProcFileParser", ex.getMessage());
        }
        return packageName;
    }
Run Code Online (Sandbox Code Playgroud)

Ano*_*p M 5

ConnectivityManager是平台级API。您可以尝试 java 反射来访问getConnectionOwnerUid您的上下文。但我建议你将compileSdk版本更新到最新

import static android.os.Process.INVALID_UID;
import static android.system.OsConstants.IPPROTO_TCP;

InetSocketAddress remoteInetSocketAddress = new InetSocketAddress(finalHost, srcPort);
InetSocketAddress localInetSocketAddress = new InetSocketAddress(1234);

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Method method = ConnectivityManager.class.getMethod("getConnectionOwnerUid", int.class, InetSocketAddress.class, InetSocketAddress.class);
int uid = (int) method.invoke(connectivityManager, IPPROTO_TCP, localInetSocketAddress, remoteInetSocketAddress);
if (uid != INVALID_UID) {
       // UID access here         
  }
Run Code Online (Sandbox Code Playgroud)

注意:该方法仅允许 VPN 应用程序查找网络连接的 UID 所有者。这意味着只有适用于调用 VPN 应用程序的 UID 连接才会得到解析。