forked from antoniomika/sish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
263 lines (222 loc) · 6.65 KB
/
http.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
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"path/filepath"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/koding/websocketproxy"
"github.com/gin-gonic/gin"
)
// ProxyHolder holds proxy and connection info
type ProxyHolder struct {
ProxyHost string
ProxyTo string
Scheme string
Weight uint // weight of a single holder for lb.
SSHConn *SSHConnection
}
func startHTTPHandler(state *State) {
releaseMode := gin.ReleaseMode
if *debug {
releaseMode = gin.DebugMode
}
gin.SetMode(releaseMode)
gin.DefaultWriter = log.Writer()
gin.ForceConsoleColor()
r := gin.New()
r.LoadHTMLGlob(filepath.Join(*configDir, "templates/*"))
r.Use(func(c *gin.Context) {
c.Set("startTime", time.Now())
SetSeed(c) // save seed to the context
clientIPAddr, _, err := net.SplitHostPort(c.Request.RemoteAddr)
if state.IPFilter.Blocked(c.ClientIP()) || state.IPFilter.Blocked(clientIPAddr) || err != nil {
c.AbortWithStatus(http.StatusForbidden)
return
}
c.Next()
}, gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
var statusColor, methodColor, resetColor string
if param.IsOutputColor() {
statusColor = param.StatusCodeColor()
methodColor = param.MethodColor()
resetColor = param.ResetColor()
}
if param.Latency > time.Minute {
// Truncate in a golang < 1.8 safe way
param.Latency = param.Latency - param.Latency%time.Second
}
if *adminToken != "" && strings.Contains(param.Path, *adminToken) {
param.Path = strings.Replace(param.Path, *adminToken, "[REDACTED]", 1)
}
if *serviceConsoleToken != "" && strings.Contains(param.Path, *serviceConsoleToken) {
param.Path = strings.Replace(param.Path, *serviceConsoleToken, "[REDACTED]", 1)
}
logLine := fmt.Sprintf("%v | %s |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
param.Request.Host,
statusColor, param.StatusCode, resetColor,
param.Latency,
param.ClientIP,
methodColor, param.Method, resetColor,
param.Path,
param.ErrorMessage,
)
if *logToClient {
hostname := strings.Split(param.Request.Host, ":")[0]
loc, ok := state.HTTPListeners.Load(hostname)
if ok {
serverpool := loc.(*ServerPool)
val, ok := param.Keys[SEED] //retrieve seed for this context
var seed int64 = -1
if ok {
seed = val.(int64)
}
proxyHolder, ok := serverpool.Select(seed)
if ok {
sendMessage(proxyHolder.SSHConn, strings.TrimSpace(logLine), true)
}
}
}
return logLine
}), gin.Recovery(), func(c *gin.Context) {
hostname := strings.Split(c.Request.Host, ":")[0]
hostIsRoot := hostname == *rootDomain
if (*adminEnabled || *serviceConsoleEnabled) && strings.HasPrefix(c.Request.URL.Path, "/_sish/") {
state.Console.HandleRequest(hostname, hostIsRoot, c)
return
}
if hostIsRoot && *redirectRoot {
c.Redirect(http.StatusFound, *redirectRootLocation)
return
}
loc, ok := state.HTTPListeners.LoadFromServerPool(hostname, c)
if !ok {
err := c.AbortWithError(http.StatusNotFound, fmt.Errorf("cannot find connection for host: %s", hostname))
if err != nil {
log.Println("Aborting with error", err)
}
return
}
log.Println("httplistener : ", hostname, loc)
reqBody, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
log.Println("Error reading request body:", err)
return
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
requestedScheme := "http"
if c.Request.TLS != nil {
requestedScheme = "https"
}
c.Request.Header.Set("X-Forwarded-Proto", requestedScheme)
proxyHolder := loc
url := *c.Request.URL
url.Host = "local"
url.Path = ""
url.RawQuery = ""
url.Fragment = ""
url.Scheme = proxyHolder.Scheme
dialer := func(network, addr string) (net.Conn, error) {
return net.Dial("unix", proxyHolder.ProxyTo)
}
tlsConfig := &tls.Config{
InsecureSkipVerify: !*verifySSL,
}
if c.IsWebsocket() {
scheme := "ws"
if url.Scheme == "https" {
scheme = "wss"
}
var checkOrigin func(r *http.Request) bool
if !*verifyOrigin {
checkOrigin = func(r *http.Request) bool {
return true
}
}
url.Scheme = scheme
wsProxy := websocketproxy.NewProxy(&url)
wsProxy.Upgrader = &websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: checkOrigin,
}
wsProxy.Dialer = &websocket.Dialer{
NetDial: dialer,
TLSClientConfig: tlsConfig,
}
gin.WrapH(wsProxy)(c)
return
}
proxy := httputil.NewSingleHostReverseProxy(&url)
proxy.Transport = &http.Transport{
Dial: dialer,
TLSClientConfig: tlsConfig,
}
if *adminEnabled || *serviceConsoleEnabled {
proxy.ModifyResponse = func(response *http.Response) error {
resBody, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Println("error reading response for webconsole:", err)
}
response.Body = ioutil.NopCloser(bytes.NewBuffer(resBody))
startTime := c.GetTime("startTime")
currentTime := time.Now()
diffTime := currentTime.Sub(startTime)
roundTime := 10 * time.Microsecond
if diffTime > time.Second {
roundTime = 10 * time.Millisecond
}
if response.Header.Get("Content-Encoding") == "gzip" {
gzData := bytes.NewBuffer(resBody)
gzReader, err := gzip.NewReader(gzData)
if err != nil {
log.Println("error reading gzip data:", err)
}
resBody, err = ioutil.ReadAll(gzReader)
if err != nil {
log.Println("error reading gzip data:", err)
}
}
requestHeaders := c.Request.Header.Clone()
requestHeaders.Add("Host", hostname)
data, err := json.Marshal(map[string]interface{}{
"startTime": startTime,
"currentTime": currentTime,
"requestIP": c.ClientIP(),
"requestTime": diffTime.Round(roundTime).String(),
"requestMethod": c.Request.Method,
"requestUrl": c.Request.URL,
"requestHeaders": requestHeaders,
"requestBody": base64.StdEncoding.EncodeToString(reqBody),
"responseHeaders": response.Header,
"responseCode": response.StatusCode,
"responseStatus": response.Status,
"responseBody": base64.StdEncoding.EncodeToString(resBody),
})
if err != nil {
log.Println("error marshaling json for webconsole:", err)
}
state.Console.BroadcastRoute(hostname, data)
return nil
}
}
gin.WrapH(proxy)(c)
})
if *httpsEnabled {
go func() {
log.Fatal(r.RunTLS(*httpsAddr, filepath.Join(*httpsPems, "fullchain.pem"), filepath.Join(*httpsPems, "privkey.pem")))
}()
}
log.Fatal(r.Run(*httpAddr))
}