Browse Source

Moved the status padding logic to be with other API code

* Following the jitter logic moved in commit 17645bd.
* More explicit padding size constant
Rod Hynes 11 years ago
parent
commit
4d93026cff
3 changed files with 47 additions and 46 deletions
  1. 24 23
      psiphon/defaults.go
  2. 22 1
      psiphon/serverApi.go
  3. 1 22
      psiphon/stats_collector.go

+ 24 - 23
psiphon/defaults.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
@@ -24,26 +24,27 @@ import (
 )
 
 const (
-	VERSION                                    = "0.0.6"
-	DATA_STORE_FILENAME                        = "psiphon.db"
-	CONNECTION_WORKER_POOL_SIZE                = 10
-	TUNNEL_POOL_SIZE                           = 1
-	TUNNEL_CONNECT_TIMEOUT                     = 15 * time.Second
-	TUNNEL_READ_TIMEOUT                        = 0 * time.Second
-	TUNNEL_WRITE_TIMEOUT                       = 5 * time.Second
-	TUNNEL_SSH_KEEP_ALIVE_PERIOD               = 60 * time.Second
-	ESTABLISH_TUNNEL_TIMEOUT                   = 60 * time.Second
-	ESTABLISH_TUNNEL_PAUSE_PERIOD              = 10 * time.Second
-	PORT_FORWARD_FAILURE_THRESHOLD             = 10
-	HTTP_PROXY_ORIGIN_SERVER_TIMEOUT           = 15 * time.Second
-	HTTP_PROXY_MAX_IDLE_CONNECTIONS_PER_HOST   = 50
-	FETCH_REMOTE_SERVER_LIST_TIMEOUT           = 10 * time.Second
-	FETCH_REMOTE_SERVER_LIST_RETRY_PERIOD      = 5 * time.Second
-	FETCH_REMOTE_SERVER_LIST_STALE_PERIOD      = 6 * time.Hour
-	PSIPHON_API_CLIENT_SESSION_ID_LENGTH       = 16
-	PSIPHON_API_SERVER_TIMEOUT                 = 20 * time.Second
-	PSIPHON_API_STATUS_REQUEST_PERIOD_MIN      = 5 * time.Minute
-	PSIPHON_API_STATUS_REQUEST_PERIOD_MAX      = 10 * time.Minute
-	PSIPHON_API_CONNECTED_REQUEST_PERIOD       = 24 * time.Hour
-	PSIPHON_API_CONNECTED_REQUEST_RETRY_PERIOD = 5 * time.Second
+	VERSION                                      = "0.0.6"
+	DATA_STORE_FILENAME                          = "psiphon.db"
+	CONNECTION_WORKER_POOL_SIZE                  = 10
+	TUNNEL_POOL_SIZE                             = 1
+	TUNNEL_CONNECT_TIMEOUT                       = 15 * time.Second
+	TUNNEL_READ_TIMEOUT                          = 0 * time.Second
+	TUNNEL_WRITE_TIMEOUT                         = 5 * time.Second
+	TUNNEL_SSH_KEEP_ALIVE_PERIOD                 = 60 * time.Second
+	ESTABLISH_TUNNEL_TIMEOUT                     = 60 * time.Second
+	ESTABLISH_TUNNEL_PAUSE_PERIOD                = 10 * time.Second
+	PORT_FORWARD_FAILURE_THRESHOLD               = 10
+	HTTP_PROXY_ORIGIN_SERVER_TIMEOUT             = 15 * time.Second
+	HTTP_PROXY_MAX_IDLE_CONNECTIONS_PER_HOST     = 50
+	FETCH_REMOTE_SERVER_LIST_TIMEOUT             = 10 * time.Second
+	FETCH_REMOTE_SERVER_LIST_RETRY_PERIOD        = 5 * time.Second
+	FETCH_REMOTE_SERVER_LIST_STALE_PERIOD        = 6 * time.Hour
+	PSIPHON_API_CLIENT_SESSION_ID_LENGTH         = 16
+	PSIPHON_API_SERVER_TIMEOUT                   = 20 * time.Second
+	PSIPHON_API_STATUS_REQUEST_PERIOD_MIN        = 5 * time.Minute
+	PSIPHON_API_STATUS_REQUEST_PERIOD_MAX        = 10 * time.Minute
+	PSIPHON_API_STATUS_REQUEST_PADDING_MAX_BYTES = 256
+	PSIPHON_API_CONNECTED_REQUEST_PERIOD         = 24 * time.Hour
+	PSIPHON_API_CONNECTED_REQUEST_RETRY_PERIOD   = 5 * time.Second
 )

+ 22 - 1
psiphon/serverApi.go

@@ -21,6 +21,7 @@ package psiphon
 
 import (
 	"bytes"
+	"encoding/base64"
 	"encoding/hex"
 	"encoding/json"
 	"errors"
@@ -156,13 +157,33 @@ func (session *Session) DoStatusRequest(statsPayload json.Marshaler) error {
 		return ContextError(err)
 	}
 
+	// Add a random amount of padding to help prevent stats updates from being
+	// a predictable size (which often happens when the connection is quiet).
+	var padding []byte
+	paddingSize, err := MakeSecureRandomInt(PSIPHON_API_STATUS_REQUEST_PADDING_MAX_BYTES)
+	// In case of randomness fail, we're going to proceed with zero padding.
+	// TODO: Is this okay?
+	if err != nil {
+		NoticeAlert("DoStatusRequest: MakeSecureRandomInt failed")
+		padding = make([]byte, 0)
+	} else {
+		padding, err = MakeSecureRandomBytes(paddingSize)
+		if err != nil {
+			NoticeAlert("DoStatusRequest: MakeSecureRandomBytes failed")
+			padding = make([]byte, 0)
+		}
+	}
+
 	// "connected" is a legacy parameter. This client does not report when
 	// it has disconnected.
 
 	url := session.buildRequestUrl(
 		"status",
 		&ExtraParam{"session_id", session.sessionId},
-		&ExtraParam{"connected", "1"})
+		&ExtraParam{"connected", "1"},
+		// TODO: base64 encoding of padding means the padding
+		// size is not exactly [0, PADDING_MAX_BYTES]
+		&ExtraParam{"padding", base64.StdEncoding.EncodeToString(padding)})
 
 	err = session.doPostRequest(url, "application/json", bytes.NewReader(statsPayloadJSON))
 	if err != nil {

+ 1 - 22
psiphon/stats_collector.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
@@ -20,7 +20,6 @@
 package psiphon
 
 import (
-	"encoding/base64"
 	"encoding/json"
 	"sync"
 )
@@ -105,23 +104,6 @@ func recordStat(stat *statsUpdate) {
 func (ss serverStats) MarshalJSON() ([]byte, error) {
 	out := make(map[string]interface{})
 
-	// Add a random amount of padding to help prevent stats updates from being
-	// a predictable size (which often happens when the connection is quiet).
-	var padding []byte
-	paddingSize, err := MakeSecureRandomInt(256)
-	// In case of randomness fail, we're going to proceed with zero padding.
-	// TODO: Is this okay?
-	if err != nil {
-		NoticeAlert("stats.serverStats.MarshalJSON: MakeSecureRandomInt failed")
-		padding = make([]byte, 0)
-	} else {
-		padding, err = MakeSecureRandomBytes(paddingSize)
-		if err != nil {
-			NoticeAlert("stats.serverStats.MarshalJSON: MakeSecureRandomBytes failed")
-			padding = make([]byte, 0)
-		}
-	}
-
 	hostBytes := make(map[string]int64)
 	bytesTransferred := int64(0)
 
@@ -134,12 +116,9 @@ func (ss serverStats) MarshalJSON() ([]byte, error) {
 	out["bytes_transferred"] = bytesTransferred
 	out["host_bytes"] = hostBytes
 
-	// Print the notice before adding the padding, since it's not interesting
 	noticeJSON, _ := json.Marshal(out)
 	NoticeInfo("sending stats: %s", noticeJSON)
 
-	out["padding"] = base64.StdEncoding.EncodeToString(padding)
-
 	// We're not using these fields, but the server requires them
 	out["page_views"] = make([]string, 0)
 	out["https_requests"] = make([]string, 0)