forked from SignTools/SignTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
803 lines (754 loc) · 21.7 KB
/
main.go
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
package main
import (
"SignTools/src/assets"
"SignTools/src/builders"
"SignTools/src/config"
"SignTools/src/storage"
"SignTools/src/tunnel"
"SignTools/src/util"
"archive/tar"
"bytes"
"encoding/xml"
"flag"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
log2 "github.com/labstack/gommon/log"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/tus/tusd/pkg/filestore"
tusd "github.com/tus/tusd/pkg/handler"
"github.com/ziflex/lecho/v2"
htmlTemplate "html/template"
"io"
log3 "log"
"mime"
"mime/multipart"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
textTemplate "text/template"
"time"
)
var formNames = assets.FormNames{
FormFileId: "file_id",
FormTweakIds: "tweak_ids",
FormProfileId: "profile_id",
FormBuilderId: "builder_id",
FormAppDebug: "app_debug",
FormAllDevices: "all_devices",
FormMac: "mac",
FormFileShare: "file_share",
FormToken: "token",
FormId: "id",
FormIdOriginal: "id_original",
FormIdProv: "id_prov",
FormIdCustom: "id_custom",
FormIdCustomText: "id_custom_text",
FormIdEncode: "id_encode",
FormIdPatch: "id_patch",
FormIdForceOriginal: "id_force_original",
FormBundleName: "bundle_name",
}
func main() {
host := flag.String("host", "", "Listen host, empty for all")
port := flag.Uint64("port", 8080, "Listen port")
configFile := flag.String("config", "signer-cfg.yml", "Configuration file")
ngrokPort := flag.Uint64("ngrok-port", 0, "Ngrok web interface port. "+
"Used to automatically parse the server_url")
cloudflaredPort := flag.Uint64("cloudflared-port", 0, "cloudflared metrics port. "+
"Used to automatically parse the server_url")
logJson := flag.Bool("log-json", false, "If enabled, outputs logs in JSON instead of pretty printing them.")
logLevel := flag.Uint("log-level", uint(zerolog.InfoLevel), "Logging level, 0 (debug) - 5 (panic).")
flag.Parse()
zerolog.SetGlobalLevel(zerolog.Level(*logLevel))
if !*logJson {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
config.Load(*configFile)
storage.Load()
switch {
case *ngrokPort != 0:
config.Current.ServerUrl = getPublicUrlFatal(&tunnel.Ngrok{Port: *ngrokPort, Proto: "https"})
case *cloudflaredPort != 0:
config.Current.ServerUrl = getPublicUrlFatal(&tunnel.Cloudflare{Port: *cloudflaredPort})
}
log.Info().Str("url", config.Current.ServerUrl).Msg("using server url")
serve(*host, *port)
}
func getPublicUrlFatal(provider tunnel.Provider) string {
log.Info().Msg("obtaining server url")
serverUrl, err := tunnel.GetPublicUrl(provider, 15*time.Second)
if err != nil {
log.Fatal().Err(err).Send()
}
return serverUrl
}
func serve(host string, port uint64) {
if err := os.MkdirAll(config.Current.SaveDir, 0700); err != nil {
log.Fatal().Err(err).Send()
}
interval := time.Duration(config.Current.CleanupIntervalMins) * time.Minute
timeout := time.Duration(config.Current.SignTimeoutMins) * time.Minute
go func() {
for range time.Tick(interval) {
storage.Jobs.Cleanup(timeout)
storage.Uploads.Cleanup(timeout)
}
}()
log.Info().Msg("setting builder secrets")
for _, builder := range config.Current.Builder {
if err := setBuilderSecrets(builder); err != nil {
log.Fatal().Err(err).Send()
}
}
e := echo.New()
e.HideBanner = true
logger := lecho.From(log.Logger, lecho.WithLevel(log2.INFO))
e.Logger = logger
e.Use(lecho.Middleware(lecho.Config{Logger: logger}))
forcedBasicAuth := middleware.BasicAuth(func(username string, password string, c echo.Context) (bool, error) {
return username == config.Current.BasicAuth.Username && password == config.Current.BasicAuth.Password, nil
})
basicAuth := func(f echo.HandlerFunc) echo.HandlerFunc {
if config.Current.BasicAuth.Enable {
return forcedBasicAuth(f)
} else {
return f
}
}
workflowKeyAuth := middleware.KeyAuth(func(s string, c echo.Context) (bool, error) {
return s == config.Current.BuilderKey, nil
})
if config.Current.RedirectHttps {
e.Pre(middleware.HTTPSRedirectWithConfig(middleware.RedirectConfig{
Code: 302,
}))
}
e.GET("/", renderIndex, basicAuth)
e.GET("/favicon.png", getFavIcon, basicAuth)
e.POST("/apps", uploadUnsignedApp, basicAuth)
e.GET("/apps/:id/signed", appResolver(getSignedApp))
e.GET("/apps/:id/tweaks", appResolver(getTweaks))
e.GET("/apps/:id/unsigned", appResolver(getUnsignedApp))
e.GET("/apps/:id/manifest", appResolver(getManifest))
e.GET("/apps/:id/resign", appResolver(resignApp), basicAuth)
e.GET("/apps/:id/delete", appResolver(deleteApp), basicAuth)
e.GET("/apps/:id/rename", appResolver(renderRenameApp), basicAuth)
e.POST("/apps/:id/rename", appResolver(renameApp), basicAuth)
e.GET("/apps/:id/2fa", appResolver(render2FAPage), basicAuth)
e.POST("/apps/:id/2fa", appResolver(set2FA), basicAuth)
e.GET("/jobs", getLastJob, workflowKeyAuth)
e.GET("/jobs/:id/2fa", jobResolver(get2FA), workflowKeyAuth)
e.POST("/jobs/:id/signed", jobResolver(uploadSignedApp), workflowKeyAuth)
e.GET("/jobs/:id/fail", jobResolver(failJob), workflowKeyAuth)
if err := addTusHandlers(e, map[string]echo.MiddlewareFunc{
"/tus/": basicAuth,
"/jobs/:id/tus/": workflowKeyAuth,
}); err != nil {
log.Fatal().Err(err).Send()
}
log.Fatal().Err(e.Start(fmt.Sprintf("%s:%d", host, port))).Send()
}
type LogToZeroLog struct {
Context string
}
func (l *LogToZeroLog) Write(p []byte) (n int, err error) {
log.Info().Str("data", strings.TrimSpace(string(p))).Msg(l.Context)
return len(p), nil
}
func addTusHandlers(e *echo.Echo, uploadEndpoints map[string]echo.MiddlewareFunc) error {
store := filestore.FileStore{
Path: storage.GetUploadsPath(),
}
composer := tusd.NewStoreComposer()
store.UseIn(composer)
logToZeroLog := LogToZeroLog{Context: "tusd"}
logger := log3.New(&logToZeroLog, "", 0)
handler, err := tusd.NewUnroutedHandler(tusd.Config{
BasePath: "/files/",
StoreComposer: composer,
NotifyCompleteUploads: true,
Logger: logger,
})
//TODO: Apply tus middleware
go func() {
for {
event := <-handler.CompleteUploads
storage.Uploads.Add(event.Upload.ID)
}
}()
if err != nil {
return err
}
for prefix, middlewareFunc := range uploadEndpoints {
e.POST(prefix, func(c echo.Context) error {
handler.PostFile(c.Response().Writer, c.Request())
return nil
}, middlewareFunc)
}
e.HEAD("/files/:file_id", func(c echo.Context) error {
handler.HeadFile(c.Response().Writer, c.Request())
return nil
})
e.PATCH("/files/:file_id", func(c echo.Context) error {
handler.PatchFile(c.Response().Writer, c.Request())
return nil
})
return nil
}
func getTweaks(c echo.Context, app storage.App) error {
tweaks, err := app.ReadDir(storage.TweaksDir)
if os.IsNotExist(err) {
return c.NoContent(404)
} else if err != nil {
return err
}
if len(tweaks) < 1 {
return c.NoContent(404)
}
appName, err := app.GetString(storage.AppName)
if err != nil {
return err
}
c.Response().Header().Set("Content-Type", mime.TypeByExtension(".tar"))
c.Response().Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s-tweaks.tar"`, appName))
writer := tar.NewWriter(c.Response().Writer)
defer writer.Close()
for _, tweak := range tweaks {
tweakPath := storage.FSName(filepath.Join(string(storage.TweaksDir), tweak.Name()))
file, err := app.GetFile(tweakPath)
if err != nil {
return err
}
defer file.Close()
stat, err := app.Stat(tweakPath)
if err != nil {
return err
}
if err := writer.WriteHeader(&tar.Header{
Name: tweak.Name(),
Mode: 0600,
Size: stat.Size(),
}); err != nil {
return err
}
if _, err := io.Copy(writer, file); err != nil {
return err
}
}
return nil
}
func renderRenameApp(c echo.Context, app storage.App) error {
appName, err := app.GetString(storage.AppName)
if err != nil {
return err
}
data := assets.RenameData{AppName: appName}
t, err := htmlTemplate.New("").Parse(assets.RenameHtml)
if err != nil {
return err
}
var result bytes.Buffer
if err := t.Execute(&result, data); err != nil {
return err
}
return c.HTMLBlob(200, result.Bytes())
}
func renameApp(c echo.Context, app storage.App) error {
if err := app.SetString(storage.AppName, c.FormValue("name")); err != nil {
return err
}
return c.Redirect(302, "/")
}
func failJob(c echo.Context, job *storage.ReturnJob) error {
if !storage.Jobs.DeleteById(job.Id) {
return errors.New("unable to delete return job " + job.Id)
}
return c.NoContent(200)
}
func setBuilderSecrets(builder builders.Builder) error {
return builder.SetSecrets(map[string]string{
"SECRET_KEY": config.Current.BuilderKey,
"SECRET_URL": config.Current.ServerUrl,
})
}
func getFavIcon(c echo.Context) error {
http.ServeContent(c.Response(), c.Request(), assets.FavIconStat.Name(), assets.FavIconStat.ModTime(), bytes.NewReader(assets.FavIconBytes))
return nil
}
func uploadSignedApp(c echo.Context, job *storage.ReturnJob) error {
app, ok := storage.Apps.Get(job.AppId)
if !ok {
return errors.New(fmt.Sprintf("return job %s appid %s not resolved", job.Id, job.AppId))
}
fileId := c.FormValue(formNames.FormFileId)
upload, ok := storage.Uploads.Get(fileId)
if !ok {
return errors.New("no app upload file with id " + fileId)
}
defer storage.Uploads.Delete(fileId)
file, err := upload.GetData()
if err != nil {
return err
}
defer file.Close()
if err := app.SetFile(storage.AppSignedFile, file); err != nil {
return err
}
if err := app.SetString(storage.AppBundleId, c.FormValue("bundle_id")); err != nil {
return err
}
if !storage.Jobs.DeleteById(job.Id) {
return errors.New("unable to delete return job " + job.Id)
}
return c.NoContent(200)
}
func get2FA(c echo.Context, job *storage.ReturnJob) error {
code := job.TwoFactorCode.Load()
if code == "" {
return c.NoContent(404)
} else {
return c.String(200, code)
}
}
func appResolver(handler func(echo.Context, storage.App) error) func(c echo.Context) error {
return func(c echo.Context) error {
id := c.Param("id")
app, ok := storage.Apps.Get(id)
if !ok {
return c.NoContent(404)
}
return handler(c, app)
}
}
func jobResolver(handler func(echo.Context, *storage.ReturnJob) error) func(c echo.Context) error {
return func(c echo.Context) error {
id := c.Param("id")
job, ok := storage.Jobs.GetById(id)
if !ok {
return c.NoContent(404)
}
return handler(c, job)
}
}
func getLastJob(c echo.Context) error {
if err := storage.Jobs.TakeLastJob(c.Response()); errors.Is(err, storage.ErrNotFound) {
return c.NoContent(404)
} else if err != nil {
return err
}
return c.NoContent(200)
}
func render2FAPage(c echo.Context, _ storage.App) error {
return c.HTML(200, assets.TwoFactorHtml)
}
func set2FA(c echo.Context, app storage.App) error {
job, ok := storage.Jobs.GetByAppId(app.GetId())
if !ok {
return errors.New("no job found for app " + app.GetId())
}
job.TwoFactorCode.Store(c.FormValue("formToken"))
return c.Redirect(302, "/")
}
func deleteApp(c echo.Context, app storage.App) error {
if err := storage.Apps.Delete(app.GetId()); err != nil {
return err
}
return c.Redirect(302, "/")
}
func getManifest(c echo.Context, app storage.App) error {
manifestBytes, err := makeManifest(getBaseUrl(c), app)
if err != nil {
return err
}
return c.Blob(200, "text/plain", manifestBytes)
}
func getBaseUrl(c echo.Context) string {
serverUrl := url.URL{
Scheme: c.Scheme(),
Host: c.Request().Host,
}
return serverUrl.String()
}
func makeManifest(baseUrl string, app storage.App) ([]byte, error) {
t, err := textTemplate.New("").Funcs(
textTemplate.FuncMap{"escape": func(text string) (string, error) {
return escapeXML(text)
}},
).Parse(assets.ManifestPlist)
if err != nil {
return nil, err
}
appName, err := app.GetString(storage.AppName)
if err != nil {
return nil, err
}
bundleId, err := app.GetString(storage.AppBundleId)
if err != nil {
return nil, err
}
downloadUrl, err := util.JoinUrls(baseUrl, "/apps", app.GetId(), "signed")
if err != nil {
return nil, err
}
data := assets.ManifestData{
DownloadUrl: downloadUrl,
BundleId: bundleId,
Title: appName,
}
var result bytes.Buffer
if err := t.Execute(&result, data); err != nil {
return nil, err
}
return result.Bytes(), nil
}
func escapeXML(str string) (string, error) {
buff := bytes.NewBuffer(nil)
if err := xml.EscapeText(buff, []byte(str)); err != nil {
return "", err
}
return buff.String(), nil
}
func getSignedApp(c echo.Context, app storage.App) error {
file, err := app.GetFile(storage.AppSignedFile)
if err != nil {
return err
}
defer file.Close()
if err := writeFileResponse(c, file, app); err != nil {
return err
}
return nil
}
func getUnsignedApp(c echo.Context, app storage.App) error {
file, err := app.GetFile(storage.AppUnsignedFile)
if err != nil {
return err
}
defer file.Close()
if err := writeFileResponse(c, file, app); err != nil {
return err
}
return nil
}
func writeFileResponse(c echo.Context, file io.ReadSeeker, app storage.App) error {
name, err := app.GetString(storage.AppName)
if err != nil {
return err
}
//TODO: Should use the file's mod time, otherwise may tell client to use cached file even though it has changed
modTime, err := app.GetModTime()
if err != nil {
return err
}
c.Response().Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
http.ServeContent(c.Response(), c.Request(), name, modTime, file)
return nil
}
func uploadUnsignedApp(c echo.Context) error {
profileId := c.FormValue(formNames.FormProfileId)
profile, ok := storage.Profiles.GetById(profileId)
if !ok {
return errors.New("no profile with id " + profileId)
}
builderId := c.FormValue(formNames.FormBuilderId)
builder, ok := config.Current.Builder[builderId]
if !ok {
return errors.New("no builder with id " + builderId)
}
var file multipart.File
var fileName string
fileId := c.FormValue(formNames.FormFileId)
if app, ok := storage.Apps.Get(fileId); ok {
readonlyFile, err := app.GetFile(storage.AppUnsignedFile)
if err != nil {
return err
}
file = readonlyFile
defer file.Close()
fileName, err = app.GetString(storage.AppName)
if err != nil {
return err
}
} else if upload, ok := storage.Uploads.Get(fileId); ok {
defer storage.Uploads.Delete(fileId)
readonlyFile, err := upload.GetData()
if err != nil {
return err
}
file = readonlyFile
defer file.Close()
info, err := upload.GetInfo()
if err != nil {
return err
}
fileName = info.MetaData["filename"]
} else {
return errors.New("no app upload file with id " + fileId)
}
signArgs := ""
if c.FormValue(formNames.FormAllDevices) != "" {
signArgs += " -a"
}
if c.FormValue(formNames.FormMac) != "" {
signArgs += " -m"
}
if c.FormValue(formNames.FormAppDebug) != "" {
signArgs += " -d"
}
if c.FormValue(formNames.FormFileShare) != "" {
signArgs += " -s"
}
if c.FormValue(formNames.FormIdEncode) != "" {
signArgs += " -e"
}
if c.FormValue(formNames.FormIdForceOriginal) != "" {
signArgs += " -o"
}
if c.FormValue(formNames.FormIdPatch) != "" {
signArgs += " -p"
}
idType := c.FormValue(formNames.FormId)
userBundleId := c.FormValue(formNames.FormIdCustomText)
if idType == formNames.FormIdProv {
signArgs += " -n"
} else if idType == formNames.FormIdCustom {
signArgs += " -b " + userBundleId
}
bundleName := c.FormValue(formNames.FormBundleName)
if bundleName != "" {
fileName = fmt.Sprintf("%s (%s)%s",
strings.TrimSuffix(fileName, filepath.Ext(fileName)), bundleName, filepath.Ext(fileName))
}
tweakMap := map[string]io.ReadSeeker{}
tweakIds := c.FormValue(formNames.FormTweakIds)
if tweakIds != "" {
for _, tweakId := range strings.Split(tweakIds, ",") {
tweak, ok := storage.Uploads.Get(tweakId)
if !ok {
return errors.New("no tweak upload file with id " + fileId)
}
defer storage.Uploads.Delete(tweakId)
readonlyFile, err := tweak.GetData()
if err != nil {
return err
}
defer readonlyFile.Close()
info, err := tweak.GetInfo()
if err != nil {
return err
}
tweakMap[info.MetaData["filename"]] = readonlyFile
}
}
app, err := storage.Apps.New(file, fileName, profile, signArgs, userBundleId, builderId, tweakMap)
if err != nil {
return err
}
if bundleName != "" {
if err := app.SetString(storage.AppBundleName, bundleName); err != nil {
return err
}
}
if err := startSign(app, builder); err != nil {
return err
}
return c.Redirect(302, "/")
}
func resignApp(c echo.Context, app storage.App) error {
builderId, err := app.GetString(storage.AppBuilderId)
if err != nil {
return err
}
builder, ok := config.Current.Builder[builderId]
if !ok {
return errors.New("no builder with id " + builderId)
}
if err := app.RemoveFile(storage.AppSignedFile); err != nil && !os.IsNotExist(err) {
return err
}
if err := app.ResetModTime(); err != nil {
return err
}
if err := startSign(app, builder); err != nil {
return err
}
return c.Redirect(302, "/")
}
func startSign(app storage.App, builder builders.Builder) error {
profileId, err := app.GetString(storage.AppProfileId)
if err != nil {
return err
}
storage.Jobs.MakeSignJob(app.GetId(), profileId)
if err := setBuilderSecrets(builder); err != nil {
return err
}
if err := builder.Trigger(); err != nil {
return err
}
statusUrl, err := builder.GetStatusUrl()
if err != nil {
return err
}
if err := app.SetString(storage.AppWorkflowUrl, statusUrl); err != nil {
return err
}
return nil
}
func logErrApp(err error, app storage.App) *zerolog.Event {
return log.Err(err).Str("app_id", app.GetId())
}
func renderIndex(c echo.Context) error {
apps, err := storage.Apps.GetAll()
if err != nil {
return err
}
data := assets.IndexData{
FormNames: formNames,
}
usingManifestProxy := false
for _, app := range apps {
isSigned, err := app.IsSigned()
if err != nil {
return errors.WithMessage(err, "get is signed")
}
modTime, err := app.GetModTime()
if err != nil {
return errors.WithMessage(err, "get mod time")
}
name, err := app.GetString(storage.AppName)
if err != nil {
logErrApp(err, app).Msg("get name")
}
workflowUrl, err := app.GetString(storage.AppWorkflowUrl)
if err != nil {
logErrApp(err, app).Msg("get workflow url")
}
bundleId, _ := app.GetString(storage.AppBundleId)
profileId, err := app.GetString(storage.AppProfileId)
if err != nil {
logErrApp(err, app).Msg("get profile id")
}
var profileName string
if profile, ok := storage.Profiles.GetById(profileId); ok {
profileName, err = profile.GetString(storage.ProfileName)
if err != nil {
logErrApp(err, app).Msg("get profile name")
}
} else {
logErrApp(err, app).Msg("get profile")
profileName = "unknown"
}
jobPending, jobExists := storage.Jobs.GetStatusByAppId(app.GetId())
var status int
if isSigned {
status = assets.AppStatusSigned
} else if jobPending {
status = assets.AppStatusWaiting
} else if jobExists {
status = assets.AppStatusProcessing
} else {
status = assets.AppStatusFailed
}
baseUrl := getBaseUrl(c)
manifestUrl := ""
if strings.HasPrefix(baseUrl, "https") {
// must be a full URL
manifestUrl, err = util.JoinUrls(baseUrl, "/apps", app.GetId(), "manifest")
if err != nil {
return errors.WithMessage(err, "build manifest url")
}
} else {
usingManifestProxy = true
downloadFullUrl, err := util.JoinUrls(baseUrl, "/apps", app.GetId(), "signed")
if err != nil {
return errors.WithMessage(err, "build download url")
}
proxyUrl := url.URL{
Scheme: "https",
Host: "ota.signtools.workers.dev",
Path: "/v1",
}
query := url.Values{
"ipa": []string{downloadFullUrl},
"title": []string{name},
"id": []string{bundleId},
}
proxyUrl.RawQuery = query.Encode()
manifestUrl = proxyUrl.String()
}
tweakCount := 0
if tweaks, err := app.ReadDir(storage.TweaksDir); err == nil {
tweakCount = len(tweaks)
} else if !os.IsNotExist(err) {
return err
}
data.Apps = append(data.Apps, assets.App{
Id: app.GetId(),
Status: status,
Name: name,
ModTime: modTime.Format(time.RFC822),
WorkflowUrl: workflowUrl,
ProfileName: profileName,
BundleId: bundleId,
ManifestUrl: manifestUrl,
DownloadSignedUrl: path.Join("/apps", app.GetId(), "signed"),
DownloadUnsignedUrl: path.Join("/apps", app.GetId(), "unsigned"),
DownloadTweaksUrl: path.Join("/apps", app.GetId(), "tweaks"),
TwoFactorUrl: path.Join("/apps", app.GetId(), "2fa"),
ResignUrl: path.Join("/apps", app.GetId(), "resign"),
DeleteUrl: path.Join("/apps", app.GetId(), "delete"),
RenameUrl: path.Join("/apps", app.GetId(), "rename"),
TweakCount: tweakCount,
})
}
if usingManifestProxy {
log.Warn().Str("base_url", getBaseUrl(c)).Msg("using OTA manifest proxy, installation may not work")
}
profiles, err := storage.Profiles.GetAll()
if err != nil {
return err
}
for _, profile := range profiles {
name, err := profile.GetString(storage.ProfileName)
if err != nil {
return err
}
isAccount, err := profile.IsAccount()
if err != nil {
return err
}
data.Profiles = append(data.Profiles, assets.Profile{
Id: profile.GetId(),
Name: name,
IsAccount: isAccount,
})
}
for builderId := range config.Current.Builder {
data.Builders = append(data.Builders, assets.Builder{
Id: builderId,
Name: builderId,
})
}
sort.Slice(data.Builders, func(i, j int) bool {
name1 := data.Builders[i].Name
name2 := data.Builders[j].Name
return name1 < name2
})
t, err := htmlTemplate.New("").Parse(assets.IndexHtml)
if err != nil {
return err
}
var result bytes.Buffer
if err := t.Execute(&result, data); err != nil {
return err
}
return c.HTMLBlob(200, result.Bytes())
}