Skip to content

Instantly share code, notes, and snippets.

@teslamint
Last active August 14, 2016 13:29
Show Gist options
  • Save teslamint/a3ff00b306edddcfb187 to your computer and use it in GitHub Desktop.
Save teslamint/a3ff00b306edddcfb187 to your computer and use it in GitHub Desktop.
Connect olleh_starbucks Wi-Fi shell script for OSX
#!/bin/bash
# -----------------------------------------------------------------------------
# Starbucks Korea Wi-Fi Authentication Script
# Code by teslamint. Public Domain.
# -----------------------------------------------------------------------------
ESSID="olleh_starbucks" # modify this in case of they changed their SSID
COOKIE_FILE="cookies.txt"
AIRPORT="/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport"
NETWORKSETUP="/usr/sbin/networksetup"
CHECK_URL="http://www.naver.com"
CHECK_STRING="<title>NAVER</title>"
OLEH_FIRST="http://first.wifi.olleh.com/starbucks/starbucks_en.php"
OLEH_REQ="https://first.wifi.olleh.com/starbucks/issue.php"
UR_NAME="John%20Doe" # Your name, please. + or %20 for space.
UR_EMAIL="[email protected]" # Your E-mail. some domains are not allowed. (e.g. kt.com)
UR_PHONE="" # Your phone number. This is optional.
# olleh wifi sites are now checking web browser's user agent string.
USER_AGENT="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7"
IP=""
MAC=""
BRANCH=""
DEBUG=${DEBUG:-"0"}
debug_msg() { # <message>
[ "$DEBUG" != "1" ] && return;
echo "$@" >&2
}
bailout() { # <message>
echo "$0: $1" >&2
exit 1
}
send_http() { # <command> ...
local cookie_file="$COOKIE_FILE" options
debug_msg "$0: use cookie file: $cookie_file"
local options=""
if [ "$DEBUG" = "1" ]; then
options="-v"
fi
curl -A "$USER_AGENT" ${options} -b "$cookie_file" -c "$cookie_file" "$@" 2>/dev/null
debug_msg "$0: curl result: $?"
}
get_cookie_value() { # <name>
local cookie_file="$COOKIE_FILE" name="$1"
[ -z "$name" ] && bailout "please give cookie variable name."
debug_msg "$0: cookie var name: $name"
grep "$name" $cookie_file|awk '{print $7;}'
}
# search olleh_starbucks SSID
search_ssid() { # <ssid>
local ssid="$1" ssid_list check
[ -z "$ssid" ] && bailout "no SSID given. exiting."
ssid_list="$($AIRPORT -s "$ssid"|grep "$ssid")"
debug_msg "$0: ssid list: $ssid_list"
[ -z "$ssid_list" ] && bailout "no $ssid SSID found. exiting."
}
# connect SSID. requires sudo depends on setting
connect_ssid() { # <ssid>
local ssid="$1"
[ -z "$ssid" ] && bailout "$0: no SSID given. exiting."
$NETWORKSETUP -setairportnetwork en0 "$ssid"
}
# test internet connectivity
test_internet() { # <url> <check_string>
local url="$1" check_string="$2" result check
[ -z "$url" ] && bailout "no URL given. exiting."
[ -z "$check_string" ] && bailout "no check string given. exiting."
result="$(send_http -L "$url")"
debug_msg "$0: test url result: $result"
check="$(echo "$result"|grep "$check_string")"
debug_msg "$0: check internet result: $check"
if [ "$check" != "" ]; then
# already connected internet
bailout "$0: already connected. exiting."
fi
}
# check relogin (if relogin is success, they tell me go to CHECK_URL)
check_login() { # <url>
local url="$1" result location check
[ -z "$url" ] && bailout "no URL given. exiting."
result="$(send_http -L "$url"|grep 'location.href')"
debug_msg "$0: result: $result"
[ -z "$result" ] && bailout "$0: cannot get response from url: $url"
location="$(echo "$result"|sed -Ee 's/(.+)?location\.href = \"([^\"]+)\"(.+)/\2/g')"
[ -z "$location" ] && bailout "$0: cannot found redirect URL. exiting."
debug_msg "$0: location: $location"
check="$(send_http -L "$location"|grep "document.location.href=\"$url\"")"
debug_msg "$0: check: $check"
if [ ! -z "$check" ]; then
bailout "$0: no need to authentication. exiting."
fi
}
# Get IP and MAC via cookie file
get_ip_and_mac() { # <request_url>
local request_url="$1" result
[ -z "$request_url" ] && bailout "$0: no request URL given. exiting."
result="$(send_http -X POST "$request_url")"
debug_msg "$0: result: $result"
check="$(echo "$result"|grep "document.location.href=\"http://www.olleh.com\"")"
debug_msg "$0: check: $check"
if [ ! -z "$check" ]; then
bailout "$0: no need to authentication. exiting."
fi
IP="$(get_cookie_value ip|head -n1)" # exclude apip
debug_msg "$0: IP: $IP"
MAC="$(get_cookie_value mac|head -n1)" # exclude mac_addr
debug_msg "$0: MAC: $MAC"
BRANCH="$(get_cookie_value branchflag)"
debug_msg "$0: BRANCH: $BRANCH"
if [ -z "$IP" ] || [ -z "$MAC" ] || [ -z "$BRANCH" ]; then
bailout "$0: failed to get IP or MAC or branch flag. check your cookie file: $COOKIE_FILE"
fi
}
# send login request
send_login_request() { # <request_url> <referer>
local request_url="$1" referer="$2" payload result
[ -z "$request_url" ] && bailout "$0: no request URL given. exiting."
[ -z "$referer" ] && referer="$OLEH_FIRST"
payload="firstflag=starbucks&branchflag=$BRANCH&lang=en&devicecode=pc&userNm=$UR_NAME&cust_email_addr=$UR_EMAIL&cust_hp_cp=$UR_PHONE&ip=$IP&mac=$MAC"
result=$(send_http -d "$payload" -e "$referer" "$request_url")
# should be: {"result_cd":"0000"}
result="$(echo "$result"|sed -Ee 's/{\"result_cd\":\"([0-9]+)\"}/\1/')"
debug_msg "auth result: $result"
case "$result" in
"1001")
bailout "$0: Your email is not accepted. Try another email domain."
;;
"2001")
bailout "$0: Failed to get response. Maybe server is busy now. Please try again later."
;;
"3001")
bailout "$0: Invalid request. Perhaps they changed the authentication logic."
;;
"0000")
# ok
;;
esac
}
# clear cookie jar
if [ -f "$COOKIE_FILE" ]; then
echo '' > "$COOKIE_FILE"
fi
search_ssid $ESSID
connect_ssid $ESSID
test_internet $CHECK_URL $CHECK_STRING
check_login $CHECK_URL
get_ip_and_mac $OLEH_FIRST
send_login_request $OLEH_REQ $OLEH_FIRST
echo "$0: auth success. enjoy free wifi."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment