/
server.go
251 lines (229 loc) · 6.83 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
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
package server
import (
"crypto/tls"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-cli/utils/tests/proxy/server/certificate"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
clilog "github.com/jfrog/jfrog-client-go/utils/log"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
)
type httpResponse func(rw http.ResponseWriter, req *http.Request)
func handleReverseProxyHttps(reverseProxy *httputil.ReverseProxy) httpResponse {
return func(rw http.ResponseWriter, req *http.Request) {
clilog.Info("*********************************************************")
clilog.Info("Scheme: ", "HTTPS")
clilog.Info("Host: ", req.Host)
clilog.Info("Method: ", req.Method)
clilog.Info("URI: ", req.RequestURI)
clilog.Info("Agent: ", req.UserAgent())
clilog.Info("*********************************************************")
reverseProxy.ServeHTTP(rw, req)
}
}
func getReverseProxyHandler(targetUrl string) (*httputil.ReverseProxy, error) {
clilog.Info("Reverse proxy URL:", targetUrl)
var err error
var target *url.URL
target, err = url.Parse(targetUrl)
if err != nil {
return nil, err
}
origHost := target.Host
d := func(req *http.Request) {
req.URL.Host = origHost
req.Host = origHost
req.URL.Scheme = target.Scheme
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
proxyErrLogger := log.New(os.Stdout, "PROXY-LOGGER", log.Ldate|log.Ltime|log.Lshortfile)
p := &httputil.ReverseProxy{Director: d, Transport: tr, ErrorLog: proxyErrLogger}
return p, nil
}
func removeProxyHeaders(r *http.Request) {
r.RequestURI = "" // this must be reset when serving a request with the client
// If no Accept-Encoding header exists, Transport will add the headers it can accept
// and would wrap the response body with the relevant reader.
r.Header.Del("Accept-Encoding")
// curl can add that, see
// https://jdebp.eu./FGA/web-proxy-connection-header.html
r.Header.Del("Proxy-Connection")
r.Header.Del("Proxy-Authenticate")
r.Header.Del("Proxy-Authorization")
// Connection, Authenticate and Authorization are single hop Header:
// http://www.w3.org/Protocols/rfc2616/rfc2616.txt
// 14.10 Connection
// The Connection general-header field allows the sender to specify
// options that are desired for that particular connection and MUST NOT
// be communicated by proxies over further connections.
r.Header.Del("Connection")
}
func copyHeaders(dst, src http.Header) {
for k := range dst {
dst.Del(k)
}
for k, vs := range src {
for _, v := range vs {
dst.Add(k, v)
}
}
}
func httpProxyHandler(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/" {
w.WriteHeader(200)
} else {
removeProxyHeaders(r)
t := &http.Transport{}
resp, err := t.RoundTrip(r)
if err != nil {
clilog.Error(err)
}
origBody := resp.Body
defer origBody.Close()
copyHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
if err := resp.Body.Close(); err != nil {
clilog.Error("Can't close response body %v", err)
}
}
}
type testProxy struct {
}
func (t *testProxy) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
if request.RequestURI == "/" {
responseWriter.WriteHeader(200)
} else {
host := request.URL.Host
request.Host = "https://" + request.Host
targetSiteCon, err := net.Dial("tcp", host)
if err != nil {
clilog.Error(err)
return
}
hij, ok := responseWriter.(http.Hijacker)
if !ok {
http.Error(responseWriter, "webserver doesn't support hijacking", http.StatusInternalServerError)
return
}
proxyClient, _, err := hij.Hijack()
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}
proxyClient.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
targetTCP, targetOK := targetSiteCon.(*net.TCPConn)
proxyClientTCP, clientOK := proxyClient.(*net.TCPConn)
if targetOK && clientOK {
go copyAndClose(targetTCP, proxyClientTCP)
go copyAndClose(proxyClientTCP, targetTCP)
}
}
}
func copyAndClose(dst, src *net.TCPConn) {
if _, err := io.Copy(dst, src); err != nil {
clilog.Error(err)
}
dst.CloseWrite()
src.CloseRead()
}
func GetProxyHttpPort() string {
port := "8099"
if httpPort := os.Getenv("PROXY_HTTP_PORT"); httpPort != "" {
port = httpPort
}
return port
}
func prepareHTTPSHandling(handler *httputil.ReverseProxy) (httpsMux *http.ServeMux, absPathCert, absPathKey string, err error) {
// We can use the same handler for both HTTP and HTTPS
httpsMux = http.NewServeMux()
httpsMux.HandleFunc("/", handleReverseProxyHttps(handler))
absPathCert, absPathKey, err = CreateNewServerCertificates()
return
}
// Creates a server cerf file and cert key file.
// Returns the absolute path of these two files.
func CreateNewServerCertificates() (certFilePath, keyCertFilePath string, err error) {
certFilePath, err = filepath.Abs(certificate.CERT_FILE)
if errorutils.CheckError(err) != nil {
return
}
keyCertFilePath, err = filepath.Abs(certificate.KEY_FILE)
if errorutils.CheckError(err) != nil {
return
}
if _, err = os.Stat(certFilePath); os.IsNotExist(err) {
err = certificate.CreateNewCert(certFilePath, keyCertFilePath)
if err != nil {
return
}
}
errorutils.CheckError(err)
return
}
func GetProxyHttpsPort() string {
port := "1024"
if httpPort := os.Getenv(tests.HttpsProxyEnvVar); httpPort != "" {
port = httpPort
}
return port
}
func startHttpsReverseProxy(proxyTarget string, requestClientCerts bool) error {
handler, err := getReverseProxyHandler(proxyTarget)
if err != nil {
return err
}
// Starts a new Go routine
httpsMux, absPathCert, absPathKey, err := prepareHTTPSHandling(handler)
if err != nil {
return err
}
if requestClientCerts {
server := &http.Server{
Addr: ":" + GetProxyHttpsPort(),
Handler: httpsMux,
TLSConfig: &tls.Config{
ClientAuth: tls.RequireAnyClientCert,
},
}
err = server.ListenAndServeTLS(absPathCert, absPathKey)
} else {
err = http.ListenAndServeTLS(":"+GetProxyHttpsPort(), absPathCert, absPathKey, httpsMux)
}
return err
}
func StartLocalReverseHttpProxy(artifactoryUrl string, requestClientCerts bool) {
if artifactoryUrl == "" {
artifactoryUrl = "http://localhost:8081/artifactory/"
}
artifactoryUrl = utils.AddTrailingSlashIfNeeded(artifactoryUrl)
err := startHttpsReverseProxy(artifactoryUrl, requestClientCerts)
// Since this function is always executed in its own go routine,
// we panic when an error occurs.
panic(err)
}
func StartHttpProxy() {
httpMux := http.NewServeMux()
httpMux.HandleFunc("/", httpProxyHandler)
port := GetProxyHttpPort()
err := http.ListenAndServe(":"+port, httpMux)
if err != nil {
panic(err)
}
}
func StartHttpsProxy() {
port := GetProxyHttpsPort()
err := http.ListenAndServe(":"+port, &testProxy{})
if err != nil {
panic(err)
}
}