Эх сурвалжийг харах

Handle ServerAlert in MobileLibrary

Rod Hynes 6 жил өмнө
parent
commit
50646a9cae

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

@@ -97,6 +97,7 @@ public class PsiphonTunnel {
         default public void onStoppedWaitingForNetworkConnectivity() {}
         default public void onActiveAuthorizationIDs(List<String> authorizations) {}
         default public void onApplicationParameter(String key, Object value) {}
+        default public void onServerAlert(String reason, String subject) {}
         default public void onExiting() {}
     }
 
@@ -780,6 +781,10 @@ public class PsiphonTunnel {
                 mHostService.onApplicationParameter(
                     notice.getJSONObject("data").getString("key"),
                     notice.getJSONObject("data").get("value"));
+            } else if (noticeType.equals("ServerAlert")) {
+                mHostService.onServerAlert(
+                    notice.getJSONObject("data").getString("reason"),
+                    notice.getJSONObject("data").getString("subject"));
             }
 
             if (diagnostic) {

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

@@ -296,6 +296,14 @@ Swift: @code func onInternetReachabilityChanged(_ currentReachability: Reachabil
  */
 - (void)onActiveAuthorizationIDs:(NSArray * _Nonnull)authorizations;
 
+/*!
+ Called when tunnel-core receives an alert from the server.
+ @param reason The reason for the alert.
+ @param subject Additional context or classification of the reason; blank for none.
+ Swift: @code func onServerAlert(_ reason: String, _ subject: String) @endcode
+ */
+- (void)onServerAlert:(NSString * _Nonnull)reason :(NSString * _Nonnull)subject;
+
 @end
 
 /*!

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

@@ -1001,6 +1001,20 @@ typedef NS_ERROR_ENUM(PsiphonTunnelErrorDomain, PsiphonTunnelErrorCode) {
             });
         }
     }
+    else if ([noticeType isEqualToString:@"ServerAlert"]) {
+        id reason = [notice valueForKeyPath:@"data.reason"];
+        id subject = [notice valueForKeyPath:@"data.subject"];
+        if (![reason isKindOfClass:[NSString class]] || ![subject isKindOfClass:[NSString class]]) {
+            [self logMessage:[NSString stringWithFormat: @"ServerAlert notice missing data.reason or data.subject: %@", noticeJSON]];
+            return;
+        }
+
+        if ([self.tunneledAppDelegate respondsToSelector:@selector(onServerAlert::)]) {
+            dispatch_sync(self->callbackQueue, ^{
+                [self.tunneledAppDelegate onServerAlert:reason:subject];
+            });
+        }
+    }
 
     else if ([noticeType isEqualToString:@"InternalError"]) {
         internalError = TRUE;