forked from subspacecommunity/subspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
402 lines (339 loc) · 10.2 KB
/
main.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/xml"
"flag"
"fmt"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/julienschmidt/httprouter"
"github.com/crewjam/saml"
"github.com/crewjam/saml/samlsp"
"github.com/gorilla/securecookie"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/acme/autocert"
)
var (
// Flags
cli = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// datadir
datadir string
// The version is set by the build command.
version string
// httpd
httpAddr string
httpHost string
httpPrefix string
// Insecure http cookies (only recommended for internal LANs/VPNs)
httpInsecure bool
// set based on httpAddr
httpIP string
httpPort string
// backlink
backlink string
// show version
showVersion bool
// show help
showHelp bool
// debug logging
debug bool
// Let's Encrypt
letsencrypt bool
// HTTP read limit
httpReadLimit int64 = 2 * (1024 * 1024)
// securetoken
securetoken *securecookie.SecureCookie
// logger
logger = log.New()
// config
config *Config
// mailer
mailer = NewMailer()
// SAML
samlSP *samlsp.Middleware
// Error page HTML
errorPageHTML = `<html><head><title>Error</title></head><body text="orangered" bgcolor="black"><h1>An error has occurred</h1></body></html>`
)
func init() {
cli.StringVar(&datadir, "datadir", "/data", "data dir")
cli.StringVar(&backlink, "backlink", "", "backlink (optional)")
cli.StringVar(&httpHost, "http-host", "", "HTTP host")
cli.StringVar(&httpAddr, "http-addr", ":80", "HTTP listen address")
cli.BoolVar(&httpInsecure, "http-insecure", false, "enable sessions cookies for http (no https) not recommended")
cli.BoolVar(&letsencrypt, "letsencrypt", true, "enable TLS using Let's Encrypt on port 443")
cli.BoolVar(&showVersion, "version", false, "display version and exit")
cli.BoolVar(&showHelp, "help", false, "display help and exit")
cli.BoolVar(&debug, "debug", false, "debug mode")
}
func main() {
var err error
cli.Parse(os.Args[1:])
usage := func(msg string) {
if msg != "" {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", msg)
}
fmt.Fprintf(os.Stderr, "Usage: %s --http-host subspace.example.com\n\n", os.Args[0])
cli.PrintDefaults()
}
if showHelp {
usage("Help info")
os.Exit(0)
}
if showVersion {
fmt.Printf("Subspace %s\n", version)
os.Exit(0)
}
// http host
if httpHost == "" {
usage("--http-host flag is required")
os.Exit(1)
}
// debug logging
logger.Out = os.Stdout
if debug {
logger.SetLevel(log.DebugLevel)
}
logger.Debugf("debug logging is enabled")
// http port
httpIP, httpPort, err := net.SplitHostPort(httpAddr)
if err != nil {
usage("invalid --http-addr: " + err.Error())
}
// Clean datadir path.
datadir = filepath.Clean(datadir)
// config
config, err = NewConfig("config.json")
if err != nil {
logger.Fatal(err)
}
// Secure token
securetoken = securecookie.New([]byte(config.FindInfo().HashKey), []byte(config.FindInfo().BlockKey))
// Configure SAML if metadata is present.
if len(config.FindInfo().SAML.IDPMetadata) > 0 {
if err := configureSAML(); err != nil {
logger.Warnf("configuring SAML failed: %s", err)
}
}
//
// Routes
//
r := &httprouter.Router{}
r.GET("/", Log(WebHandler(indexHandler, "index")))
r.GET("/help", Log(WebHandler(helpHandler, "help")))
r.GET("/configure", Log(WebHandler(configureHandler, "configure")))
r.POST("/configure", Log(WebHandler(configureHandler, "configure")))
// SAML
r.GET("/sso", Log(ssoHandler))
r.GET("/saml/metadata", Log(samlHandler))
r.POST("/saml/metadata", Log(samlHandler))
r.GET("/saml/acs", Log(samlHandler))
r.POST("/saml/acs", Log(samlHandler))
r.GET("/signin", Log(WebHandler(signinHandler, "signin")))
r.GET("/signout", Log(WebHandler(signoutHandler, "signout")))
r.POST("/signin", Log(WebHandler(signinHandler, "signin")))
r.GET("/forgot", Log(WebHandler(forgotHandler, "forgot")))
r.POST("/forgot", Log(WebHandler(forgotHandler, "forgot")))
r.GET("/settings", Log(WebHandler(settingsHandler, "settings")))
r.POST("/settings", Log(WebHandler(settingsHandler, "settings")))
r.GET("/user/edit/:user", Log(WebHandler(userEditHandler, "user/edit")))
r.POST("/user/edit", Log(WebHandler(userEditHandler, "user/edit")))
r.GET("/user/delete/:user", Log(WebHandler(userDeleteHandler, "user/delete")))
r.POST("/user/delete", Log(WebHandler(userDeleteHandler, "user/delete")))
r.GET("/profile/add", Log(WebHandler(profileAddHandler, "profile/add")))
r.POST("/profile/add", Log(WebHandler(profileAddHandler, "profile/add")))
r.GET("/profile/connect/:profile", Log(WebHandler(profileConnectHandler, "profile/connect")))
r.GET("/profile/delete/:profile", Log(WebHandler(profileDeleteHandler, "profile/delete")))
r.POST("/profile/delete", Log(WebHandler(profileDeleteHandler, "profile/delete")))
r.GET("/profile/config/wireguard/:profile", Log(WebHandler(wireguardConfigHandler, "profile/config/wireguard")))
r.GET("/profile/qrconfig/wireguard/:profile", Log(WebHandler(wireguardQRConfigHandler, "profile/qrconfig/wireguard")))
r.GET("/static/*path", staticHandler)
//
// Server
//
httpTimeout := 10 * time.Minute
maxHeaderBytes := 10 * (1024 * 1024)
// Plain text web server for use behind a reverse proxy.
if !letsencrypt {
httpd := &http.Server{
Handler: r,
Addr: net.JoinHostPort(httpIP, httpPort),
WriteTimeout: httpTimeout,
ReadTimeout: httpTimeout,
MaxHeaderBytes: maxHeaderBytes,
}
hostport := net.JoinHostPort(httpHost, httpPort)
if httpPort == "80" {
hostport = httpHost
}
logger.Infof("Subspace version: %s %s", version, &url.URL{
Scheme: "http",
Host: hostport,
Path: httpPrefix,
})
logger.Fatal(httpd.ListenAndServe())
}
// Let's Encrypt TLS mode
// autocert
certmanager := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(filepath.Join(datadir, "letsencrypt")),
HostPolicy: func(_ context.Context, host string) error {
host = strings.TrimPrefix(host, "www.")
if host == httpHost {
return nil
}
if host == config.FindInfo().Domain {
return nil
}
return fmt.Errorf("autocert: host %q not permitted by HostPolicy", host)
},
}
// http redirect to https and Let's Encrypt auth
go func() {
redir := httprouter.New()
redir.GET("/*path", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Scheme = "https"
r.URL.Host = httpHost
http.Redirect(w, r, r.URL.String(), http.StatusFound)
})
httpd := &http.Server{
Handler: certmanager.HTTPHandler(redir),
Addr: net.JoinHostPort(httpIP, "80"),
WriteTimeout: httpTimeout,
ReadTimeout: httpTimeout,
MaxHeaderBytes: maxHeaderBytes,
}
if err := httpd.ListenAndServe(); err != nil {
logger.Fatalf("http server on port 80 failed: %s", err)
}
}()
// TLS
tlsConfig := tls.Config{
GetCertificate: certmanager.GetCertificate,
NextProtos: []string{"http/1.1"},
Rand: rand.Reader,
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
}
// Override default for TLS.
if httpPort == "80" {
httpPort = "443"
httpAddr = net.JoinHostPort(httpIP, httpPort)
}
httpsd := &http.Server{
Handler: r,
Addr: httpAddr,
WriteTimeout: httpTimeout,
ReadTimeout: httpTimeout,
MaxHeaderBytes: maxHeaderBytes,
}
// Enable TCP keep alives on the TLS connection.
tcpListener, err := net.Listen("tcp", httpAddr)
if err != nil {
logger.Fatalf("listen failed: %s", err)
return
}
tlsListener := tls.NewListener(tcpKeepAliveListener{tcpListener.(*net.TCPListener)}, &tlsConfig)
hostport := net.JoinHostPort(httpHost, httpPort)
if httpPort == "443" {
hostport = httpHost
}
logger.Infof("Subspace version: %s %s", version, &url.URL{
Scheme: "https",
Host: hostport,
Path: "/",
})
logger.Fatal(httpsd.Serve(tlsListener))
}
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (l tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := l.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(10 * time.Minute)
return tc, nil
}
func configureSAML() error {
info := config.FindInfo()
if len(info.SAML.IDPMetadata) == 0 {
return fmt.Errorf("no IDP metadata")
}
entity := &saml.EntityDescriptor{}
err := xml.Unmarshal([]byte(info.SAML.IDPMetadata), entity)
if err != nil && err.Error() == "expected element type <EntityDescriptor> but have <EntitiesDescriptor>" {
entities := &saml.EntitiesDescriptor{}
if err := xml.Unmarshal([]byte(info.SAML.IDPMetadata), entities); err != nil {
return err
}
err = fmt.Errorf("no entity found with IDPSSODescriptor")
for i, e := range entities.EntityDescriptors {
if len(e.IDPSSODescriptors) > 0 {
entity = &entities.EntityDescriptors[i]
err = nil
}
}
}
if err != nil {
return err
}
keyPair, err := tls.X509KeyPair(info.SAML.Certificate, info.SAML.PrivateKey)
if err != nil {
return fmt.Errorf("failed to load SAML keypair: %s", err)
}
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
if err != nil {
return fmt.Errorf("failed to parse SAML certificate: %s", err)
}
rootURL := url.URL{
Scheme: "https",
Host: httpHost,
Path: "/",
}
newsp, err := samlsp.New(samlsp.Options{
URL: rootURL,
Key: keyPair.PrivateKey.(*rsa.PrivateKey),
Certificate: keyPair.Leaf,
IDPMetadata: entity,
CookieName: SessionCookieNameSSO,
CookieDomain: httpHost, // TODO: this will break if using a custom domain.
CookieSecure: !httpInsecure,
Logger: logger,
AllowIDPInitiated: true,
})
if err != nil {
logger.Warnf("failed to configure SAML: %s", err)
samlSP = nil
return fmt.Errorf("failed to configure SAML: %s", err)
}
samlSP = newsp
logger.Infof("successfully configured SAML")
return nil
}
func BestDomain() string {
domain := config.FindInfo().Domain
if domain != "" {
return domain
}
return httpHost
}