-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
340 lines (293 loc) Β· 9.48 KB
/
main.go
File metadata and controls
340 lines (293 loc) Β· 9.48 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"github.com/minio/minio/pkg/env"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/minio/minio/cmd/rest"
"golang.org/x/net/http2"
)
// ParsePublicCertFile - parses public cert into its *x509.Certificate equivalent.
func ParsePublicCertFile(certFile string) (x509Certs []*x509.Certificate, err error) {
// Read certificate file.
var data []byte
if data, err = ioutil.ReadFile(certFile); err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
// Trimming leading and tailing white spaces.
data = bytes.TrimSpace(data)
// Parse all certs in the chain.
current := data
for len(current) > 0 {
var pemBlock *pem.Block
if pemBlock, current = pem.Decode(current); pemBlock == nil {
return nil, fmt.Errorf("could not read PEM block from file %s", certFile)
}
var x509Cert *x509.Certificate
if x509Cert, err = x509.ParseCertificate(pemBlock.Bytes); err != nil {
return nil, err
}
x509Certs = append(x509Certs, x509Cert)
}
if len(x509Certs) == 0 {
return nil, fmt.Errorf("empty public certificate file %s", certFile)
}
return x509Certs, nil
}
var (
targetEndpoint string
tlsKey string
tlsCrt string
//globalDNSCache *xhttp.DNSCache
)
func init() {
targetEndpoint = os.Getenv("TARGET_ENDPOINT")
flag.StringVar(&tlsCrt, "tls-certificate", "/certs/tls.crt", "TLS certificate")
flag.StringVar(&tlsKey, "tls-key", "/certs/tls.key", "cert key")
//globalDNSCache = xhttp.NewDNSCache(3*time.Second, 10*time.Second, nil)
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func newInternodeHTTPTransport(tlsConfig *tls.Config, dialTimeout time.Duration) func() *http.Transport {
// For more details about various values used here refer
// https://golang.org/pkg/net/http/#Transport documentation
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
//DialContext: xhttp.DialContextWithDNSCache(globalDNSCache, xhttp.NewInternodeDialContext(dialTimeout)),
MaxIdleConnsPerHost: 1024,
IdleConnTimeout: 15 * time.Second,
ResponseHeaderTimeout: 3 * time.Minute, // Set conservative timeouts for MinIO internode.
TLSHandshakeTimeout: 15 * time.Second,
ExpectContinueTimeout: 15 * time.Second,
TLSClientConfig: tlsConfig,
// Go net/http automatically unzip if content-type is
// gzip disable this feature, as we are always interested
// in raw stream.
DisableCompression: true,
}
if tlsConfig != nil {
http2.ConfigureTransport(tr)
}
return func() *http.Transport {
return tr
}
}
// Secure Go implementations of modern TLS ciphers
// The following ciphers are excluded because:
// - RC4 ciphers: RC4 is broken
// - 3DES ciphers: Because of the 64 bit blocksize of DES (Sweet32)
// - CBC-SHA256 ciphers: No countermeasures against Lucky13 timing attack
// - CBC-SHA ciphers: Legacy ciphers (SHA-1) and non-constant time
// implementation of CBC.
// (CBC-SHA ciphers can be enabled again if required)
// - RSA key exchange ciphers: Disabled because of dangerous PKCS1-v1.5 RSA
// padding scheme. See Bleichenbacher attacks.
var secureCipherSuites = []uint16{
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
}
// Go only provides constant-time implementations of Curve25519 and NIST P-256 curve.
var secureCurves = []tls.CurveID{tls.X25519, tls.CurveP256}
// dmt has support for multiple certificates. It expects the following structure:
//
// /etc/dmt/tls/
// ββ example.com/
// β β
// β ββ public.crt
// β ββ private.key
// ββ foobar.org/
// β
// ββ public.crt
// ββ private.key
// ...
//
// Therefore, we read all filenames in the cert directory and check
// for each directory whether it contains a public.crt and private.key.
// If so, we try to add it to certs in *http.Server* config.
// NOTE: Directories just need to be named there is no requirement
// on the right name or domain related to the certs.
func loadTLSCerts(dirname string) ([]tls.Certificate, error) {
dirs, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, err
}
var certs []tls.Certificate
for _, dir := range dirs {
// Regular file types are all ignored.
if dir.IsDir() {
cert, err := tls.LoadX509KeyPair(filepath.Join(dirname, dir.Name(), "public.crt"),
filepath.Join(dirname, dir.Name(), "private.key"))
if err != nil {
return nil, err
}
certs = append(certs, cert)
}
}
return certs, nil
}
func main() {
flag.Parse()
//defer globalDNSCache.Stop()
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
// In some systems (like Windows) system cert pool is
// not supported or no certificates are present on the
// system - so we create a new cert pool.
rootCAs = x509.NewCertPool()
}
//// Add the global public crts as part of global root CAs
//for _, publicCrt := range caCerts {
// rootCAs.AddCert(publicCrt)
//}
transport := newInternodeHTTPTransport(&tls.Config{
RootCAs: rootCAs,
NextProtos: []string{"h2", "http/1.1"},
// TLS hardening
MinVersion: tls.VersionTLS12,
CipherSuites: secureCipherSuites,
CurvePreferences: secureCurves,
PreferServerCipherSuites: true,
InsecureSkipVerify: true,
}, rest.DefaultTimeout)()
fakeOrigin := os.Getenv("FAKE_ORIGIN")
log.Printf("Fake Origin: %s\n", fakeOrigin)
r := mux.NewRouter().PathPrefix("/")
r.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
director := func(r *http.Request) {
log.Println("############")
log.Printf("%+v", r.Header)
log.Println("ORIGINAL destination")
log.Println("r.URL", r.URL)
log.Println("r.URL.Host", r.URL.Host)
log.Println("r.Host", r.Host)
log.Println("replaced")
log.Println("############")
r.Host = targetEndpoint
r.Header.Add("X-Forwarded-Host", r.Host)
r.Header.Add("X-Real-IP", r.RemoteAddr)
r.Header.Del("Origin")
r.Header.Add("Origin", fakeOrigin)
//if secureBackend {
// r.URL.Scheme = "https"
//} else {
// r.URL.Scheme = "http"
//}
r.URL.Scheme = "https"
r.URL.Host = targetEndpoint
log.Println("-----------")
log.Printf("%+v", r.Header)
log.Println("new routed destination")
log.Println("targetEndpoint", targetEndpoint)
log.Println("r.URL", r.URL)
log.Println("r.URL.Host", r.URL.Host)
log.Println("r.Host", r.Host)
log.Println("r.RemoteAddr", r.RemoteAddr)
log.Println("replaced")
log.Println("-----------")
}
respFunc := func(response *http.Response) error {
if len(response.Cookies()) > 0 {
var cookieJar []*http.Cookie
//cookiiee := response.Cookies()[0]
for _, cookiiee := range response.Cookies() {
//if cookiiee.Name == "authToken" {
// log.Println("-----------------Response--------")
// cooka := response.Header.Get("Set-Cookie")
// log.Println("")
// log.Println(cooka)
// log.Println("")
// cookiiee.Secure = false
// cookiiee.SameSite = http.SameSiteDefaultMode
//
// cookieJar = append(cookieJar, cookiiee)
//}
//if cookiiee.Name == "refreshToken" {
// log.Println("-----------------Response--------")
// cooka := response.Header.Get("Set-Cookie")
// log.Println("")
// log.Println(cooka)
// log.Println("")
// cookiiee.Secure = false
// cookiiee.SameSite = http.SameSiteDefaultMode
//
// cookieJar = append(cookieJar, cookiiee)
//}
//if cookiiee.Name == "vid" {
// log.Println("-----------------Response--------")
// cooka := response.Header.Get("Set-Cookie")
// log.Println("")
// log.Println(cooka)
// log.Println("")
// cookiiee.Secure = false
// cookiiee.SameSite = http.SameSiteDefaultMode
//
// cookieJar = append(cookieJar, cookiiee)
//}
log.Println("-----------------Response--------")
cooka := response.Header.Get("Set-Cookie")
log.Println("")
log.Println(cooka)
log.Println("")
cookiiee.Secure = false
cookiiee.SameSite = http.SameSiteDefaultMode
cookieJar = append(cookieJar, cookiiee)
}
response.Header.Del("Set-Cookie")
for _, cookiiee := range cookieJar {
response.Header.Add("Set-Cookie", cookiiee.String())
}
log.Printf("%+v", response.Header)
log.Println("")
}
return nil
}
proxy := &httputil.ReverseProxy{
Director: director,
Transport: transport,
ModifyResponse: respFunc,
}
proxy.ServeHTTP(w, r)
})
port := env.Get("PROXY_PORT", "4233")
loggedRouter := handlers.CombinedLoggingHandler(os.Stdout, r.GetHandler())
s := &http.Server{
Handler: loggedRouter,
Addr: fmt.Sprintf(":%s", port),
MaxHeaderBytes: 1 << 20,
TLSConfig: &tls.Config{
// TLS hardening
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
NextProtos: []string{"h2", "http/1.1"},
//Certificates: certs,
CipherSuites: secureCipherSuites,
CurvePreferences: secureCurves,
InsecureSkipVerify: true,
},
}
if tlsCrt != "" && tlsKey != "" {
log.Println("Serving TLS Server")
log.Fatalln(s.ListenAndServeTLS(tlsCrt, tlsKey))
} else {
log.Println("Serving Plain Server")
log.Fatalln(s.ListenAndServe())
}
}