Skip to content

Commit

Permalink
pass http test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasen committed Dec 4, 2015
1 parent 197256e commit 6dd9b18
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
)

var (
_cipherRequestHeader = []byte("X-CipherOrigin:")
_cipherRequestHeader = []byte("x-cipher-origin")
_maxHTTPHeaderSize = 4096 * 2
)

Expand Down Expand Up @@ -102,8 +102,8 @@ func main() {

_SecretPassphase = []byte(os.Getenv("SECRET"))

mhs := strconv.Atoi(os.Getenv("MAX_HTTP_HEADER_SIZE"))
if mhs > _maxHTTPHeaderSize {
mhs, err := strconv.Atoi(os.Getenv("MAX_HTTP_HEADER_SIZE"))
if err == nil && mhs > _maxHTTPHeaderSize {
_maxHTTPHeaderSize = mhs
}

Expand Down Expand Up @@ -187,13 +187,16 @@ func handleConn(c net.Conn) {
c.Write([]byte{0x07})
return
}
header.Write(line)
header.Write([]byte("\n"))
if bytes.HasPrefix(line, _cipherRequestHeader) {
cipherAddr = bytes.TrimSpace(line[len(_cipherRequestHeader):])

if bytes.HasPrefix(bytes.ToLower(line), _cipherRequestHeader) {
log.Println("HTTP3.4")
cipherAddr = bytes.TrimSpace(line[(len(_cipherRequestHeader) + 1):])
break
}

header.Write(line)
header.Write([]byte("\n"))

if header.Len() > _maxHTTPHeaderSize {
c.Write([]byte{0x08})
return
Expand Down
91 changes: 60 additions & 31 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"strconv"
"testing"
Expand All @@ -20,6 +22,7 @@ import (

var (
_echoServerAddr = []byte("127.0.0.1:62863")
_httpServerAddr = []byte("127.0.0.1:62865")
_expectAESCiphertext = []byte("U2FsdGVkX19KIJ9OQJKT/yHGMrS+5SsBAAjetomptQ0=")
_secret = []byte("p0S8rX680*48")
_defaultFrontdAddr = "127.0.0.1:" + strconv.Itoa(_DefaultPort)
Expand Down Expand Up @@ -47,6 +50,11 @@ func TestMain(m *testing.M) {

go main()

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
go http.ListenAndServe(string(_httpServerAddr), nil)

rand.Seed(time.Now().UnixNano())

// TODO: better way to wait for server to start
Expand Down Expand Up @@ -87,18 +95,6 @@ func servEcho() {
}
}

func TestTextDecryptAES(t *testing.T) {
o := aes256cbc.New()

dec, err := o.Decrypt(_secret, _expectAESCiphertext)
if err != nil {
panic(err)
}
if !bytes.Equal(dec, _echoServerAddr) {
panic(errors.New("not match"))
}
}

func encryptText(plaintext, passphrase []byte) ([]byte, error) {
o := aes256cbc.New()

Expand Down Expand Up @@ -139,25 +135,6 @@ func testEchoRound(conn net.Conn) {
}
}

func TestEchoServer(t *testing.T) {
var conn net.Conn
var err error
if *reuseTest {
conn, err = reuseport.Dial("tcp", "127.0.0.1:0", string(_echoServerAddr))
} else {
conn, err = net.Dial("tcp", string(_echoServerAddr))
}
if err != nil {
panic(err)
}
defer conn.Close()

n := rand.Int() % 10
for i := 0; i < n; i++ {
testEchoRound(conn)
}
}

func testProtocol(cipherAddr []byte) {
// * test decryption
var conn net.Conn
Expand Down Expand Up @@ -188,6 +165,37 @@ func testProtocol(cipherAddr []byte) {
}
}

func TestTextDecryptAES(t *testing.T) {
o := aes256cbc.New()

dec, err := o.Decrypt(_secret, _expectAESCiphertext)
if err != nil {
panic(err)
}
if !bytes.Equal(dec, _echoServerAddr) {
panic(errors.New("not match"))
}
}

func TestEchoServer(t *testing.T) {
var conn net.Conn
var err error
if *reuseTest {
conn, err = reuseport.Dial("tcp", "127.0.0.1:0", string(_echoServerAddr))
} else {
conn, err = net.Dial("tcp", string(_echoServerAddr))
}
if err != nil {
panic(err)
}
defer conn.Close()

n := rand.Int() % 10
for i := 0; i < n; i++ {
testEchoRound(conn)
}
}

func TestProtocolDecrypt(*testing.T) {
b, err := encryptText(_echoServerAddr, _secret)
if err != nil {
Expand All @@ -196,6 +204,27 @@ func TestProtocolDecrypt(*testing.T) {
testProtocol(b)
}

func TestHTTPServer(t *testing.T) {
cipherAddr, err := encryptText(_httpServerAddr, _secret)
if err != nil {
panic(err)
}

client := &http.Client{}
req, _ := http.NewRequest("GET", "http://"+string(_defaultFrontdAddr), nil)
req.Header.Set(string(_cipherRequestHeader), string(cipherAddr))
res, _ := client.Do(req)

b, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}

if bytes.Compare(b, []byte("OK")) != 0 {
t.Fail()
}
}

// TODO: test decryption with extra bytes in packet and check data

// TODO: test decryption with seperated packet simulate loss connection and check data
Expand Down

0 comments on commit 6dd9b18

Please sign in to comment.