@@ -58,11 +58,16 @@ func init() {
58
58
gob .Register (tmpl.PhoneState {})
59
59
}
60
60
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 {
62
63
path := fmt .Sprintf ("/play/%s/%s" , compId , chalID )
63
64
if host == "" {
64
65
return path
65
66
}
67
+ scheme := "http"
68
+ if isHttps {
69
+ scheme = "https"
70
+ }
66
71
u := url.URL {
67
72
// TODO breadchris check if the original request was https
68
73
Scheme : scheme ,
@@ -142,9 +147,10 @@ func (s *Handler) Handle() (string, http.Handler) {
142
147
143
148
// TODO breadchris find dependencies of referenced challenge and build those
144
149
challenges := map [string ]string {}
150
+ isHttps := r .TLS != nil
145
151
for _ , n := range graph .Nodes {
146
152
view := ""
147
- chalURL := ChalURL (r . URL . Scheme , compId , n .Meta .Id , r .Host )
153
+ chalURL := ChalURL (isHttps , compId , n .Meta .Id , r .Host )
148
154
switch u := n .Challenge .(type ) {
149
155
case * chalgen.Node_Base :
150
156
switch t := u .Base .Type .(type ) {
@@ -171,6 +177,23 @@ func (s *Handler) Handle() (string, http.Handler) {
171
177
}
172
178
}
173
179
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
+
174
197
for _ , n := range graph .Nodes {
175
198
if n .Meta .Id == chalId {
176
199
switch u := n .Challenge .(type ) {
@@ -277,22 +300,12 @@ func (s *Handler) Handle() (string, http.Handler) {
277
300
278
301
var newUrls []string
279
302
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 )
291
304
if err != nil {
292
305
http .Error (w , err .Error (), http .StatusInternalServerError )
293
306
return
294
307
}
295
- newUrls = append (newUrls , buf . String () )
308
+ newUrls = append (newUrls , ul )
296
309
}
297
310
t .Filemanager .Urls = newUrls
298
311
@@ -354,22 +367,11 @@ func (s *Handler) Handle() (string, http.Handler) {
354
367
}
355
368
}
356
369
for _ , app := range t .Phone .Apps {
357
- nt , err := ttemplate . New ( "app" ). Parse (app .Url )
370
+ app . Url , err = templChals (app .Url )
358
371
if err != nil {
359
372
http .Error (w , err .Error (), http .StatusInternalServerError )
360
373
return
361
374
}
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 ()
373
375
}
374
376
templ .Handler (tmpl .Page (tmpl .Phone (tmpl.PhoneState {
375
377
Flag : n .Meta .Flag ,
@@ -445,23 +447,11 @@ func (s *Handler) Handle() (string, http.Handler) {
445
447
446
448
for _ , p := range t .Slack .Channels {
447
449
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 )
460
451
if err != nil {
461
452
http .Error (w , err .Error (), http .StatusInternalServerError )
462
453
return
463
454
}
464
- n .Content = buf .String ()
465
455
}
466
456
}
467
457
@@ -494,22 +484,11 @@ func (s *Handler) Handle() (string, http.Handler) {
494
484
return
495
485
case * chalgen.Challenge_Twitter :
496
486
for _ , p := range t .Twitter .Posts {
497
- nt , err := ttemplate . New ( "twitter" ). Parse (p .Content )
487
+ p . Content , err = templChals (p .Content )
498
488
if err != nil {
499
489
http .Error (w , err .Error (), http .StatusInternalServerError )
500
490
return
501
491
}
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 ()
513
492
}
514
493
err := twitterTmpl .Execute (w , struct {
515
494
Twitter * chalgen.Twitter
@@ -576,6 +555,23 @@ func (s *Handler) Handle() (string, http.Handler) {
576
555
s := tmpl.SearchState {
577
556
SearchURL : templ .SafeURL (baseURL + "/search" ),
578
557
}
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
+
579
575
if p == "search" {
580
576
err := r .ParseForm ()
581
577
if err != nil {
@@ -606,6 +602,11 @@ func (s *Handler) Handle() (string, http.Handler) {
606
602
}
607
603
return
608
604
}
605
+ _ , err := w .Write ([]byte (n .Meta .Flag + "\n " ))
606
+ if err != nil {
607
+ http .Error (w , err .Error (), http .StatusInternalServerError )
608
+ return
609
+ }
609
610
for _ , h := range s {
610
611
_ , err := w .Write ([]byte (h .Hash + "\n " ))
611
612
if err != nil {
0 commit comments