-
Notifications
You must be signed in to change notification settings - Fork 6
/
run_lock.sh
executable file
·128 lines (102 loc) · 2.46 KB
/
run_lock.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
#! /bin/bash
# we can use or provide local and safe in memory filesystem, so when computer is powered off, all data are gone
TMPDIR=$XDG_RUNTIME_DIR
if [ -z $TMPDIR ]
then
TMPDIR=/tmp/DATA
fi
if [ ! -d $TMPDIR ]
then
TMPDIR=/tmp
fi
if [ -f "./constants.sh" ]
then
. ./constants.sh
fi
CMD=$1
if [ -z $CMD ]
then
CMD=$DEFAULTCMD
if [ -z $CMD ]
then
CMD=html
fi
fi
if [ -z "$TEMPLATE" ]
then
TEMPLATE="./template.html"
fi
if [ ! -f "$TEMPLATE" ]
then
echo "ERROR: template $TEMPLATE does not exists"
exit 1
fi
out=$3
if [ -z "$out" ]
then
out="/dev/stdout"
fi
id=$2
if [ -z "$id" ]
then
id=$(./random_card.sh)
fi
#first run the PHP script with the card UID as the input parameter and capture the response
json_data=$( php -f create.php $id)
#if there was a timeout waiting for lock, bailout
if [ -z "$json_data" ]
then
echo "ERROR: Timeout while waiting for lock"
exit 1
fi
# if there was an error in the PHP script generating everything, the respnse will start with ERROR
if [[ $json_data == ERROR* ]]
then
echo $json_data
exit 1
fi
# convert the json output from PHP to base64
json_base64=$(printf "%s" "$json_data" | base64)
# build out the sed s/find/replace string
str='s|SCRIPT_WILL_REPLACE_ME|'
str+=$json_base64
str+='|'
# build out the sed command
# inject the json_base64 into the HTML template then base64 encode all the HTML
cmd="sed '"
cmd+=$str
cmd+="' $TEMPLATE"
# run the sed find/replace + base64 encode
html_data=$(eval $cmd)
chrome_string="data:text/html;base64,$html_data"
if [ ! -z $PROXY ]
then
PROXY_ARG=--proxy-server="$PROXY"
fi
# pass the base64 encoded HTML into the chrome address bar for local rendering.
if [[ $CMD == "mac" ]]
then
open -a "Google Chrome.app" "data:text/html;base64,$(echo $html_data | base64)" --args --incognito
elif [[ $CMD == "linux" ]]
then
f=$(mktemp -p $TMPDIR --suffix .html)
echo "$html_data" > "$f"
#echo "$json_data" > "$f.json"
chromium "$f" --no-sandbox $PROXY_ARG
shred -u "$f"
elif [[ $CMD == "pdf" ]]
then
f=$(mktemp -p $TMPDIR --suffix .html)
echo "$html_data" > "$f"
chromium --headless --disable-gpu --no-margins --print-to-pdf-no-header --run-all-compositor-stages-before-draw --print-to-pdf="$f.pdf" "$f" --no-sandbox $PROXY_ARG
shred -u "$f"
cat "$f.pdf" > "$out"
shred -u "$f.pdf"
elif [[ $CMD == "html" ]]
then
echo "$html_data" > "$out"
else
echo "ERROR: Command is not valid."
exit 1
fi
exit 0