· 5 years ago · Jul 13, 2020, 10:38 AM
1#!/bin/sh
2set -e
3# install via:
4# 'curl -sSL https://app.genesis-hive.com/downloads/hivedatacollector.sh | sh'
5# or:
6# 'wget -qO- https://app.genesis-hive.com/downloads/hivedatacollector.sh | sh'
7#
8
9# How to invoke this script via terminal without any GUI
10# ./hivedatacollector.sh mail@mail.com password123 12 172.17.0.2/16 "Enigma 1"
11
12email=${1:-""}
13password=${2:-""}
14farmid=${3:-""}
15scanRange=${4:-""}
16dc_name=${5:-""}
17two_fa=
18userkey=
19dckey=
20farm_list=
21farmid_list=
22hive_url="https://app.genesis-hive.com"
23
24
25echo $email
26
27HIVE_DIR="/opt/hive"
28
29: ${INTERACTIVE:=1}
30
31# set -x
32
33do_about() {
34 local about_text
35 about_text="\
36This install skript will install a Hive Datacollector on a recent \
37(15.04 and up) Ubuntu or Raspbian Stretch System. Use your Genesis \
38Hive Login, choose your farm and you're ready to go."
39 [ $INTERACTIVE -eq 1 ] \
40 && whiptail --msgbox "$about_text" 10 60 || true
41}
42
43die() {
44 [ $INTERACTIVE -eq 1 ] \
45 && whiptail --msgbox "$*" 8 40 \
46 || printf "%s\n" "$*" >&2
47 exit 2
48}
49
50# check if we have super powers
51check_root() {
52 user="$(id -un 2>/dev/null || true)"
53 sh_c='sh -c'
54 if [ ${user} != 'root' ]; then
55 die "Script needs root privileges to install."
56 fi
57}
58
59# check for previous installation
60check_install() {
61 # check if none-zero file exists
62 [ -s "${HIVE_DIR}/config.json" ] && die "Already installed. Will stop now." || true
63}
64
65check_dist() {
66 # check for ubuntu
67 local year month
68 set +e
69 lsb_dist=$(lsb_release -a 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'id' | cut -d ':' -f 2 | tr -d '[[:space:]]')
70 dist_version=$(lsb_release -a 2>&1 | grep -i 'release' | cut -d ':' -f 2 | tr -d '[[:space:]]')
71 set -e
72 year=${dist_version%.*}
73 month=${dist_version#*.}
74 [ "$lsb_dist" = "raspbian" ] && return
75 [ $year -lt 15 ] || ( [ $year = 15 ] && [ $month -le 4 ] ) && die "Your ${lsb_dist} ${dist_version} is too old." || true
76}
77
78set_scanRange() {
79 scanRange=$(ip a show scope global | awk '/inet/ {print $2 ; exit}')
80 scanRange=$(whiptail --nocancel --inputbox "enter your network range to scan for miners" 10 60 $scanRange 3>&1 1>&2 2>&3)
81 echo $scanRange | grep -q -e '^[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\/[[:digit:]]\{1,2\}$' \
82 || die "Not a network range."
83}
84
85# download and install required packages
86install_packages() {
87 local pkgs_fs pkgs_install
88
89 printf "updating package list.."
90 apt-get -q update
91 printf "done.\n"
92
93 pkgs_install="jq build-essential" # jq for json parsing, build-essential for compiling python packages
94 pkgs_fs="openssh-server openssh-client sshpass nmap arp-scan openvpn stunnel4 python-apt"
95
96 printf "installing packages.."
97 apt-get -q -y install ${pkgs_install} ${pkgs_fs}
98 printf "done.\n"
99}
100
101ask_login() {
102 email=$(whiptail --inputbox "Hive email:" 8 40 3>&1 1>&2 2>&3)
103 password=$(whiptail --passwordbox "Hive password:" 8 40 3>&1 1>&2 2>&3)
104 two_fa=$(whiptail --inputbox "Hive 2FA-Code:" 8 40 3>&1 1>&2 2>&3)
105}
106
107ask_farmid() {
108 # needs farm_list
109 # needs farmid_list
110 # sets farmid
111 local farm
112 # farm_list="Anton Berta Caesar"
113 # famdid_list="1 5 40"
114
115 farm_menu=
116
117 IFS=:
118 for f in $farm_list; do
119 farm_menu="${farm_menu}$f$IFS''$IFS"
120 done
121
122 farm=$(whiptail --nocancel --ok-button Select --menu "Select your Farm:" 15 40 5 \
123 ${farm_menu} 3>&1 1>&2 2>&3)
124
125 n=1
126 for f in $farm_list; do [ $f = $farm ] && break || n=$(( n+1 )); done
127 farmid=$(printf "$farmid_list" | cut -d ":" -f $n)
128 unset IFS
129}
130
131check_hive_url() {
132 # check if a alternative HIVE_URL is set
133 [ ! -z "$HIVE_URL" ] && hive_url=$HIVE_URL || true
134}
135
136get_user_apikey() {
137 # needs email and password
138 # sets userkey
139 local result
140
141 if [ -z "$password" ] | [ -z "$email" ]; then
142 ask_login
143 fi
144
145 result=$(curl --silent --data-urlencode "email=$email" \
146 --data-urlencode "password=$password" \
147 --data-urlencode "twoFaCode=$two_fa" \
148 "$hive_url/scripts.php?id=login")
149
150 response_status=$(echo "$result" | jq '.status' | tr -d '"')
151
152 if [ "$response_status" = "error" ]
153 then
154 echo "Password or 2FA code is incorrect. Please try again"
155 exit 1
156 fi
157
158 userkey=$(printf "$result" | jq -r '.result.apiKey')
159}
160
161get_farm_list() {
162 # needs userkey
163 # sets farm_list farmid_list
164 local result
165
166 result=$(curl --silent "$hive_url/scripts.php?id=getFarms&apiKey=$userkey")
167 farm_list=$(printf "$result" | jq -r '[.result[].name] | join (":")')
168 farmid_list=$(printf "$result" | jq -r '[.result[].id] | join (":")')
169}
170
171download_datacollector() {
172 # downloads datacollector
173 local tfile
174 tfile=$(tempfile)
175
176 printf "downloading farmserver package ..."
177 curl --silent -o ${tfile} "${hive_url}/downloads/devices/datacollector.tar.gz" \
178 || die "Failed to download farmserver."
179 tar -xzf ${tfile} -C /
180 rm ${tfile}
181
182 printf "done.\n"
183}
184
185# download and install VPN certificate + dc api key
186download_dc_data() {
187 # needs userkey
188 # needs farmid
189 # sets dckey
190 local cert
191 local reqf
192 cert=$(tempfile)
193 reqf=$(tempfile)
194
195 printf "downloading dc data ..."
196 printf "{\"type\":\"getDcInstallData\",\"apiKey\":\"${userkey}\",\"data\":{\"farmId\":\"${farmid}\",\"dcName\":\"${dc_name}\"}}" >${reqf}
197 curl --silent -o ${cert} --form "0=@${reqf}" "${hive_url}/api.php" || die "Downloading certificate failed."
198 jq -e '.status == "ok" and .result.base64CertArchive | type != "string"' ${cert} >/dev/null || die "Downloading certificate failed."
199 dckey=$(jq -r '.result.dcApiKey' ${cert})
200 jq -r '.result.base64CertArchive' ${cert} | base64 -d | tar -xz -C /etc/openvpn/
201 rm ${reqf}
202 rm ${cert}
203
204 printf "done.\n"
205}
206
207install_datacollector() {
208 sh /tmp/setup_datacollector.sh
209 rm /tmp/setup_datacollector.sh
210 printf '{\n\t"apiKey": "%s", \n\t"scanRange": "%s", \n\t"hiveUrl": "%s"\n}' ${dckey} ${scanRange} ${hive_url} > ${HIVE_DIR}/config.json
211}
212
213restart_services() {
214 systemctl daemon-reload
215 systemctl restart openvpn.service
216}
217
218# pack everything in a function to ensure only a complete script executes
219main() {
220 do_about
221 check_root
222 check_install
223 check_dist
224 install_packages
225 check_hive_url
226 get_user_apikey
227 get_farm_list
228 ask_farmid
229 set_scanRange
230 download_datacollector
231 download_dc_data
232 install_datacollector
233 restart_services
234 whiptail --msgbox "Successfully installed Hive Datacollector." 8 60
235}
236
237main