[tor-commits] [meek/extension] Move helper code to its own file.

dcf at torproject.org dcf at torproject.org
Sat Mar 22 07:14:07 UTC 2014


commit baf34b96cf3969eb0a517d5993a39632e98c2f26
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Mar 22 00:13:11 2014 -0700

    Move helper code to its own file.
---
 meek-client/helper.go      |  104 ++++++++++++++++++++++++++++++++++++++++++++
 meek-client/meek-client.go |   90 --------------------------------------
 2 files changed, 104 insertions(+), 90 deletions(-)

diff --git a/meek-client/helper.go b/meek-client/helper.go
new file mode 100644
index 0000000..98fd771
--- /dev/null
+++ b/meek-client/helper.go
@@ -0,0 +1,104 @@
+package main
+
+import (
+	"bytes"
+	"encoding/binary"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"io"
+	"io/ioutil"
+	"net"
+	"net/http"
+	"time"
+)
+
+// The code in this file has to do communicating with the meek-http-helper
+// browser extension.
+
+type JSONRequest struct {
+	Method string            `json:"method,omitempty"`
+	URL    string            `json:"url,omitempty"`
+	Header map[string]string `json:"header,omitempty"`
+	Body   []byte            `json:"body,omitempty"`
+}
+
+type JSONResponse struct {
+	Error  string `json:"error,omitempty"`
+	Status int    `json:"status"`
+	Body   []byte `json:"body"`
+}
+
+// Ask a locally running browser extension to make the request for us.
+func roundTripWithHelper(buf []byte, info *RequestInfo) (*http.Response, error) {
+	s, err := net.DialTCP("tcp", nil, options.HelperAddr)
+	if err != nil {
+		return nil, err
+	}
+	defer s.Close()
+
+	// Encode our JSON.
+	req := JSONRequest{
+		Method: "POST",
+		URL:    info.URL.String(),
+		Header: make(map[string]string),
+		Body:   buf,
+	}
+	req.Header["X-Session-Id"] = info.SessionID
+	if info.Host != "" {
+		req.Header["Host"] = info.Host
+	}
+	encReq, err := json.Marshal(&req)
+	if err != nil {
+		return nil, err
+	}
+	// log.Printf("encoded %s", encReq)
+
+	// Send the request.
+	s.SetWriteDeadline(time.Now().Add(helperWriteTimeout))
+	err = binary.Write(s, binary.BigEndian, uint32(len(encReq)))
+	if err != nil {
+		return nil, err
+	}
+	_, err = s.Write(encReq)
+	if err != nil {
+		return nil, err
+	}
+
+	// Read the response.
+	var length uint32
+	s.SetReadDeadline(time.Now().Add(helperReadTimeout))
+	err = binary.Read(s, binary.BigEndian, &length)
+	if err != nil {
+		return nil, err
+	}
+	if length > maxHelperResponseLength {
+		return nil, errors.New(fmt.Sprintf("helper's returned data is too big (%d > %d)",
+			length, maxHelperResponseLength))
+	}
+	encResp := make([]byte, length)
+	_, err = io.ReadFull(s, encResp)
+	if err != nil {
+		return nil, err
+	}
+	// log.Printf("received %s", encResp)
+
+	// Decode their JSON.
+	var jsonResp JSONResponse
+	err = json.Unmarshal(encResp, &jsonResp)
+	if err != nil {
+		return nil, err
+	}
+	if jsonResp.Error != "" {
+		return nil, errors.New(fmt.Sprintf("helper returned error: %s", jsonResp.Error))
+	}
+
+	// Mock up an HTTP response.
+	resp := http.Response{
+		Status:        http.StatusText(jsonResp.Status),
+		StatusCode:    jsonResp.Status,
+		Body:          ioutil.NopCloser(bytes.NewReader(jsonResp.Body)),
+		ContentLength: int64(len(jsonResp.Body)),
+	}
+	return &resp, nil
+}
diff --git a/meek-client/meek-client.go b/meek-client/meek-client.go
index 930b123..6ce95f0 100644
--- a/meek-client/meek-client.go
+++ b/meek-client/meek-client.go
@@ -4,13 +4,10 @@ import (
 	"bytes"
 	"crypto/rand"
 	"encoding/base64"
-	"encoding/binary"
-	"encoding/json"
 	"errors"
 	"flag"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"net"
 	"net/http"
@@ -82,93 +79,6 @@ func roundTripWithHTTP(buf []byte, info *RequestInfo) (*http.Response, error) {
 	return tr.RoundTrip(req)
 }
 
-type JSONRequest struct {
-	Method string            `json:"method,omitempty"`
-	URL    string            `json:"url,omitempty"`
-	Header map[string]string `json:"header,omitempty"`
-	Body   []byte            `json:"body,omitempty"`
-}
-
-type JSONResponse struct {
-	Error  string `json:"error,omitempty"`
-	Status int    `json:"status"`
-	Body   []byte `json:"body"`
-}
-
-// Ask a locally running browser extension to make the request for us.
-func roundTripWithHelper(buf []byte, info *RequestInfo) (*http.Response, error) {
-	s, err := net.DialTCP("tcp", nil, options.HelperAddr)
-	if err != nil {
-		return nil, err
-	}
-	defer s.Close()
-
-	// Encode our JSON.
-	req := JSONRequest{
-		Method: "POST",
-		URL:    info.URL.String(),
-		Header: make(map[string]string),
-		Body:   buf,
-	}
-	req.Header["X-Session-Id"] = info.SessionID
-	if info.Host != "" {
-		req.Header["Host"] = info.Host
-	}
-	encReq, err := json.Marshal(&req)
-	if err != nil {
-		return nil, err
-	}
-	// log.Printf("encoded %s", encReq)
-
-	// Send the request.
-	s.SetWriteDeadline(time.Now().Add(helperWriteTimeout))
-	err = binary.Write(s, binary.BigEndian, uint32(len(encReq)))
-	if err != nil {
-		return nil, err
-	}
-	_, err = s.Write(encReq)
-	if err != nil {
-		return nil, err
-	}
-
-	// Read the response.
-	var length uint32
-	s.SetReadDeadline(time.Now().Add(helperReadTimeout))
-	err = binary.Read(s, binary.BigEndian, &length)
-	if err != nil {
-		return nil, err
-	}
-	if length > maxHelperResponseLength {
-		return nil, errors.New(fmt.Sprintf("helper's returned data is too big (%d > %d)",
-			length, maxHelperResponseLength))
-	}
-	encResp := make([]byte, length)
-	_, err = io.ReadFull(s, encResp)
-	if err != nil {
-		return nil, err
-	}
-	// log.Printf("received %s", encResp)
-
-	// Decode their JSON.
-	var jsonResp JSONResponse
-	err = json.Unmarshal(encResp, &jsonResp)
-	if err != nil {
-		return nil, err
-	}
-	if jsonResp.Error != "" {
-		return nil, errors.New(fmt.Sprintf("helper returned error: %s", jsonResp.Error))
-	}
-
-	// Mock up an HTTP response.
-	resp := http.Response{
-		Status:        http.StatusText(jsonResp.Status),
-		StatusCode:    jsonResp.Status,
-		Body:          ioutil.NopCloser(bytes.NewReader(jsonResp.Body)),
-		ContentLength: int64(len(jsonResp.Body)),
-	}
-	return &resp, nil
-}
-
 func sendRecv(buf []byte, conn net.Conn, info *RequestInfo) (int64, error) {
 	roundTrip := roundTripWithHTTP
 	if options.HelperAddr != nil {



More information about the tor-commits mailing list