Просмотр исходного кода

Merge pull request #680 from efryntov/connected-region-callback

Mobile libs: add connected region callback
Rod Hynes 1 год назад
Родитель
Сommit
db032a5ea6

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

@@ -131,6 +131,11 @@ public class PsiphonTunnel {
         default public void onTrafficRateLimits(long upstreamBytesPerSecond, long downstreamBytesPerSecond) {}
         default public void onTrafficRateLimits(long upstreamBytesPerSecond, long downstreamBytesPerSecond) {}
         default public void onApplicationParameters(Object parameters) {}
         default public void onApplicationParameters(Object parameters) {}
         default public void onServerAlert(String reason, String subject, List<String> actionURLs) {}
         default public void onServerAlert(String reason, String subject, List<String> actionURLs) {}
+        /**
+         * Called when tunnel-core reports connected server region information.
+         * @param region The server region received.
+         */
+        default public void onConnectedServerRegion(String region) {}
         default public void onExiting() {}
         default public void onExiting() {}
     }
     }
 
 
@@ -1079,6 +1084,9 @@ public class PsiphonTunnel {
                       enableUdpGwKeepalive();
                       enableUdpGwKeepalive();
                     }
                     }
                 }
                 }
+                // Also report the tunnel's egress region to the host service
+                mHostService.onConnectedServerRegion(
+                        notice.getJSONObject("data").getString("serverRegion"));
             } else if (noticeType.equals("ApplicationParameters")) {
             } else if (noticeType.equals("ApplicationParameters")) {
                 mHostService.onApplicationParameters(
                 mHostService.onApplicationParameters(
                     notice.getJSONObject("data").get("parameters"));
                     notice.getJSONObject("data").get("parameters"));

+ 7 - 0
MobileLibrary/Android/SampleApps/TunneledWebView/app/build.gradle

@@ -35,4 +35,11 @@ dependencies {
     implementation 'androidx.appcompat:appcompat:1.0.0'
     implementation 'androidx.appcompat:appcompat:1.0.0'
     // always specify exact library version in your real project to avoid non-deterministic builds
     // always specify exact library version in your real project to avoid non-deterministic builds
     implementation 'ca.psiphon:psiphontunnel:2.+'
     implementation 'ca.psiphon:psiphontunnel:2.+'
+
+    // For the latest version compile the library from source, see MobileLibrary/Android/README.md
+    // in the Psiphon-Labs/psiphon-tunnel-core repository, copy the ca.psiphon.aar artifact to
+    // the libs folder under the app module and replace the above line
+    // (e.g. replace implementation 'ca.psiphon:psiphontunnel:2.+')
+    // with the following line:
+    // implementation files('libs/ca.psiphon.aar')
 }
 }

+ 9 - 0
MobileLibrary/Android/SampleApps/TunneledWebView/app/src/main/java/ca/psiphon/tunneledwebview/MainActivity.java

@@ -8,6 +8,8 @@ package ca.psiphon.tunneledwebview;
 import android.content.Context;
 import android.content.Context;
 import android.os.Bundle;
 import android.os.Bundle;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.app.AppCompatActivity;
+
+import android.util.Log;
 import android.webkit.WebSettings;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebView;
 import android.widget.ArrayAdapter;
 import android.widget.ArrayAdapter;
@@ -61,6 +63,7 @@ import ca.psiphon.PsiphonTunnel;
 public class MainActivity extends AppCompatActivity
 public class MainActivity extends AppCompatActivity
         implements PsiphonTunnel.HostService {
         implements PsiphonTunnel.HostService {
 
 
+    private static final String TAG = "TunneledWebView";
     private ListView mListView;
     private ListView mListView;
     private WebView mWebView;
     private WebView mWebView;
 
 
@@ -152,6 +155,7 @@ public class MainActivity extends AppCompatActivity
             public void run() {
             public void run() {
                 mLogMessages.add(message);
                 mLogMessages.add(message);
                 mListView.setSelection(mLogMessages.getCount() - 1);
                 mListView.setSelection(mLogMessages.getCount() - 1);
+                Log.d(TAG, "logMessage: " + message);
             }
             }
         });
         });
     }
     }
@@ -249,6 +253,11 @@ public class MainActivity extends AppCompatActivity
         loadWebView();
         loadWebView();
     }
     }
 
 
+    @Override
+    public void onConnectedServerRegion(String region) {
+        logMessage("connected server region: " + region);
+    }
+
     @Override
     @Override
     public void onHomepage(String url) {
     public void onHomepage(String url) {
         logMessage("home page: " + url);
         logMessage("home page: " + url);

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

@@ -299,6 +299,12 @@ WWAN or vice versa or VPN state changed
  */
  */
 - (void)onApplicationParameters:(NSDictionary * _Nonnull)parameters;
 - (void)onApplicationParameters:(NSDictionary * _Nonnull)parameters;
 
 
+
+/*!
+ Called when tunnel-core reports connected server region information
+ @param region The server region received.
+ */
+- (void)onConnectedServerRegion:(NSString * _Nonnull)region;
 @end
 @end
 
 
 /*!
 /*!

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

@@ -1174,6 +1174,18 @@ typedef NS_ERROR_ENUM(PsiphonTunnelErrorDomain, PsiphonTunnelErrorCode) {
             });
             });
         }
         }
     }
     }
+    else if ([noticeType isEqualToString:@"ActiveTunnel"]) {
+        id region = [notice valueForKeyPath:@"data.serverRegion"];
+        if (![region isKindOfClass:[NSString class]]) {
+            [self logMessage:[NSString stringWithFormat: @"ActiveTunnel notice missing data.serverRegion: %@", noticeJSON]];
+            return;
+        }
+        if ([self.tunneledAppDelegate respondsToSelector:@selector(onConnectedServerRegion:)]) {
+            dispatch_sync(self->callbackQueue, ^{
+                [self.tunneledAppDelegate onConnectedServerRegion:region];
+            });
+        }
+    }
     else if ([noticeType isEqualToString:@"InternalError"]) {
     else if ([noticeType isEqualToString:@"InternalError"]) {
         internalError = TRUE;
         internalError = TRUE;
     }
     }

+ 4 - 0
MobileLibrary/iOS/SampleApps/TunneledWebRequest/TunneledWebRequest/AppDelegate.swift

@@ -365,4 +365,8 @@ extension AppDelegate: TunneledAppDelegate {
             self.httpProxyPort = port
             self.httpProxyPort = port
         }
         }
     }
     }
+
+    func onConnectedServerRegion(_ region: String) {
+        NSLog("onConnectedServerRegion(%@)", region)
+    }
 }
 }

+ 2 - 1
psiphon/controller.go

@@ -1001,7 +1001,8 @@ loop:
 			NoticeActiveTunnel(
 			NoticeActiveTunnel(
 				connectedTunnel.dialParams.ServerEntry.GetDiagnosticID(),
 				connectedTunnel.dialParams.ServerEntry.GetDiagnosticID(),
 				connectedTunnel.dialParams.TunnelProtocol,
 				connectedTunnel.dialParams.TunnelProtocol,
-				connectedTunnel.dialParams.ServerEntry.SupportsSSHAPIRequests())
+				connectedTunnel.dialParams.ServerEntry.SupportsSSHAPIRequests(),
+				connectedTunnel.dialParams.ServerEntry.Region)
 
 
 			if isFirstTunnel {
 			if isFirstTunnel {
 
 

+ 3 - 2
psiphon/notice.go

@@ -678,12 +678,13 @@ func NoticeRequestedTactics(dialParams *DialParameters) {
 }
 }
 
 
 // NoticeActiveTunnel is a successful connection that is used as an active tunnel for port forwarding
 // NoticeActiveTunnel is a successful connection that is used as an active tunnel for port forwarding
-func NoticeActiveTunnel(diagnosticID, protocol string, isTCS bool) {
+func NoticeActiveTunnel(diagnosticID, protocol string, isTCS bool, serverRegion string) {
 	singletonNoticeLogger.outputNotice(
 	singletonNoticeLogger.outputNotice(
 		"ActiveTunnel", noticeIsDiagnostic,
 		"ActiveTunnel", noticeIsDiagnostic,
 		"diagnosticID", diagnosticID,
 		"diagnosticID", diagnosticID,
 		"protocol", protocol,
 		"protocol", protocol,
-		"isTCS", isTCS)
+		"isTCS", isTCS,
+		"serverRegion", serverRegion)
 }
 }
 
 
 // NoticeSocksProxyPortInUse is a failure to use the configured LocalSocksProxyPort
 // NoticeSocksProxyPortInUse is a failure to use the configured LocalSocksProxyPort