Skip to content

Commit b38e822

Browse files
committed
Based on @fhdumay suggestion, supporting multiple shadow sides
1 parent 014222d commit b38e822

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

cmd/simple-tee-proxy.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"log"
67
"net"
78
)
89

10+
type multipleHosts []string
11+
12+
func (i *multipleHosts) String() string {
13+
return fmt.Sprintf("%s", *i)
14+
}
15+
16+
func (i *multipleHosts) Set(value string) error {
17+
*i = append(*i, value)
18+
return nil
19+
}
20+
921
func main() {
1022
var hostingSide string
1123
var forwardSide string
12-
var shadowSide string
24+
var shadowSide multipleHosts
1325

1426
flag.StringVar(&hostingSide, "hosting", "0.0.0.0:7700", "What the external address is of the proxy")
1527
flag.StringVar(&forwardSide, "forward", "localhost:8800", "Where to forward all tcp connections to")
16-
flag.StringVar(&shadowSide, "shadow", "localhost:9900", "Where to send a copy of all connections to, the replies will be ignored, and the proxy will continue working regardless of the shadow side of things")
28+
flag.Var(&shadowSide, "shadow", "Where to send a copy of all connections to, the replies will be ignored, and the proxy will continue working regardless of the shadow side of things")
1729

1830
flag.Parse()
1931

@@ -32,11 +44,14 @@ func main() {
3244
}
3345
}
3446

35-
func forward(source net.Conn, forwardSide, shadowSide string) {
47+
func forward(source net.Conn, forwardSide string, shadowSide []string) {
3648
defer source.Close()
3749

3850
forwardQueue := make(chan []byte, 512)
39-
shadowQueue := make(chan []byte, 512)
51+
shadowQueues := make([]chan []byte, len(shadowSide))
52+
for s := range shadowQueues {
53+
shadowQueues[s] = make(chan []byte, 512)
54+
}
4055

4156
go func() {
4257
for {
@@ -45,12 +60,16 @@ func forward(source net.Conn, forwardSide, shadowSide string) {
4560
read, err := source.Read(buffer)
4661
if err != nil {
4762
close(forwardQueue)
48-
close(shadowQueue)
63+
for _, q := range shadowQueues {
64+
close(q)
65+
}
4966
return
5067
}
5168
if read > 0 {
5269
forwardQueue <- buffer[:read]
53-
shadowQueue <- buffer[:read]
70+
for _, q := range shadowQueues {
71+
q <- buffer[:read]
72+
}
5473
}
5574
}
5675

@@ -59,7 +78,10 @@ func forward(source net.Conn, forwardSide, shadowSide string) {
5978
replyQueue := make(chan []byte, 512)
6079

6180
go connectBackend(forwardSide, forwardQueue, replyQueue)
62-
go connectBackend(shadowSide, shadowQueue, nil)
81+
82+
for i := range shadowQueues {
83+
go connectBackend(shadowSide[i], shadowQueues[i], nil)
84+
}
6385

6486
for r := range replyQueue {
6587
source.Write(r)

0 commit comments

Comments
 (0)