-
Notifications
You must be signed in to change notification settings - Fork 1
/
charlesrec
executable file
·66 lines (55 loc) · 1.68 KB
/
charlesrec
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
#!/bin/bash
CHARLES=/Applications/Charles.app/Contents/MacOS/Charles
kill_charles() {
CHARLES_PID=$1
echo "stopping Charles (pid: $CHARLES_PID)"
curl --silent -I --proxy http://localhost:8888 --fail http://control.charles/quit >/dev/zero
for i in {1..5};
do
sleep 2
kill -0 $CHARLES_PID >/dev/zero 2>&1
if [ $? == 0 ]; then
continue
else
return
fi
done
kill -9 $CHARLES_PID >/dev/zero 2>&1
}
exit_tasks() {
CHARLES_PID=$1
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
RANDOM=$(date +%s%N | cut -b10-19)
echo 'downloading har file...'
curl --proxy http://localhost:8888 --fail http://control.charles/session/export-har -o "charles_$TIMESTAMP_$RANDOM.har"
# recording can't be stopped before the file is downloaded (otherwise the file contains webinterface html)
echo 'stopping recording...'
curl --silent -I --proxy http://localhost:8888 --fail http://control.charles/recording/stop >/dev/zero
kill_charles $CHARLES_PID
echo "done, saved to file: charles_$TIMESTAMP_$RANDOM.har"
exit 0
}
trap "kill_charles $CHARLES_PID" SIGINT SIGTERM
$CHARLES -headless >/dev/zero 2>&1 &
CHARLES_PID=$!
# wait until webinterface is up
while true
do
curl --silent -I --proxy http://localhost:8888 --fail http://control.charles/ >/dev/zero
if [ $? == 0 ]; then
echo 'charles webinterface is running'
break
fi
done
curl --silent -I --proxy http://localhost:8888 --fail http://control.charles/recording/start >/dev/zero
if [ $? != 0 ]; then
echo 'could not start recording, quitting...'
kill_charles $CHARLES_PID
exit 1
fi
echo 'recording started (press CTRL+C to stop and save)'
trap "exit_tasks $CHARLES_PID" SIGINT SIGTERM
while true
do
sleep 2
done