Procházet zdrojové kódy

Add in-proxy notices handling in Android and iOS libraries

Eugene Fryntov před 1 rokem
rodič
revize
6079468a9a

+ 23 - 0
MobileLibrary/Android/PsiphonTunnel/PsiphonTunnel.java

@@ -131,6 +131,20 @@ public class PsiphonTunnel {
         default public void onTrafficRateLimits(long upstreamBytesPerSecond, long downstreamBytesPerSecond) {}
         default public void onApplicationParameters(Object parameters) {}
         default public void onServerAlert(String reason, String subject, List<String> actionURLs) {}
+        /**
+         * Called when tunnel-core emits a message to be displayed to the in-proxy operator.
+         * @param message The operator message received.
+         */
+        default void onInproxyOperatorMessage(String message) {}
+
+        /**
+         * Called when tunnel-core reports proxy usage statistics.
+         * @param connectingClients Number of clients connecting to the proxy.
+         * @param connectedClients Number of clients currently connected to the proxy.
+         * @param bytesUp  Bytes uploaded through the proxy since the last report.
+         * @param bytesDown Bytes downloaded through the proxy since the last report.
+         */
+        default void onInproxyProxyActivity(int connectingClients, int connectedClients,long bytesUp, long bytesDown) {}
         default public void onExiting() {}
     }
 
@@ -1092,6 +1106,15 @@ public class PsiphonTunnel {
                     notice.getJSONObject("data").getString("reason"),
                     notice.getJSONObject("data").getString("subject"),
                     actionURLsList);
+            } else if (noticeType.equals("InproxyOperatorMessage")) {
+                mHostService.onInproxyOperatorMessage( notice.getJSONObject("data").getString("message"));
+            } else if (noticeType.equals("InproxyProxyActivity")) {
+                JSONObject data = notice.getJSONObject("data");
+                mHostService.onInproxyProxyActivity(
+                        data.getInt("connectingClients"),
+                        data.getInt("connectedClients"),
+                        data.getLong("bytesUp"),
+                        data.getLong("bytesDown"));
             }
 
             if (diagnostic) {

+ 24 - 0
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.h

@@ -299,6 +299,30 @@ WWAN or vice versa or VPN state changed
  */
 - (void)onApplicationParameters:(NSDictionary * _Nonnull)parameters;
 
+
+/*!
+ Called when tunnel-core emits a message to be displayed to the in-proxy operator
+ @param message The operator message received.
+ */
+- (void)onInproxyOperatorMessage:(NSString * _Nonnull)message;
+
+/*!
+ Called when tunnel-core reports in-proxy usage statistics
+ @param connectingClients Number of clients connecting to the proxy.
+ @param connectedClients Number of clients currently connected to the proxy.
+ @param bytesUp Bytes uploaded through the proxy since the last report.
+ @param bytesDown Bytes downloaded through the proxy since the last report.
+ */
+- (void)onInproxyProxyActivity:(int)connectingClients
+              connectedClients:(int)connectedClients
+                       bytesUp:(long)bytesUp
+                     bytesDown:(long)bytesDown;
+
+/*!
+ Called when tunnel-core reports connected server region information
+ @param region The server region received.
+ */
+- (void)onConnectedServerRegion:(NSString * _Nonnull)region;
 @end
 
 /*!

+ 27 - 0
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m

@@ -1174,6 +1174,33 @@ typedef NS_ERROR_ENUM(PsiphonTunnelErrorDomain, PsiphonTunnelErrorCode) {
             });
         }
     }
+    else if ([noticeType isEqualToString:@"InproxyOperatorMessage"]) {
+        id message = [notice valueForKeyPath:@"data.message"];
+        if (![message isKindOfClass:[NSString class]]) {
+            [self logMessage:[NSString stringWithFormat: @"InproxyOperatorMessage notice missing data.message: %@", noticeJSON]];
+            return;
+        }
+        if ([self.tunneledAppDelegate respondsToSelector:@selector(onInproxyOperatorMessage:)]) {
+            dispatch_sync(self->callbackQueue, ^{
+                [self.tunneledAppDelegate onInproxyOperatorMessage:message];
+            });
+        }
+    }
+    else if ([noticeType isEqualToString:@"InproxyProxyActivity"]) {
+        id connectingClients = [notice valueForKeyPath:@"data.connectingClients"];
+        id connectedClients = [notice valueForKeyPath:@"data.connectedClients"];
+        id bytesUp = [notice valueForKeyPath:@"data.bytesUp"];
+        id bytesDown = [notice valueForKeyPath:@"data.bytesDown"];
+        if (![connectingClients isKindOfClass:[NSNumber class]] || ![connectedClients isKindOfClass:[NSNumber class]] || ![bytesUp isKindOfClass:[NSNumber class]] || ![bytesDown isKindOfClass:[NSNumber class]]) {
+            [self logMessage:[NSString stringWithFormat: @"InproxyProxyActivity notice has invalid data types: %@", noticeJSON]];
+            return;
+        }
+        if ([self.tunneledAppDelegate respondsToSelector:@selector(onInproxyProxyActivity:connectedClients:bytesUp:bytesDown:)]) {
+            dispatch_sync(self->callbackQueue, ^{
+                [self.tunneledAppDelegate onInproxyProxyActivity:[connectingClients intValue] connectedClients:[connectedClients intValue] bytesUp:[bytesUp longValue] bytesDown:[bytesDown longValue]];
+            });
+        }
+    }
     else if ([noticeType isEqualToString:@"InternalError"]) {
         internalError = TRUE;
     }