-
Notifications
You must be signed in to change notification settings - Fork 10
/
protocol.go
62 lines (49 loc) · 1.04 KB
/
protocol.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
package protocol
import (
"encoding/gob"
"fmt"
"io"
)
// TODO: error handling
type proxyInfo struct {
ServedAt string
ConnectTo string
}
func send(c io.ReadWriteCloser, d interface{}) {
enc := gob.NewEncoder(c)
enc.Encode(d)
}
func receive(c io.ReadWriteCloser, d interface{}) {
dec := gob.NewDecoder(c)
dec.Decode(d)
}
func SendProxyInfo(c io.ReadWriteCloser, at, to string) {
send(c, &proxyInfo{ServedAt: at, ConnectTo: to})
}
func ReceiveProxyInfo(c io.ReadWriteCloser) (at, to string, err error) {
var p proxyInfo
receive(c, &p)
return p.ServedAt, p.ConnectTo, nil
}
type hostRequest struct {
Host string
}
func SendSubRequest(c io.ReadWriteCloser, h string) {
send(c, &hostRequest{Host: h})
}
func ReceiveSubRequest(c io.ReadWriteCloser) string {
var h hostRequest
receive(c, &h)
return h.Host
}
func SendConnRequest(c io.ReadWriteCloser) {
fmt.Fprintf(c, "new")
}
func ReceiveConnRequest(c io.ReadWriteCloser) bool {
var buf [len("new")]byte
c.Read(buf[0:])
if string(buf[0:]) == "new" {
return true
}
return true
}