forked from glitsj16/firejail-handler-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firejail-handler-bittorrent-ctl
113 lines (105 loc) · 3.99 KB
/
firejail-handler-bittorrent-ctl
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
#!/bin/sh
#
## BitTorrent URL handler for Firejail
#+ (1) inside the sandbox `firejail-handler-bittorrent` drops a URL
#+ at a read/write accessible location
#+ (2) outside the sandbox this script picks up the URL and starts
#+ a fully sandboxed bittorrent application
#+ (defaults to a local transmission-daemon)
# shellcheck disable=SC2034
### vars
## source common settings
_settings_file="firejail-handler-settings-extra.inc"
if [ -f "${HOME}/.config/firejail/${_settings_file}" ]; then
#+ use settings defined by user
. "${HOME}/.config/firejail/${_settings_file}"
elif [ -f "/etc/firejail/${_settings_file}" ]; then
#+ use system-wide settings
. "/etc/firejail/${_settings_file}"
else
#+ fall back to hard-coded values
if [ "$(which xdg-user-dir)" ]; then
_drop_dir="$(xdg-user-dir DOWNLOAD)"
else
_xdg_config_home="$(env | grep -c "XDG_CONFIG_HOME")"
if [ "$_xdg_config_home" = 1 ]; then
if [ -s "${XDG_CONFIG_HOME}/user-dirs.dirs" ]; then
_drop_dir="$(grep "^XDG_DOWNLOAD_DIR=" "${XDG_CONFIG_HOME}/user-dirs.dirs" | awk '{split($0,a,"="); print a[2]}' | sed 's/"//g')"
fi
elif [ -s "${HOME}/.config/user-dirs.dirs" ]; then
_drop_dir="$(grep "^XDG_DOWNLOAD_DIR=" "${HOME}/.config/user-dirs.dirs" | awk '{split($0,a,"="); print a[2]}' | sed 's/"//g')"
else
_drop_dir="${HOME}/Downloads"
fi
fi
# bittorrent client watch dir for .torrent files
_download_dir="$_drop_dir"
## drop dir location where the URL data will be saved temporarily
#+ IMPORTANT: this needs to be read/write accessible in the sandbox
_drop_dir="${_drop_dir}/.firejail-dropzone"
## protocol handlers
#+ bittorrent
_drop_file_bt="${_drop_dir}/bittorrent.drop"
_media_dl_mode="local"
_rpc_bin="/usr/local/bin/transmission-remote"
_rpc_port_local="9091"
_rpc_port_tunnel="9999"
_sshfs_mountpoint="/mnt/sshfs-remote"
_systemd_service="transmission-daemon"
#+ youtube
_drop_file_yt="${_drop_dir}/youtube.drop"
_media_player_bin="/usr/local/bin/mpv"
fi
_selfie="$(basename "$0")"
### logic
# ensure drop location exists
[ ! -d "$_drop_dir" ] && mkdir -p "$_drop_dir"
# ensure needed commands are available
if [ ! -x "/usr/bin/inotifywait" ]; then
echo "Command not found: /usr/bin/inotifywait - aborting"
exit 1
fi
if [ ! -x "$_rpc_bin" ]; then
echo "Command not found: ${_cmd} - aborting"
exit 1
fi
## watch drop location
[ -e "$_drop_file_bt" ] && rm -f "$_drop_file_bt"
touch "$_drop_file_bt"
inotifywait -m -e modify --format '%f' "$_drop_file_bt" | while read -r line
do
if [ -s "$_drop_file_bt" ]; then
#+ get bittorrent data
_bt_data="$(head -n 1 "$_drop_file_bt")"
_drop_is_magnet="$(grep -cf "^magnet" "$_drop_file_bt")"
_drop_is_torrent_file="$(grep -cf "\.torrent" "$_drop_file_bt")"
if [ "$_drop_is_magnet" != 0 ]; then
#+ we have a magnet link
# add magnet
"$_rpc_bin" "$_rpc_port" -a "$_bt_data" > /dev/null 2>&1 &
# notify user
if [ -x "/usr/bin/notify-send" ]; then
_bt_name="$(echo "$_bt_data" | awk '{split($0,a,"&"); print a[2]}' | sed "s|dn=||" | sed "s|+| |g" | sed "s|%27|\'|g")"
notify-send "Firejail :: BT handler" "Magnet added\\n${_bt_name}" &
fi
elif [ "$_drop_is_torrent_file" != 0 ]; then
#+ we have a .torrent file
# add .torrent
# moved into the configured watch dir, nothing else to do here
# notify user
if [ -x "/usr/bin/notify-send" ]; then
_bt_name="$(echo "$_bt_data" | sed "s/.torrent//")"
notify-send "Firejail :: BT handler" "Torrent added\\n${_bt_name}" &
fi
else
#+ assume error
# notify user
if [ -x "/usr/bin/notify-send" ]; then
notify-send "Firejail :: BT handler" "ERROR processing link [${_selfie}]" &
fi
fi
# reset
truncate -s 0 "$_drop_file_bt" &
fi
done
exit 0