-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathewcurl
executable file
·86 lines (76 loc) · 2.73 KB
/
ewcurl
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
#!/bin/sh
#
# edgeworkers-curl (ewcurl)
#
curlcmd=curl
error=0
verbose=0
account_option=
hostname=
section=papi
usage() {
echo "Usage: "
echo " `basename $0` [ewcurl options] -- [curl options] [curl URL]" 1>&2
echo " optons:" 1>&2
echo " -a (optional) Account Switch Key for Akamai CLI command." 1>&2
echo " -h Hostname for EdgeWorkers target property." 1>&2
echo " -s (optional) Section name in ~/.edgerc for Akamai CLI credential." 1>&2
echo " [default value is 'papi']." 1>&2
echo "" 1>&2
}
opts=":a:h:s:v"
while getopts ${opts} opt
do
case ${opt} in
a)
[ "x${OPTARG}" = "x--" ] && echo "`basename $0`: Invalid argument for -${opt}: --" 1>&2 && error=3
account_option="--accountkey ${OPTARG}"
;;
h)
[ "x${OPTARG}" = "x--" ] && echo "`basename $0`: Invalid argument: --" 1>&2 && error=3
hostname="${OPTARG}"
;;
s)
[ "x${OPTARG}" = "x--" ] && echo "`basename $0`: Invalid argument: --" 1>&2 && error=3
section="${OPTARG}"
;;
v)
verbose=1
;;
\?) echo "`basename $0`: Invalid option: -${OPTARG}" 1>&2 && error=1 ;;
:) echo "`basename $0`: Invalid option: -${OPTARG} requires an argument" 1>&2 && error=2 ;;
esac
done
shift $((OPTIND-1))
[ "x${hostname}" = "x" ] && echo "`basename $0`: Insuccifient options - No hostname option." 1>&2 && error=4
[ ${error} -gt 0 ] && usage && exit ${error}
#
# Run scirpt
#
options="--section ${section} ${account_option}"
if [ "${verbose}" = "1" ]; then
echo "Akamai EdgeWorkers Hostname: ${hostname}" 1>&2
echo "Akamai EdgeWorkers CLI options: ${options}" 1>&2
echo "Curl options: $*" 1>&2
fi
tokenfile=$HOME/.akamai-ew-trace-token
akamai_ew_trace_header="Akamai-EW-Trace"
touch ${tokenfile}
now=`date +%s`
tokenexp=`cat ${tokenfile} | grep ${hostname} | sed "s;${hostname}=;;" | sed 's;.*~exp=\([0-9]*\)~.*;\1;'`
if [ "x${tokenexp}" = "x" ] || [ "${now}" -ge "${tokenexp}" ]; then
token=`akamai edgeworkers ${options} auth --expiry 60 ${hostname} | grep "^${akamai_ew_trace_header}"`
if [ ! "x${token}" = "x" ]; then
cat ${tokenfile} | grep -v "^${hostname}=" > ${tokenfile}.tmp
echo "${hostname}=${token}" >> ${tokenfile}.tmp
mv ${tokenfile}.tmp ${tokenfile}
fi
fi
token=`cat ${tokenfile} | grep ${hostname} | sed "s;${hostname}=;;"`
if [ "${verbose}" = "1" ]; then
echo EW Trace AuthToken: ${token} 1>&2
fi
${curlcmd} \
-H 'Pragma: akamai-x-cache-on, akamai-x-cache-remote-on, akamai-x-ew-debug, akamai-x-ew-debug-rp' \
-H "${token}" \
$*