Skip to content

Commit

Permalink
fix: small formatting fixes in ipsw dl pcc --info output
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Oct 29, 2024
1 parent e3ba6fa commit 0885754
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
40 changes: 38 additions & 2 deletions cmd/ipsw/cmd/download/download_pcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ THE SOFTWARE.
package download

import (
"encoding/hex"
"fmt"
"sort"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/apex/log"
"github.com/blacktop/ipsw/internal/download"
"github.com/fatih/color"
Expand All @@ -34,7 +38,9 @@ import (
func init() {
DownloadCmd.AddCommand(pccCmd)

pccCmd.Flags().BoolP("info", "i", false, "Show PCC Release info")
pccCmd.Flags().StringP("output", "o", "", "Output directory to save files to")
viper.BindPFlag("download.pcc.info", pccCmd.Flags().Lookup("info"))
viper.BindPFlag("download.pcc.output", pccCmd.Flags().Lookup("output"))

pccCmd.SetHelpFunc(func(c *cobra.Command, s []string) {
Expand Down Expand Up @@ -83,9 +89,39 @@ var pccCmd = &cobra.Command{
if err != nil {
return err
}
sort.Sort(download.ByPccIndex(releases))

for _, release := range releases {
fmt.Println(release)
if len(releases) == 0 {
return fmt.Errorf("no PCC Releases found")
}

if viper.GetBool("download.pcc.info") {
log.Infof("Found %d PCC Releases", len(releases))
for _, release := range releases {
fmt.Println(release)
}
} else {
var choices []string
for _, r := range releases {
choices = append(choices, fmt.Sprintf("%04d: %s [created: %s]",
r.Index,
hex.EncodeToString(r.GetReleaseHash()),
r.GetTimestamp().AsTime().Format("2006-01-02 15:04:05"),
))
}

choice := 0
prompt := &survey.Select{
Message: "PCC Release to download:",
Options: choices,
PageSize: 15,
}
if err := survey.AskOne(prompt, &choice); err == terminal.InterruptErr {
log.Warn("Exiting...")
return nil
}

return releases[choice].Download(viper.GetString("download.pcc.output"))
}

return nil
Expand Down
33 changes: 21 additions & 12 deletions internal/download/pcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
"time"

"github.com/apex/log"
"github.com/blacktop/go-plist"
"github.com/blacktop/ipsw/internal/download/pcc"
"github.com/blacktop/ipsw/internal/utils"
Expand Down Expand Up @@ -71,24 +71,29 @@ type PCCRelease struct {
*ATLeaf
}

type ByPccIndex []PCCRelease

func (a ByPccIndex) Len() int { return len(a) }
func (a ByPccIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByPccIndex) Less(i, j int) bool { return a[i].Index > a[j].Index }

func (r PCCRelease) String() string {
var out string
out += fmt.Sprintf("Index: %d\n", r.Index)
out += fmt.Sprintf("%d) %s\n", r.Index, hex.EncodeToString(r.GetReleaseHash()))
out += fmt.Sprintf("Type: %s\n", pcc.ATLogDataType(r.Type).String())
out += fmt.Sprintf("Timestamp: %s\n", time.Time(r.GetTimestamp().AsTime()).Format(time.RFC3339))
out += fmt.Sprintf("Expires: %s\n", time.UnixMilli(r.ExpiryMS).Format(time.RFC3339))
out += fmt.Sprintf("Hash: %s\n", hex.EncodeToString(r.GetReleaseHash()))
out += "Assets:\n"
for _, asset := range r.GetAssets() {
out += fmt.Sprintf(" Type: %s\n", asset.GetType().String())
out += fmt.Sprintf(" Variant: %s\n", asset.GetVariant())
out += fmt.Sprintf(" Digest: (%s) %s\n", strings.TrimPrefix(asset.Digest.GetDigestAlg().String(), "DIGEST_ALG_"), hex.EncodeToString(asset.Digest.GetValue()))
out += fmt.Sprintf(" URL: %s\n", asset.GetUrl())
out += fmt.Sprintf(" [%s]\n", strings.TrimPrefix(asset.GetType().String(), "ASSET_TYPE_"))
out += fmt.Sprintf(" Variant: %s\n", asset.GetVariant())
out += fmt.Sprintf(" Digest: (%s) %s\n", strings.TrimPrefix(asset.Digest.GetDigestAlg().String(), "DIGEST_ALG_"), hex.EncodeToString(asset.Digest.GetValue()))
out += fmt.Sprintf(" URL: %s\n", asset.GetUrl())
}
out += "Tickets:\n"
hash := sha256.New()
hash.Write(r.Ticket.ApTicket.Bytes)
out += fmt.Sprintf(" ApTicket: %s\n", hex.EncodeToString(hash.Sum(nil)))
out += fmt.Sprintf(" OS: %s\n", hex.EncodeToString(hash.Sum(nil)))
out += fmt.Sprintf(" [expires: %s]\n", time.UnixMilli(r.ExpiryMS).Format("2006-01-02 15:04:05"))
out += fmt.Sprintf(" [created: %s]\n", r.GetTimestamp().AsTime().Format("2006-01-02 15:04:05"))
out += " Cryptexes:\n"
for i, ct := range r.Ticket.CryptexTickets {
hash.Reset()
Expand All @@ -101,6 +106,12 @@ func (r PCCRelease) String() string {
return out
}

func (r PCCRelease) Download(output string) error {
log.Infof("Downloading PCC Release for %d", r.Index)
log.Warn("Download not implemented")
return nil
}

func parseAtLeaf(r *bytes.Reader) (*ATLeaf, error) {
var leaf ATLeaf

Expand Down Expand Up @@ -166,8 +177,6 @@ func GetPCCReleases(proxy string) ([]PCCRelease, error) {
}
res.Body.Close()

os.WriteFile("bag.plist", body, 0644)

var bag BagResponse
if _, err := plist.Unmarshal(body, &bag); err != nil {
return nil, fmt.Errorf("cannot unmarshal plist: %v", err)
Expand Down

0 comments on commit 0885754

Please sign in to comment.