Explorar el Código

GetNetworkID changes

- Don't report "NONE" or "UNKNOWN" as network IDs.
  Return "" when unable to determine network info.
  (TODO: skip tactics in tunnel-core in this case)

- Android fix: catch permission exceptions and return
  partial IDs ("WIFI", etc.) when detailed info is
  unavailable due to specific missing permissions.
Rod Hynes hace 8 años
padre
commit
346df14bac

+ 39 - 17
MobileLibrary/Android/PsiphonTunnel/PsiphonTunnel.java

@@ -374,27 +374,49 @@ public class PsiphonTunnel extends Psi.PsiphonProvider.Stub {
         // The network ID contains potential PII. In tunnel-core, the network ID
         // is used only locally in the client and not sent to the server.
 
+        String networkID = "";
+
         Context context = mHostService.getContext();
-        String networkIdentifier = "UNKNOWN";
-        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
-        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
-        WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
-        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
-        TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
-        if (activeNetworkInfo == null) {
-            networkIdentifier = "NONE";
-        } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
-            networkIdentifier = "WIFI";
-            if (wifiInfo != null) {
-                networkIdentifier += "-" + wifiInfo.getBSSID();
+        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);;
+        NetworkInfo activeNetworkInfo = null;
+        try {
+            activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
+
+        } catch (java.lang.Exception e) {
+            // May get exceptions due to missing permissions like android.permission.ACCESS_NETWORK_STATE.
+        }
+
+        if (activeNetworkInfo != null && activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
+
+            networkID = "WIFI";
+
+            try {
+                WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
+                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
+                if (wifiInfo != null) {
+                    networkID += "-" + wifiInfo.getBSSID();
+                }
+            } catch (java.lang.Exception e) {
+                // May get exceptions due to missing permissions like android.permission.ACCESS_WIFI_STATE.
+                // Fall through and use just "WIFI"
             }
-        } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
-            networkIdentifier = "MOBILE";
-            if (telephonyManager != null) {
-                networkIdentifier += "-" + telephonyManager.getNetworkOperator();
+
+        } else if (activeNetworkInfo != null && activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
+
+            networkID = "MOBILE";
+
+            try {
+                TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
+                if (telephonyManager != null) {
+                    networkID += "-" + telephonyManager.getNetworkOperator();
+                }
+            } catch (java.lang.Exception e) {
+                // May get exceptions due to missing permissions.
+                // Fall through and use just "MOBILE"
             }
         }
-        return networkIdentifier;
+
+        return networkID;
     }
 
     //----------------------------------------------------------------------------------------------

+ 7 - 9
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m

@@ -1095,30 +1095,28 @@
     // The network ID contains potential PII. In tunnel-core, the network ID
     // is used only locally in the client and not sent to the server.
 
-    NSMutableString *networkIdentifier = [NSMutableString stringWithString:@"UNKNOWN"];
+    NSMutableString *networkID = [NSMutableString stringWithString:@""];
     NetworkStatus status = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];
-    if (status == NotReachable) {
-        [networkIdentifier setString:@"NONE"];
-    } else if (status == ReachableViaWiFi) {
-        [networkIdentifier setString:@"WIFI"];
+    if (status == ReachableViaWiFi) {
+        [networkID setString:@"WIFI"];
         NSArray *networkInterfaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();
         for (NSString *networkInterfaceName in networkInterfaceNames) {
             NSDictionary *networkInterfaceInfo = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)networkInterfaceName);
             if (networkInterfaceInfo[@"BSSID"]) {
-                [networkIdentifier appendFormat:@"-%@", networkInterfaceInfo[@"BSSID"]];
+                [networkID appendFormat:@"-%@", networkInterfaceInfo[@"BSSID"]];
             }
         }
     } else if (status == ReachableViaWWAN) {
-        [networkIdentifier setString:@"MOBILE"];
+        [networkID setString:@"MOBILE"];
         CTTelephonyNetworkInfo *telephonyNetworkinfo = [[CTTelephonyNetworkInfo alloc] init];
         CTCarrier *cellularProvider = [telephonyNetworkinfo subscriberCellularProvider];
         if (cellularProvider != nil) {
             NSString *mcc = [cellularProvider mobileCountryCode];
             NSString *mnc = [cellularProvider mobileNetworkCode];
-            [networkIdentifier appendFormat:@"-%@-%@", mcc, mnc];
+            [networkID appendFormat:@"-%@-%@", mcc, mnc];
         }
     }
-    return networkIdentifier;
+    return networkID;
 }
 
 - (void)notice:(NSString *)noticeJSON {