aboutsummaryrefslogtreecommitdiff
path: root/litterbox
blob: cf2b2092f3f98a1e3c292aed6fcbac81a7ec746d (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
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
#!/bin/bash

# API spec: https://litterbox.catbox.moe/tools.php
# curl -F "reqtype=fileupload" -F "time=1h" -F "fileToUpload=@filename" \
#   https://litterbox.catbox.moe/resources/internals/api.php
# time can be 1h, 12h, 24h, or 72h
# The following file types are currently not allowed:
# .exe, .scr, .cpl, .doc*, .jar
# ...so this script will gzip those automatically.

SELF="$( basename $0 )"
APIURL="https://litterbox.catbox.moe/resources/internals/api.php"
URLS=""
EXPIRE="1"
DELAY="${DELAY:-2}"
UPLOAD_COUNT=0

die() {
  echo "$SELF: fatal: $@" 1>&2
  exit 1
}

warn() {
  echo "$SELF: warning: $@" 1>&2
}

info() {
  [ "$VERBOSE" = "1" ] && echo "$SELF: info: $@" 1>&2
}

usage() {
  cat <<EOF
$SELF: upload files to litterbox.catbox.moe

Usage: $SELF [-e hours] [-g] [-z] [-c] [-v] file <file ...>
       $SELF -d [-o output] url <url ...?

Each file can be a filename, a directory name, or "-" for standard
input. If no files are given, $SELF reads from standard input.

Once a file is uploaded, its URL is printed on stdout.
If xsel is installed, it's also copied to the X paste buffer.

When uploading multiple files, all the URLs are copied to
the X buffer (separated by spaces). Also, a delay of $DELAY
seconds is added between uploads, to avoid hammering the site.

Options:
  -e   Set expiration time in hours. Allowed values are 1, 12,
       24, or 72. Default is $EXPIRE.

  -g   Upload gzipped files. This is automatically enabled
       when uploading files whose names end in .exe, .scr,
       .cpl, .doc*, .jar (unless -z is given). Also, directories
       are automatically tarred/gzipped (unless -z is given).

  -z   Upload zipped files or dirs.

  -t   Upload tarred and gzipped directory. Won't work with
       single files (use -g or -z for those).

  -c   Upload encrypted file. Currently this means the file
       is encrypted with 'openssl enc -pbkdf2 -aes-256-cbc', and
       you're prompted for an encryption key. Whoever downloads
       the encrypted file will have to decrypt it with the -d
       option, or else do it manually.

  -d   Download and decrypt previously uploaded file(s) from URL(s).
       Equivalent to downloading the file normally, then piping it
       through:
         openssl enc -d -pbkdf2 -aes-256-cbc
       ...which means the file will be printed to your terminal,
       unless you use shell redirection or the -o option.  The options
       above (-e -g -z -t -c) have no effect in download mode.

  -o   Set output file for -d option. Note that giving multiple URLs
       will result in all the data being written to the same file.

  -v   Verbose operation.

Note: With the -c and -d option, you will be prompted interactively
for the encryption key (password), on the process's controlling tty.

Author: B. Watson <urchlay@slackware.uk>
License: WTFPL (Do WTF you want with this)
EOF
}

set_expiration() {
  case "$1" in
    1|12|24|72) ;; # OK
    *) die "invalid expiration time, allowed values are: 1 12 24 72" ;;
  esac
  EXPIRE="$1"
}

add_url() {
  if [ "$URLS" = "" ]; then
    URLS="$1"
  else
    URLS+=" $1"
  fi
  : $(( UPLOAD_COUNT++ ))
}

copy_urls() {
  if ! xsel --version &>/dev/null; then
    warn "xsel not found in PATH, not copying URL(s) to clipboard"
    return
  fi

  if [ "$DISPLAY" = "" ]; then
    warn "DISPLAY not set in env, not copying URL(s) to clipboard"
    return
  fi

  echo -n "$URLS" | xsel -i
}

compress_file() {
  local ext
  local basefile
  local is_dir

  [ -d "$FILE" ] && is_dir=1

  if [ "$ZIP" = "1" ]; then
    ext=.zip
  else
    ext=.gz
  fi

  basefile="$( basename "$FILE" )"

  # TODO: create a random tmp dir, don't use /tmp!
  TMPFILE="${TMP:-/tmp}/$basefile$ext"
  rm -f "$TMPFILE"
  if [ "$ZIP" = "1" ]; then
    zip -qr "$TMPFILE" "$FILE"
    info "zipped $FILE as $TMPFILE"
  else
    if [ "$is_dir" = 1 ]; then
      tar cfz "$TMPFILE" "$FILE"
      info "tarred up $FILE as $TMPFILE"
    else
      gzip -9 < "$FILE" > "$TMPFILE"
      info "gzipped $FILE as $TMPFILE"
    fi
  fi
  FILE="$TMPFILE"
}

# TODO: curl options:
# --max-time --retry --retry-max-time
upload_file() {
  local url
  local use_compression

  info "checking $FILE"

  case "$FILE" in
    *.exe|*.scr|*.cpl|*.doc*|*.jar)
      warn "forbidden file extension, compression forced"
      use_compression=1 ;;
    *)
      use_compression="$COMPRESS" ;;
  esac

  if [ "$use_compression" = 1 ]; then
    # can't easily zip stdin, write it to a file
    if [ "$FILE" = "-" ]; then
      FILE="${TMP:-/tmp}/$SELF.stdin"
      cat > "$FILE"
      if [ "$?" != 0 ]; then
        rm -f "$FILE"
        return
      fi
    fi
    compress_file # resets FILE, sets TMPFILE
  fi

  # at this point, we have an actual file, even if input was stdin,
  # so we can encrypt it.
  if [ "$ENC" = "1" ]; then
    CRYPTFILE="$FILE.aes"
    openssl enc -pbkdf2 -aes-256-cbc < "$FILE" > "$CRYPTFILE"
    if [ "$?" != "0" ]; then
      warn "encryption failed, skipping file"
      return
    fi
    FILE="$CRYPTFILE"
  fi

  if [ "$URLS" !=  "" ]; then
    info "sleeping $DELAY sec between uploads"
    sleep "$DELAY"
  fi

  info "uploading $FILE"

  url="$(
  cat "$FILE" | \
    curl --silent \
      -F "reqtype=fileupload" \
      -F "time=${EXPIRE}h" \
      -F "fileToUpload=@-" \
      "$APIURL"
  )"
  # TODO: check curl's exit status!

  case "$url" in
    https://litter.catbox.moe/*) add_url "$url" ;;
    *) warn "upload failed" ;;
  esac
}

download_and_decrypt() {
  local url="$1"
  info "downloading and decrypting $url"
  curl --silent "$url" | openssl enc -d -pbkdf2 -aes-256-cbc
  [ "$?" != 0 ] && warn "download/decrypt failed"
}

# main()
if [ "$1" = "--help" ]; then
  usage ; exit 0
fi

while getopts ":e:gzcvdo:" OPT; do
  case "$OPT" in
    e) set_expiration "$OPTARG"; opt_e=1 ;;
    o) DLOUT="$OPTARG" ;;
    g) GZIP=1     ;;
    z) ZIP=1      ;;
    c) ENC=1      ;;
    v) VERBOSE=1  ;;
    d) DOWNLOAD=1 ;;
    *) die "invalid option -$OPTARG, try --help" ;;
  esac
done

shift $(($OPTIND - 1))

[ "$GZIP" = 1 -a "$ZIP" = 1 ] && die "can't give both -g and -z"
[ "$GZIP" = 1 -o "$ZIP" = 1 ] && COMPRESS=1

if [ "$DOWNLOAD" = 1 ]; then
  [ "$GZIP"   = 1 ]  && warn "-g option ignored when using -d"
  [ "$ZIP"    = 1 ]  && warn "-z option ignored when using -d"
  [ "$ENC"    = 1 ]  && warn "-c option ignored when using -d"
  [ "$opt_e"  = 1 ]  && warn "-e option ignored when using -d"
  [ "$DLOUT" != "" ] && exec 1>"$DLOUT" || die "can't redirect stdout"
  for arg; do
    download_and_decrypt "$arg"
  done
  exit 0
else
  [ "$DLOUT" != "" ] && warn "-o option ignored when not using -d"
fi

info "expire time set to $EXPIRE hour(s)"

[ "$1" = "" ] && set -- -
for arg; do
  TMPFILE=""
  CRYPTFILE=""

  if [ "$arg" = "-" -o -f "$arg" ]; then
    FILE="$arg"
  elif [ -d "$arg" ]; then
    FILE="$arg"
    compress_file # sets FILE
  else
    warn "$arg not a regular file, ignoring"
    continue
  fi

  upload_file

  if [ "$TMPFILE" != "" ]; then
    info "removing temp file $TMPFILE"
    rm -f "$TMPFILE"
  fi

  if [ "$TMPFILE" != "" ]; then
    info "removing temp encrypted file $CRYPTFILE"
    rm -f "$CRYPTFILE"
  fi
done

if [ "$UPLOAD_COUNT" = "0" ]; then
  die "no files uploaded"
fi

echo "$URLS"
copy_urls

info "$UPLOAD_COUNT file(s) uploaded"

exit 0