Skip to content

Instantly share code, notes, and snippets.

@dekokun
Last active October 5, 2017 11:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekokun/15162d10e7c4edc91102 to your computer and use it in GitHub Desktop.
Save dekokun/15162d10e7c4edc91102 to your computer and use it in GitHub Desktop.
curlのproxy protocol対応
#!/bin/bash
# [nabeken/mikoi: mikoi is a HAProxy's proxy protocol enabler for command line tools.](https://github.com/nabeken/mikoi) のインストールが必要なので注意
# usage: curlの代わりにpcurlを使うだけで動くはずです
last_option=${@:$#}
# 最後のオプションを:で分割して最後のものからpathを取り除いたものがportっぽい
portlike=$(echo $last_option | awk -F":" '{print $NF }' | awk -F"/" '{print $1 }')
# 最後のオプションを:で分割して最初のものがschemeっぽい
schemelike=$(echo $last_option | awk -F":" '{print $1 }')
# 最後のオプションを/で分割して最後のものがpathっぽい
pathlike="/"$(echo $last_option | awk -F"/" '{print $NF }')
# scheme部分とpath部分を削る
hostlike=$(echo $last_option | awk -F":" 'BEGIN {FS=":";OFS=":"} {$1="";print $0}' | sed -e 's#^://##' | sed -e 's#/.*$##')
# schemeっぽいものが実際にschemeか判定。httpとhttpsしか取得していないです
if [ "$schemelike" = "https" ]
then
port=443
scheme="https"
elif [ "$schemelike" = "http" ]
then
scheme="http"
else
echo please input scheme http:// or https://
exit
fi
## portっぽいものが数値かどうかの判定。数値だったらportとみなす
expr "$portlike" + 1 >/dev/null 2>&1
if [ $? -lt 2 ]
then
port=$portlike
# port部分を削る
host=$(echo $hostlike | awk -F":" 'BEGIN {FS=":";OFS=":"} {$NF="";print $0}' | sed -e 's/:$//')
else
# portがなければ、scheme判定で443を付与していなければ80で
if [ -z "$port" ]; then
port=80
fi
host=$hostlike
fi
# 後はmikoiよろしく
mikoi -P -H $host -p $port -- curl "${@:1:$#-1}" ${scheme}://127.0.0.1:{}${pathlike}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment