forked from zoldur/CropCoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcropcoin.sh
executable file
·297 lines (259 loc) · 9 KB
/
cropcoin.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
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
#!/bin/bash
TMP_FOLDER=$(mktemp -d)
CONFIG_FILE="cropcoin.conf"
BINARY_FILE="/usr/local/bin/cropcoind"
CROP_REPO="https://github.com/Cropdev/CropDev.git"
COIN_TGZ='https://github.com/zoldur/CropCoin/files/1804270/cropcoind.gz'
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
function compile_error() {
if [ "$?" -gt "0" ];
then
echo -e "${RED}Failed to compile $@. Please investigate.${NC}"
exit 1
fi
}
function checks() {
if [[ $(lsb_release -d) != *16.04* ]]; then
echo -e "${RED}You are not running Ubuntu 16.04. Installation is cancelled.${NC}"
exit 1
fi
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}$0 must be run as root.${NC}"
exit 1
fi
if [ -n "$(pidof cropcoind)" ]; then
echo -e "${GREEN}\c"
read -e -p "Cropcoind is already running. Do you want to add another MN? [Y/N]" NEW_CROP
echo -e "{NC}"
clear
else
NEW_CROP="new"
fi
}
function prepare_system() {
echo -e "Prepare the system to install Cropcoin master node."
apt-get update >/dev/null 2>&1
DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -qq upgrade >/dev/null 2>&1
apt install -y software-properties-common >/dev/null 2>&1
echo -e "${GREEN}Adding bitcoin PPA repository"
apt-add-repository -y ppa:bitcoin/bitcoin >/dev/null 2>&1
echo -e "Installing required packages, it may take some time to finish.${NC}"
apt-get update >/dev/null 2>&1
apt-get install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" make software-properties-common \
build-essential libtool autoconf libssl-dev libboost-dev libboost-chrono-dev libboost-filesystem-dev libboost-program-options-dev \
libboost-system-dev libboost-test-dev libboost-thread-dev sudo automake git wget pwgen curl libdb4.8-dev bsdmainutils \
libdb4.8++-dev libminiupnpc-dev libgmp3-dev ufw fail2ban pwgen
clear
if [ "$?" -gt "0" ];
then
echo -e "${RED}Not all required packages were installed properly. Try to install them manually by running the following commands:${NC}\n"
echo "apt-get update"
echo "apt -y install software-properties-common"
echo "apt-add-repository -y ppa:bitcoin/bitcoin"
echo "apt-get update"
echo "apt install -y make build-essential libtool software-properties-common autoconf libssl-dev libboost-dev libboost-chrono-dev libboost-filesystem-dev \
libboost-program-options-dev libboost-system-dev libboost-test-dev libboost-thread-dev sudo automake git pwgen curl libdb4.8-dev \
bsdmainutils libdb4.8++-dev libminiupnpc-dev libgmp3-dev ufw fail2ban "
exit 1
fi
clear
echo -e "Checking if swap space is needed."
PHYMEM=$(free -g|awk '/^Mem:/{print $2}')
if [ "$PHYMEM" -lt "2" ];
then
echo -e "${GREEN}Server is running with less than 2G of RAM, creating 2G swap file.${NC}"
dd if=/dev/zero of=/swapfile bs=1024 count=2M
chmod 600 /swapfile
mkswap /swapfile
swapon -a /swapfile
else
echo -e "${GREEN}Server running with at least 2G of RAM, no swap needed.${NC}"
fi
clear
}
function deploy_binaries() {
cd $TMP
wget -q $COIN_TGZ >/dev/null 2>&1
gunzip cropcoind.gz >/dev/null 2>&1
chmod +x cropcoind >/dev/null 2>&1
cp cropcoind /usr/local/bin/ >/dev/null 2>&1
}
function ask_permission() {
echo -e "${RED}I trust zoldur and want to use binaries compiled on his server.${NC}."
echo -e "Please type ${RED}YES${NC} if you want to use precompiled binaries, or type anything else to compile them on your server"
read -e ZOLDUR
}
function compile_cropcoin() {
echo -e "Clone git repo and compile it. This may take some time. Press a key to continue."
read -n 1 -s -r -p ""
git clone $CROP_REPO $TMP_FOLDER
cd $TMP_FOLDER/src
mkdir obj/support
mkdir obj/crypto
make -f makefile.unix
compile_error cropcoin
cp -a cropcoind $BINARY_FILE
clear
}
function enable_firewall() {
echo -e "Installing ${GREEN}fail2ban${NC} and setting up firewall to allow ingress on port ${GREEN}$CROPCOINPORT${NC}"
ufw allow $CROPCOINPORT/tcp comment "Cropcoin MN port" >/dev/null
ufw allow $[CROPCOINPORT+1]/tcp comment "Cropcoin RPC port" >/dev/null
ufw allow ssh >/dev/null 2>&1
ufw limit ssh/tcp >/dev/null 2>&1
ufw default allow outgoing >/dev/null 2>&1
echo "y" | ufw enable >/dev/null 2>&1
systemctl enable fail2ban >/dev/null 2>&1
systemctl start fail2ban >/dev/null 2>&1
}
function systemd_cropcoin() {
cat << EOF > /etc/systemd/system/$CROPCOINUSER.service
[Unit]
Description=Cropcoin service
After=network.target
[Service]
Type=forking
User=$CROPCOINUSER
Group=$CROPCOINUSER
WorkingDirectory=$CROPCOINHOME
ExecStart=$BINARY_FILE -daemon
ExecStop=$BINARY_FILE stop
Restart=always
PrivateTmp=true
TimeoutStopSec=60s
TimeoutStartSec=10s
StartLimitInterval=120s
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
sleep 3
systemctl start $CROPCOINUSER.service
systemctl enable $CROPCOINUSER.service >/dev/null 2>&1
if [[ -z $(pidof cropcoind) ]]; then
echo -e "${RED}Cropcoind is not running${NC}, please investigate. You should start by running the following commands as root:"
echo "systemctl start $CROPCOINUSER.service"
echo "systemctl status $CROPCOINUSER.service"
echo "less /var/log/syslog"
exit 1
fi
}
function ask_port() {
DEFAULTCROPCOINPORT=17720
read -p "CROPCOIN Port: " -i $DEFAULTCROPCOINPORT -e CROPCOINPORT
: ${CROPCOINPORT:=$DEFAULTCROPCOINPORT}
}
function ask_user() {
DEFAULTCROPCOINUSER="cropcoin"
read -p "Cropcoin user: " -i $DEFAULTCROPCOINUSER -e CROPCOINUSER
: ${CROPCOINUSER:=$DEFAULTCROPCOINUSER}
if [ -z "$(getent passwd $CROPCOINUSER)" ]; then
useradd -m $CROPCOINUSER
USERPASS=$(pwgen -s 12 1)
echo "$CROPCOINUSER:$USERPASS" | chpasswd
CROPCOINHOME=$(sudo -H -u $CROPCOINUSER bash -c 'echo $HOME')
DEFAULTCROPCOINFOLDER="$CROPCOINHOME/.cropcoin"
read -p "Configuration folder: " -i $DEFAULTCROPCOINFOLDER -e CROPCOINFOLDER
: ${CROPCOINFOLDER:=$DEFAULTCROPCOINFOLDER}
mkdir -p $CROPCOINFOLDER
chown -R $CROPCOINUSER: $CROPCOINFOLDER >/dev/null
else
clear
echo -e "${RED}User exits. Please enter another username: ${NC}"
ask_user
fi
}
function check_port() {
declare -a PORTS
PORTS=($(netstat -tnlp | awk '/LISTEN/ {print $4}' | awk -F":" '{print $NF}' | sort | uniq | tr '\r\n' ' '))
ask_port
while [[ ${PORTS[@]} =~ $CROPCOINPORT ]] || [[ ${PORTS[@]} =~ $[CROPCOINPORT+1] ]]; do
clear
echo -e "${RED}Port in use, please choose another port:${NF}"
ask_port
done
}
function create_config() {
RPCUSER=$(pwgen -s 8 1)
RPCPASSWORD=$(pwgen -s 15 1)
cat << EOF > $CROPCOINFOLDER/$CONFIG_FILE
rpcuser=$RPCUSER
rpcpassword=$RPCPASSWORD
rpcallowip=127.0.0.1
rpcport=$[CROPCOINPORT+1]
listen=1
server=1
daemon=1
port=$CROPCOINPORT
EOF
}
function create_key() {
echo -e "Enter your ${RED}Masternode Private Key${NC}. Leave it blank to generate a new ${RED}Masternode Private Key${NC} for you:"
read -e CROPCOINKEY
if [[ -z "$CROPCOINKEY" ]]; then
sudo -u $CROPCOINUSER /usr/local/bin/cropcoind -conf=$CROPCOINFOLDER/$CONFIG_FILE -datadir=$CROPCOINFOLDER
sleep 5
if [ -z "$(pidof cropcoind)" ]; then
echo -e "${RED}Cropcoind server couldn't start. Check /var/log/syslog for errors.{$NC}"
exit 1
fi
CROPCOINKEY=$(sudo -u $CROPCOINUSER $BINARY_FILE -conf=$CROPCOINFOLDER/$CONFIG_FILE -datadir=$CROPCOINFOLDER masternode genkey)
sudo -u $CROPCOINUSER $BINARY_FILE -conf=$CROPCOINFOLDER/$CONFIG_FILE -datadir=$CROPCOINFOLDER stop
fi
}
function update_config() {
sed -i 's/daemon=1/daemon=0/' $CROPCOINFOLDER/$CONFIG_FILE
NODEIP=$(curl -s4 icanhazip.com)
cat << EOF >> $CROPCOINFOLDER/$CONFIG_FILE
logtimestamps=1
maxconnections=256
masternode=1
masternodeaddr=$NODEIP:$CROPCOINPORT
masternodeprivkey=$CROPCOINKEY
EOF
chown -R $CROPCOINUSER: $CROPCOINFOLDER >/dev/null
}
function important_information() {
echo
echo -e "================================================================================================================================"
echo -e "Cropcoin Masternode is up and running as user ${GREEN}$CROPCOINUSER${NC} and it is listening on port ${GREEN}$CROPCOINPORT${NC}."
echo -e "${GREEN}$CROPCOINUSER${NC} password is ${RED}$USERPASS${NC}"
echo -e "Configuration file is: ${RED}$CROPCOINFOLDER/$CONFIG_FILE${NC}"
echo -e "Start: ${RED}systemctl start $CROPCOINUSER.service${NC}"
echo -e "Stop: ${RED}systemctl stop $CROPCOINUSER.service${NC}"
echo -e "VPS_IP:PORT ${RED}$NODEIP:$CROPCOINPORT${NC}"
echo -e "MASTERNODE PRIVATEKEY is: ${RED}$CROPCOINKEY${NC}"
echo -e "================================================================================================================================"
}
function setup_node() {
ask_user
check_port
create_config
create_key
update_config
enable_firewall
systemd_cropcoin
important_information
}
##### Main #####
clear
checks
if [[ ("$NEW_CROP" == "y" || "$NEW_CROP" == "Y") ]]; then
setup_node
exit 0
elif [[ "$NEW_CROP" == "new" ]]; then
prepare_system
ask_permission
if [[ "$ZOLDUR" == "YES" ]]; then
deploy_binaries
else
compile_cropcoin
fi
setup_node
else
echo -e "${GREEN}Cropcoind already running.${NC}"
exit 0
fi