/
appengine.go
141 lines (121 loc) · 3.06 KB
/
appengine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package panda
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/hex"
"errors"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
"golang.org/x/net/proxy"
)
type HTTPMeetingPlace struct {
TorAddress string
URL string
}
const payloadBytes = 1 << 15
func (hmp *HTTPMeetingPlace) Padding() int {
return payloadBytes
}
func (hmp *HTTPMeetingPlace) attemptExchange(log func(string, ...interface{}), id, message []byte) (*http.Response, []byte, error) {
serverURL, err := url.Parse(hmp.URL)
if err != nil {
return nil, nil, err
}
dialer, err := proxy.SOCKS5("tcp", hmp.TorAddress, nil, proxy.Direct)
if err != nil {
return nil, nil, err
}
host := serverURL.Host
if strings.IndexRune(host, ':') == -1 {
if serverURL.Scheme == "https" {
host += ":443"
} else {
host += ":80"
}
}
hostname, _, err := net.SplitHostPort(host)
if err != nil {
return nil, nil, err
}
log("Connecting to %s via Tor", host)
rawConn, err := dialer.Dial("tcp", host)
if err != nil {
return nil, nil, err
}
var conn net.Conn
if serverURL.Scheme == "https" {
tlsConn := tls.Client(rawConn, &tls.Config{
ServerName: hostname,
})
log("Starting TLS handshake with %s", host)
if err := tlsConn.Handshake(); err != nil {
rawConn.Close()
log("TLS handshake to %s failed: %s", host, err)
return nil, nil, err
}
conn = tlsConn
} else {
conn = rawConn
}
defer conn.Close()
body := bytes.NewReader(message)
request, err := http.NewRequest("POST", hmp.URL+"/"+hex.EncodeToString(id), body)
if err != nil {
return nil, nil, err
}
request.ContentLength = int64(len(message))
request.Header.Add("Content-Type", "application/octet-stream")
request.Write(conn)
log("Request sent to %s. Awaiting reply", host)
response, err := http.ReadResponse(bufio.NewReader(conn), request)
if err != nil {
return nil, nil, err
}
var responseBody []byte
if response.Body != nil {
r := &io.LimitedReader{R: response.Body, N: payloadBytes + 1}
responseBody, err = ioutil.ReadAll(r)
}
return response, responseBody, err
}
func (hmp *HTTPMeetingPlace) Exchange(log func(string, ...interface{}), id, message []byte, shutdown chan struct{}) ([]byte, error) {
delay := 15 * time.Second
for {
response, body, err := hmp.attemptExchange(log, id, message)
if err != nil {
log("PANDA exchange failed. Will try again in %s: %s", delay, err)
goto Sleep
}
switch response.StatusCode {
case 409:
return nil, errors.New("The transaction failed because this key has been used recently by two other parties")
case 204:
log("PANDA exchange waiting for other party. Will try again in %s", delay)
goto Sleep
case 200:
if len(body) > payloadBytes {
return nil, errors.New("Reply from server is too large")
}
return body, nil
default:
log("Request resulted in unexpected HTTP status %d. Will try again in %s", response.StatusCode, delay)
}
Sleep:
select {
case <-shutdown:
return nil, ShutdownErr
case <-time.After(delay):
delay *= 2
if delay > time.Hour {
delay = time.Hour
}
}
}
panic("unreachable")
}