-
Notifications
You must be signed in to change notification settings - Fork 0
/
websh
executable file
·97 lines (84 loc) · 1.88 KB
/
websh
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
#!/bin/bash
# Khronos | websh
URL="https://websh.jiro4989.com/api/shellgei"
FILE="-"
MODE="default"
code=""
images=""
usage(){
cat <<_USAGE
Usage: $0 -[rfh] [-p COMMAND] [-i IMAGE] [FILE]
With no FILE, or when FILE is -, read standard input.
-r : print raw response
-f : print response in readable format
-p COMMAND: read standard input and pass it to the standard input of COMMAND on the websh
-i IMAGE: upload IMAGE to websh
-h : display this help and exit
_USAGE
}
while getopts rfp:i:h OPT; do
case "$OPT" in
"r") MODE="raw"
;;
"f") MODE="format"
;;
"p") MODE="pipe"
code="\"base64 -d <<<$(base64 -w0)|$OPTARG\""
;;
"i") images="$images,\"$(base64 -w0 < "$OPTARG")\""
;;
"h") usage
exit 0
;;
"?") usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ $# -ge 1 ]; then
FILE="$1"
fi
images="${images:1}"
if [ "$MODE" != "pipe" ]; then
if [ "$FILE" = "-" ]; then
code="$(jq -Rs .)"
else
code="$(jq -Rs . < "$FILE")"
fi
fi
data="$(
curl \
-sS \
-X POST \
-d "{\"code\":$code,\"images\":[$images]}" \
"$URL"
)"
timestamp="$(date +%Y%m%d_%H%M%S)"
if [ "$MODE" = "raw" ]; then
printf "%s\n" "$data"
exit $(jq -r .status <<<"$data")
fi
sysmsg="$(jq -r .system_message <<<"$data")"
case "$MODE" in
"default" | "pipe")
jq -r .stdout <<<"$data"|head -c -1
test -n "$sysmsg"&&printf "%s\n" "$sysmsg" >&2
;;
"format")
echo "[stdout]"
jq -r .stdout <<<"$data"
echo "[stderr]"
jq -r .stderr <<<"$data"
echo "[system message]"
jq -r .system_message <<<"$data"
;;
esac
num=$(($(jq '.images |length' <<<"$data")-1))
for i in $(seq -w 0000 $num);do
tmp="$(mktemp -p .)"
jq -r ".images[$i].image" <<<"$data"|base64 -d >$tmp
type=($(file -b $tmp))
mv "$tmp" "websh-$timestamp-$i.${type[0]}"
done
exit $(jq -r .status <<<"$data")