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
|
package main
import (
"crypto/tls"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
mimedb "github.com/lupine/go-mimedb"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/cors"
log "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-pages/internal/artifact"
"gitlab.com/gitlab-org/gitlab-pages/internal/httperrors"
"gitlab.com/gitlab-org/gitlab-pages/metrics"
)
const xForwardedProto = "X-Forwarded-Proto"
const xForwardedProtoHTTPS = "https"
var (
corsHandler = cors.New(cors.Options{AllowedMethods: []string{"GET"}})
)
type theApp struct {
appConfig
domains domains
lock sync.RWMutex
Artifact *artifact.Artifact
}
func (a *theApp) isReady() bool {
return a.domains != nil
}
func (a *theApp) domain(host string) *domain {
host = strings.ToLower(host)
a.lock.RLock()
defer a.lock.RUnlock()
domain, _ := a.domains[host]
return domain
}
func (a *theApp) ServeTLS(ch *tls.ClientHelloInfo) (*tls.Certificate, error) {
if ch.ServerName == "" {
return nil, nil
}
if domain := a.domain(ch.ServerName); domain != nil {
tls, _ := domain.ensureCertificate()
return tls, nil
}
return nil, nil
}
func (a *theApp) healthCheck(w http.ResponseWriter, r *http.Request, https bool) {
if a.isReady() {
w.Write([]byte("success"))
} else {
http.Error(w, "not yet ready", http.StatusServiceUnavailable)
}
}
func (a *theApp) redirectToHTTPS(w http.ResponseWriter, r *http.Request, statusCode int) {
u := *r.URL
u.Scheme = "https"
u.Host = r.Host
u.User = nil
http.Redirect(w, r, u.String(), statusCode)
}
func (a *theApp) getHostAndDomain(r *http.Request) (host string, domain *domain) {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}
return host, a.domain(host)
}
func (a *theApp) tryAuxiliaryHandlers(w http.ResponseWriter, r *http.Request, https bool, host string, domain *domain) bool {
// short circuit content serving to check for a status page
if r.RequestURI == a.appConfig.StatusPath {
a.healthCheck(w, r, https)
return true
}
// Add auto redirect
if !https && a.RedirectHTTP {
a.redirectToHTTPS(w, r, http.StatusTemporaryRedirect)
return true
}
// In the event a host is prefixed with the artifact prefix an artifact
// value is created, and an attempt to proxy the request is made
if a.Artifact.TryMakeRequest(host, w, r) {
return true
}
if !a.isReady() {
httperrors.Serve503(w)
return true
}
if domain == nil {
httperrors.Serve404(w)
return true
}
if !https && domain.isHTTPSOnly(r) {
a.redirectToHTTPS(w, r, http.StatusMovedPermanently)
return true
}
return false
}
func (a *theApp) serveContent(ww http.ResponseWriter, r *http.Request, https bool) {
w := newLoggingResponseWriter(ww)
defer w.Log(r)
metrics.SessionsActive.Inc()
defer metrics.SessionsActive.Dec()
host, domain := a.getHostAndDomain(r)
if a.tryAuxiliaryHandlers(&w, r, https, host, domain) {
return
}
// Serve static file, applying CORS headers if necessary
if a.DisableCrossOriginRequests {
domain.ServeHTTP(&w, r)
} else {
corsHandler.ServeHTTP(&w, r, domain.ServeHTTP)
}
metrics.ProcessedRequests.WithLabelValues(strconv.Itoa(w.status), r.Method).Inc()
}
func (a *theApp) ServeHTTP(ww http.ResponseWriter, r *http.Request) {
https := r.TLS != nil
a.serveContent(ww, r, https)
}
func (a *theApp) ServeProxy(ww http.ResponseWriter, r *http.Request) {
forwardedProto := r.Header.Get(xForwardedProto)
https := forwardedProto == xForwardedProtoHTTPS
a.serveContent(ww, r, https)
}
func (a *theApp) UpdateDomains(domains domains) {
a.lock.Lock()
defer a.lock.Unlock()
a.domains = domains
}
func (a *theApp) Run() {
var wg sync.WaitGroup
// Listen for HTTP
for _, fd := range a.ListenHTTP {
wg.Add(1)
go func(fd uintptr) {
defer wg.Done()
err := listenAndServe(fd, a.ServeHTTP, a.HTTP2, nil)
if err != nil {
fatal(err)
}
}(fd)
}
// Listen for HTTPS
for _, fd := range a.ListenHTTPS {
wg.Add(1)
go func(fd uintptr) {
defer wg.Done()
err := listenAndServeTLS(fd, a.RootCertificate, a.RootKey, a.ServeHTTP, a.ServeTLS, a.HTTP2)
if err != nil {
fatal(err)
}
}(fd)
}
// Listen for HTTP proxy requests
for _, fd := range a.ListenProxy {
wg.Add(1)
go func(fd uintptr) {
defer wg.Done()
err := listenAndServe(fd, a.ServeProxy, a.HTTP2, nil)
if err != nil {
fatal(err)
}
}(fd)
}
// Serve metrics for Prometheus
if a.ListenMetrics != 0 {
wg.Add(1)
go func(fd uintptr) {
defer wg.Done()
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}).ServeHTTP
err := listenAndServe(fd, handler, false, nil)
if err != nil {
fatal(err)
}
}(a.ListenMetrics)
}
go watchDomains(a.Domain, a.UpdateDomains, time.Second)
wg.Wait()
}
func runApp(config appConfig) {
a := theApp{appConfig: config}
if config.ArtifactsServer != "" {
a.Artifact = artifact.New(config.ArtifactsServer, config.ArtifactsServerTimeout, config.Domain)
}
configureLogging(config.LogFormat)
if err := mimedb.LoadTypes(); err != nil {
log.WithError(err).Warn("Loading extended MIME database failed")
}
a.Run()
}
|