-
Notifications
You must be signed in to change notification settings - Fork 0
/
homunculus.sh
executable file
·141 lines (126 loc) · 3.26 KB
/
homunculus.sh
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
#!/bin/bash
SHIP="zod"
CODE="lidlut-tabwed-pillex-ridrup"
BASE_URL="localhost:8081"
auth_res=$(curl -i -X POST --silent -d "password=$CODE" "$BASE_URL/~/login")
cookie_header="$(echo "$auth_res" | grep -i 'set-cookie:' | awk '{print $2}' | sed 's/.$//')"
channel_id="$(date +'%Y%m%d')$RANDOM"
message_id=0
sub_id=0
stop_flag=false
start_connection() {
send_size
((message_id++))
sub_id=$message_id
curl -X PUT \
--cookie "$cookie_header" \
-d "[{
\"id\":$message_id,
\"action\":\"subscribe\",
\"ship\":\"$SHIP\",
\"app\":\"homunculus\",
\"path\":\"/homunculus-http\"
}]" \
"$BASE_URL/~/channel/$channel_id" \
> /dev/null 2>&1
}
end_connection() {
((message_id++))
curl -X PUT \
--cookie "$cookie_header" \
-d "[{
\"id\":$message_id,
\"action\":\"unsubscribe\"
\"subscription\":$sub_id
}]" \
"$BASE_URL/~/channel/$channel_id" \
> /dev/null 2>&1
((message_id++))
curl -X PUT \
--cookie "$cookie_header" \
-d "[{
\"id\":$message_id,
\"action\":\"delete\"
}]" \
"$BASE_URL/~/channel/$channel_id" \
> /dev/null 2>&1
stop_flag=true
}
poke() {
((message_id++))
curl -X PUT \
--cookie "$cookie_header" \
-d "[{
\"id\":$message_id,
\"action\":\"poke\",
\"ship\":\"$SHIP\",
\"app\":\"homunculus\",
\"mark\":\"json\",
\"json\":$1
}]" \
"$BASE_URL/~/channel/$channel_id" \
> /dev/null 2>&1
}
ack() {
((message_id++))
curl -X PUT \
--cookie "$cookie_header" \
-d "[{
\"id\":$message_id,
\"action\":\"ack\",
\"event-id\":$1
}]" \
"$BASE_URL/~/channel/$channel_id" \
> /dev/null 2>&1
}
stream() {
local event_id=0
curl -N --silent \
-H "Accept:text/event-stream" \
--cookie "$cookie_header" \
"$BASE_URL/~/channel/$channel_id" |
while read -r -s line; do
if [[ "${line:0:4}" = "id: " ]]; then
event_id="${line:4}"
elif [[ "${line:0:6}" = "data: " ]] && grep -q "json" <<< "$line"; then
dat=$(echo "${line:6}" | jq -r '.json')
echo -e -n "$dat"
ack $event_id
fi
done
}
read_input() {
while read -r -s -N 1 char; do
if $stop_flag; then
break
elif [[ "$char" = $'\033' ]] || [[ "$char" =~ [[:cntrl:]] ]]; then
read -r -s -t 0.001 chars
printf -v seq "%q" "$char$chars"
if [[ "${seq:0:1}" = '$' ]]; then
eval sequence=${seq:1}
else
eval sequence=$seq
fi
poke "\"\\$sequence\""
elif [[ "$char" = '\' ]] || [[ "$char" = '"' ]]; then
poke "\"\\$char\""
else
poke "\"$char\""
fi
done
}
send_size() {
cols=$(tput cols)
rows=$(tput lines)
poke "[$cols, $rows]"
}
PS1=
stty -echo
echo -e "\e[?1000;1006;1015h"
start_connection
stream &
trap "end_connection" INT
read_input
clear
stty echo
echo -e "\e[?1000;1006;1015l"