This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.sh
409 lines (339 loc) · 10.9 KB
/
setup.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/bin/bash
chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
genstr16() {
for i in {1..16} ; do
echo -n "${chars:RANDOM%${#chars}:1}"
done
}
genstr48() {
for i in {1..48} ; do
echo -n "${chars:RANDOM%${#chars}:1}"
done
}
genstr32() {
for i in {1..32} ; do
echo -n "${chars:RANDOM%${#chars}:1}"
done
}
genstr8() {
for i in {1..8} ; do
echo -n "${chars:RANDOM%${#chars}:1}"
done
}
# Updates
sudo apt update
sudo apt upgrade -y
# Node.js
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install nodejs -y
npm i pm2 -g
npm i typescript -g
sudo apt install nginx -y
sudo apt install git -y
sudo apt install snapd -y
sudo snap install core; sudo snap refresh core
sudo snap install go --classic
sudo snap install --classic certbot
# Firewall
sudo ufw allow 22
yes | sudo ufw enable
sudo ufw allow 3000
sudo ufw allow 2005
sudo ufw allow 2000
sudo ufw allow 9000
sudo ufw allow 8080
sudo ufw allow 27071
sudo ufw allow 80
sudo ufw allow 443
# Root directory
mkdir sharex
cd sharex
# Start script
echo 'pm2 stop all
pm2 delete all
cd bot
pm2 start start.sh --name "bot"
cd ../frontend
pm2 start start.sh --name "frontend"
cd ../cdn
pm2 start start.sh --name "cdn"
cd ../backend
pm2 start start.sh --name "backend"
cd ../..
pm2 start minio.sh --name "s3"
pm2 list' > start.sh
chmod +x start.sh
# Git clones
git clone https://github.com/Higure-wtf/Backend backend
git clone https://github.com/Higure-wtf/Bot bot
git clone https://github.com/Higure-wtf/Proxy cdn
git clone https://github.com/Higure-wtf/Frontend frontend
echo -e "\e[1;32mPlease follow this tutorial to setup Cloudflare. https://setup.elixr.host/cloudflare-setup/untitled When you're done, press enter.\e[0m"
read
# Config
cd ..
echo "# Cloudflare
DOMAIN=
CLOUDFLARE_API_KEY=
CLOUDFLARE_ACCOUNT_ID=
CLOUDFLARE_EMAIL=
# Discord
DISCORD_WEBHOOK_URL=
DISCORD_LINK_APP_ID=
DISCORD_CLIENT_ID=
DISCORD_CLIENT_SECRET=
# Bot
DISCORD_USER_ROLE= # Discord 'User' role ID
DISCORD_SERVER_ID=
DISCORD_BOT_TOKEN=
ADMIN_ROLE=
OWNERS= # Discord user IDs separated by a space
BOOSTER_ROLE=
# Misc
ADMIN_USER_NAME= # Username for the admin user that will be created
" > config
echo -e "\e[1;32mPlease put in all the values in the 'config' file and press enter\e[0m"
read
source config
echo -e "\e[1;32mNow you can delete the config file\e[0m"
cd sharex
IP="$(ip a show eth0 | grep "inet " | awk '{print $2}' | sed --expression='s/\/24//g')"
# Replacing the domain
find . -type f -exec sed -i "s/[Hh]igure\.wtf/$DOMAIN/g" {} \;
# Frontend
cd frontend
echo "BACKEND_URL=https://api.$DOMAIN
" > .env
# Installing node modules and building
npm i -g
npm i
npm run build
# Start script for PM2
echo "npm run start" > start.sh
chmod +x start.sh
# Backend
cd ../backend
# Installing node modules and building
npm i -g
npm i
npm run build
# Start script for PM2
echo "npm run start" > start.sh
chmod +x start.sh
API_KEY="$(genstr48)"
S3_ACCESS_KEY_ID="$(genstr16)"
S3_SECRET_KEY="$(genstr32)"
MONGODB_PASSWORD="$(genstr8)"
echo "PORT=2001
MONGO_URI=mongodb://127.0.0.1:27017/sharex
API_KEY=$API_KEY
BACKEND_URL=https://api.$DOMAIN
FRONTEND_URL=https://$DOMAIN
S3_SECRET_KEY=$S3_SECRET_KEY
S3_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
S3_ENDPOINT=https://cdn.$DOMAIN:9000
S3_BUCKET=files
CLOUDFLARE_API_KEY=$CLOUDFLARE_API_KEY
CLOUDFLARE_ACCOUNT_ID=$CLOUDFLARE_ACCOUNT_ID
CLOUDFLARE_EMAIL=$CLOUDFLARE_EMAIL
WEBHOOK_URL=$DISCORD_WEBHOOK_URL
CUSTOM_DOMAIN_WEBHOOK=$DISCORD_WEBHOOK_URL
ACCESS_TOKEN_SECRET=ef203360-f032-4643-b5b5-35c6e1399bb5
REFRESH_TOKEN_SECRET=ac27e6c2-0912-4d47-b6e6-17d4ea5f0a05
DISCORD_CLIENT_ID=$DISCORD_CLIENT_ID
DISCORD_CLIENT_SECRET=$DISCORD_CLIENT_SECRET
DISCORD_LOGIN_URL=https://discord.com/api/oauth2/authorize?client_id=$DISCORD_LINK_APP_ID&redirect_uri=https%3A%2F%2Fapi.$DOMAIN%2Fauth%2Fdiscord%2Flogin%2Fcallback&response_type=code&scope=identify
DISCORD_LINK_URL=https://discord.com/api/oauth2/authorize?client_id=$DISCORD_LINK_APP_ID&redirect_uri=https%3A%2F%2Fapi.$DOMAIN%2Fauth%2Fdiscord%2Flink%2Fcallback&response_type=code&scope=identify%20guilds%20guilds.join
DISCORD_LOGIN_REDIRECT_URI=https://api.$DOMAIN/auth/discord/login/callback
DISCORD_LINK_REDIRECT_URI=https://api.$DOMAIN/auth/discord/link/callback
DISCORD_ROLES=$DISCORD_USER_ROLE
DISCORD_SERVER_ID=$DISCORD_SERVER_ID
DISCORD_BOT_TOKEN=$DISCORD_BOT_TOKEN
" > .env
# MongoDB
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt update
sudo apt install mongodb-org -y
sudo systemctl enable --now mongod.service
sleep 2
systemctl status mongod.service
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
echo "use admin
db.createUser(
{
user: \"admin\",
pwd: \"$MONGODB_PASSWORD\",
roles: [ { role: \"userAdminAnyDatabase\", db: \"admin\" } ]
}
)
use sharex
db.counter.insert({_id:\"counter\",count:1})
db.counters.insert({_id:\"counter\",counters:1,count:0,storageUsed:0})
db.createCollection(\"domains\")
db.createCollection(\"files\")
db.createCollection(\"invisibleurls\")
db.createCollection(\"invites\")
db.createCollection(\"passwordresets\")
db.createCollection(\"permissions\")
db.createCollection(\"refreshtokens\")
db.createCollection(\"shorteners\")
db.createCollection(\"users\")
" | mongo
echo '{"_id":"6ed52f08-0619-4057-bc22-f2ad9107893c","invitedUsers":[],"uid":453,"username":"'"$ADMIN_USER_NAME"'","password":"$argon2i$v=19$m=4096,t=3,p=1$0z9v19NfpvBEiZi/hG3z9Q$XPzL4u0glI5ibMAREttWRy5Yz3ylDKo3E+DvLA6IVHQ","invite":"4249d4c8e5-249d4c8e5a9-9d4c8e","key":"'"$ADMIN_USER_NAME"'_b169e9bc871ba02daca274a5133d62","premium":false,"lastDomainAddition":null,"lastKeyRegen":null,"lastUsernameChange":null,"lastFileArchive":null,"email":"'"$CLOUDFLARE_EMAIL"'","emailVerified":true,"emailVerificationKey":"970ba8c23c506b7a30661063f3a710","discord":{"id":null,"avatar":null},"strikes":0,"disabled":false,"blacklisted":{"status":false,"reason":null},"uploads":0,"invites":1,"invitedBy":"GGORG","registrationDate":{"$date":"2021-07-07T17:08:39.607Z"},"lastLogin":null,"admin":true,"bypassAltCheck":false,"settings":{"domain":{"name":"i.'"$DOMAIN"'","subdomain":null},"randomDomain":{"enabled":false,"domains":[]},"embed":{"enabled":true,"color":"#13ed7c","title":"default","description":"default","author":"default","randomColor":true,"sitename":"default"},"embedprofile2":{"enabled":true,"color":"#13ed7c","title":"default","description":"default","author":"default","randomColor":true,"sitename":"default"},"embedprofile3":{"enabled":true,"color":"#13ed7c","title":"default","description":"default","author":"default","randomColor":true,"sitename":"default"},"fakeUrl":{"enabled":false,"url":"example.com"},"autoWipe":{"enabled":false,"interval":3600000},"showLink":false,"invisibleUrl":false,"longUrl":false},"__v":0}' > /tmp/user.json
mongoimport --db sharex --collection users --file /tmp/user.json
echo -e "\e[1;32mYour MongoDB login URI is: mongodb://admin:$MONGODB_PASSWORD@$IP/admin\e[0m"
# CDN
cd ../cdn
# Replacing the db name
sed -i "s/client\.Database(\"higure\")/client\.Database(\"sharex\")/g" main.go
# Building
go mod init module-path
go mod tidy
go build
echo "PORT=2005
MONGO_URI=mongodb://127.0.0.1:27017/sharex
AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=$S3_SECRET_KEY
S3_ENDPOINT=http://127.0.0.1:9000
S3_BUCKET_NAME=files
" > .env
# Start script for PM2
echo "go run main.go" > start.sh
chmod +x start.sh
# MinIO
cd ../..
wget https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
echo "MINIO_ROOT_USER=$S3_ACCESS_KEY_ID MINIO_ROOT_PASSWORD=$S3_SECRET_KEY ./minio server /mnt/data" > minio.sh
chmod +x minio.sh
pm2 start minio.sh --name "s3"
wget https://dl.min.io/client/mc/release/linux-amd64/mc
chmod +x mc
./mc alias set myminio/ http://127.0.0.1:9000 $S3_ACCESS_KEY_ID $S3_SECRET_KEY
./mc mb myminio/files
./mc policy set download myminio/files/*
./mc policy set none myminio/files/
# Bot
cd sharex/bot
echo "BOT_TOKEN=$DISCORD_BOT_TOKEN
API_KEY=$API_KEY
API_KEY=607cdbae-73b6-49d2-b023-5d3667083766
PREFIX=!
ADMIN_ROLE=$ADMIN_ROLE
BACKEND_URL=https://api.$DOMAIN
OWNERS=$OWNERS
BOOSTER_ROLE=$BOOSTER_ROLE
IGNORED_CHANNELS=0
" > .env
sed -i 's/super(BOT_TOKEN);/super(BOT_TOKEN, null);/g' src/client/index.ts
# Installing node modules and building
npm i -g
npm i
npm run build
# Start script for PM2
echo "npm run start" > start.sh
chmod +x start.sh
# Nginx
cd ..
echo "server {
server_name i.$DOMAIN;
listen 80;
listen [::]:80;
return 301 https://\$host\$request_uri;
}
server {
server_name i.$DOMAIN;
location / {
proxy_pass http://localhost:2005;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
}
}
server {
server_name api.$DOMAIN;
listen 80;
listen [::]:80;
return 301 https://\$host\$request_uri;
}
server {
server_name api.$DOMAIN;
location / {
proxy_pass http://localhost:2001;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
}
}
server {
server_name $DOMAIN;
listen 80;
listen [::]:80;
return 301 https://\$host\$request_uri;
}
server {
server_name $DOMAIN;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
}
}
server {
server_name cdn.$DOMAIN;
listen 80;
listen [::]:80;
return 301 https://\$host\$request_uri;
}
server {
server_name cdn.$DOMAIN;
location / {
proxy_pass http://localhost:9000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
}
}
" > /etc/nginx/sites-available/default
sudo nginx -t
echo -e "\e[1;32mPlease go to Cloudflare and disable proxy for every DNS record. When you're done, press enter.\e[0m"
read
echo "$CLOUDFLARE_EMAIL
Y
N
" | sudo certbot --nginx
sudo nginx -t
sudo systemctl restart nginx
# Cors
cd backend/src
sed -i "s/app\.use\(
cors\(\{
credentials: true,
origin: \[
'https:\/\/www.$DOMAIN',
'https:\/\/$DOMAIN',
'http:/\/localhost:3000',
'http:\/\/localhost:3000',
\],
methods: \['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'\],
\}\)
\);/app.use\(
cors\(\{
credentials: true,
origin: \[
'https:\/\/www.$DOMAIN',
'https:\/\/$DOMAIN',
'https:\/\/cdn.$DOMAIN',
'https:\/\/api.$DOMAIN',
'http:\/\/api.$DOMAIN',
'http:\/\/localhost:2001',
'http:\/\/localhost:2005'
'http:\/\/localhost:3000',
'http:\/\/localhost:3000',
\],
methods: \['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'\],
\}\)
\);/g" app.ts
# Done!
cd ../..
./start.sh
echo -e "\e[1;32mDONE!! Login at https://$DOMAIN/ with the username '$ADMIN_USER_NAME' and password 'adminpassword123'. Link your discord and change the password. Done!\e[0m"