-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer
executable file
·263 lines (242 loc) · 8.45 KB
/
transfer
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
#! /usr/bin/env zsh
autoload is-at-least
show_help(){
echo "Usage of transfer"
echo
echo "transfer [-t <n>] [-d <d>] [-p <s>|-e] <filename/folder>"
echo
echo "--times/-t <n> limit to n downloads"
echo "--days/-d <n> limit to n days"
echo "--recipient/-r <s> encrypt for recipient"
echo "--password/-p <s> encrypt using gpg and the specified password"
echo "--encrypt/-e encrypt using gpg and a generated password"
echo "--serverencrypt/-j encrypt using server side encryption"
echo "-q/-s only print download url to stdout, "
echo " use -qq/-ss to disable progress"
echo "-v print extended debug information"
echo
echo "Environment:"
echo " - TRANSFER_BASE_URL The baseurl of the transfer server, "
echo " default transfer.sh"
echo " - TRANSFER_TRANSFORMER Default is cat, which is replaced by "
echo " a proper gpg call, if -r/-e/-p is "
echo " specified, default is cat"
}
local TRACE=10
local DEBUG=20
local INFO=30
local WARNING=40
local ERROR=50
local log_level=$INFO
rawurlencode() {
# Thanks to https://stackoverflow.com/a/10660730
local string="$*"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
REPLY="${encoded}"
}
dolog() {
local origlevel=${1:-INFO}
if [[ $origlevel =~ '[0-9]+' ]]; then
level=$origlevel
else
level=${(P)origlevel}
fi
shift
local message=$*
if [ $level -ge $log_level ]; then
printf "[%s]: % 8s: %s: %s\n" "$(date +"%Y%m%d-%H%M%S")" "$origlevel" "${funcstack[@]:1:1}" "$message" >&2
fi
}
checkloglevel(){
local origlevel=${1:-INFO}
if [[ $origlevel =~ '[0-9]+' ]]; then
level=$origlevel
else
level=${(P)origlevel}
fi
if [ $level -ge $log_level ]; then
return 0
fi
return 1
}
transfer() {
local base_url headers extra_headers password
base_url=${TRANSFER_BASE_URL:-https://transfer.sh}
zmodload zsh/zutil
extraopt=
if is-at-least 5.8;then
extraopt=' -F'
fi
zparseopts $extraopt -E -D \
- \
-days:=days d:=days \
-times:=dl t:=dl \
-passwordlength:=password_length l:=password_length \
q+=silent s+=silent \
v+=verbose \
p:=password -password:=password \
e=secure -encrypt=secure \
j=serversecure -serverencrypt=serversecure \
r+:=recipient -recipient+:=recipient \
h=help -help=help \
|| (dolog ERROR "parameter issue" && return -1)
if [[ ${#help} -gt 0 || $# -eq 0 ]]; then
show_help
return 1
fi
if [[ ${#silent} -gt 0 ]]; then
log_level=$(($INFO+${#silent}*10))
elif [[ ${#verbose} -gt 0 ]]; then
log_level=$(($INFO-${#verbose}*10))
fi
if [[ ${#password_length} -gt 0 ]]; then
password_length=${password_length[-1]}
else
password_length=24
fi
transformer=${TRANSFER_TRANSFORMER:-"cat"}
days=${days[-1]}
dl=${dl[-1]}
dolog TRACE "base_url: ${base_url}"
dolog TRACE "days: ${days:-not set}"
dolog TRACE "times: ${dl:-not set}"
dolog TRACE "password: ${password:-not set}"
dolog TRACE "password length: ${password_length:-not set}"
dolog TRACE "secure: ${secure:-not set}"
dolog TRACE "serversecure: ${serversecure:-not set}"
dolog TRACE "recipient ${recipient:-not set}"
# Transformer is used to probably encrypt the content before uploading the file
extra_headers=
# Check limits
if [[ -n $dl && $dl -ne 0 ]]; then
dolog INFO "Limit to $dl downloads"
extra_headers="${extra_headers} -H 'Max-Downloads: $dl'"
fi
if [[ -n $days && $days -ne 0 ]]; then
dolog INFO "Limit availability to $days days"
extra_headers="${extra_headers} -H 'Max-Days: $days'"
fi
# Encryption
if [[ ${#recipient} -gt 0 ]]; then
dolog INFO "Using local gpg encryption"
transformer="gpg --batch --yes -o- --encrypt "
for ((i=2; i<=${#recipient[@]}; i+=2)); do
rec=${recipient[$i]}
transformer="$transformer --recipient $rec"
done
elif [[ ${#password} -gt 0 || ${#secure} -gt 0 ]]; then
dolog INFO "Using local gpg password encryption"
# If we do not have a password, lets generate one.
if [[ "${#password}" -eq 0 ]]; then
dolog INFO "Generating random password"
password=$(openssl rand -base64 ${password_length:-24})
else
password=${password[-1]}
fi
password_type="gpg"
transformer="gpg --symmetric --batch --yes -o- --passphrase $password"
password_url_hash="#$password"
decryption="curl %s | gpg --batch --yes --decrypt --passphrase $password -o %s"
elif [[ ${#password} -gt 0 || ${#serversecure} -gt 0 ]]; then
dolog INFO "Using server side encryption"
# If we do not have a password, lets generate one.
if [[ "${#password}" -eq 0 ]]; then
dolog INFO "Generating random password"
password=$(openssl rand -base64 ${password_length:-24})
else
password=${password[-1]}
fi
password_type="Server Side Encryption"
extra_headers="${extra_headers} -H 'X-Encrypt-Password: $password'"
decryption="curl -H 'X-Decrypt-Password: $password' %s -o %s"
fi
# create temporary file for header
headers=$(mktemp -t transfer-header.XXXXXXX)
# Build the curl command
base_curl="curl ${extra_headers} -D $headers"
if $(checkloglevel WARNING); then
base_curl="$base_curl --progress-bar"
else
base_curl="$base_curl -s"
fi
base_curl="$base_curl --upload-file -"
if tty -s; then
file="$1"
rawurlencode $(basename "$file")
file_name=$REPLY
original_file_name=$file_name
if [ ! -e "$file" ]; then
dolog ERROR "$file: No such file or directory">&2
return 1
fi
if [ -d "$file" ]; then
file_name="$file_name.zip"
dolog DEBUG "command (cd $file && zip -r -q - .) | eval $transformer | eval $base_curl ${base_url}/$file_name"
url=$( (cd "$file" && zip -r -q - .) | eval $transformer | eval $base_curl "${base_url}/$file_name" | tee /dev/null)
else
dolog DEBUG "command cat $file | eval $transformer | eval $base_curl ${base_url}/$file_name"
url=$(cat "$file" | eval $transformer | eval $base_curl "${base_url}/$file_name" | tee /dev/null)
fi
else
# 'cat'-ing to transfer
file_name=$1
original_file_name='<cat>'
dolog DEBUG "Receiving from pipe"
dolog DEBUG "command eval $transformer | eval $base_curl ${base_url}/$file_name"
url=$(eval $transformer | eval $base_curl "${base_url}/$file_name" | tee /dev/null)
fi
if [[ $url =~ "https://" ]]; then
# if we get an url returned
delete_url=$(cat $headers | grep --color=never -i 'x-url-delete:' | sed 's/x-url-delete: //')
delete_url="${delete_url//$'\r'}"
if $(checkloglevel INFO); then
printf "Web URL: %s\n" "$url"
printf "Download URL: %s\n" "${url/$base_url\//$base_url/get/}"
printf "Show URL: %s\n" "${url/$base_url\//$base_url/inline/}"
if [[ ${#password} -gt 0 ]]; then
# In case a password has been set, we show it here again
print "*********************"
printf "Password (%s): %s\n" "${password_type}" "${password}"
printf "Download (%s): %s\n" "${password_type}" "$(printf ${decryption} "${url/$base_url\//$base_url/get/}" "${file_name}")"
print "*********************"
fi
if [[ ${#recipient} -gt 0 ]]; then
print "*********************"
print "Encrypted for:"
for ((i=2; i<=${#recipient[@]}; i+=2)); do
printf " - %s\n" "${recipient[$i]}"
done
print "*********************"
fi
printf "Delete URL: %s\n" "$delete_url"
printf "Delete command: curl -X DELETE %s\n" "$delete_url"
else
#include the password in the url, if this is our only output
echo -n $url$password_url_hash
fi
mkdir -p ~/.local/share/transfer/
touch ~/.local/share/transfer/uploads.txt
echo "$(pwd)|$original_file_name|$url|$delete_url|$days|$dl" >> ~/.local/share/transfer/uploads.txt
elif [[ $url =~ 'virus' ]]; then
# Seems the server detected a virus
echo "Clamav scan found a virus in the file(s) you tried to upload."
else
# Neither an url, nor a virus report, so we dump the answer and the headers
echo $url
echo "**** 8< -- Headers --"
cat $headers
echo "**** >8 -- ******* --"
fi
rm $headers
}
transfer $*