carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package console
2
3import (
4 "context"
5 "crypto/subtle"
6 "log"
7 "net/http"
8 "net/url"
9 "strings"
10 "time"
11
12 "github.com/carlosframework/platform/internal/release"
13)
14
15type BrowserRestartCopy struct {
16 AppTitle, AppLine, HostTitle, HostLine string
17 Button, Success, Audit string
18 RateLimited, Failed string
19}
20
21var approvedBrowserRestartCopy = BrowserRestartCopy{
22 AppTitle: "Restart all app instances?",
23 AppLine: "Request a restart for this app's running instances. Sleeping instances stay asleep.",
24 HostTitle: "Restart {host}?",
25 HostLine: "Only {host} receives a restart request. If it is asleep, it remains asleep.",
26 Button: "Restart",
27 Success: "Restart requested for {target}.",
28 Audit: "Restart requested for {target}.",
29 RateLimited: "Too many restart requests. Try again later.",
30 Failed: "The restart request failed. Try again.",
31}
32
33func (s *Server) requestAppRestart(ctx context.Context, acct, app string) error {
34 return release.TouchRestartGen(ctx, s.cfg.Releases, acct, app)
35}
36
37func (s *Server) handleAppRestart(w http.ResponseWriter, r *http.Request) {
38 s.handleBrowserRestart(w, r, "", false)
39}
40
41func (s *Server) handleInstanceRestart(w http.ResponseWriter, r *http.Request) {
42 s.handleBrowserRestart(w, r, strings.TrimSpace(r.PathValue("host")), true)
43}
44
45func (s *Server) handleBrowserRestart(w http.ResponseWriter, r *http.Request, host string, scoped bool) {
46 addr, raw, acct, role, ok := s.account(w, r)
47 if !ok {
48 return
49 }
50 if subtle.ConstantTimeCompare([]byte(r.FormValue("csrf")), []byte(raw)) != 1 {
51 s.renderMessage(w, r, http.StatusForbidden, "Refused", "Cross-site request blocked.")
52 return
53 }
54 app := r.PathValue("app")
55 has, err := s.store.HasAppIn(acct.ID, app)
56 if err != nil {
57 s.renderMessage(w, r, http.StatusInternalServerError, "Error", "Could not read your apps. Please try again.")
58 return
59 }
60 if !has {
61 http.NotFound(w, r)
62 return
63 }
64 if scoped && host == "" {
65 http.NotFound(w, r)
66 return
67 }
68 var scope release.RestartScope
69 if host != "" {
70 if s.cfg.Registry == "" {
71 http.NotFound(w, r)
72 return
73 }
74 reg, err := openRegistry(s.cfg.Registry)
75 if err != nil {
76 log.Printf("console: browser restart registry: %v", err)
77 http.NotFound(w, r)
78 return
79 }
80 routes, err := reg.List()
81 if err != nil {
82 log.Printf("console: browser restart routes: %v", err)
83 http.NotFound(w, r)
84 return
85 }
86 found := false
87 for _, rt := range routes {
88 if rt.Host == host && rt.App == app && release.RouteAccount(rt) == acct.Sqid && release.Adoptable(rt) {
89 found = true
90 break
91 }
92 }
93 if !found {
94 http.NotFound(w, r)
95 return
96 }
97 scope = release.RestartScope{Kind: release.RestartScopeHost, Value: host}
98 }
99 words := approvedBrowserRestartCopy
100 if !confirmed(r) {
101 cv := &ConfirmView{Action: r.URL.Path, Fields: [][2]string{{"csrf", raw}}, CancelTab: "instances"}
102 if host == "" {
103 cv.Title, cv.Lines, cv.Button = words.AppTitle, []string{words.AppLine}, words.Button
104 } else {
105 cv.Title = strings.ReplaceAll(words.HostTitle, "{host}", host)
106 cv.Lines, cv.Button = []string{strings.ReplaceAll(words.HostLine, "{host}", host)}, words.Button
107 }
108 s.renderConfirm(w, r, addr, raw, acct, role, app, cv)
109 return
110 }
111 if !s.allowWrite(Principal{Kind: PrincipalUser, Address: addr}, time.Now()) {
112 s.setFlash(w, "err", words.RateLimited)
113 http.Redirect(w, r, "/a/"+url.PathEscape(acct.Sqid)+"/apps/"+url.PathEscape(app)+"/instances", http.StatusSeeOther)
114 return
115 }
116 if s.cfg.Releases == nil {
117 s.setFlash(w, "err", words.Failed)
118 http.Redirect(w, r, "/a/"+url.PathEscape(acct.Sqid)+"/apps/"+url.PathEscape(app)+"/instances", http.StatusSeeOther)
119 return
120 }
121 ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
122 defer cancel()
123 var restartErr error
124 if host == "" {
125 restartErr = s.requestAppRestart(ctx, acct.Sqid, app)
126 } else {
127 restartErr = s.writeRestartScope(ctx, acct.Sqid, app, scope)
128 }
129 if restartErr != nil {
130 log.Printf("console: browser restart %s/%s/%s: %v", acct.Sqid, app, host, restartErr)
131 s.setFlash(w, "err", words.Failed)
132 } else {
133 target := app
134 if host != "" {
135 target = host
136 }
137 detail := strings.ReplaceAll(words.Audit, "{target}", target)
138 success := strings.ReplaceAll(words.Success, "{target}", target)
139 targetType := "app"
140 if host != "" {
141 targetType = "instance"
142 }
143 s.auditActivity(acct, app, addr, Activity{Action: "restart", TargetType: targetType, Target: target, Detail: detail})
144 s.bumpSerial(ctx)
145 s.setFlash(w, "ok", success)
146 }
147 http.Redirect(w, r, "/a/"+url.PathEscape(acct.Sqid)+"/apps/"+url.PathEscape(app)+"/instances", http.StatusSeeOther)
148}
149