-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimprovised.go
executable file
·151 lines (121 loc) · 2.84 KB
/
improvised.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
package main
import (
"context"
"errors"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/jessevdk/go-flags"
"github.com/pires/go-proxyproto"
"io"
"log"
"math/rand"
"net"
"os"
"strconv"
"time"
)
var ctx = context.Background()
var opts struct {
RedisAddress string `long:"redisAddress" description:"Redis address" required:"true"`
RedisUsername string `long:"redisUsername" description:"Redis username"`
RedisPassword string `long:"redisPassword" description:"Redis password"`
RedisDB int `long:"redisDB" description:"Redis db number" default:"0"`
LoadBalancerPort int `short:"p" long:"port" description:"The port where improvised will run" default:"8888"`
}
func main() {
log.Println("Starting Improvised !")
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
port := opts.LoadBalancerPort
listener, err := net.Listen("tcp", ":"+strconv.Itoa(port))
checkError(err)
log.Printf("Listening to " + strconv.Itoa(port))
source := Redis{
Options: &redis.Options{
Addr: opts.RedisAddress,
Username: opts.RedisUsername,
Password: opts.RedisPassword,
DB: opts.RedisDB,
},
}
err = source.init()
if err != nil {
panic(err)
}
rand.Seed(time.Now().Unix())
if err != nil {
panic("connection error:" + err.Error())
}
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Accept Error:", err)
continue
}
dstServer := source.getRandom()
if dstServer == nil {
closeConnection(conn)
continue
}
dst, err := net.ResolveTCPAddr("tcp", *dstServer)
if err != nil {
fmt.Println("Resolve Error:", err)
continue
}
copyConn(conn, dst)
}
}
func copyConn(src net.Conn, dstAddr *net.TCPAddr) {
dst, err := net.DialTCP("tcp", nil, dstAddr)
if err != nil {
panic("Dial Error:" + err.Error())
}
srcRemoteAddr := src.RemoteAddr().(*net.TCPAddr)
destRemoteAddr := dst.RemoteAddr().(*net.TCPAddr)
header := &proxyproto.Header{
Version: 2,
Command: proxyproto.PROXY,
TransportProtocol: proxyproto.TCPv4,
SourceAddr: srcRemoteAddr,
DestinationAddr: destRemoteAddr,
}
_, err = header.WriteTo(dst)
checkError(err)
done := make(chan struct{})
go func() {
defer closeConnection(dst)
defer closeConnection(src)
_, _ = io.Copy(dst, src)
done <- struct{}{}
}()
go func() {
defer closeConnection(dst)
defer closeConnection(src)
_, _ = io.Copy(src, dst)
done <- struct{}{}
}()
<-done
<-done
}
var errNetClosing = errors.New("use of closed network connection")
func isErrNetClosing(err error) bool {
if e, ok := err.(*net.OpError); ok {
return e.Err.Error() == errNetClosing.Error()
}
return false
}
func closeConnection(conn net.Conn) {
err := conn.Close()
if err != nil {
if !isErrNetClosing(err) {
fmt.Println(err)
}
}
}
func checkError(err error) {
if err != nil {
fmt.Println(err)
}
}