-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
146 lines (124 loc) · 2.79 KB
/
handlers.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
package main
import (
"bytes"
"context"
"io"
"log"
"net"
"runtime"
"sync"
)
func Accepter(ctx context.Context, l net.Listener) {
for {
// Listen for an incoming connection
select {
case <-ctx.Done():
l.Close()
return
default:
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Printf("incomming connection established: %s", conn.RemoteAddr().String())
go connectionHandler(ctx, conn)
}
}
}
func connectionHandler(ctx context.Context, conn net.Conn) {
ctx, cancel := context.WithCancel(ctx)
ErrCh := make(chan error)
responseOut := make(chan *[]byte, 1)
proxyRequest := make(chan *[]byte, 1)
ApiRequest := make(chan *[]byte, 1)
defer func() {
log.Printf("closing connection with %s\n", conn.RemoteAddr().String())
close(proxyRequest)
close(responseOut)
close(ApiRequest)
close(ErrCh)
cancel()
conn.Close()
}()
remote, err := net.Dial("tcp", *ForwardAddr)
if err != nil {
log.Printf("unable to establish remote connection: %v", err)
return
}
defer remote.Close()
log.Printf("Connected to remote host: %s\n", remote.RemoteAddr().String())
go SourceSenderWorker(ctx, responseOut, conn, ErrCh)
proxyResponse := ProxyWorker(ctx, proxyRequest, remote, ErrCh)
numWorkers := runtime.NumCPU()
results := make([]<-chan *[]byte, numWorkers)
for i := 0; i < numWorkers; i++ {
results[i] = APIWorker(ctx, ApiRequest, ErrCh)
}
ApiResponses := FanIn(ctx, results...)
quit := false
go func() {
defer func() {
cancel()
quit = true
}()
for {
select {
case err = <-ErrCh:
if err == io.EOF {
log.Printf("proxy worker: remote connection closed")
return
}
log.Printf("worker return error: %s", err)
return
case <-ctx.Done():
return
}
}
}()
for !quit {
buf, err := Read(conn)
if err != nil {
if err == io.EOF {
log.Println("remote connection closed")
cancel()
return
}
log.Printf("error reading: %s\n", err)
cancel()
return
}
//log.Printf("message received: %s\n", string(buf))
if idx := bytes.Index(buf, []byte("CSNQ")); idx < 0 {
log.Println("message is not CSNQ, passed through")
proxyRequest <- &buf
responseOut <- <-proxyResponse
} else {
log.Println("message is CSNQ, passed to API")
ApiRequest <- &buf
responseOut <- <-ApiResponses
}
}
}
func FanIn(ctx context.Context, channels ...<-chan *[]byte) <-chan *[]byte {
var wg sync.WaitGroup
multiplexStream := make(chan *[]byte)
multiplex := func(ctx context.Context, c <-chan *[]byte) {
defer wg.Done()
for msg := range c {
select {
case <-ctx.Done():
return
case multiplexStream <- msg:
}
}
}
wg.Add(len(channels))
for _, channel := range channels {
go multiplex(ctx, channel)
}
go func() {
wg.Wait()
close(multiplexStream)
}()
return multiplexStream
}