-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
215 lines (168 loc) · 4.18 KB
/
server.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
206
207
208
209
210
211
212
213
214
215
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/pkg/profile"
"io/ioutil"
"net"
"os"
"os/signal"
"syscall"
)
var local string
var nounsecure string
var localmtlsport string
var localport string
var localmtls string
var profileapp string
var remoteAddressandPort string
var byteread int = 1024 * 1024
var certdirpath string
func main() {
certdirpath = os.Getenv("certdirpath")
nounsecure = os.Getenv("nounsecure")
localport = os.Getenv("localport")
localmtlsport = os.Getenv("localmtlsport")
local = "localhost:" + localport
localmtls = "localhost:" + localmtlsport
remoteAddressandPort = os.Getenv("remote_addr_and_port")
profileapp = os.Getenv("profile")
if profileapp == "yes" {
defer profile.Start(profile.MemProfile).Stop()
}
if localmtlsport == "" && localport == "" {
panic("Nothing is set to listen, you need either a unsecure, secure or both port to listen on")
}
if localport == "" && nounsecure != "true" {
panic("Set the environment variable localport=<port>")
} else {
if nounsecure != "true" {
fmt.Println("unsecure connection is listening on : ", local)
}
}
if remoteAddressandPort == "" {
panic("Set the environment variable remote_addr_and_port=<remote addr>:<port>")
} else {
fmt.Println("backend is listening on : ", remoteAddressandPort)
}
// Use mTLS ?
if localmtlsport != "" {
fmt.Println("secure mTLS is listening on : ", localmtls)
if certdirpath == "" {
fmt.Println("certdirpath environment variable empty so using server.crt, server.key and ca.crt for certs")
} else {
fmt.Println("using certs " + certdirpath + "server.crt" + " " + certdirpath + "server.key" + " " + certdirpath + "ca.crt for mTLS")
}
rootPEM, err := ioutil.ReadFile(certdirpath + "ca.crt")
cert, err := tls.LoadX509KeyPair(certdirpath+"server.crt", certdirpath+"server.key")
if err != nil {
panic("Error loading server.crt, server.key or ca.crt")
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(string(rootPEM)))
if !ok {
panic("error with appending cert")
}
config := tls.Config{Certificates: []tls.Certificate{cert}, ClientAuth: tls.RequireAndVerifyClientCert, ClientCAs: roots}
listenermtls, err := tls.Listen("tcp", localmtls, &config)
if err != nil {
panic(err)
}
go func() {
for {
conn1, err := listenermtls.Accept()
if err != nil {
break
}
go mtlsproxyConnection(conn1)
}
}()
}
// Unsecure server
if nounsecure != "" {
fmt.Println("unsecure is not listening")
quitChannel := make(chan os.Signal, 1)
signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM)
<-quitChannel
fmt.Println("Adios!")
} else {
lAddr, err := net.ResolveTCPAddr("tcp", local)
if err != nil {
panic(err)
}
listener, err := net.ListenTCP("tcp", lAddr)
if err != nil {
panic(err)
}
for {
conn2, err := listener.AcceptTCP()
if err != nil {
break
}
go proxyConnection(conn2)
}
}
}
func mtlsproxyConnection(conn net.Conn) {
rAddr, err := net.ResolveTCPAddr("tcp", remoteAddressandPort)
if err != nil {
panic(err)
}
rConn, err := net.DialTCP("tcp", nil, rAddr)
if err != nil {
panic(err)
}
defer rConn.Close()
// Request loop
go func() {
for {
data := make([]byte, byteread)
n, err := conn.Read(data)
if err != nil {
break
}
rConn.Write(data[:n])
}
}()
// Response loop
for {
data := make([]byte, byteread)
n, err := rConn.Read(data)
if err != nil {
break
}
conn.Write(data[:n])
}
}
func proxyConnection(conn *net.TCPConn) {
rAddr, err := net.ResolveTCPAddr("tcp", remoteAddressandPort)
if err != nil {
panic(err)
}
rConn, err := net.DialTCP("tcp", nil, rAddr)
if err != nil {
panic(err)
}
defer rConn.Close()
// Request loop
go func() {
for {
data := make([]byte, byteread)
n, err := conn.Read(data)
if err != nil {
break
}
rConn.Write(data[:n])
}
}()
// Response loop
for {
data := make([]byte, byteread)
n, err := rConn.Read(data)
if err != nil {
break
}
conn.Write(data[:n])
}
}