Skip to content

Commit 07fb7d1

Browse files
committed
update
1 parent 694f57a commit 07fb7d1

File tree

4 files changed

+55
-54
lines changed

4 files changed

+55
-54
lines changed

pkg/backend/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ func (b *Backend) GetHomePage(
491491
if node.Meta.Entrypoint {
492492
entrypoints = append(entrypoints, &xctf.Entrypoint{
493493
Name: node.Meta.Name,
494-
Route: chals.ChalURL("https", id, node.Meta.Id, ""),
494+
Route: chals.ChalURL(true, id, node.Meta.Id, ""),
495495
})
496496
}
497497
}

pkg/chals/chals.go

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ func init() {
5858
gob.Register(tmpl.PhoneState{})
5959
}
6060

61-
func ChalURL(scheme, compId, chalID, host string) string {
61+
// TODO breadchris base url should be configured from the environment?
62+
func ChalURL(isHttps bool, compId, chalID, host string) string {
6263
path := fmt.Sprintf("/play/%s/%s", compId, chalID)
6364
if host == "" {
6465
return path
6566
}
67+
scheme := "http"
68+
if isHttps {
69+
scheme = "https"
70+
}
6671
u := url.URL{
6772
// TODO breadchris check if the original request was https
6873
Scheme: scheme,
@@ -142,9 +147,10 @@ func (s *Handler) Handle() (string, http.Handler) {
142147

143148
// TODO breadchris find dependencies of referenced challenge and build those
144149
challenges := map[string]string{}
150+
isHttps := r.TLS != nil
145151
for _, n := range graph.Nodes {
146152
view := ""
147-
chalURL := ChalURL(r.URL.Scheme, compId, n.Meta.Id, r.Host)
153+
chalURL := ChalURL(isHttps, compId, n.Meta.Id, r.Host)
148154
switch u := n.Challenge.(type) {
149155
case *chalgen.Node_Base:
150156
switch t := u.Base.Type.(type) {
@@ -171,6 +177,23 @@ func (s *Handler) Handle() (string, http.Handler) {
171177
}
172178
}
173179

180+
templChals := func(tl string) (string, error) {
181+
nt, err := ttemplate.New("templ").Parse(tl)
182+
if err != nil {
183+
return "", err
184+
}
185+
var buf bytes.Buffer
186+
err = nt.Execute(&buf, struct {
187+
Challenges map[string]string
188+
}{
189+
Challenges: challenges,
190+
})
191+
if err != nil {
192+
return "", err
193+
}
194+
return buf.String(), nil
195+
}
196+
174197
for _, n := range graph.Nodes {
175198
if n.Meta.Id == chalId {
176199
switch u := n.Challenge.(type) {
@@ -277,22 +300,12 @@ func (s *Handler) Handle() (string, http.Handler) {
277300

278301
var newUrls []string
279302
for _, ul := range t.Filemanager.Urls {
280-
nt, err := ttemplate.New("app").Parse(ul)
281-
if err != nil {
282-
http.Error(w, err.Error(), http.StatusInternalServerError)
283-
return
284-
}
285-
var buf bytes.Buffer
286-
err = nt.Execute(&buf, struct {
287-
Challenges map[string]string
288-
}{
289-
Challenges: challenges,
290-
})
303+
ul, err = templChals(ul)
291304
if err != nil {
292305
http.Error(w, err.Error(), http.StatusInternalServerError)
293306
return
294307
}
295-
newUrls = append(newUrls, buf.String())
308+
newUrls = append(newUrls, ul)
296309
}
297310
t.Filemanager.Urls = newUrls
298311

@@ -354,22 +367,11 @@ func (s *Handler) Handle() (string, http.Handler) {
354367
}
355368
}
356369
for _, app := range t.Phone.Apps {
357-
nt, err := ttemplate.New("app").Parse(app.Url)
370+
app.Url, err = templChals(app.Url)
358371
if err != nil {
359372
http.Error(w, err.Error(), http.StatusInternalServerError)
360373
return
361374
}
362-
var buf bytes.Buffer
363-
err = nt.Execute(&buf, struct {
364-
Challenges map[string]string
365-
}{
366-
Challenges: challenges,
367-
})
368-
if err != nil {
369-
http.Error(w, err.Error(), http.StatusInternalServerError)
370-
return
371-
}
372-
app.Url = buf.String()
373375
}
374376
templ.Handler(tmpl.Page(tmpl.Phone(tmpl.PhoneState{
375377
Flag: n.Meta.Flag,
@@ -445,23 +447,11 @@ func (s *Handler) Handle() (string, http.Handler) {
445447

446448
for _, p := range t.Slack.Channels {
447449
for _, n := range p.Messages {
448-
nt, err := ttemplate.New("slack").Parse(n.Content)
449-
if err != nil {
450-
slog.Error("failed to parse slack message template", "channel", p.Name, "message", n.Content)
451-
http.Error(w, err.Error(), http.StatusInternalServerError)
452-
return
453-
}
454-
var buf bytes.Buffer
455-
err = nt.Execute(&buf, struct {
456-
Challenges map[string]string
457-
}{
458-
Challenges: challenges,
459-
})
450+
n.Content, err = templChals(n.Content)
460451
if err != nil {
461452
http.Error(w, err.Error(), http.StatusInternalServerError)
462453
return
463454
}
464-
n.Content = buf.String()
465455
}
466456
}
467457

@@ -494,22 +484,11 @@ func (s *Handler) Handle() (string, http.Handler) {
494484
return
495485
case *chalgen.Challenge_Twitter:
496486
for _, p := range t.Twitter.Posts {
497-
nt, err := ttemplate.New("twitter").Parse(p.Content)
487+
p.Content, err = templChals(p.Content)
498488
if err != nil {
499489
http.Error(w, err.Error(), http.StatusInternalServerError)
500490
return
501491
}
502-
var buf bytes.Buffer
503-
err = nt.Execute(&buf, struct {
504-
Challenges map[string]string
505-
}{
506-
Challenges: challenges,
507-
})
508-
if err != nil {
509-
http.Error(w, err.Error(), http.StatusInternalServerError)
510-
return
511-
}
512-
p.Content = buf.String()
513492
}
514493
err := twitterTmpl.Execute(w, struct {
515494
Twitter *chalgen.Twitter
@@ -576,6 +555,23 @@ func (s *Handler) Handle() (string, http.Handler) {
576555
s := tmpl.SearchState{
577556
SearchURL: templ.SafeURL(baseURL + "/search"),
578557
}
558+
t.Search.Password, err = templChals(t.Search.Password)
559+
if err != nil {
560+
http.Error(w, err.Error(), http.StatusInternalServerError)
561+
return
562+
}
563+
564+
var newEntries []string
565+
for _, se := range t.Search.Entry {
566+
se, err = templChals(se)
567+
if err != nil {
568+
http.Error(w, err.Error(), http.StatusInternalServerError)
569+
return
570+
}
571+
newEntries = append(newEntries, se)
572+
}
573+
t.Search.Entry = newEntries
574+
579575
if p == "search" {
580576
err := r.ParseForm()
581577
if err != nil {
@@ -606,6 +602,11 @@ func (s *Handler) Handle() (string, http.Handler) {
606602
}
607603
return
608604
}
605+
_, err := w.Write([]byte(n.Meta.Flag + "\n"))
606+
if err != nil {
607+
http.Error(w, err.Error(), http.StatusInternalServerError)
608+
return
609+
}
609610
for _, h := range s {
610611
_, err := w.Write([]byte(h.Hash + "\n"))
611612
if err != nil {

pkg/chals/tmpl/phone.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ templ App(state PhoneState, i int, a *chalgen.App) {
6161
if a.Html != "" {
6262
@templ.Raw(a.Html)
6363
} else {
64-
<iframe src={a.Url} class="w-full h-full"></iframe>
64+
<iframe src={a.Url} class="w-full h-96"></iframe>
6565
}
6666
}
6767
}

pkg/chals/tmpl/phone_templ.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)