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

Update Android Library/App to integrate with latest core interfaces

Rod Hynes 11 лет назад
Родитель
Сommit
fa752adb24

+ 27 - 21
AndroidApp/app/src/main/java/ca/psiphon/psibot/Psiphon.java

@@ -49,12 +49,12 @@ public class Psiphon extends Psi.PsiphonProvider.Stub {
 
     // PsiphonProvider.Notice
     @Override
-    public void Notice(String message) {
-        message = message.trim();
+    public void Notice(String noticeJSON) {
+        noticeJSON = noticeJSON.trim();
 
-        android.util.Log.d("PSIPHON", message);
-        parseMessage(message);
-        Log.addEntry(message);
+        android.util.Log.d("PSIPHON", noticeJSON);
+        parseMessage(noticeJSON);
+        Log.addEntry(noticeJSON);
     }
 
     // PsiphonProvider.BindToDevice
@@ -72,8 +72,11 @@ public class Psiphon extends Psi.PsiphonProvider.Stub {
         mLocalHttpProxyPort = 0;
         mHomePages = new HashSet<String>();
 
+        // TODO: supply embedded server list
+        String embeddedServerEntryList = "";
+
         try {
-            Psi.Start(loadConfig(mVpnService), this);
+            Psi.Start(loadConfig(mVpnService), embeddedServerEntryList, this);
         } catch (Exception e) {
             throw new Utils.PsibotError("failed to start Psiphon", e);
         }
@@ -172,21 +175,24 @@ public class Psiphon extends Psi.PsiphonProvider.Stub {
         return json.toString();
     }
 
-    private synchronized void parseMessage(String message) {
-        // TODO: this is based on tentative log line formats
-        final String socksProxy = "SOCKS-PROXY-PORT ";
-        final String httpProxy = "HTTP-PROXY-PORT ";
-        final String homePage = "HOMEPAGE ";
-        final String tunnelStarted = "TUNNELS 1";
-        int index;
-        if (-1 != (index = message.indexOf(socksProxy))) {
-            mLocalSocksProxyPort = Integer.parseInt(message.substring(index + socksProxy.length()));
-        } else if (-1 != (index = message.indexOf(httpProxy))) {
-            mLocalHttpProxyPort = Integer.parseInt(message.substring(index + httpProxy.length()));
-        } else if (-1 != (index = message.indexOf(homePage))) {
-            mHomePages.add(message.substring(index + homePage.length()));
-        } else if (message.contains(tunnelStarted)) {
-            mTunnelStartedSignal.countDown();
+    private synchronized void parseMessage(String noticeJSON) {
+        try {
+            JSONObject notice = new JSONObject(noticeJSON);
+            String noticeType = notice.getString("noticeType");
+            if (noticeType.equals("Tunnels")) {
+                int count = notice.getJSONObject("data").getInt("count");
+                if (count == 1) {
+                    mTunnelStartedSignal.countDown();
+                }
+            } else if (noticeType.equals("ListeningSocksProxyPort")) {
+                mLocalSocksProxyPort = notice.getJSONObject("data").getInt("port");
+            } else if (noticeType.equals("ListeningHttpProxyPort")) {
+                mLocalHttpProxyPort = notice.getJSONObject("data").getInt("port");
+            } else if (noticeType.equals("Homepage")) {
+                mHomePages.add(notice.getJSONObject("data").getString("url"));
+            }
+        } catch (JSONException e) {
+            // Ignore notice
         }
     }
 }

+ 7 - 6
AndroidApp/app/src/main/java/go/psi/Psi.java

@@ -12,7 +12,7 @@ public abstract class Psi {
     public interface PsiphonProvider extends go.Seq.Object {
         public void BindToDevice(long fileDescriptor);
         
-        public void Notice(String message);
+        public void Notice(String noticeJSON);
         
         public static abstract class Stub implements PsiphonProvider {
             static final String DESCRIPTOR = "go.psi.PsiphonProvider";
@@ -32,8 +32,8 @@ public abstract class Psi {
                     return;
                 }
                 case Proxy.CALL_Notice: {
-                    String param_message = in.readUTF16();
-                    this.Notice(param_message);
+                    String param_noticeJSON = in.readUTF16();
+                    this.Notice(param_noticeJSON);
                     return;
                 }
                 default:
@@ -63,11 +63,11 @@ public abstract class Psi {
                 Seq.send(DESCRIPTOR, CALL_BindToDevice, _in, _out);
             }
             
-            public void Notice(String message) {
+            public void Notice(String noticeJSON) {
                 go.Seq _in = new go.Seq();
                 go.Seq _out = new go.Seq();
                 _in.writeRef(ref);
-                _in.writeUTF16(message);
+                _in.writeUTF16(noticeJSON);
                 Seq.send(DESCRIPTOR, CALL_Notice, _in, _out);
             }
             
@@ -76,10 +76,11 @@ public abstract class Psi {
         }
     }
     
-    public static void Start(String configJson, PsiphonProvider provider) throws Exception {
+    public static void Start(String configJson, String embeddedServerEntryList, PsiphonProvider provider) throws Exception {
         go.Seq _in = new go.Seq();
         go.Seq _out = new go.Seq();
         _in.writeUTF16(configJson);
+        _in.writeUTF16(embeddedServerEntryList);
         _in.writeRef(provider.ref());
         Seq.send(DESCRIPTOR, CALL_Start, _in, _out);
         String _err = _out.readUTF16();

+ 5 - 4
AndroidLibrary/go_psi/go_psi.go

@@ -5,7 +5,7 @@
 package go_psi
 
 import (
-	"github.com/Psiphon-Labs/psiphon-tunnel-core/AndroidLibrary/psi"
+	"."
 	"golang.org/x/mobile/bind/seq"
 )
 
@@ -23,14 +23,15 @@ func (p *proxyPsiphonProvider) BindToDevice(fileDescriptor int) {
 	seq.Transact((*seq.Ref)(p), proxyPsiphonProviderBindToDeviceCode, out)
 }
 
-func (p *proxyPsiphonProvider) Notice(message string) {
+func (p *proxyPsiphonProvider) Notice(noticeJSON string) {
 	out := new(seq.Buffer)
-	out.WriteUTF16(message)
+	out.WriteUTF16(noticeJSON)
 	seq.Transact((*seq.Ref)(p), proxyPsiphonProviderNoticeCode, out)
 }
 
 func proxy_Start(out, in *seq.Buffer) {
 	param_configJson := in.ReadUTF16()
+	param_embeddedServerEntryList := in.ReadUTF16()
 	var param_provider psi.PsiphonProvider
 	param_provider_ref := in.ReadRef()
 	if param_provider_ref.Num < 0 {
@@ -38,7 +39,7 @@ func proxy_Start(out, in *seq.Buffer) {
 	} else {
 		param_provider = (*proxyPsiphonProvider)(param_provider_ref)
 	}
-	err := psi.Start(param_configJson, param_provider)
+	err := psi.Start(param_configJson, param_embeddedServerEntryList, param_provider)
 	if err == nil {
 		out.WriteUTF16("")
 	} else {

+ 7 - 6
AndroidLibrary/java_psi/go/psi/Psi.java

@@ -12,7 +12,7 @@ public abstract class Psi {
     public interface PsiphonProvider extends go.Seq.Object {
         public void BindToDevice(long fileDescriptor);
         
-        public void Notice(String message);
+        public void Notice(String noticeJSON);
         
         public static abstract class Stub implements PsiphonProvider {
             static final String DESCRIPTOR = "go.psi.PsiphonProvider";
@@ -32,8 +32,8 @@ public abstract class Psi {
                     return;
                 }
                 case Proxy.CALL_Notice: {
-                    String param_message = in.readUTF16();
-                    this.Notice(param_message);
+                    String param_noticeJSON = in.readUTF16();
+                    this.Notice(param_noticeJSON);
                     return;
                 }
                 default:
@@ -63,11 +63,11 @@ public abstract class Psi {
                 Seq.send(DESCRIPTOR, CALL_BindToDevice, _in, _out);
             }
             
-            public void Notice(String message) {
+            public void Notice(String noticeJSON) {
                 go.Seq _in = new go.Seq();
                 go.Seq _out = new go.Seq();
                 _in.writeRef(ref);
-                _in.writeUTF16(message);
+                _in.writeUTF16(noticeJSON);
                 Seq.send(DESCRIPTOR, CALL_Notice, _in, _out);
             }
             
@@ -76,10 +76,11 @@ public abstract class Psi {
         }
     }
     
-    public static void Start(String configJson, PsiphonProvider provider) throws Exception {
+    public static void Start(String configJson, String embeddedServerEntryList, PsiphonProvider provider) throws Exception {
         go.Seq _in = new go.Seq();
         go.Seq _out = new go.Seq();
         _in.writeUTF16(configJson);
+        _in.writeUTF16(embeddedServerEntryList);
         _in.writeRef(provider.ref());
         Seq.send(DESCRIPTOR, CALL_Start, _in, _out);
         String _err = _out.readUTF16();

+ 21 - 16
AndroidLibrary/psi/psi.go

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Psiphon Inc.
+ * Copyright (c) 2015, Psiphon Inc.
  * All rights reserved.
  *
  * This program is free software: you can redistribute it and/or modify
@@ -33,28 +33,18 @@ import (
 )
 
 type PsiphonProvider interface {
-	Notice(message string)
+	Notice(noticeJSON string)
 
 	// TODO: return 'error'; at the moment gobind doesn't
 	// work with interface function return values.
 	BindToDevice(fileDescriptor int)
 }
 
-type logRelay struct {
-	provider PsiphonProvider
-}
-
-func (lr *logRelay) Write(p []byte) (n int, err error) {
-	// TODO: buffer incomplete lines
-	lr.provider.Notice(string(p))
-	return len(p), nil
-}
-
 var controller *psiphon.Controller
 var shutdownBroadcast chan struct{}
 var controllerWaitGroup *sync.WaitGroup
 
-func Start(configJson string, provider PsiphonProvider) error {
+func Start(configJson, embeddedServerEntryList string, provider PsiphonProvider) error {
 
 	if controller != nil {
 		return fmt.Errorf("already started")
@@ -64,17 +54,32 @@ func Start(configJson string, provider PsiphonProvider) error {
 	if err != nil {
 		return fmt.Errorf("error loading configuration file: %s", err)
 	}
+	config.BindToDeviceProvider = provider
 
 	err = psiphon.InitDataStore(config)
 	if err != nil {
 		return fmt.Errorf("error initializing datastore: %s", err)
 	}
 
-	log.SetOutput(&logRelay{provider: provider})
+	psiphon.SetNoticeOutput(psiphon.NewNoticeReceiver(
+		func(notice []byte) {
+			provider.Notice(string(notice))
+		}))
 
-	config.BindToDeviceProvider = provider
+	serverEntries, err := psiphon.DecodeAndValidateServerEntryList(embeddedServerEntryList)
+	if err != nil {
+		log.Fatalf("error decoding embedded server entry list: %s", err)
+	}
+	err = psiphon.StoreServerEntries(serverEntries, false)
+	if err != nil {
+		log.Fatalf("error storing embedded server entry list: %s", err)
+	}
+
+	controller, err = psiphon.NewController(config)
+	if err != nil {
+		return fmt.Errorf("error initializing controller: %s", err)
+	}
 
-	controller = psiphon.NewController(config)
 	shutdownBroadcast = make(chan struct{})
 	controllerWaitGroup = new(sync.WaitGroup)
 	controllerWaitGroup.Add(1)