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

Refactored streaming embedded server list into its own function

Amir Khan 8 лет назад
Родитель
Сommit
96236d0c8b
3 измененных файлов с 28 добавлено и 12 удалено
  1. 3 0
      .gitignore
  2. 23 10
      MobileLibrary/psi/psi.go
  3. 2 2
      psiphon/dataStore.go

+ 3 - 0
.gitignore

@@ -54,3 +54,6 @@ build/
 
 # Visual Studio Code
 .vscode/
+
+# Intellij IDEs
+.idea/

+ 23 - 10
MobileLibrary/psi/psi.go

@@ -51,7 +51,7 @@ var controllerWaitGroup *sync.WaitGroup
 
 func Start(
 	configJson, embeddedServerEntryList,
-	embeddedServerEntryListFileName string,
+	embeddedServerEntryListPath string,
 	provider PsiphonProvider,
 	useDeviceBinder bool, useIPv6Synthesizer bool) error {
 
@@ -91,19 +91,15 @@ func Start(
 		return fmt.Errorf("error initializing datastore: %s", err)
 	}
 
-	// if embeddedServerEntryListFileName is not empty, ignore embeddedServerEntryList.
-	if embeddedServerEntryListFileName != "" {
+	// if embeddedServerEntryListPath is not empty, ignore embeddedServerEntryList.
+	if embeddedServerEntryListPath != "" {
 
-		serverEntriesFile, err := os.Open(embeddedServerEntryListFileName)
-		if err != nil {
-			return fmt.Errorf("failed to read remote server list: %s", common.ContextError(err))
-		}
-		defer serverEntriesFile.Close()
+		err := streamingStoreEntriesWithIOReader(embeddedServerEntryListPath)
 
-		err = psiphon.StreamingStoreServerEntriesWithIOReader(serverEntriesFile, protocol.SERVER_ENTRY_SOURCE_EMBEDDED)
 		if err != nil {
-			return fmt.Errorf("failed to store common remote server list: %s", common.ContextError(err))
+			return err
 		}
+
 	} else {
 
 		serverEntries, err := protocol.DecodeServerEntryList(
@@ -184,3 +180,20 @@ func GetPacketTunnelDNSResolverIPv4Address() string {
 func GetPacketTunnelDNSResolverIPv6Address() string {
 	return tun.GetTransparentDNSResolverIPv6Address().String()
 }
+
+// Opens file pointed to by embeddedServerEntryListPath, and stores the entries
+// using StreamingStoreServerEntries
+func streamingStoreEntriesWithIOReader(embeddedServerEntryListPath string) error {
+	serverEntriesFile, err := os.Open(embeddedServerEntryListPath)
+	if err != nil {
+		return fmt.Errorf("failed to read remote server list: %s", common.ContextError(err))
+	}
+	defer serverEntriesFile.Close()
+
+	err = psiphon.StreamingStoreServerEntriesWithIOReader(serverEntriesFile, protocol.SERVER_ENTRY_SOURCE_EMBEDDED)
+	if err != nil {
+		return fmt.Errorf("failed to store common remote server list: %s", common.ContextError(err))
+	}
+
+	return nil
+}

+ 2 - 2
psiphon/dataStore.go

@@ -24,13 +24,13 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"io"
 	"math/rand"
 	"os"
 	"path/filepath"
 	"strings"
 	"sync"
 	"time"
-	"io"
 
 	"github.com/Psiphon-Inc/bolt"
 	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
@@ -269,7 +269,7 @@ func StoreServerEntries(serverEntries []*protocol.ServerEntry, replaceIfExists b
 	return nil
 }
 
-// Wrapper around StreamingStoreServerEntries
+// StreamingStoreServerEntriesWithIOReader is a wrapper around StreamingStoreServerEntries
 // an io.Reader is passed instead of an instance of StreamingServerEntryDecoder
 func StreamingStoreServerEntriesWithIOReader(serverListReader io.Reader, serverEntrySource string) error {