/
tranx-client.go
199 lines (152 loc) 路 4.47 KB
/
tranx-client.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package receiver
import (
"fmt"
"net"
"time"
"context"
"github.com/schollz/pake/v3"
"github.com/abdfnx/tran/tools"
"github.com/gorilla/websocket"
"github.com/abdfnx/tran/models"
"github.com/abdfnx/tran/core/crypt"
"github.com/abdfnx/tran/models/protocol"
)
func (r *Receiver) ConnectToTranx(tranxAddress string, tranxPort int, password models.Password) (*websocket.Conn, error) {
// establish websocket connection to tranx server
tranxConn, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://%s:%d/establish-receiver", tranxAddress, tranxPort), nil)
if err != nil {
return nil, err
}
err = r.establishSecureConnection(tranxConn, password)
if err != nil {
return nil, err
}
senderIP, senderPort, err := r.doTransferHandshake(tranxConn)
if err != nil {
return nil, err
}
directConn, err := r.probeSender(senderIP, senderPort)
if err == nil {
// notify sender through tranx that we will be using direct communication
tools.WriteEncryptedMessage(tranxConn, protocol.TransferMessage{Type: protocol.ReceiverDirectCommunication}, r.crypt)
// tell tranx to close the connection
tranxConn.WriteJSON(protocol.TranxMessage{Type: protocol.ReceiverToTranxClose})
return directConn, nil
}
r.usedRelay = true
tools.WriteEncryptedMessage(tranxConn, protocol.TransferMessage{Type: protocol.ReceiverRelayCommunication}, r.crypt)
transferMsg, err := tools.ReadEncryptedMessage(tranxConn, r.crypt)
if err != nil {
return nil, err
}
if transferMsg.Type != protocol.SenderRelayAck {
return nil, err
}
return tranxConn, nil
}
func (r *Receiver) probeSender(senderIP net.IP, senderPort int) (*websocket.Conn, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
d := 250 * time.Millisecond
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("could not establish a connection to the sender server")
default:
dialer := websocket.Dialer{HandshakeTimeout: d}
wsConn, _, err := dialer.Dial(fmt.Sprintf("ws://%s:%d/tran", senderIP.String(), senderPort), nil)
if err != nil {
time.Sleep(d)
d = d * 2
continue
}
return wsConn, nil
}
}
}
func (r *Receiver) doTransferHandshake(wsConn *websocket.Conn) (net.IP, int, error) {
tcpAddr, _ := wsConn.LocalAddr().(*net.TCPAddr)
msg := protocol.TransferMessage{
Type: protocol.ReceiverHandshake,
Payload: protocol.ReceiverHandshakePayload{
IP: tcpAddr.IP,
},
}
err := tools.WriteEncryptedMessage(wsConn, msg, r.crypt)
if err != nil {
return nil, 0, err
}
msg, err = tools.ReadEncryptedMessage(wsConn, r.crypt)
if err != nil {
return nil, 0, err
}
if msg.Type != protocol.SenderHandshake {
return nil, 0, protocol.NewWrongMessageTypeError([]protocol.TransferMessageType{protocol.SenderHandshake}, msg.Type)
}
handshakePayload := protocol.SenderHandshakePayload{}
err = tools.DecodePayload(msg.Payload, &handshakePayload)
if err != nil {
return nil, 0, err
}
r.payloadSize = handshakePayload.PayloadSize
return handshakePayload.IP, handshakePayload.Port, nil
}
func (r *Receiver) establishSecureConnection(wsConn *websocket.Conn, password models.Password) error {
// init curve in background
pakeCh := make(chan *pake.Pake)
pakeErr := make(chan error)
go func() {
var err error
p, err := pake.InitCurve([]byte(password), 1, "p256")
pakeErr <- err
pakeCh <- p
}()
wsConn.WriteJSON(protocol.TranxMessage{
Type: protocol.ReceiverToTranxEstablish,
Payload: protocol.PasswordPayload{
Password: tools.HashPassword(password),
},
})
msg, err := tools.ReadTranxMessage(wsConn, protocol.TranxToReceiverPAKE)
if err != nil {
return err
}
pakePayload := protocol.PakePayload{}
err = tools.DecodePayload(msg.Payload, &pakePayload)
if err != nil {
return err
}
// check if we had an issue with the PAKE2 initialization error
if err = <-pakeErr; err != nil {
return err
}
p := <-pakeCh
err = p.Update(pakePayload.Bytes)
if err != nil {
return err
}
wsConn.WriteJSON(protocol.TranxMessage{
Type: protocol.ReceiverToTranxPAKE,
Payload: protocol.PakePayload{
Bytes: p.Bytes(),
},
})
msg, err = tools.ReadTranxMessage(wsConn, protocol.TranxToReceiverSalt)
if err != nil {
return err
}
saltPayload := protocol.SaltPayload{}
err = tools.DecodePayload(msg.Payload, &saltPayload)
if err != nil {
return err
}
sessionKey, err := p.SessionKey()
if err != nil {
return err
}
r.crypt, err = crypt.New(sessionKey, saltPayload.Salt)
if err != nil {
return err
}
return nil
}