Browse Source

Fix: use correct fragmentor up/down params; update test

Rod Hynes 7 years ago
parent
commit
4577bc7104
2 changed files with 51 additions and 14 deletions
  1. 27 8
      psiphon/common/fragmentor/fragmentor.go
  2. 24 6
      psiphon/common/fragmentor/fragmentor_test.go

+ 27 - 8
psiphon/common/fragmentor/fragmentor.go

@@ -66,16 +66,35 @@ func newConfig(
 	isUpstream bool,
 	tunnelProtocol string) *Config {
 
-	coinFlip := p.WeightedCoinFlip(parameters.FragmentorDownstreamProbability)
-	tunnelProtocols := p.TunnelProtocols(parameters.FragmentorDownstreamLimitProtocols)
+	probability := parameters.FragmentorProbability
+	limitProtocols := parameters.FragmentorLimitProtocols
+	minTotalBytes := parameters.FragmentorMinTotalBytes
+	maxTotalBytes := parameters.FragmentorMaxTotalBytes
+	minWriteBytes := parameters.FragmentorMinWriteBytes
+	maxWriteBytes := parameters.FragmentorMaxWriteBytes
+	minDelay := parameters.FragmentorMinDelay
+	maxDelay := parameters.FragmentorMaxDelay
+
+	if !isUpstream {
+		probability = parameters.FragmentorDownstreamProbability
+		limitProtocols = parameters.FragmentorDownstreamLimitProtocols
+		minTotalBytes = parameters.FragmentorDownstreamMinTotalBytes
+		maxTotalBytes = parameters.FragmentorDownstreamMaxTotalBytes
+		minWriteBytes = parameters.FragmentorDownstreamMinWriteBytes
+		maxWriteBytes = parameters.FragmentorDownstreamMaxWriteBytes
+		minDelay = parameters.FragmentorDownstreamMinDelay
+		maxDelay = parameters.FragmentorDownstreamMaxDelay
+	}
+
+	coinFlip := p.WeightedCoinFlip(probability)
+	tunnelProtocols := p.TunnelProtocols(limitProtocols)
 
 	if !coinFlip || (len(tunnelProtocols) > 0 && common.Contains(tunnelProtocols, tunnelProtocol)) {
 		return nil
 	}
 
 	bytesToFragment, err := common.MakeSecureRandomRange(
-		p.Int(parameters.FragmentorDownstreamMinTotalBytes),
-		p.Int(parameters.FragmentorDownstreamMaxTotalBytes))
+		p.Int(minTotalBytes), p.Int(maxTotalBytes))
 	if err != nil {
 		bytesToFragment = 0
 	}
@@ -87,10 +106,10 @@ func newConfig(
 	return &Config{
 		isUpstream:      isUpstream,
 		bytesToFragment: bytesToFragment,
-		minWriteBytes:   p.Int(parameters.FragmentorDownstreamMinWriteBytes),
-		maxWriteBytes:   p.Int(parameters.FragmentorDownstreamMaxWriteBytes),
-		minDelay:        p.Duration(parameters.FragmentorDownstreamMinDelay),
-		maxDelay:        p.Duration(parameters.FragmentorDownstreamMaxDelay),
+		minWriteBytes:   p.Int(minWriteBytes),
+		maxWriteBytes:   p.Int(maxWriteBytes),
+		minDelay:        p.Duration(minDelay),
+		maxDelay:        p.Duration(maxDelay),
 	}
 }
 

+ 24 - 6
psiphon/common/fragmentor/fragmentor_test.go

@@ -29,6 +29,8 @@ import (
 	"time"
 
 	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
+	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
+	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
 	"golang.org/x/sync/errgroup"
 )
 
@@ -55,6 +57,24 @@ func TestFragmentor(t *testing.T) {
 	minDelay := 2 * time.Millisecond
 	maxDelay := 2 * time.Millisecond
 
+	clientParameters, err := parameters.NewClientParameters(nil)
+	if err != nil {
+		t.Fatalf("parameters.NewClientParameters failed: %s", err)
+	}
+	_, err = clientParameters.Set("", false, map[string]interface{}{
+		"FragmentorProbability":    1.0,
+		"FragmentorLimitProtocols": protocol.TunnelProtocols{},
+		"FragmentorMinTotalBytes":  bytesFragmented,
+		"FragmentorMaxTotalBytes":  bytesFragmented,
+		"FragmentorMinWriteBytes":  minWriteBytes,
+		"FragmentorMaxWriteBytes":  maxWriteBytes,
+		"FragmentorMinDelay":       minDelay,
+		"FragmentorMaxDelay":       maxDelay,
+	})
+	if err != nil {
+		t.Fatalf("ClientParameters.Set failed: %s", err)
+	}
+
 	testGroup, testCtx := errgroup.WithContext(context.Background())
 
 	testGroup.Go(func() error {
@@ -86,14 +106,12 @@ func TestFragmentor(t *testing.T) {
 		if err != nil {
 			return common.ContextError(err)
 		}
+		config := NewUpstreamConfig(clientParameters.Get(), "")
+		t.Logf("%+v", config.GetMetrics())
 		conn = NewConn(
-			conn,
+			config,
 			func(message string) { t.Logf(message) },
-			bytesFragmented,
-			minWriteBytes,
-			maxWriteBytes,
-			minDelay,
-			maxDelay)
+			conn)
 		defer conn.Close()
 		_, err = conn.Write(data)
 		if err != nil {