Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format jq code for legibility #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions vpn-refresh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@ sleep 5
# MYPUBLICIP=$(curl -s http://whatismyip.akamai.com/)
# echo "My public IP is ${MYPUBLICIP}"

# fetch ProtonVPN server info
LOGICALS=$(curl -sk -H "Cache-Control: no-cache" -H "Accept: application/json" https://api.protonmail.ch/vpn/logicals)
# ProtonVPN server info query
LOGICALS='curl -sk -H "Cache-Control: no-cache" -H "Accept: application/json" https://api.protonmail.ch/vpn/logicals'

# query for optimal server
SERVER=$(echo "$LOGICALS" | jq '.LogicalServers | map(select(.Status == 1 and .Tier == 2 and .City != null and (.City | (contains("Atlanta") or contains("Dallas") or contains("Chicago"))))) | [sort_by(.Score, .Load)[]][0] | .Servers[0]')
IPSTRING=$(echo "$SERVER" | jq '.EntryIP')
# query to get an optimal server
OPTIMAL_SERVER_QUERY=<<'QUERY'
.LogicalServers |
map(
select(
[.Status,.Tier] == [1, 2] and
.City != null and
(.City | test("(Atlanta|Dallas|Chicago)") # Now they have *two* problems.
)
) |
sort_by(.Score, .Load)[0].Servers[0] |
"\(.EntryIP) \(.Label)"
QUERY

$LOGICALS | jq --raw-output "$OPTIMAL_SERVER_QUERY" | read -r $IPSTRING $LABELSTRING

if [ -n "$IPSTRING" ]
then
Expand All @@ -39,7 +51,6 @@ then

# replace "THISISMYPROTONVPNUSERNAME" with your ProtonVPN username
USERNAMEPRE="THISISMYPROTONVPNUSERNAME+b:"
LABELSTRING=$(echo "$SERVER" | jq '.Label')
LABELSTRING="${LABELSTRING%\"}"
LABEL="${LABELSTRING#\"}"
USERNAME="$USERNAMEPRE$LABEL"
Expand Down