Repository navigation
Expand file tree
/
Copy pathapi.go
More file actions
208 lines (190 loc) · 5.5 KB
/
Copy pathapi.go
File metadata and controls
208 lines (190 loc) · 5.5 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
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
package portkit
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddytls"
)
// APIHandler serves https://portkit.localhost: the discovery endpoint at
// /services and an index page at /.
type APIHandler struct {
app *App
}
func (APIHandler) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.portkit_api",
New: func() caddy.Module { return new(APIHandler) },
}
}
func (h *APIHandler) Provision(ctx caddy.Context) (err error) {
h.app, err = loadApp(ctx)
return err
}
// DiscoveredService is one entry of GET /services. Commands and
// environments are left out.
type DiscoveredService struct {
Name string `json:"name"`
URL string `json:"url"`
Mode string `json:"mode"`
Cwd string `json:"cwd,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Subdomains map[string]string `json:"subdomains,omitempty"`
Running bool `json:"running"`
}
func (h *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
switch r.URL.Path {
case "/services":
if r.Method != http.MethodGet && r.Method != http.MethodHead {
return caddyhttp.Error(http.StatusMethodNotAllowed, nil)
}
list, err := discover(h.app.Services, r.URL.Query()["label"], r.URL.Query().Get("cwd"))
if err != nil {
return caddyhttp.Error(http.StatusBadRequest, err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(map[string]any{"services": list})
case "/":
return servePage(w, r, http.StatusOK, indexPage(h.app.Services))
}
return caddyhttp.Error(http.StatusNotFound, nil)
}
// discover lists services that have all the given labels ("key=value") and,
// if cwd is set, whose working directory is cwd or one of its parents,
// deepest first.
func discover(services map[string]*Service, labels []string, cwd string) ([]DiscoveredService, error) {
want := map[string]string{}
for _, l := range labels {
k, v, ok := strings.Cut(l, "=")
if !ok {
return nil, fmt.Errorf("label filter %q must be key=value", l)
}
want[k] = v
}
if cwd != "" {
cwd = filepath.Clean(cwd)
}
out := []DiscoveredService{}
for _, name := range sortedNames(services) {
s := services[name]
if s.Owner != nil && !s.Owner.alive() {
continue
}
match := true
for k, v := range want {
if s.Labels[k] != v {
match = false
}
}
if !match {
continue
}
if cwd != "" {
c := filepath.Clean(s.Cwd)
if s.Cwd == "" || (c != cwd && !strings.HasPrefix(cwd, strings.TrimSuffix(c, "/")+"/")) {
continue
}
}
d := DiscoveredService{Name: name, URL: ServiceURL(name), Mode: s.Mode, Cwd: s.Cwd, Labels: s.Labels, Running: running(name, s)}
for sub := range s.Subdomains {
if d.Subdomains == nil {
d.Subdomains = map[string]string{}
}
d.Subdomains[sub] = ServiceURL(sub + "." + name)
}
out = append(out, d)
}
if cwd != "" {
sort.SliceStable(out, func(i, j int) bool { return len(out[i].Cwd) > len(out[j].Cwd) })
}
return out, nil
}
// running reports whether a service is up now, without starting it.
func running(name string, s *Service) bool {
switch s.Mode {
case ModeOnDemand:
st := sup.State(name).State
return st == "running" || st == "starting"
case ModePort:
_, ok := probePort(s.Port)
return ok
default:
return s.Owner.alive()
}
}
// Permission allows on-demand certificates for any name under .localhost
// with up to four labels, registered or not, so the help page loads for
// unregistered names. At most 10 new unregistered names a minute get one,
// so a runaway script can't fill the disk.
type Permission struct {
app *App
}
func (Permission) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "tls.permission.portkit",
New: func() caddy.Module { return new(Permission) },
}
}
func (p *Permission) Provision(ctx caddy.Context) (err error) {
p.app, err = loadApp(ctx)
return err
}
const newNamesPerMinute = 10
var certNames = &nameLimiter{allowed: map[string]bool{}}
type nameLimiter struct {
mu sync.Mutex
allowed map[string]bool
recent []time.Time
}
func (p *Permission) CertificateAllowed(_ context.Context, host string) error {
name, ok := nameFromHost(host)
if !ok {
return fmt.Errorf("%s is not under .localhost", host)
}
if name != reservedName {
labels := strings.Split(name, ".")
if len(labels) > maxLabels {
return fmt.Errorf("%s has more than %d labels under .localhost", host, maxLabels)
}
for _, l := range labels {
if !ValidLabel(l) {
return fmt.Errorf("%s is not a valid name", host)
}
}
}
if _, _, svc := p.app.lookup(host); svc != nil || name == reservedName {
return nil
}
return certNames.allow(name, time.Now())
}
func (l *nameLimiter) allow(name string, now time.Time) error {
l.mu.Lock()
defer l.mu.Unlock()
if l.allowed[name] {
return nil
}
cutoff := now.Add(-time.Minute)
for len(l.recent) > 0 && l.recent[0].Before(cutoff) {
l.recent = l.recent[1:]
}
if len(l.recent) >= newNamesPerMinute {
return fmt.Errorf("more than %d new unregistered names in a minute", newNamesPerMinute)
}
l.recent = append(l.recent, now)
l.allowed[name] = true
return nil
}
var (
_ caddyhttp.MiddlewareHandler = (*APIHandler)(nil)
_ caddytls.OnDemandPermission = (*Permission)(nil)
)