-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinstall-lancache.sh
executable file
·302 lines (255 loc) · 11.2 KB
/
install-lancache.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
297
298
299
300
301
302
#!/bin/bash
# Following checks if you're running as root or not
if [[ "$EUID" -ne 0 ]]; then
echo "Please run with sudo or as root."
exit 1
fi
# Changeable variables, leaving the defaults is fine
# File path for lancache
lc_srv_loc="/srv/lancache"
# Primary DNS Server
lc_dns1="8.8.8.8"
# Secondary DNS Server
lc_dns2="1.1.1.1"
# Proxy cache size, measued in Megabytes (MB). Default is 500GB
lc_max_size="500000m"
# Variables you should most likely not touch
# Unless you know what you are doing
lc_dl_dir="/usr/local/lancache"
lc_base_folder="$lc_dl_dir/lancache-installer"
lc_tmp_ip="/tmp/services_ips.txt"
lc_tmp_unbound="$lc_base_folder/etc/unbound/unbound.conf"
lc_tmp_hosts="$lc_base_folder/etc/hosts"
lc_tmp_yaml="$lc_base_folder/etc/netplan/01-netcfg.yaml"
lc_nginx_loc="/etc/nginx"
lc_unbound_loc="/etc/unbound"
lc_netdata="/etc/netdata/netdata.conf"
lc_nginx_systemd="/etc/systemd/system/nginx.service"
lc_network=$(hostname -I | awk '{ print $1 }')
lc_gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}')
if_name=$(ifconfig | grep flags | awk -F: '{print $1;}' | grep -Fvx -e lo)
lc_hostname=$(hostname)
TIMESTAMP=$(date +%s)
# Chcecking to see if NGINX repository already in system.
if [[ -d /etc/apt/sources.list.d/nginx.list ]]; then
echo "Nginx Repository Already Added to System..."
else
echo "Adding NGINX repository to Ubuntu repository"
curl -s https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
echo "deb [arch=amd64] https://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" > /etc/apt/sources.list.d/nginx.list
echo "deb-src https://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" >> /etc/apt/sources.list.d/nginx.list
fi
# Update packages
echo "Installing package updates..."
universeCheck=$(apt-cache policy |grep universe)
if [[ -z $universeCheck ]]; then
echo "Adding universe repository..."
apt-add-repository universe
else
apt -y update
fi
apt -y upgrade
#apt -y dist-upgrade
# Install required packages
echo "Installing required updates..."
apt -y install nginx sniproxy unbound nmon httpry netdata
# Arrays used
# Services used and set ip for and created the lancache folders for
declare -a lc_services=(arenanetworks apple blizzard hirez gog glyph microsoft origin riot steam sony enmasse wargaming uplay zenimax digitalextremes pearlabyss epicgames)
#declare -a lc_exclude_unbound=(steam)
# Installer Folders
declare -a lc_folders=(config data logs temp)
# Log Folders
declare -a lc_logfolders=(access errors keys)
declare -a ip_eth=$(ip link show | grep ens | tr ":" " " | awk '{ print $2 }')
for int in ${ip_eth[@]}; do
inet_eth=$(ip route get $lc_dns1 | tr " " " " | awk '{ print $5 }' )
if [[ "$inet_eth" == "$int" ]]; then
lc_ip_eth=$int
fi
done
lc_ip=$(/bin/ip -4 addr show $lc_ip_eth | grep -oP "(?<=inet ).*(?=br)")
# 1st octet
lc_ip_p1=$(echo ${lc_ip} | tr "." " " | awk '{ print $1 }')
# 2nd octet
lc_ip_p2=$(echo ${lc_ip} | tr "." " " | awk '{ print $2 }')
# 3rd octet
lc_ip_p3=$(echo ${lc_ip} | tr "." " " | awk '{ print $3 }')
# 4th octet
lc_ip_p4=$(echo ${lc_ip} | tr "." " " | awk '{ print $4 }' | cut -f1 -d "/")
# Subnet
lc_ip_sn=$(echo ${lc_ip} | sed 's:.*/::' )
########### Update lancache config folder from github########################################
# Chcecking to see if directory exists before attempting to remove and add to prevent bash errors
if [[ -d $lc_base_folder ]]; then
echo "Removing old lancache install directory..."
rm -rfv $lc_base_folder
fi
if [[ ! -d $lc_base_folder ]]; then
echo "Creating new lancache install directory..."
mkdir -p $lc_base_folder
fi
cd $lc_dl_dir
git clone -b master http://github.com/nexusofdoom/lancache-installer
echo "Configuring IP Addressing..."
for service in ${lc_services[@]}; do
# Check if the folder exists if not creates it
if [[ ! -d "/tmp/data/$service" ]]; then
mkdir -p /tmp/data/$service
fi
#new stuff TTE - CARl LOOP
while grep -q "lc-host-${service}" "${lc_tmp_hosts}" >> /dev/null
do
# Increases the IP with Every Run
lc_ip_p4=$(expr $lc_ip_p4 + 1)
#new stuff TTE - CARl LOOP
sed -i '0,/lc-host-'$service'/s//'$lc_ip_p1.$lc_ip_p2.$lc_ip_p3.$lc_ip_p4'/' "$lc_tmp_hosts"
done
done
# Return each line of the file.. Seem by default it trims space/tabs
while read line; do
# skip line if substring doesn't exist in line
if [[ $line != *"lancache-"* ]] ; then continue; fi
#Repalce all tabs and spaces with a single space per line
cleanLine=$(echo "$line" | tr -s '[:blank:]')
# Search and replace array
#Split string into array base off space
#array[0]=192.168.x.x
#array[1}=lc-lancache-steamA
sar=($(echo "$line" | tr ' ' '\n'))
# If array is NOT size two then skip
# WARNING - Yes the # does need be there!
if [[ "${#sar[@]}" -ne 2 ]] ; then continue; fi
# Replace
# WARNING - When using variable need use double quote (not a single quote)
IPAddress=${sar[0]}
hostName=${sar[1]}
# Repalce lancache with lc-host.. So lc-lancache-steamA with lc-host-steamA
IP_Name=${hostName/lancache/lc-host}
# Search all lc-host-'server' and replace the IP address
# WARNING - When using variable need use double quote (not a single quote)
# Update services with correct ip address for DNS for clients
sed -i "s|${IP_Name}|${IPAddress}|gI" "$lc_tmp_unbound"
# May want to replace with Replace ONLY first matching file
# This Changes the yaml File with the correct IP Adresses for services
sed -i "s|${IP_Name}|${IPAddress}/${lc_ip_sn}|gI" "$lc_tmp_yaml"
done <"$lc_tmp_hosts"
# This Changes the Unbound File with the correct IP Adresses for lc-host-ip
sed -i 's|lc-host-ip|'$lc_network'|g' $lc_tmp_unbound
# This Corrects the Host File For The Netplan with gateway
sed -i 's|lc-host-gateway|'$lc_gateway'|g' $lc_tmp_yaml
# This Corrects the Host File For The Netplan with primary network
sed -i 's|lc-host-network|'$lc_network'/'$lc_ip_sn'|g' $lc_tmp_yaml
# This Corrects the Host File For The Netplan with interface name
sed -i 's|lc-host-vint|'$if_name'|g' $lc_tmp_yaml
# This Corrects the loopback to bind to primary IP Address
sed -i 's|127.0.0.1|'$lc_network'|g' $lc_netdata
# This corrects the hostname pointing to the loopback
sed -i "s|lc-hostname|$lc_hostname|g" $lc_tmp_hosts
# This corrects lc-host-proxybind with the correct IP
sed -i "s|lc-host-proxybind|$lc_network|g" $lc_tmp_hosts
#for logfolder in ${lc_logfolders[@]}; do
#Check if the folder exists if not creates it
#if [[ ! -d "$lc_base_folder/$folder" ]]; then
#mkdir -p $lc_base_folder/$logfolder
#fi
#done
### Change file limits
# Need to get the limits into the /etc/security/limits.conf * soft nofile 65536 * hard nofile 65536
echo "Setting security limits..."
mv /etc/security/limits.conf /etc/security/limits.conf.$TIMESTAMP.bak
echo '* soft nofile 65536' >> /etc/security/limits.conf
echo '* hard nofile 65536' >> /etc/security/limits.conf
# Change Ownership of folders
echo "Adding lancache directory structure and setting permissions..."
mkdir $lc_srv_loc
for i in ${lc_services[@]}
do
mkdir -p ${lc_srv_loc}/data/${i}
done
for i in ${lc_logfolders[@]}
do
mkdir -p ${lc_srv_loc}/logs/${i}
done
chown -R www-data:www-data $lc_srv_loc
# Setting the directory path for lancache
echo "Configuring lancache directory structure in nginx..."
sed -i "s|lc-srv-loc|$lc_srv_loc|g" $lc_base_folder/etc/nginx/sites-available/*.conf
sed -i "s|lc-srv-loc|$lc_srv_loc|g" $lc_base_folder/etc/nginx/lancache/caches
# Setting the max_size limit for eache cache
echo "Configuring proxy cache size..."
sed -i "s|lc-max-size|$lc_max_size|g" $lc_base_folder/etc/nginx/lancache/caches
# Setting specified DNS Servers
echo "Setting specified DNS Servers..."
sed -i "s|lc-dns1|$lc_dns1|g" $lc_base_folder/etc/nginx/nginx.conf
sed -i "s|lc-dns2|$lc_dns2|g" $lc_base_folder/etc/nginx/nginx.conf
sed -i "s|lc-dns1|$lc_dns1|g" $lc_base_folder/etc/nginx/lancache/resolver
sed -i "s|lc-dns2|$lc_dns2|g" $lc_base_folder/etc/nginx/lancache/resolver
sed -i "s|lc-dns1|$lc_dns1|g" $lc_tmp_yaml
sed -i "s|lc-dns2|$lc_dns2|g" $lc_tmp_yaml
sed -i "s|lc-dns1|$lc_dns1|g" $lc_tmp_unbound
sed -i "s|lc-dns2|$lc_dns2|g" $lc_tmp_unbound
sed -i "s|lc-dns1|$lc_dns1|g" $lc_base_folder/etc/sniproxy.conf
# Change the Proxy Bind in Lancache Configs
echo "Setting Proxy Bind address in lancache configs..."
sed -i 's|lc-host-proxybind|'$lc_network'|g' $lc_base_folder/etc/nginx/sites-available/*.conf
# Moving nginx configs
echo "Configuring nginx..."
mv $lc_nginx_loc/nginx.conf $lc_nginx_loc/nginx.conf.$TIMESTAMP.bak
cp $lc_base_folder/etc/nginx/nginx.conf $lc_nginx_loc/nginx.conf
#mkdir -p $lc_nginx_loc/conf/lancache
cp -rfv $lc_base_folder/etc/nginx/lancache $lc_nginx_loc
mkdir $lc_nginx_loc/sites-available/
cp $lc_base_folder/etc/nginx/sites-available/*.conf $lc_nginx_loc/sites-available/
cp $lc_dl_dir/lancache-installer/etc/systemd/system/nginx.service $lc_nginx_systemd
# Moving sniproxy configs
echo "Configuring sniproxy..."
mv /etc/default/sniproxy /etc/default/sniproxy.$TIMESTAMP.bak
cp $lc_base_folder/etc/default/sniproxy /etc/default/sniproxy
mv /etc/sniproxy.conf /etc/sniproxy.conf.$TIMESTAMP.bak
cp $lc_base_folder/etc/sniproxy.conf /etc/sniproxy.conf
# Moving unbound configs
echo "Configuring unbound..."
mv /etc/unbound/unbound.conf /etc/unbound/unbound.conf.$TIMESTAMP.bak
cp $lc_base_folder/etc/unbound/unbound.conf /etc/unbound/unbound.conf
#Disable Systemd.resolve so unbound can start
# Configuring startup services
echo "Configuring services to run on boot..."
systemctl enable nginx
systemctl enable nginx.service
systemctl enable sniproxy
systemctl enable unbound
systemctl enable netdata
#Fix Unbound startup service
cp $lc_dl_dir/lancache-installer/etc/systemd/system/multi-user.target.wants/unbound.service /etc/systemd/system/multi-user.target.wants/unbound.service
systemctl daemon-reload
# Move hosts and network interface values into place.
echo "Configuring network interfaces and hosts file..."
mv /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.$TIMESTAMP.bak
cp $lc_base_folder/etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml
mv /etc/hosts /etc/hosts.$TIMESTAMP.bak
cp $lc_base_folder/etc/hosts /etc/hosts
echo "##############################################################################################"
echo "Current interface name: $if_name"
echo "##############################################################################################"
echo ""
#ip link | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2;getline}'
#if_name=$(ifconfig | grep flags | awk -F: '{print $1;}' | grep -Fvx -e lo)
### To Do Still
### Systemd Scripts for everything
### ... and stuff I forgot ...
### I have no problem with you redistributing this under your own name
### Just leave the following piece of line in there
### Base created by Geoffrey "bn_" @ https://github.com/bntjah
#echo $lc_base_folder
echo "##############################################################################################"
echo "DNS Server IP Address / Point Clients to Use this DNS Server: $lc_network"
echo "##############################################################################################"
echo ""
#echo $lc_base_folder
#echo $lc_ip_p4
#echo $lc_gateway
echo "##############################################################################################"
echo "Reboot System"
echo "##############################################################################################"
echo ""