-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathitunes.go
108 lines (87 loc) · 2.54 KB
/
itunes.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
package ipsw
import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"strings"
"howett.net/plist"
)
type BuildNumber string
func (b BuildNumber) String() string {
return string(b)
}
type IndividualBuild struct {
BuildVersion BuildNumber
DocumentationURL string
FirmwareURL string
FirmwareSHA1 string
ProductVersion string
}
type BuildInformation struct {
Restore *IndividualBuild
Update *IndividualBuild
SameAs BuildNumber
OfferRestoreAsUpdate bool
}
type Identifier string
func (i Identifier) String() string {
return string(i)
}
type VersionWrapper struct {
MobileDeviceSoftwareVersions map[Identifier]map[BuildNumber]*BuildInformation
}
type iTunesVersionMaster struct {
MobileDeviceSoftwareVersionsByVersion map[string]*VersionWrapper
MobileDeviceSoftwareVersions map[string]*VersionWrapper
}
// Given a device, find a URL from the version master
// the URL itself doesn't matter, so long as it's an IPSW for the device we requested
func (vm *iTunesVersionMaster) GetSoftwareURLFor(identifier string) (string, error) {
for _, deviceSoftwareVersions := range vm.MobileDeviceSoftwareVersionsByVersion {
for i, builds := range deviceSoftwareVersions.MobileDeviceSoftwareVersions {
if i.String() == identifier {
for _, build := range builds {
if build.Restore != nil {
// don't return protected ones if we can avoid it
if strings.Contains(build.Restore.FirmwareURL, "protected://") {
continue
}
return build.Restore.FirmwareURL, nil
}
}
}
}
}
for _, deviceSoftwareVersions := range vm.MobileDeviceSoftwareVersions {
for i, builds := range deviceSoftwareVersions.MobileDeviceSoftwareVersions {
if i.String() == identifier {
for _, build := range builds {
if build.Restore != nil {
// don't return protected ones if we can avoid it
if strings.Contains(build.Restore.FirmwareURL, "protected://") {
continue
}
return build.Restore.FirmwareURL, nil
}
}
}
}
}
return "", errors.New("Unable to find identifier")
}
// NewiTunesVersionMaster creates a new iTunesVersionMaster struct, parsed and ready to use
func NewiTunesVersionMaster(url string) (*iTunesVersionMaster, error) {
resp, err := DefaultClient.Get(fmt.Sprintf("%s?%d", url, rand.Int()))
if err != nil {
return nil, err
}
defer resp.Body.Close()
document, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
vm := iTunesVersionMaster{}
_, err = plist.Unmarshal(document, &vm)
return &vm, err
}