summaryrefslogtreecommitdiffstats
path: root/elchos/root-image/krebs/lib/filehooker
blob: b3bef435b52554214ab424c032c10b19a70d60e1 (plain)
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
#@include core
. /krebs/lib/core
#@include network
. /krebs/lib/network
ncdc_user=${ncdc_user:-hooker}
ncdc_bin=${ncdc_bin:-/usr/bin/ncdc}


ncdc_config(){
    # maybe we want to use the running ncdc process and communicate via tmux send-keys ?
    txt="$(cat)"
#    printf "%s" "$txt"
    ! sudo -u $ncdc_user /usr/bin/tmux has-session -t dcpp && echo "ncdc session must be running" && exit 1
       sudo -u $ncdc_user /usr/bin/tmux send-keys -t dcpp:ncdc "$txt"  C-m
}

ncdc_configure_netshare(){
    : "${1?provide path to share}"
    rnd=`hexdump -n 2 -e '/2 "%u"' /dev/urandom`
    rnd_name="${2:-share_$rnd}"
    info "removing old share $rnd_name"
    (echo "/unshare $rnd_name" ) | ncdc_config
    info "adding share $rnd_name ($1)"
    (echo "/share $rnd_name $1") | ncdc_config
}

ncdc_configure_nick(){
    nick=${1?nick must be provided}
    info "configuring DC Nick: $nick"
    echo "/nick $nick" | ncdc_config
}
ncdc_configure_hub(){
    rnd=`hexdump -n 2 -e '/2 "%u"' /dev/urandom`
    hub=${1?adcs://localhost:2781}
    hubname="${2:-hub_$rnd}"
    info "setting active as true"
    (echo "/set active true") | ncdc_config
    info "configuring DC Hub: $hub, activating autconnect"
    (echo "/open ${hubname} ${hub}" ;
    echo "/hset autoconnect true") | ncdc_config
}

ncdc_download(){
install_dir="$(dirname "${ncdc_bin}")"
info "installing ncdc to $install_dir"
curl http://dev.yorhel.nl/download/ncdc-linux-x86_64-1.19.tar.gz | tar xz -C "$install_dir"
}
ncdc_install(){
useradd -m $ncdc_user ||:
}

ncdc_autostart(){
# only systemd
# punani install tmux
cat > /etc/systemd/system/ncdc@.service <<EOF
[Unit]
Description=ncdc
Requires=network.target local-fs.target

[Service]
Type=oneshot
RemainAfterExit=yes
KillMode=none
User=%I
ExecStart=/usr/bin/tmux new-session -s dcpp -n ncdc -d ncdc
ExecStop=/usr/bin/tmux send-keys -t dcpp:ncdc "/quit" C-m

[Install]
WantedBy=multi-user.target
EOF
systemctl enable ncdc@$ncdc_user
}

# 20gig in bytes
min_netshare_size=${min_netshare_size:-20000000000}
get_disksize(){
fdisk -l ${1?provide disk} | grep '^Disk ' | cut -d\  -f 5
}

prepare_netshares(){
    count=0
    fdisk -l  | grep '^Disk '  | egrep '(/dev/sd|/dev/hd)' | cut -d\  -f 2 | tr -d : | while read disk;do
        size=$(get_disksize $disk)
        if test "$size" -gt "$min_netshare_size";
        then
            info "using $disk with $size bytes"
            dd if=/dev/zero of=$disk bs=1M count=1 >/dev/null
            sleep 1
            (printf "o\nn\np\n\n\n\nw\n\n") |fdisk $disk >/dev/null ||:
            #partprobe $disk
            mkfs.btrfs -f ${disk}1 >/dev/null
            uuid="$(blkid ${disk}1 -o value | head -n 1)"
            mountpoint="/media/vag${count}"
            mkdir -p "$mountpoint"
            echo "UUID=$uuid  $mountpoint btrfs rw,relatime,space_cache 0 0" >> /etc/fstab
            echo "$mountpoint"
            : $((count++))
        else
            info "skipping $disk"
        fi
    done
}
install_tor_announce(){
# systemd only
info "writing tor_announce.service"
cat > /etc/systemd/system/tor_announce.service<<EOF
[Unit]
Description=Announce Tor Hidden Address
After=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/tor_announce

[Install]
WantedBy=multi-user.target
EOF
info "writing tor_announce to /usr/bin/tor_announce"
printf '#!/bin/sh\nsleep 20\n' > /usr/bin/tor_announce
http_get conf.krebsco.de/tor_publish_ssh >> /usr/bin/tor_announce
chmod +x /usr/bin/tor_announce
info "enable tor_announce"
systemctl enable tor_announce
systemctl start tor_announce
}
is_mounted(){
    cat /etc/mtab| cut -d\  -f 1 | grep -q "^$1$" && info "$1 is already mounted"
}
share_all_partitions(){
    count=0
    # all /dev/sdX and all mapped devices
    (find /dev -name '[shv]d[a-z][0-9]';find /dev/mapper ! -type d ;find /dev -name 'md[0-9][0-9]*')| while read disk;do
        size=$(get_disksize $disk 2>/dev/null)

        if test "$size" -gt "$min_netshare_size" 2>/dev/null ; #&& ! is_mounted "$disk";
        then
          info "trying disk $disk"
          mountpoint=/media/vag$count
          mkdir -p $mountpoint
          umount $disk >/dev/null 2>&1  && info "remounting $disk" || :
          umount $mountpoint  >/dev/null 2>&1 && info "unmounting old mountpoint $mountpoint" || :
          ! mount $disk $mountpoint   >/dev/null 2>&1 && error "cannot mount $disk"  && continue
          chown "$ncdc_user" "$mountpoint"
          : $((count++))
          ncdc_configure_netshare "$mountpoint" "$(basename $mountpoint)" 2>/dev/null
          info "$mountpoint is mounted and shared"
        else
          info "skipping $disk"
        fi
    done
}