-
-
Notifications
You must be signed in to change notification settings - Fork 169
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
feat(ngrok): Adding ngrok feature #769
Open
bruno-blackbird
wants to merge
1
commit into
wardenenv:main
Choose a base branch
from
blackbird-agency:feature/warden-ngrok
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env bash | ||
[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1 | ||
|
||
WARDEN_ENV_PATH="$(locateEnvPath)" || exit $? | ||
loadEnvConfig "${WARDEN_ENV_PATH}" || exit $? | ||
assertDockerRunning | ||
|
||
if [[ ${WARDEN_NGROK:-0} -eq 0 ]]; then | ||
fatal "Ngrok environment is not used (WARDEN_NGROK=0)." | ||
fi | ||
|
||
source "${WARDEN_DIR}/utils/ngrok.sh" | ||
|
||
if (( ${#WARDEN_PARAMS[@]} == 0 )) || [[ "${WARDEN_PARAMS[0]}" == "help" ]]; then | ||
warden ngrok --help || exit $? && exit $? | ||
fi | ||
|
||
## load connection information for the mysql service | ||
|
||
## sub-command execution | ||
case "${WARDEN_PARAMS[0]}" in | ||
init) | ||
if [[ "${WARDEN_PARAMS[1]}" == "" ]]; then | ||
domain="$TRAEFIK_DOMAIN"; | ||
if [[ ${TRAEFIK_SUBDOMAIN} != "" ]]; then | ||
domain="${TRAEFIK_SUBDOMAIN}.${domain}" | ||
fi | ||
mkdir -p ${WARDEN_ENV_PATH}/.warden; #create directoy if does not exists | ||
generateNgrokConfigurationFile "${WARDEN_ENV_PATH}/.warden/ngrok.yml" "$domain" | ||
else | ||
mkdir -p ${WARDEN_ENV_PATH}/.warden; #create directoy if does not exists | ||
generateNgrokConfigurationFile "${WARDEN_ENV_PATH}/.warden/ngrok.yml" ${WARDEN_PARAMS[@]:1} | ||
fi | ||
if [ ! -f ${WARDEN_ENV_PATH}/.warden/ngrok.caddy ]; then | ||
touch ${WARDEN_ENV_PATH}/.warden/ngrok.caddy | ||
$WARDEN_DIR/bin/warden ngrok refresh-config | ||
fi | ||
|
||
warning "You have to run the command \"warden ngrok refresh-config\" after every ngrok container start. It will generate the config for the reverse proxy allowing correct url rewrites and mapping." | ||
|
||
;; | ||
refresh-config) | ||
if [[ ${CADDY_NGROK_TARGET_SERVICE} == "" ]]; then | ||
if [[ ${WARDEN_VARNISH} == "1" ]]; then | ||
CADDY_NGROK_TARGET_SERVICE=varnish; | ||
else | ||
CADDY_NGROK_TARGET_SERVICE=nginx; | ||
fi | ||
fi | ||
if [[ ${CADDY_NGROK_TARGET_PORT} == "" ]]; then | ||
CADDY_NGROK_TARGET_PORT=80; | ||
fi | ||
mkdir -p ${WARDEN_ENV_PATH}/.warden; #create directoy if does not exists | ||
generateCaddyConfigurationFile "${WARDEN_ENV_PATH}/.warden/ngrok.caddy" "$CADDY_NGROK_TARGET_SERVICE" "$CADDY_NGROK_TARGET_PORT" | ||
displayNgrokUrls | ||
;; | ||
ls) | ||
displayNgrokUrls | ||
;; | ||
*) | ||
fatal "The command \"${WARDEN_PARAMS[0]}\" does not exist. Please use --help for usage." | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env bash | ||
[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1 | ||
|
||
WARDEN_USAGE=$(cat <<EOF | ||
\033[33mUsage:\033[0m | ||
db <sub-command> | ||
|
||
\033[33mOptions:\033[0m | ||
-h, --help Display this help menu | ||
|
||
\033[33mCommands:\033[0m | ||
init <domain1> <domain2> <...> Initialize the ngrok configuration file with the default TRAEFIK_DOMAIN. | ||
refresh-config Regenerate the caddy configuration to match the urls in ngrok. | ||
ls Show the list of ngrok urls | ||
EOF | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
services: | ||
ngrok: | ||
hostname: "${WARDEN_ENV_NAME}-ngrok" | ||
image: ngrok/ngrok:${NGROK_VERSION:-latest} | ||
environment: | ||
NGROK_AUTHTOKEN: ${NGROK_AUTHTOKEN} | ||
volumes: | ||
- ${WARDEN_ENV_PATH:-.}/.warden/ngrok.yml:/etc/ngrok.yml:ro,cached | ||
command: | ||
- "start" | ||
- "--all" | ||
- "--config" | ||
- "/etc/ngrok.yml" | ||
|
||
|
||
caddy-ngrok: | ||
hostname: "${WARDEN_ENV_NAME}-caddy-ngrok" | ||
image: ${WARDEN_IMAGE_REPOSITORY}/caddy-ngrok:${CADDY_NGROK_VERSION:-latest} | ||
volumes: | ||
- ${WARDEN_ENV_PATH:-.}/.warden/ngrok.caddy:/etc/caddy/Caddyfile:ro,cached | ||
command: | ||
- "caddy" | ||
- "run" | ||
- "--watch" | ||
- "--config" | ||
- "/etc/caddy/Caddyfile" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,6 @@ REDIS_HOST=redis | |
REDIS_PORT=6379 | ||
|
||
MAIL_DRIVER=sendmail | ||
|
||
WARDEN_NGROK=0 | ||
NGROK_AUTHTOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ BLACKFIRE_CLIENT_TOKEN= | |
BLACKFIRE_SERVER_ID= | ||
BLACKFIRE_SERVER_TOKEN= | ||
|
||
WARDEN_NGROK=0 | ||
NGROK_AUTHTOKEN= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,5 @@ BLACKFIRE_CLIENT_TOKEN= | |
BLACKFIRE_SERVER_ID= | ||
BLACKFIRE_SERVER_TOKEN= | ||
|
||
WARDEN_NGROK=0 | ||
NGROK_AUTHTOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ RABBITMQ_VERSION=3.8 | |
REDIS_VERSION=5.0 | ||
VARNISH_VERSION=6.0 | ||
|
||
WARDEN_NGROK=0 | ||
NGROK_AUTHTOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ RABBITMQ_VERSION=3.8 | |
REDIS_VERSION=5.0 | ||
VARNISH_VERSION=6.0 | ||
|
||
WARDEN_NGROK=0 | ||
NGROK_AUTHTOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,6 @@ DB_PORT=3306 | |
DB_DATABASE=wordpress | ||
DB_USERNAME=wordpress | ||
DB_PASSWORD=wordpress | ||
|
||
WARDEN_NGROK=0 | ||
NGROK_AUTHTOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#!/usr/bin/env bash | ||
[[ ! ${WARDEN_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1 | ||
|
||
function replaceDots() { | ||
local input_string="$1" | ||
local output_string="${input_string//./\\.}" | ||
echo "$output_string" | ||
} | ||
|
||
function displayNgrokUrls() { | ||
# Sample text input | ||
local text=$($WARDEN_DIR/bin/warden env logs ngrok | grep name=) | ||
|
||
# Extract name and url using grep and awk | ||
local names=$(echo "$text" | grep -o 'name=[^ ]*' | awk -F'=' '{print $2}') | ||
local urls=$(echo "$text" | grep -o 'url=[^ ]*' | awk -F'=https://' '{print $2}') | ||
|
||
local names=($(echo "${names[@]}" | tr '\n' ' ')) | ||
local urls=($(echo "${urls[@]}" | tr '\n' ' ')) | ||
|
||
# Check if the number of names and urls match | ||
if [[ ${#names[@]} -ne ${#urls[@]} ]]; then | ||
echo "Error: The number of names and URLs doesn't match!" | ||
exit 1 | ||
fi | ||
|
||
# Return only unique urls, latest values in logs are replacing oldest values | ||
declare -A unique_urls | ||
for ((i = 0; i < ${#names[@]}; i++)); do | ||
unique_urls[${names[i]}]=${urls[i]} | ||
done | ||
|
||
for name in "${!unique_urls[@]}"; do | ||
echo "Use url: https://${name} for https://${unique_urls[$name]}" | ||
done | ||
} | ||
|
||
function generateCaddyConfigurationFile() { | ||
local target_file="$1" | ||
local target_service="$2" | ||
local target_port="$3" | ||
|
||
# Sample text input | ||
local text=$($WARDEN_DIR/bin/warden env logs ngrok | grep name=) | ||
|
||
# Extract name and url using grep and awk | ||
local names=$(echo "$text" | grep -o 'name=[^ ]*' | awk -F'=' '{print $2}') | ||
local urls=$(echo "$text" | grep -o 'url=[^ ]*' | awk -F'=https://' '{print $2}') | ||
|
||
local names=($(echo "${names[@]}" | tr '\n' ' ')) | ||
local urls=($(echo "${urls[@]}" | tr '\n' ' ')) | ||
|
||
# Check if the number of names and urls match | ||
if [[ ${#names[@]} -ne ${#urls[@]} ]]; then | ||
echo "Error: The number of names and URLs doesn't match!" | ||
exit 1 | ||
fi | ||
|
||
# Return only unique urls, latest values in logs are replacing oldest values | ||
declare -A unique_urls | ||
for ((i = 0; i < ${#names[@]}; i++)); do | ||
unique_urls[${names[i]}]=${urls[i]} | ||
done | ||
|
||
echo "{ | ||
order replace after encode | ||
} | ||
:2080 { | ||
@get { | ||
method GET | ||
path /* | ||
} | ||
handle @get { | ||
replace {" > $target_file; | ||
|
||
# Loop through the urls to get before and after replacement | ||
for name in "${!unique_urls[@]}"; do | ||
echo " ${name} ${unique_urls[$name]}" >> $target_file; | ||
done | ||
|
||
echo " } | ||
} | ||
|
||
reverse_proxy $target_service:$target_port { | ||
header_up Accept-Encoding identity | ||
header_up X-Forwarded-Proto https" >> $target_file; | ||
|
||
# Loop through the arrays and map the headers to be replaced | ||
for name in "${!unique_urls[@]}"; do | ||
echo " header_up * $(replaceDots ${unique_urls[$name]}) ${name} | ||
header_down * $(replaceDots ${name}) ${unique_urls[$name]}" >> $target_file; | ||
done | ||
|
||
echo " } | ||
}" >> $target_file; | ||
} | ||
|
||
function generateNgrokConfigurationFile() { | ||
local target_file="$1" | ||
local domains="${@:2}" | ||
echo 'version: "2" | ||
log: stdout | ||
tunnels:'> $target_file; | ||
for domain in $domains ; do | ||
echo ' '${domain}': | ||
proto: "http" | ||
addr: "caddy-ngrok:2080"' >> $target_file; | ||
done | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bruno-blackbird Reviewing the entrypoint script in the ngrok image, I think all you'd need to do here is just specify the
NGROK_CONFIG
environment variable to be/etc/ngrok.yml
. The entrypoint script will start listening on all tunnels in the config file by default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bap14 you mean instead of passing the command, yes, maybe it will be cleaner if I add it this way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bruno-blackbird Yes, that's correct.