-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
127 lines (108 loc) · 3.42 KB
/
Copy pathmain.go
File metadata and controls
127 lines (108 loc) · 3.42 KB
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
// Package main implements podfather, a simple web dashboard for rootless Podman.
// It connects to the Podman API socket and renders container/image information
// server-side using Go templates. No JavaScript, no external dependencies.
package main
import (
"context"
"crypto/rand"
"fmt"
"log"
"net/http"
"os"
"strings"
"sync"
"sync/atomic"
"time"
)
type ctxKey int
const reqIDKey ctxKey = 0
const csrfTokenKey ctxKey = 1
func reqID(ctx context.Context) string {
if id, ok := ctx.Value(reqIDKey).(string); ok {
return id
}
return "-"
}
// Server holds all per-instance state for the podfather web server.
type Server struct {
basePath string
hostname string
enableAutoUpdate bool
externalApps []App
podmanClient *http.Client
podmanBaseURL string
autoUpdateMu sync.Mutex
currentAutoUpdate atomic.Pointer[autoUpdateResult]
}
func (s *Server) newMux(podmanBin string) *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("GET /{$}", s.handleRoot)
mux.HandleFunc("GET /apps", s.handleApps)
mux.HandleFunc("GET /containers", s.handleContainers)
mux.HandleFunc("GET /container/{id}", s.handleContainer)
mux.HandleFunc("GET /images", s.handleImages)
mux.HandleFunc("GET /image/{id}", s.handleImage)
mux.HandleFunc("GET /logo.svg", handleLogo)
mux.HandleFunc("POST /auto-update", s.handleAutoUpdatePost(podmanBin))
mux.HandleFunc("GET /auto-update", s.handleAutoUpdatePage)
mux.HandleFunc("GET /auto-update/events", s.handleAutoUpdateEvents)
return mux
}
func main() {
sock := socketPath()
addr := "127.0.0.1:8080"
if a := os.Getenv("LISTEN_ADDR"); a != "" {
addr = a
}
hostname, _ := os.Hostname()
s := &Server{
basePath: strings.TrimRight(os.Getenv("BASE_PATH"), "/"),
hostname: hostname,
enableAutoUpdate: os.Getenv("ENABLE_AUTOUPDATE_BUTTON") == "true",
externalApps: parseExternalApps(),
podmanClient: newPodmanClient(sock),
podmanBaseURL: "http://d/v4.0.0/libpod",
}
mux := s.newMux("podman")
var handler http.Handler = mux
if s.basePath != "" {
handler = http.StripPrefix(s.basePath, mux)
}
host := addr
if strings.HasPrefix(host, ":") {
host = "localhost" + host
}
log.Printf("podfather listening on http://%s%s (socket: %s)", host, s.basePath, sock)
handler = s.csrfProtect(handler)
log.Fatal(http.ListenAndServe(addr, logRequests(handler)))
}
type statusWriter struct {
http.ResponseWriter
status int
}
func (w *statusWriter) WriteHeader(code int) {
w.status = code
w.ResponseWriter.WriteHeader(code)
}
func (w *statusWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
func logRequests(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var buf [4]byte
rand.Read(buf[:])
id := fmt.Sprintf("%x", buf)
ctx := context.WithValue(r.Context(), reqIDKey, id)
r = r.WithContext(ctx)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("Referrer-Policy", "no-referrer")
w.Header().Set("Content-Security-Policy", "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src 'self'; connect-src 'self'; form-action 'self'")
start := time.Now()
sw := &statusWriter{ResponseWriter: w, status: http.StatusOK}
next.ServeHTTP(sw, r)
log.Printf("[%s] %s %s %d %s", id, r.Method, r.URL.Path, sw.status, time.Since(start).Round(time.Millisecond))
})
}