File
Blob: gateway/apex.go
| 1 | package gateway |
| 2 | |
| 3 | import ( |
| 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 | |
| 17 | const bodyLimit = 1 << 10 // 1kb |
| 18 | //go:embed index.html |
| 19 | var index string |
| 20 | |
| 21 | //go:embed quic.png |
| 22 | var quicPng []byte |
| 23 | |
| 24 | //go:embed endpoints.md |
| 25 | var endpointsDoc string |
| 26 | |
| 27 | var view = template.Must(template.New("index").Parse(index)) |
| 28 | |
| 29 | type 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 | |
| 38 | func (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 | |
| 53 | func (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 | |
| 59 | func (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 | r.Use(a.internalProxy) |
| 78 | r.Mount("/ui", http.StripPrefix("/_internal/ui", ui.Assets())) |
| 79 | |
| 80 | if a.handlers.Overview != nil { |
| 81 | r.Handle("/", ui.OperatorPage()) |
| 82 | r.Get("/overview.json", a.handlers.Overview.ServeHTTP) |
| 83 | } |
| 84 | r.Get("/endpoints", func(w http.ResponseWriter, _ *http.Request) { |
| 85 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 86 | w.Write([]byte(endpointsDoc)) |
| 87 | }) |
| 88 | |
| 89 | if a.handlers.Acme != nil { |
| 90 | r.Mount("/acme", a.handlers.Acme) |
| 91 | } |
| 92 | if a.handlers.Chord != nil { |
| 93 | r.Mount("/chord", a.handlers.Chord) |
| 94 | } |
| 95 | if a.handlers.TunnelServer != nil { |
| 96 | page := ui.OperatorPage() |
| 97 | r.Handle("/tun", page) |
| 98 | r.Handle("/tun/", page) |
| 99 | r.Handle("/tun/{id}/*", page) |
| 100 | r.Mount("/api/tun", a.handlers.TunnelServer) |
| 101 | } |
| 102 | if a.handlers.Migrator != nil { |
| 103 | r.Mount("/migrator", a.handlers.Migrator) |
| 104 | } |
| 105 | |
| 106 | r.Mount("/debug", middleware.Profiler()) |
| 107 | |
| 108 | // catch-all helper |
| 109 | r.HandleFunc("/*", func(w http.ResponseWriter, _ *http.Request) { |
| 110 | w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 111 | w.Write([]byte(endpointsDoc)) |
| 112 | }) |
| 113 | }) |
| 114 | } |