-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.sh
More file actions
executable file
·115 lines (102 loc) · 2.11 KB
/
client.sh
File metadata and controls
executable file
·115 lines (102 loc) · 2.11 KB
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
#!/bin/bash
# configurations
#
# Set CURL to curl binary path, empty string to disable curl
# Set WGET to wget binary path, empty string to disable wget
# One of CURL and WGET must be set. CURL is used if both are set.
remote="http://localhost:9801"
if [[ $SERVER != "" ]]
then
remote="$SERVER"
fi
function urlencode() {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-])
printf "$c"
;;
*)
printf "$c" | xxd -p -c1 | while read x
do
printf "%%%s" "$x"
done
;;
esac
done
}
# with curl
function c {
uri="$1"
wait="$2"
secret="$3"
opts="-sL"
if [[ $DEBUG != "" ]]
then
opts="-SL"
fi
$CURL -X POST \
--data-urlencode "uri=${uri}" \
--data-urlencode "wait=${wait}" \
--data-urlencode "secret=${secret}" \
$opts \
"${remote}/grab"
}
# with wget
function w {
uri=$(urlencode "$1")
wait=$(urlencode "$2")
secret=$(urlencode "$3")
opts="-O -"
if [[ $DEBUG == "" ]]
then
opts="-q -O -"
fi
$WGET \
--post-data="uri=${uri}&wait=${wait}&secret=${secret}" \
$opts \
"${remote}/grab"
}
function help {
echo "Usage: ${1} uri wait [secret]"
echo ''
echo 'Example:'
echo " ${1} http://google.com 'div#xfoot > script'"
echo ''
echo 'Envvars:'
echo ' SERVER: Specify location of your server'
echo " SERVER=http://example.com ${1} http://google.com 'div#xfoot > script'"
echo ' DEBUG: Make curl/wget show basic messages'
echo " DEBUG=1 ${1} http://google.com 'div#xfoot > script'"
}
if [[ $1 == "" || $2 == "" ]]
then
help "$0"
exit 1
fi
# validate configurations
if [[ $CURL == "" && $WGET == "" ]]
then
# invalid, try auto detect
which curl > /dev/null 2>& 1
if [[ $? == 0 ]]
then
CURL=curl
else
which wget > /dev/null 2>&1
if [[ $? == 0 ]]
then
WGET=wget
else
echo 'None of CURL and WGET is set, and we cannot determine path to any of it.'
exit 1
fi
fi
fi
if [[ $CURL == "" ]]
then
w "$1" "$2" "$3"
else
c "$1" "$2" "$3"
fi