forked from AvinashReddy3108/rclone-mount-magisk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.sh
executable file
·454 lines (390 loc) · 13.3 KB
/
service.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/system/bin/sh
MODDIR=${0%/*}
# Do NOT assume where your module will be located.
# ALWAYS use $MODDIR if you need to know where this script
# and module is placed.
# This will make sure your module will work
# if Magisk changes it's mount point in the future
# MODULE VARS
CLOUDROOTMOUNTPOINT=/mnt/cloud
USER_CONFDIR=/sdcard/Android/.rclone
USER_CONF=$USER_CONFDIR/rclone.conf
PROFILE=0
DATA_MEDIA=/data/media
RUNTIME_R=/mnt/runtime/read
RUNTIME_W=/mnt/runtime/write
RUNTIME_D=/mnt/runtime/default
BINDPOINT_R=${RUNTIME_R}/emulated/${PROFILE}/Cloud
BINDPOINT_W=${RUNTIME_W}/emulated/${PROFILE}/Cloud
BINDPOINT_D=${RUNTIME_D}/emulated/${PROFILE}/Cloud
BINDPOINT_P=/mnt/pass_through/${PROFILE}/emulated/${PROFILE}/Cloud
SD_BINDPOINT=${BINDPOINT_P}
DISABLE=0
NETCHK=1
# DEFAULT RCLONE PARAMETERS
CONFIGFILE=${MODDIR}/.config/rclone/rclone.conf
LOGFILE=${USER_CONFDIR}/rclone.log
LOGLEVEL=NOTICE
CACHE=/data/rclone/cache
CACHE_BACKEND=/data/rclone/cache/cache-backend
CACHEMODE=off
READCHUNKSIZE=1M
CACHEMAXSIZE=1G
CHUNKSIZE=1M
CHUNKTOTAL=1G
CACHEWORKERS=1
CACHEINFOAGE=1h0m0s
DIRCACHETIME=30m0s
ATTRTIMEOUT=30s
BUFFERSIZE=0
READAHEAD=128k
M_UID=0
M_GID=1015
DIRPERMS=0775
FILEPERMS=0644
UMASK=002
BINDSD=1
SYNC_WIFI=1
SYNC_CHARGE=0
SYNC_BATTLVL=0
HTTP=0
HTTP_ADDR=127.0.0.1:38762
FTP=0
FTP_ADDR=127.0.0.1:38763
SFTP=0
SFTP_ADDR=127.0.0.1:38722
SFTP_USER=
SFTP_PASS=
if [[ -z ${INTERACTIVE} ]]; then
INTERACTIVE=0
fi
if [[ ! -d ${MODDIR}/.config/rclone ]]; then
mkdir -p ${MODDIR}/.config/rclone
fi
gethostip() {
ip=$(ip route list match 0 table all scope global | cut -F3)
if [ -z "$ip" ]; then
return 1
else
echo "$ip"
fi
}
while ! (gethostip); do
sleep 3
done
NETCHK_ADDR=$(gethostip)
custom_params() {
if [[ ${remote} = global ]]; then
PARAMS="DISABLE LOGFILE LOGLEVEL CACHEMODE CHUNKSIZE CHUNKTOTAL CACHEWORKERS CACHEINFOAGE DIRCACHETIME ATTRTIMEOUT BUFFERSIZE READAHEAD M_UID M_GID DIRPERMS FILEPERMS READONLY BINDSD ADD_PARAMS REPLACE_PARAMS NETCHK NETCHK_IF NETCHK_ADDR HTTP FTP HTTP_ADDR FTP_ADDR SFTP SFTP_ADDR SFTP_USER SFTP_PASS PROFILE ISOLATE"
else
PARAMS="DISABLE LOGFILE LOGLEVEL CACHEMODE CHUNKSIZE CHUNKTOTAL CACHEWORKERS CACHEINFOAGE DIRCACHETIME ATTRTIMEOUT BUFFERSIZE READAHEAD M_UID M_GID DIRPERMS FILEPERMS READONLY BINDSD SDBINDPOINT ADD_PARAMS REPLACE_PARAMS PROFILE ISOLATE SDSYNCDIRS SYNC_WIFI SYNC_BATTLVL SYNC_CHARGE SUBPATH"
fi
BAD_SYNTAX="(^\s*#|^\s*$|^\s*[a-z_][^[:space:]]*=[^;&\(\`]*$)"
if [[ -e ${USER_CONFDIR}/.${remote}.param ]]; then
echo "Found .${remote}.param"
if ! [[ $(egrep -q -iv "${BAD_SYNTAX}" ${USER_CONFDIR}/.${remote}.param) ]]; then
echo "loading .${remote}.param"
# FIX: Unnecessary and very inefficient double loop
for PARAM in ${PARAMS[@]}; do
while read -r VAR; do
if [[ "$(echo "${VAR}" | grep "$PARAM=")" ]]; then
echo "Importing ${VAR}"
VALUE="$(echo ${VAR} | cut -d '=' -f2)"
VALUE=\"${VALUE}\"
# Unnecessary echo in a subshell execution below? Why not just:
# eval "${PARAM}""=""${VALUE}"
eval $(echo "${PARAM}""=""${VALUE}")
fi
done <${USER_CONFDIR}/.${remote}.param
done
else
echo ".${remote}.param contains bad syntax"
fi
fi
}
global_params() {
remote=global
custom_params
unset remote
echo
}
net_chk() {
if [ -z ${NETCHK_IF} ]; then
NETCHK_IF=" "
else
NETCHK_IF=" -I ${NETCHK_IF} "
fi
ping ${NETCHK_IF} -c 5 ${NETCHK_ADDR}
}
sd_unbind() {
if [[ -z ${SDBINDPOINT} ]]; then
UNBINDPOINT=${BINDPOINT_D}/${remote}
umount -lf ${UNBINDPOINT} >>/dev/null 2>&1
UNBINDPOINT=${BINDPOINT_R}/${remote}
umount -lf ${UNBINDPOINT} >>/dev/null 2>&1
UNBINDPOINT=${BINDPOINT_W}/${remote}
umount -lf ${UNBINDPOINT} >>/dev/null 2>&1
UNBINDPOINT="$SD_BINDPOINT/$remote"
umount -lf "$UNBINDPOINT" >>/dev/null 2>&1
else
USER_BINDPOINT=${SDBINDPOINT}
UNBINDPOINT=${RUNTIME_D}/emulated/${PROFILE}/${USER_BINDPOINT}
umount -lf ${UNBINDPOINT} >>/dev/null 2>&1
UNBINDPOINT=${RUNTIME_R}/emulated/${PROFILE}/${USER_BINDPOINT}
umount -lf ${UNBINDPOINT} >>/dev/null 2>&1
UNBINDPOINT=${RUNTIME_W}/emulated/${PROFILE}/${USER_BINDPOINT}
umount -lf ${UNBINDPOINT} >>/dev/null 2>&1
fi
}
sd_binder() {
if [[ -d ${RUNTIME_D} ]] && [[ ${BINDSD} = 1 ]]; then
if [[ -z ${SDBINDPOINT} ]]; then
mkdir -p ${DATA_MEDIA}/${PROFILE}/Cloud/${remote}
chown media_rw:media_rw ${DATA_MEDIA}/${PROFILE}/Cloud/$remote
BINDPOINT=${BINDPOINT_D}/${remote}
mount --bind ${CLOUDROOTMOUNTPOINT}/${remote} ${BINDPOINT} >>/dev/null 2>&1
BINDPOINT=${BINDPOINT_R}/${remote}
if ! mount | grep -q ${BINDPOINT}; then
mount --bind ${CLOUDROOTMOUNTPOINT}/${remote} ${BINDPOINT} >>/dev/null 2>&1
fi
BINDPOINT=${BINDPOINT_W}/${remote}
if ! mount | grep -q ${BINDPOINT}; then
mount --bind ${CLOUDROOTMOUNTPOINT}/${remote} ${BINDPOINT} >>/dev/null 2>&1
fi
BINDPOINT="$SD_BINDPOINT/$remote"
if ! mount | grep -q "$BINDPOINT"; then
mount --bind "$CLOUDROOTMOUNTPOINT/$remote" "$BINDPOINT" >>/dev/null 2>&1
fi
echo "[$remote] available at: -> [/sdcard/Cloud/${remote}] BINDPOINT=$BINDPOINT"
else
mkdir ${DATA_MEDIA}/${PROFILE}/${SDBINDPOINT} >>/dev/null 2>&1
chown media_rw:media_rw ${DATA_MEDIA}/${PROFILE}/${SDBINDPOINT}
USER_BINDPOINT=${SDBINDPOINT}
BINDPOINT=${RUNTIME_D}/emulated/${PROFILE}/${USER_BINDPOINT}
mount --bind ${CLOUDROOTMOUNTPOINT}/${remote} ${BINDPOINT} >>/dev/null 2>&1
BINDPOINT=${RUNTIME_R}/emulated/${PROFILE}/${USER_BINDPOINT}
if ! mount | grep -q ${BINDPOINT}; then
mount --bind ${CLOUDROOTMOUNTPOINT}/${remote} ${BINDPOINT} >>/dev/null 2>&1
fi
BINDPOINT=${RUNTIME_W}/emulated/${PROFILE}/${USER_BINDPOINT}
if ! mount | grep -q ${BINDPOINT}; then
mount --bind ${CLOUDROOTMOUNTPOINT}/${remote} ${BINDPOINT} >>/dev/null 2>&1
fi
echo "[$remote] available at: -> [/storage/emulated/${PROFILE}/${SDBINDPOINT}]"
unset BINDPOINT
fi
fi
}
syncd_service() {
if [[ ! -z ${SDSYNCDIRS} ]]; then
export PIDFILE=${MODDIR}/.tmp/${remote}-syncd.pids
kill -9 $(cat ${PIDFILE}) >>/dev/null 2>&1
rm ${PIDFILE} >>/dev/null 2>&1
if [[ ! -d ${MODDIR}/.tmp ]]; then
mkdir -p ${MODDIR}/.tmp
fi
export CLOUDROOTMOUNTPOINT
export PROFILE
export MODDIR
export remote
export SYNCWIFI
export SYNC_BATTLVL
export SYNC_CHARGE
export NETCHK_ADDR
IFS=$':'
for SYNCDIR in ${SDSYNCDIRS[@]}; do
export SYNCDIR
${MODDIR}/syncd.sh >>/dev/null 2>&1 &
done
fi
unset IFS
unset SDSYNCDIRS
unset SYNCDIR
SYNC_BATTLVL=0
SYNC_WIFI=1
SYNC_CHARGE=0
}
reset_params() {
unset IFS
unset SDBINDPOINT
unset BINDSD
unset ISOLATE
unset RCLONE_PARAMS
unset REPLACE_PARAMS
unset ADD_PARAMS
unset SYNCDIR
unset SDSYNCDIRS
unset PIDFILE
unset SUBPATH
LOGFILE=${USER_CONFDIR}/rclone.log
LOGLEVEL=NOTICE
CACHEMODE=off
READCHUNKSIZE=1M
CACHEMAXSIZE=1G
CHUNKSIZE=1M
CHUNKTOTAL=1G
CACHEWORKERS=1
CACHEINFOAGE=1h0m0s
DIRCACHETIME=30m0s
ATTRTIMEOUT=30s
BUFFERSIZE=0
READAHEAD=128k
M_UID=0
M_GID=1015
DIRPERMS=0775
FILEPERMS=0644
UMASK=002
PROFILE=0
SYNC_WIFI=1
SYNC_BATTLVL=0
SYNC_CHARGE=0
}
rclone_mount() {
if [[ ${READONLY} = 1 ]]; then
READONLY=" --read-only "
else
READONLY=" "
fi
if [[ ${ADD_PARAMS} = 0 ]]; then
unset ADD_PARAMS
fi
if [[ ${REPLACE_PARAMS} = 0 ]]; then
unset REPLACE_PARAMS
fi
if [[ -z ${REPLACE_PARAMS} ]]; then
RCLONE_PARAMS=" --log-file ${LOGFILE} --log-level ${LOGLEVEL} --vfs-cache-mode ${CACHEMODE} --cache-dir ${CACHE} --cache-chunk-path ${CACHE_BACKEND} --cache-db-path ${CACHE_BACKEND} --cache-tmp-upload-path ${CACHE} --vfs-read-chunk-size ${READCHUNKSIZE} --vfs-cache-max-size ${CACHEMAXSIZE} --cache-chunk-size ${CHUNKSIZE} --cache-chunk-total-size ${CHUNKTOTAL} --cache-workers ${CACHEWORKERS} --cache-info-age ${CACHEINFOAGE} --dir-cache-time ${DIRCACHETIME} --attr-timeout ${ATTRTIMEOUT} --cache-chunk-no-memory --use-mmap --buffer-size ${BUFFERSIZE} --max-read-ahead ${READAHEAD} --no-modtime --no-checksum --uid ${M_UID} --gid ${M_GID} --allow-other --dir-perms ${DIRPERMS} --file-perms ${FILEPERMS} --umask ${UMASK} ${READONLY} ${ADD_PARAMS} "
elif [[ ! -z ${REPLACE_PARAMS} ]]; then
RCLONE_PARAMS=" ${REPLACE_PARAMS} "
fi
if [[ -z ${ADD_PARAMS} ]]; then
ADD_PARAMS=" "
elif [[ ! -z ${ADD_PARAMS} ]]; then
ADD_PARAMS=" ${ADD_PARAMS} "
fi
echo "[${remote}] available at: -> [${CLOUDROOTMOUNTPOINT}/${remote}]"
mkdir -p ${CLOUDROOTMOUNTPOINT}/${remote}
nice -n 19 ionice -c 2 -n 7 ${MODDIR}/rclone mount "${remote}:${SUBPATH}" ${CLOUDROOTMOUNTPOINT}/${remote} --config ${CONFIGFILE} ${RCLONE_PARAMS} --daemon >>/dev/null 2>&1 &
}
COUNT=0
if [[ ${INTERACTIVE} = 0 ]]; then
until [[ $(getprop sys.boot_completed) = 1 ]] && [[ $(getprop dev.bootcomplete) = 1 ]] && [[ $(getprop init.svc.bootanim) = stopped ]] && [[ -e ${USER_CONF} ]] || [[ ${COUNT} -eq 240 ]]; do
sleep 5
((++COUNT))
done
fi
DECRYPT_CHK() {
ls sdcard | grep -q -w "Android"
}
if [[ ${COUNT} -eq 240 ]] || [[ ! -d /sdcard/Android ]]; then
exit 0
fi
if [[ ! -d ${USER_CONFDIR} ]]; then
mkdir ${USER_CONFDIR}
fi
if [[ -e ${USER_CONFDIR}/.disable ]] && [[ ${INTERACTIVE} = 0 ]]; then
exit 0
fi
if [[ ${INTERACTIVE} = 0 ]]; then
export INTERACTIVE=1
${MODDIR}/service.sh
exit
fi
global_params
if [ ! -d "$CLOUDROOTMOUNTPOINT" ]; then
mkdir -p "$CLOUDROOTMOUNTPOINT"
chown root:sdcard_rw "$CLOUDROOTMOUNTPOINT"
touch "$CLOUDROOTMOUNTPOINT"/.nomedia
chown root:sdcard_rw "$CLOUDROOTMOUNTPOINT"/.nomedia
chmod 0775 "$CLOUDROOTMOUNTPOINT"/.nomedia
fi
if [ ! -f "$CLOUDROOTMOUNTPOINT/.nomedia" ]; then
touch "$CLOUDROOTMOUNTPOINT"/.nomedia
chown root:sdcard_rw "$CLOUDROOTMOUNTPOINT"/.nomedia
chmod 0644 "$CLOUDROOTMOUNTPOINT"/.nomedia
fi
if [[ ! -d ${CACHE} ]]; then
mkdir -p ${CACHE}
chown root:sdcard_rw ${CACHE}
chmod 0775 ${CACHE}
fi
if [[ -d ${CACHE} ]]; then
chown root:sdcard_rw ${CACHE}
chmod 0775 ${CACHE}
fi
if [[ ! -d ${CACHE_BACKEND} ]]; then
mkdir -p ${CACHE_BACKEND}
fi
if [[ -d ${CACHE_BACKEND} ]]; then
chown root:sdcard_rw ${CACHE_BACKEND}
chmod 0775 ${CACHE_BACKEND}
fi
if [[ ! -L ${RUNTIME_R}/cloud ]]; then
ln -sf ${CLOUDROOTMOUNTPOINT} ${RUNTIME_R}/cloud
fi
if [[ ! -L ${RUNTIME_W}/cloud ]]; then
ln -sf ${CLOUDROOTMOUNTPOINT} ${RUNTIME_W}/cloud
fi
if [[ ! -L ${RUNTIME_D}/cloud ]]; then
ln -sf ${CLOUDROOTMOUNTPOINT} ${RUNTIME_D}/cloud
fi
if [[ ! -L "$BINDPOINT_P" ]]; then
ln -sf "$CLOUDROOTMOUNTPOINT" "$BINDPOINT_P"
fi
if [ ! -f "$BINDPOINT_P/.nomedia" ]; then
touch "$BINDPOINT_P"/.nomedia
chown root:sdcard_rw "$BINDPOINT_P"/.nomedia
chmod 0644 "$BINDPOINT_P"/.nomedia
fi
if [[ -e ${USER_CONF} ]]; then
cp ${USER_CONF} ${CONFIGFILE}
chmod 0600 ${CONFIGFILE}
fi
if [[ ${NETCHK} = 1 ]]; then
until net_chk || [[ ${COUNT} = 60 ]]; do
sleep 5
((++COUNT))
done >>/dev/null 2>&1
fi
echo "Default CACHEMODE ${CACHEMODE}"
sleep 5
LD_LIBRARY_PATH=${MODDIR} ${MODDIR}/rclone listremotes --config ${CONFIGFILE} | cut -f1 -d: |
while read remote; do
echo
list_remote=${remote}
CLOUDROOTMOUNTPOINT=/mnt/cloud
PROFILE=0
DISABLE=0
READONLY=0
global_params >>/dev/null 2>&1
remote=${list_remote}
custom_params
if [[ ${ISOLATE} = 1 ]] && [[ ${PROFILE} -gt 0 ]] && [[ ${BINDSD} = 1 ]]; then
CLOUDROOTMOUNTPOINT=/data/media/${PROFILE}/.cloud
fi
if [[ ${DISABLE} = 1 ]] || [[ -e ${USER_CONFDIR}/.${remote}.disable ]]; then
echo "${remote} disabled by user"
continue
fi
sd_unbind
rclone_mount
sd_binder
syncd_service
reset_params
done
echo
if [[ ${HTTP} = 1 ]]; then
if (${MODDIR}/rclone serve http ${CLOUDROOTMOUNTPOINT} --addr ${HTTP_ADDR} --no-checksum --no-modtime --read-only >>/dev/null 2>&1 &) then
echo "HTTP Server: http://${HTTP_ADDR}"
fi
fi
if [[ ${FTP} = 1 ]]; then
if (${MODDIR}/rclone serve ftp ${CLOUDROOTMOUNTPOINT} --addr ${FTP_ADDR} --no-checksum --no-modtime --read-only >>/dev/null 2>&1 &) then
echo "FTP Server: ftp://${FTP_ADDR}"
fi
fi
if [[ ${SFTP} = 1 ]] && [[ ! -z ${SFTP_USER} ]] && [[ ! -z ${SFTP_PASS} ]]; then
if (${MODDIR}/rclone serve sftp ${CLOUDROOTMOUNTPOINT} --addr ${SFTP_ADDR} --user ${SFTP_USER} --pass ${SFTP_PASS} --no-checksum --no-modtime --read-only >>/dev/null 2>&1 &) then
echo "SFTP Server: sftp://${SFTP_ADDR}"
fi
fi
exit