-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
191 lines (138 loc) · 4.06 KB
/
proxy.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
package p2p
import (
"net"
"bufio"
"fmt"
"log"
"bytes"
"encoding/hex"
"github.com/tokenbankteam/eos-go"
)
type actionType int
const (
addRoute actionType = iota
)
type routeAction struct {
actionType actionType
route *Route
}
type routeCommunication struct {
Route *Route `json:"route"`
DestinationConnection net.Conn
Envelope *eos.P2PMessageEnvelope
}
var routerActionChannel = make(chan routeAction)
func (p *Proxy) handleRouteAction(channel chan routeAction) {
for routeAction := range channel {
//todo : handle action type
go p.startForwarding(routeAction.route)
}
}
var transmissionChannel = make(chan routeCommunication)
func (p *Proxy) handleTransmission(channel chan routeCommunication) {
for communication := range channel {
//_, err := communication.DestinationConnection.Write(communication.Envelope.Payload)
buf := new(bytes.Buffer)
encoder := eos.NewEncoder(buf)
err := encoder.Encode(communication.Envelope)
if err != nil {
fmt.Println("Sender encode error: ", err)
}
fmt.Println("Data to send: ", hex.EncodeToString(buf.Bytes()))
_, err = communication.DestinationConnection.Write(buf.Bytes())
if err != nil {
fmt.Println("Sender comm error: ", err)
}
}
}
var router = make(chan routeCommunication)
var routingChannels []chan routeCommunication
func (p *Proxy) handleRouting(routingChannel chan routeCommunication) {
for communication := range routingChannel {
for _, channel := range routingChannels {
channel <- communication
}
}
}
var postProcessChannel = make(chan routeCommunication)
func (p *Proxy) handlePostProcess(postProcessChannel chan routeCommunication, postProcessorChannels []chan Message) {
for communication := range postProcessChannel {
pp := Message{
Route: communication.Route,
Envelope: communication.Envelope,
}
for _, c := range postProcessorChannels {
c <- pp
}
}
}
func (p *Proxy) handlePluginPostProcess(handle Handler, channel chan Message) {
for postProcessable := range channel {
handle.Handle(postProcessable)
}
}
func (p *Proxy) startForwarding(route *Route) {
fmt.Printf("Starting forwarding [%s] -> [%s] \n", route.From, route.To)
ln, err := net.Listen("tcp", route.From)
if err != nil {
fmt.Println("error: ", err)
return
}
for {
fmt.Printf("Accepting connection on port [%s]\n", route.From)
fromConn, err := ln.Accept()
if err != nil {
fmt.Println("error: ", err)
}
fmt.Printf("Connection on port [%s]\n", route.From)
toConn, err := net.Dial("tcp", route.To)
if err != nil {
fmt.Println("error: ", err)
fromConn.Close()
} else {
fmt.Println("Connected to: ", route.To)
go p.handleConnection(fromConn, toConn, route)
go p.handleConnection(toConn, fromConn, &Route{From: route.To, To: route.From})
}
}
}
func (p *Proxy) handleConnection(connection net.Conn, forwardConnection net.Conn, route *Route) (err error) {
r := bufio.NewReader(connection)
for {
envelope, err := eos.ReadP2PMessageData(r)
if err != nil {
fmt.Printf("Connection error from [%s] to [%s] : %s\n ", route.From, route.To, err)
forwardConnection.Close()
log.Fatal("Handle connection, ", err)
}
router <- routeCommunication{
Route: route,
DestinationConnection: forwardConnection,
Envelope: envelope,
}
}
}
type Proxy struct {
Routes []*Route
Handlers []Handler
}
func (p *Proxy) Start() {
done := make(chan bool)
var postProcessorChannels []chan Message
for _, plugin := range p.Handlers {
pc := make(chan Message)
postProcessorChannels = append(postProcessorChannels, pc)
go p.handlePluginPostProcess(plugin, pc)
}
routingChannels = []chan routeCommunication{transmissionChannel, postProcessChannel}
go p.handleRouteAction(routerActionChannel)
go p.handleRouting(router)
go p.handleTransmission(transmissionChannel)
go p.handlePostProcess(postProcessChannel, postProcessorChannels)
for _, route := range p.Routes {
routerActionChannel <- routeAction{actionType: addRoute, route: route}
}
fmt.Println("Proxy started")
<-done
fmt.Println("Proxy will stop")
}