Skip to content
File

Blob: gateway/apex.go

go127 lines
1package gateway
2 
3import (
4 _ "embed"
5 "net/http"
6 "strconv"
7 "text/template"
8 
9 "go.miragespace.co/specter/spec/protocol"
10 "go.miragespace.co/specter/ui"
11 "go.miragespace.co/specter/util"
12 
13 "github.com/go-chi/chi/v5"
14 "github.com/go-chi/chi/v5/middleware"
15)
16 
17const bodyLimit = 1 << 10 // 1kb
18//go:embed index.html
19var index string
20 
21//go:embed quic.png
22var quicPng []byte
23 
24//go:embed endpoints.md
25var endpointsDoc string
26 
27var view = template.Must(template.New("index").Parse(index))
28 
29type apexServer struct {
30 handlers InternalHandlers
31 limiter func(http.Handler) http.Handler
32 internalProxy func(http.Handler) http.Handler
33 pkiServer protocol.PKIService
34 authUser string
35 authPass string
36}
37 
38func (a *apexServer) handleRoot(w http.ResponseWriter, r *http.Request) {
39 var root string
40 if r.ProtoAtLeast(2, 0) {
41 root = r.Host
42 } else {
43 root = r.TLS.ServerName
44 }
45 w.Header().Set("Content-Type", "text/html; charset=UTF-8")
46 view.Execute(w, struct {
47 RootDomain string
48 }{
49 RootDomain: root,
50 })
51}
52 
53func (a *apexServer) handleLogo(w http.ResponseWriter, r *http.Request) {
54 w.Header().Set("Content-Type", "image/png")
55 w.Header().Set("Content-Length", strconv.Itoa(len(quicPng)))
56 w.Write(quicPng)
57}
58 
59func (a *apexServer) Mount(r *chi.Mux) {
60 r.Use(a.limiter)
61 r.Use(middleware.Recoverer)
62 r.Use(middleware.NoCache)
63 r.Use(middleware.Compress(5, "text/*"))
64 r.Get("/", a.handleRoot)
65 r.Get("/quic.png", a.handleLogo)
66 if a.pkiServer != nil {
67 pkiServer := protocol.NewPKIServiceServer(a.pkiServer)
68 r.With(util.LimitBody(bodyLimit)).Mount(pkiServer.PathPrefix(), pkiServer)
69 }
70 if a.authUser == "" || a.authPass == "" {
71 return
72 }
73 r.Route("/_internal", func(r chi.Router) {
74 r.Use(middleware.BasicAuth("internal", map[string]string{
75 a.authUser: a.authPass,
76 }))
77 // Check the original browser request before an internal proxy can change
78 // its destination authority. Non-browser authenticated API calls remain
79 // usable without introducing another session or CSRF-token store.
80 r.Use(http.NewCrossOriginProtection().Handler)
81 r.Use(a.internalProxy)
82 r.Mount("/ui", http.StripPrefix("/_internal/ui", ui.Assets()))
83 
84 if a.handlers.Overview != nil {
85 r.Handle("/", ui.OperatorPage())
86 r.Get("/overview.json", a.handlers.Overview.ServeHTTP)
87 }
88 if a.handlers.Configuration != nil {
89 page := ui.OperatorPage()
90 r.Handle("/config/dns", page)
91 r.Handle("/config/dns/", page)
92 r.Handle("/config/waf", page)
93 r.Handle("/config/waf/", page)
94 r.Mount("/api/config", a.handlers.Configuration)
95 }
96 r.Get("/endpoints", func(w http.ResponseWriter, _ *http.Request) {
97 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
98 w.Write([]byte(endpointsDoc))
99 })
100 
101 if a.handlers.Acme != nil {
102 r.Mount("/acme", a.handlers.Acme)
103 }
104 if a.handlers.Chord != nil {
105 r.Mount("/chord", a.handlers.Chord)
106 }
107 if a.handlers.TunnelServer != nil {
108 page := ui.OperatorPage()
109 r.Handle("/tun", page)
110 r.Handle("/tun/", page)
111 r.Handle("/tun/{id}/*", page)
112 r.Mount("/api/tun", a.handlers.TunnelServer)
113 }
114 if a.handlers.Migrator != nil {
115 r.Mount("/migrator", a.handlers.Migrator)
116 }
117 
118 r.Mount("/debug", middleware.Profiler())
119 
120 // catch-all helper
121 r.HandleFunc("/*", func(w http.ResponseWriter, _ *http.Request) {
122 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
123 w.Write([]byte(endpointsDoc))
124 })
125 })
126}