-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.sh
executable file
·177 lines (140 loc) · 3.65 KB
/
main.sh
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
#!/usr/bin/env bash
log_info() {
echo -e "\033[32m[info]\033[0m ${1}"
}
log_debug() {
echo -e "\033[34m[debug]\033[0m ${1}"
}
log_warn() {
echo -e "\033[33m[warn]\033[0m ${1}" >&2
}
log_error() {
echo -e "\033[31m[error]\033[0m ${1}" >&2
}
# Assert that the account is set.
if [[ -z "${KEYGEN_ACCOUNT}" ]]
then
log_error 'env var KEYGEN_ACCOUNT is not set!'
exit 1
fi
# Assert a license key is set or provided.
if [[ -z "${KEYGEN_LICENSE}" ]]
then
log_warn "env var KEYGEN_LICENSE is not set!"
echo -ne "\033[34mPlease enter a license key: \033[0m"
read KEYGEN_LICENSE
fi
# Assert jq is installed.
if ! command -v jq &> /dev/null
then
log_error 'jq command is not installed!'
exit 1
fi
# Detect current OS.
os="${OS:-$(uname -s | tr '[:upper:]' '[:lower:]')}"
case "${os}"
in
msys*|mingw*)
os='windows'
;;
cygwin*)
os='linux'
;;
esac
if [[ -z "${os}" ]]
then
log_error 'unable to detect operating system'
exit 1
fi
# Fingerprint current machine.
fingerprint=''
case "${os}"
in
darwin)
fingerprint=$(ioreg -d2 -c IOPlatformExpertDevice | awk -F\" '/IOPlatformUUID/{print $(NF-1)}')
;;
linux)
fingerprint=$(cat /var/lib/dbus/machine-id 2>/dev/null)
# Fallback to checking the /etc directory.
if [[ -z "${fingerprint}" ]]
then
fingerprint=$(cat /etc/machine-id 2>/dev/null)
fi
;;
*bsd)
fingerprint=$(cat /etc/hostid 2>/dev/null)
;;
*)
log_error "unsupported operating system: ${os}"
exit 1
;;
esac
if [[ -z "${fingerprint}" ]]
then
log_error 'unable to fingerprint machine'
exit 1
fi
# Hash the fingerprint, to anonymize it.
fingerprint=$(echo -n "$fingerprint" | shasum -a 256 | head -c 64)
# Validate the license, scoped to the current machine's fingerprint.
read -d '\n' code id <<<$(
curl -s -X POST "https://api.keygen.sh/v1/accounts/${KEYGEN_ACCOUNT}/licenses/${KEYGEN_LICENSE}/actions/validate" \
-H "Authorization: License ${KEYGEN_LICENSE}" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Keygen-Version: 1.2' \
-d '{
"meta": {
"scope": { "fingerprint": "'$fingerprint'" }
}
}' | jq '.meta.code, .data.id' --raw-output
)
case "${code}"
in
# When license is already valid, that means the machine has already been activated.
VALID)
log_info "license ${id} is already activated!"
exit 0
;;
# Otherwise, attempt to activate the machine.
FINGERPRINT_SCOPE_MISMATCH|NO_MACHINES|NO_MACHINE)
log_debug "license ${id} has not been activated yet!"
log_debug 'activating...'
debug=$(mktemp)
status=$(
curl -s -X POST "https://api.keygen.sh/v1/accounts/${KEYGEN_ACCOUNT}/machines" \
-H "Authorization: License ${KEYGEN_LICENSE}" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Keygen-Version: 1.2' \
-w '%{http_code}' \
-o "$debug" \
-d '{
"data": {
"type": "machines",
"attributes": {
"fingerprint": "'$fingerprint'",
"platform": "'$os'"
},
"relationships": {
"license": {
"data": { "type": "licenses", "id": "'$id'" }
}
}
}
}'
)
if [[ "$status" -eq 201 ]]
then
log_info "license ${id} has been activated!"
exit 0
fi
log_error "license activation failed: ${status}"
log_debug "$(cat $debug | jq -Mc '.errors[] | [.detail, .code]')"
exit 1
;;
*)
log_error "License is invalid: ${code}"
exit 1
;;
esac