This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
205 lines (180 loc) · 5.68 KB
/
handle.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
200
201
202
203
204
205
package dione
import (
ctx "context"
"crypto/aes"
"crypto/cipher"
"errors"
"fmt"
"github.com/cloudflare/circl/kem/kyber/kyber1024"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"io"
)
type Handler struct {
h host.Host
dhtInput chan<- GeneralCommand
}
func forwardStream(target network.Stream, sourceReader io.Reader, sourceWriter io.Writer) {
targetReader := io.Reader(target)
targetWriter := io.Writer(target)
go forwardWriter(targetReader, sourceWriter)
forwardWriter(sourceReader, targetWriter)
}
func forwardWriter(reader io.Reader, writer io.Writer) {
for {
buffer := make([]byte, network.MessageSizeMax)
n, err := reader.Read(buffer)
if err != nil {
if err == io.EOF || err.Error() == "stream reset" {
return
}
fmt.Printf("Comparing to %#v\n", errors.New("stream reset"))
fmt.Printf("Error from forward Writer %#v\n", err)
panic(err)
}
n, err = writer.Write(buffer[:n])
if err != nil {
panic(err)
}
}
}
func (h *Handler) readMessage(stream io.ReadWriter, m protoreflect.ProtoMessage) error {
readBuffer := make([]byte, network.MessageSizeMax)
// Read message from stream
n, err := stream.Read(readBuffer)
if err != nil {
fmt.Println("Error reading message from stream:", err)
return err
}
err = proto.Unmarshal(readBuffer[:n], m)
if err != nil {
fmt.Println("Error unmarshalling message:", err)
return err
}
return nil
}
func (h *Handler) writeMessage(stream io.ReadWriter, m protoreflect.ProtoMessage) {
// Write Message to Stream
data, err := proto.Marshal(m)
if err != nil {
panic(err)
}
_, err = stream.Write(data)
if err != nil {
panic(err)
}
}
func (h *Handler) handleStream(stream network.Stream) {
fmt.Println("Got a new stream!")
for {
m := new(GeneralRequest)
err := h.readMessage(stream, m)
// fmt.Printf("Got Message %v\n", m)
if err != nil {
if err == io.EOF || err == errors.New("stream reset") {
return
}
fmt.Printf("Got Error during handling of request from %v -> %v\n", stream.Conn().RemotePeer(), err)
return
}
if m.GetClosestProviderRequest() != nil {
clPrvReq := m.GetClosestProviderRequest()
general := new(GeneralCommand)
closest, response := newClosestCommand(clPrvReq.Key)
general.closest = &closest
h.dhtInput <- *general
closestPeers := <-response
responseStrings := make([]string, 0)
for _, closestPeer := range closestPeers {
peerString := peer.Encode(closestPeer)
responseStrings = append(responseStrings, peerString)
}
generalResp := new(GeneralResponse)
generalClosestResp := new(GeneralResponse_ClosestProviderResponse)
closestResp := new(ClosestProviderResponse)
closestResp.Provider = responseStrings
generalClosestResp.ClosestProviderResponse = closestResp
generalResp.GeneralResponseKind = generalClosestResp
fmt.Println("Formulated response")
h.writeMessage(stream, generalResp)
fmt.Println("Wrote Message")
} else if m.GetInitTunnelRequest() != nil {
initTnlReq := m.GetInitTunnelRequest()
targetId, err := peer.Decode(initTnlReq.Target)
if err != nil {
panic(err)
}
pk := kyber1024.PublicKey{}
pk.Unpack(initTnlReq.Pk)
ct, ss := make([]byte, kyber1024.CiphertextSize), make([]byte, kyber1024.SharedKeySize)
pk.EncapsulateTo(ct, ss, nil)
fmt.Println("Getting read to connect to target")
targetStream, err := h.h.NewStream(ctx.TODO(), targetId, requestId)
if err != nil {
panic(err)
}
generalResp := new(GeneralResponse)
generalTunnelResp := new(GeneralResponse_InitTunnelResponse)
generalTunnel := new(InitTunnelResponse)
generalTunnel.Ciphertext = ct
generalTunnel.Status = ConnectionStatus_SUCCESS
generalTunnelResp.InitTunnelResponse = generalTunnel
generalResp.GeneralResponseKind = generalTunnelResp
h.writeMessage(stream, generalResp)
block, err := aes.NewCipher(ss)
if err != nil {
panic(err)
}
var iv [aes.BlockSize]byte
enc := cipher.NewOFB(block, iv[:])
encReader := &cipher.StreamReader{S: enc, R: stream}
encWriter := &cipher.StreamWriter{S: enc, W: stream}
/*
plaintext := []byte("Hello World")
_, err = encWriter.Write(plaintext)
if err != nil {
panic(err)
}
*/
fmt.Println("Forwarding stream")
forwardStream(targetStream, encReader, encWriter)
fmt.Println("Done with this")
} else if m.GetPutKadRequest() != nil {
putKadReq := m.GetPutKadRequest()
key := putKadReq.Key
value := putKadReq.Value
general := new(GeneralCommand)
putCommand := newPutCommand(key, value)
general.put = &putCommand
h.dhtInput <- *general
generalResponse := new(GeneralResponse)
generalPutResponse := new(GeneralResponse_PutKadResponse)
putResponse := new(PutKadResponse)
putResponse.Status = ConnectionStatus_SUCCESS
generalPutResponse.PutKadResponse = putResponse
generalResponse.GeneralResponseKind = generalPutResponse
h.writeMessage(stream, generalResponse)
} else if m.GetGetKadRequest() != nil {
getKadReq := m.GetGetKadRequest()
key := getKadReq.Key
general := new(GeneralCommand)
getCommand, response := newGetCommand(key)
general.get = &getCommand
h.dhtInput <- *general
value := <-response
fmt.Println("Got result from Kad")
generalResponse := new(GeneralResponse)
generalGetResponse := new(GeneralResponse_GetKadResponse)
getResponse := new(GetKadResponse)
getResponse.Value = value
generalGetResponse.GetKadResponse = getResponse
generalResponse.GeneralResponseKind = generalGetResponse
h.writeMessage(stream, generalResponse)
} else {
fmt.Println(m.String())
}
}
}