· 7 years ago · Sep 12, 2018, 08:52 PM
1#!/usr/bin/env bash
2# DWC Network Installer script by kyle95wm/beanjr - re-written for CoWFC
3# Warn Raspberry Pi users - probably a better way of doing this
4if [ -d /home/pi/ ] ; then
5 echo "THIS SCRIPT IS NOT SUPPORTED ON RASPBERRY PI!"
6 echo "Please use the older script here: https://github.com/kyle95wm/dwc_network_installer"
7 exit 1
8fi
9# Check if we already installed the server
10if [ -f /etc/.dwc_installed ] ; then
11echo "You already installed CoWFC. There is no need to re-run it.
12Perhaps some time down the road we can offer an uninstall option.
13For now, if you wish to uninstall everything, just nuke your system.
14You shouldn't have anything else on it anyways."
15exit 999
16fi
17# ensure running as root
18if [ "$(id -u)" != "0" ]; then
19 exec sudo "$0" "$@"
20fi
21
22# We will test internet connectivity using ping
23if ping -c 4 google.com >/dev/nul ; then
24 echo "Internet is OK"
25else
26 echo "Internet connection test failed!"
27 exit 1
28fi
29
30# We'll assume the user is from an English locale
31if [ ! -f /var/www/.locale-done ] ; then
32 if ! locale-gen en_US.UTF-8 ; then
33 apt-get install -y language-pack-en-base
34 fi
35fi
36export LANG=en_US.UTF-8
37export LC_ALL=en_US.UTF-8
38
39# We'll create our secret locale file
40touch /var/www/.locale-done
41
42# Variables used by the script in various sections to pre-fill long commandds
43C1="0" # A counting variable
44C2="0" # A counting variable
45IP="" # Used for user input
46mod1="proxy" # This is a proxy mod that is dependent on the other 2
47mod2="proxy_http" # This is related to mod1
48mod3="php7.1"
49UPDATE_FILE="$0.tmp"
50UPDATE_BASE="https://raw.githubusercontent.com/kyle95wm/cowfc_installer/master/cowfc.sh"
51# Functions
52
53function update {
54# The following lines will check for an update to this script if the -s switch
55# is not used.
56
57# Original code by Dennis Simpson
58# Modified by Kyle Warwick-Mathieu
59echo "Checking if script is up to date, please wait"
60wget -nv -O "$UPDATE_FILE" "$UPDATE_BASE" >& /dev/null
61if ! diff "$0" "$UPDATE_FILE" >& /dev/null && [ -s "$UPDATE_FILE" ]; then
62 mv "$UPDATE_FILE" "$0"
63 chmod +x "$0"
64 echo "$0 updated"
65 "$0" -s
66 exit
67else
68 rm "$UPDATE_FILE" # If no updates are available, simply remove the file
69 fi
70}
71
72function create_apache_vh_nintendo {
73# This function will create virtual hosts for Nintendo's domains in Apache
74echo "Creating Nintendo virtual hosts...."
75touch /etc/apache2/sites-available/gamestats2.gs.nintendowifi.net.conf
76touch /etc/apache2/sites-available/gamestats.gs.nintendowifi.net.conf
77touch /etc/apache2/sites-available/nas-naswii-dls1-conntest.nintendowifi.net.conf
78touch /etc/apache2/sites-available/sake.gs.nintendowifi.net.conf
79cat >/etc/apache2/sites-available/gamestats2.gs.nintendowifi.net.conf <<EOF
80<VirtualHost *:80>
81ServerAdmin webmaster@localhost
82ServerName gamestats2.gs.nintendowifi.net
83ServerAlias "gamestats2.gs.nintendowifi.net, gamestats2.gs.nintendowifi.net"
84ProxyPreserveHost On
85ProxyPass / http://127.0.0.1:9002/
86ProxyPassReverse / http://127.0.0.1:9002/
87</VirtualHost>
88EOF
89
90cat >/etc/apache2/sites-available/gamestats.gs.nintendowifi.net.conf <<EOF
91<VirtualHost *:80>
92ServerAdmin webmaster@localhost
93ServerName gamestats.gs.nintendowifi.net
94ServerAlias "gamestats.gs.nintendowifi.net, gamestats.gs.nintendowifi.net"
95ProxyPreserveHost On
96ProxyPass / http://127.0.0.1:9002/
97ProxyPassReverse / http://127.0.0.1:9002/
98</VirtualHost>
99EOF
100
101cat >/etc/apache2/sites-available/nas-naswii-dls1-conntest.nintendowifi.net.conf <<EOF
102<VirtualHost *:80>
103ServerAdmin webmaster@localhost
104ServerName naswii.nintendowifi.net
105ServerAlias "naswii.nintendowifi.net, naswii.nintendowifi.net"
106ServerAlias "nas.nintendowifi.net"
107ServerAlias "nas.nintendowifi.net, nas.nintendowifi.net"
108ServerAlias "dls1.nintendowifi.net"
109ServerAlias "dls1.nintendowifi.net, dls1.nintendowifi.net"
110ServerAlias "conntest.nintendowifi.net"
111ServerAlias "conntest.nintendowifi.net, conntest.nintendowifi.net"
112ProxyPreserveHost On
113ProxyPass / http://127.0.0.1:9000/
114ProxyPassReverse / http://127.0.0.1:9000/
115</VirtualHost>
116EOF
117
118cat >/etc/apache2/sites-available/sake.gs.nintendowifi.net.conf <<EOF
119<VirtualHost *:80>
120ServerAdmin webmaster@localhost
121ServerName sake.gs.nintendowifi.net
122ServerAlias sake.gs.nintendowifi.net *.sake.gs.nintendowifi.net
123ServerAlias secure.sake.gs.nintendowifi.net
124ServerAlias secure.sake.gs.nintendowifi.net *.secure.sake.gs.nintendowifi.net
125ProxyPass / http://127.0.0.1:8000/
126CustomLog ${APACHE_LOG_DIR}/access.log combined
127</VirtualHost>
128EOF
129
130echo "Done!"
131echo "enabling...."
132a2ensite *.nintendowifi.net.conf
133service apache2 restart
134}
135
136function create_apache_vh_wiimmfi {
137# This function will create virtual hosts for Wiimmfi's domains in Apache
138echo "Creating Wiimmfi virtual hosts...."
139touch /etc/apache2/sites-available/gamestats2.gs.wiimmfi.de.conf
140touch /etc/apache2/sites-available/gamestats.gs.wiimmfi.de.conf
141touch /etc/apache2/sites-available/nas-naswii-dls1-conntest.wiimmfi.de.conf
142touch /etc/apache2/sites-available/sake.gs.wiimmfi.de.conf
143cat >/etc/apache2/sites-available/gamestats2.gs.wiimmfi.de.conf <<EOF
144<VirtualHost *:80>
145ServerAdmin webmaster@localhost
146ServerName gamestats2.gs.wiimmfi.de
147ServerAlias "gamestats2.gs.wiimmfi.de, gamestats2.gs.wiimmfi.de"
148ProxyPreserveHost On
149ProxyPass / http://127.0.0.1:9002/
150ProxyPassReverse / http://127.0.0.1:9002/
151</VirtualHost>
152EOF
153
154cat >/etc/apache2/sites-available/gamestats.gs.wiimmfi.de.conf <<EOF
155<VirtualHost *:80>
156ServerAdmin webmaster@localhost
157ServerName gamestats.gs.wiimmfi.de
158ServerAlias "gamestats.gs.wiimmfi.de, gamestats.gs.wiimmfi.de"
159ProxyPreserveHost On
160ProxyPass / http://127.0.0.1:9002/
161ProxyPassReverse / http://127.0.0.1:9002/
162</VirtualHost>
163EOF
164
165cat >/etc/apache2/sites-available/nas-naswii-dls1-conntest.wiimmfi.de.conf <<EOF
166<VirtualHost *:80>
167ServerAdmin webmaster@localhost
168ServerName naswii.wiimmfi.de
169ServerAlias "naswii.wiimmfi.de, naswii.wiimmfi.de"
170ServerAlias "nas.wiimmfi.de"
171ServerAlias "nas.wiimmfi.de, nas.wiimmfi.de"
172ServerAlias "dls1.wiimmfi.de"
173ServerAlias "dls1.wiimmfi.de, dls1.wiimmfi.de"
174ServerAlias "conntest.wiimmfi.de"
175ServerAlias "conntest.wiimmfi.de, conntest.wiimmfi.de"
176ProxyPreserveHost On
177ProxyPass / http://127.0.0.1:9000/
178ProxyPassReverse / http://127.0.0.1:9000/
179</VirtualHost>
180EOF
181
182cat >/etc/apache2/sites-available/sake.gs.wiimmfi.de.conf <<EOF
183<VirtualHost *:80>
184ServerAdmin webmaster@localhost
185ServerName sake.gs.wiimmfi.de
186ServerAlias sake.gs.wiimmfi.de *.sake.gs.wiimmfi.de
187ServerAlias secure.sake.gs.wiimmfi.de
188ServerAlias secure.sake.gs.wiimmfi.de *.secure.sake.gs.wiimmfi.de
189ProxyPass / http://127.0.0.1:8000/
190CustomLog ${APACHE_LOG_DIR}/access.log combined
191</VirtualHost>
192EOF
193
194echo "Done!"
195echo "enabling...."
196a2ensite *.wiimmfi.de.conf
197service apache2 restart
198}
199function apache_mods {
200a2enmod $mod1 $mod2
201service apache2 restart
202if ! a2enmod $mod3 ; then
203a2dismod mpm_event
204a2enmod $mod3
205service apache2 restart
206fi
207service apache2 restart
208}
209
210function dns_config {
211# This function will configure dnsmasq
212echo "----------Lets configure DNSMASQ now----------"
213sleep 3s
214echo "Adding Google DNS (8.8.8.8) to config"
215# We add Google's DNS server to our server so that anyone with our DNS server can still resolve hostnames to IP
216# addresses outside our DNS server. Useful for Dolphin testing
217cat >>/etc/dnsmasq.conf <<EOF
218server=8.8.8.8
219EOF
220sleep 2s
221echo "What is your EXTERNAL IP?"
222echo "NOTE: If you plan on using this on a LAN, put the IP of your Linux system instead"
223echo "It's also best practice to make this address static in your /etc/network/interfaces file"
224echo "your LAN IP is"
225hostname -I | cut -f1 -d' '
226echo "Your external IP is:"
227curl -4 -s icanhazip.com
228echo "Please type in either your LAN or external IP"
229read -re IP
230cat >>/etc/dnsmasq.conf <<EOF # Adds your IP you provide to the end of the DNSMASQ config file
231address=/nintendowifi.net/$IP
232address=/wiimmfi.de/$IP
233EOF
234clear
235echo "DNSMasq setup completed!"
236clear
237service dnsmasq restart
238clear
239}
240
241function install_required_packages {
242# Add PHP 7.1 repo
243if [ ! -f "/var/www/.php71-added" ] ; then
244 echo "Adding the PHP 7.1 repository. Please follow any prompts."
245if ! add-apt-repository ppa:ondrej/php ; then
246 apt-get install -y software-properties-common python-software-properties -y
247 add-apt-repository ppa:ondrej/php
248fi
249 sleep 2s
250 echo "Creating file to tell the script you already added the repo"
251 touch "/var/www/.php71-added"
252 echo "I will now reboot your server to free up resources for the next phase"
253 reboot
254 exit
255else
256 echo "The PHP 7.1 repo is already added. If you believe this to ben an error, please type 'rm -rf /var/www/.php71-added' to remove the file which prevents the repository from being added again."
257fi
258# Fix dpkg problems that happened somehow
259dpkg --configure -a
260echo "Updating & installing PHP 7.1 onto your system..."
261apt-get update
262apt-get install php7.1 -y
263# Install the other required packages
264apt-get install apache2 python2.7 python-twisted dnsmasq git curl -y
265}
266function config_mysql {
267echo "We will now configure MYSQL server."
268debconf-set-selections <<< 'mysql-server mysql-server/root_password password passwordhere'
269debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password passwordhere'
270apt-get -y install mysql-server
271# We will now set the new mysql password in the AdminPage.php file.
272# Do not change "passwordhere", as this will be the base for replacing it later
273# The below sed command has NOT been tested so we don't know if this will work or not.
274#sed -i -e 's/passwordhere/passwordhere/g' /var/www/html/_site/AdminPage.php
275# Next we will install two more packages to make mysql and sqlite work with PHP
276apt-get install php7.1-mysql -y
277apt-get install sqlite php7.1-sqlite3 -y
278# Now we will set up our first admin user
279echo "Now we're going to set up our first Admin Portal user."
280read -rp "Please enter the username you wish to use: " firstuser
281read -rp "Please enter a password: " password
282hash=$(/var/www/CoWFC/SQL/bcrypt-hash "$password")
283echo "We will now set the rank for $firstuser"
284echo "At the moment, this does nothing. However in later releases, we plan to restrict who can do what."
285echo "1: First Rank"
286echo "2: Second Rank"
287echo "3: Third Rank"
288read -rp "Please enter a rank number [1-3]: " firstuserrank
289echo "That's all the informatio I'll need for now."
290echo "Setting up the cowfc users database"
291echo "create database cowfc" | mysql -u root -ppasswordhere
292echo "Now importing dumped cowfc database..."
293mysql -u root -ppasswordhere cowfc < /var/www/CoWFC/SQL/cowfc.sql
294echo "Now inserting user $firstuser into the database with password $password, hashed as $hash."
295echo "insert into users (Username, Password, Rank) values ('$firstuser','$hash','$firstuserrank');" | mysql -u root -ppasswordhere cowfc
296}
297function re {
298echo "For added security, we recommend setting up Google's reCaptcha.
299
300However, not many people would care about this, so we're making it optional.
301
302Feel free to press the ENTER key at the prompt, to skip reCaptcha setup, or 'y' to proceed with recaptcha setup."
303read -rp "Would you like to set up reCaptcha on this server? [y/N]: " recaptchacontinue
304if [ "$recaptchacontinue" == y ] ; then
305echo "In order to log into your Admin interface, you will need to set up reCaptcha keys. This script will walk you through it"
306echo "Please make an account over at https://www.google.com/recaptcha/"
307# Next we will ask the user for their secret key and site keys
308read -rp "Please enter the SECRET KEY you got from setting up reCaptcha: " secretkey
309read -rp "Please enter the SITE KEY you got from setting up reCaptcha: " sitekey
310echo "Thank you! I will now add your SECRET KEY and SITE KEY to /var/www/html/_admin/Auth/Login.php"
311# Replace SECRET_KEY_HERE with the secret key from our $secretkey variable
312#sed -i -e "s/SECRET_KEY_HERE/$secretkey/g" /var/www/html/_admin/Auth/Login.php
313sed -i -e "s/SECRET_KEY_HERE/$secretkey/g" /var/www/html/config.ini
314# Replace SITE_KEY_HERE with the site key from our $sitekey variable
315#sed -i -e "s/SITE_KEY_HERE/$sitekey/g" /var/www/html/_admin/Auth/Login.php
316sed -i -e "s/recaptcha_site = SITE_KEY_HERE/recaptcha_site = $sitekey/g" /var/www/html/config.ini
317else
318sed -i -e "s/recaptcha_enabled = 1/recaptcha_enabled = 0/g" /var/www/html/config.ini
319fi
320}
321function set-server-name {
322echo "This recent CoWFC update allows you to set your server's name"
323echo "This is useful if you want to whitelabel your server, and not advertise it as CoWFC"
324read -rp "Please enter the server name, or press ENTER to accept the default [CoWFC]: " servernameconfig
325if [ -z "$servernameconfig" ] ; then
326echo "Using CoWFC as the server name."
327else
328echo "Setting server name to $servernameconfig"
329sed -i -e "s/name = 'CoWFC'/name = '$servernameconfig'/g" /var/www/html/config.ini
330fi
331}
332function add-cron {
333echo "Checking if there is a cron available for $USER"
334if ! crontab -l -u "$USER" |grep "@reboot sh /start-altwfc.sh >/cron-logs/cronlog 2>&1" ; then
335echo "No cron job is currently installed"
336echo "Working the magic. Hang tight!"
337cat > /start-altwfc.sh <<EOF
338#!/bin/sh
339cd /
340chmod 777 /var/www/dwc_network_server_emulator -R
341cd var/www/dwc_network_server_emulator
342python master_server.py
343cd /
344EOF
345chmod 777 /start-altwfc.sh
346mkdir -p /cron-logs
347if ! which crontab ; then
348apt-get install cron -y
349fi
350echo "Creating the cron job now!"
351echo "@reboot sh /start-altwfc.sh >/cron-logs/cronlog 2>&1" >/tmp/alt-cron
352crontab -u "$USER" /tmp/alt-cron
353echo "Done!"
354fi
355}
356function install_website {
357# First we will delete evertyhing inside of /var/www/html
358rm -rf /var/www/html/*
359# Let's download the HTML5 template SBAdmin so that the Admin GUI looks nice
360# Download the stuff
361#wget https://github.com/BlackrockDigital/startbootstrap-sb-admin/archive/gh-pages.zip -O sb-admin.zip
362#unzip sb-admin.zip
363#if [ $? != "0" ] ; then
364# apt-get install unzip -y
365# unzip sb-admin.zip
366#fi
367# Copy required directories and files to /var/www/html
368#cp /var/www/startbootstrap-sb-admin-gh-pages/css/ /var/www/html/ -R && cp /var/www/startbootstrap-sb-admin-gh-pages/js /var/www/html/ -R && cp /var/www/startbootstrap-sb-admin-gh-pages/scss/ /var/www/html/ -R && cp /var/www/startbootstrap-sb-admin-gh-pages/vendor/ /var/www/html/ -R
369
370# We'll download and install the main template next
371
372#wget https://html5up.net/landed/download -O html5up-landed.zip
373#unzip html5up-landed.zip -d landed
374# We could put varous cp commands here to copy the needed files
375
376# Then we will copy the website files from our CoWFC Git
377cp /var/www/CoWFC/Web/* /var/www/html -R
378chmod 777 /var/www/html/bans.log
379# Let's restart Apache now
380service apache2 restart
381echo "Creating gpcm.db file"
382touch /var/www/dwc_network_server_emulator/gpcm.db
383chmod 777 /var/www/dwc_network_server_emulator/ -R
384}
385
386# MAIN
387# Call update function
388if [ "$1" != "-s" ]; then # If there is no -s argument then run the updater
389 update # This will call our update function
390fi
391#echo "******************************************* WARNING!*******************
392#*****************************************************************************
393#IT HAS BEEN DISCOVERED THAT BUILDS ON THE LATEST UBUNTU UPDATES WILL FAIL!
394#*****************************************************************************
395#"
396#read -p "Press [ENTER] to continue at your own risk, or ctrl+c to abort."
397# First we will check if we are on Ubuntu - this isn't 100% going to work,
398# but if we're running Debian, it should be enough for what we need this check
399# to do.
400if [ -f /etc/lsb-release ] ; then
401if grep -q "14.04" /etc/lsb-release ; then
402 CANRUN="TRUE"
403elif [ -f /var/www/.aws_install ] ; then
404 CANRUN="TRUE"
405else
406 echo "It looks like you are not running Ubuntu 14.04."
407 echo "If you are running Ubuntu 16.04, this script MIGHT work, but dnsmasq doesn't work properly on platforms like Vultr."
408 read -rp "Would you like to give it a try? (n): [y/n] " giveitatry
409 if [ "$giveitatry" == "y" ] ; then
410 CANRUN="TRUE"
411 else
412 CANRUN="FALSE"
413 fi
414fi
415fi
416
417# Determine if our script can run
418if [ "$CANRUN" == "TRUE" ] ; then
419 # Our script can run since we are on Ubuntu
420 # Put commands or functions on these lines to continue with script execution.
421 # The first thing we will do is to update our package repos but let's also make sure that the user is running the script in the proper directory /var/www
422 if [ "$PWD" == "/var/www" ] ; then
423 apt-get update
424 # Let's install required packages first.
425 install_required_packages
426 # Then we will check to see if the Gits for CoWFC and dwc_network_server_emulator exist
427 if [ ! -d "/var/www/CoWFC" ] ; then
428 echo "Git for CoWFC does not exist in /var/www/"
429 while ! git clone https://github.com/kyle95wm/CoWFC.git && [ "$C1" -le "4" ] ; do
430 echo "GIT CLONE FAILED! Retrying....."
431 (( C1=C1+1 ))
432 done
433if [ "$C1" == "5" ] ; then
434 echo "Giving up"
435 exit 1
436fi
437 fi
438 if [ ! -d "/var/www/dwc_network_server_emulator" ] ; then
439 echo "Git for dwc_network_server_emulator does not exist in /var/www"
440 #git clone https://github.com/mh9924/dwc_network_server_emulator.git
441 while ! git clone https://github.com/kyle95wm/dwc_network_server_emulator.git && [ "$C2" -le "4" ] ; do
442 echo "GIT CLONE FAILED! Retrying......"
443 (( C2=C2+1 ))
444 done
445if [ "$C2" == "5" ] ; then
446 echo "Giving up"
447 exit 1
448fi
449 echo "Setting proper file permissions"
450 chmod 777 /var/www/dwc_network_server_emulator/ -R
451 fi
452# Configure DNSMASQ
453dns_config
454# Let's set up Apache now
455create_apache_vh_nintendo
456create_apache_vh_wiimmfi
457apache_mods # Enable reverse proxy mod and PHP 7.1
458install_website # Install the web contents for CoWFC
459config_mysql # We will set up the mysql password as "passwordhere" and create our first user
460re # Set up reCaptcha
461add-cron #Makes it so master server can start automatically on boot
462set-server-name # Set your server's name
463#a fix to fix issue: polaris-/dwc_network_server_emulator#413
464cat >>/etc/apache2/apache2.conf <<EOF
465HttpProtocolOptions Unsafe LenientMethods Allow0.9
466EOF
467echo "Thank you for installing CoWFC."
468echo "If you wish to access the admin GUI, please go to http://$IP/?page=admin§ion=Dashboard"
469read -rp "Please hit the ENTER key to reboot now, or press ctrl+c and reboot whenever it is convenient for you: [ENTER] " rebootenterkey
470if [ -z "$rebootenterkey" ] ; then
471reboot
472fi
473# Let's make our hidden file so that our script will know that we've already installed the server
474# This will prevent accidental re-runs
475touch /etc/.dwc_installed
476reboot
477exit 0
478# DO NOT PUT COMMANDS UNDER THIS FI
479fi
480else
481 echo "Sorry, you do not appear to be running a supported Opperating System."
482 echo "Please make sure you are running Ubuntu 14.04, and try again!"
483 exit 1
484fi