diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..02a19f3 --- /dev/null +++ b/start.sh @@ -0,0 +1,85 @@ +#!/bin/sh +set -eu + +script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +state_dir="$script_dir/state" +pid_file="$state_dir/server.pid" +log_file="$state_dir/server.log" +token_file="$state_dir/token" +config_file="$state_dir/config.env" + +host="${GIT_CRED_PROXY_HOST:-127.0.0.1}" +port="${GIT_CRED_PROXY_PORT:-18765}" +protocols="${GIT_CRED_PROXY_PROTOCOLS:-https}" +allowed_hosts="${GIT_CRED_PROXY_ALLOWED_HOSTS:-}" +public_url="${GIT_CRED_PROXY_PUBLIC_URL:-http://host.docker.internal:${port}}" + +mkdir -p "$state_dir" + +if [ -f "$pid_file" ]; then + old_pid=$(cat "$pid_file" 2>/dev/null || true) + if [ -n "${old_pid:-}" ] && kill -0 "$old_pid" 2>/dev/null; then + printf 'Proxy already running: pid=%s\n' "$old_pid" + printf 'Container URL: %s\n' "$public_url" + printf 'Token file: %s\n' "$token_file" + exit 0 + fi + rm -f "$pid_file" +fi + +if [ ! -f "$token_file" ]; then + if ! command -v openssl >/dev/null 2>&1; then + printf 'openssl is required to create the token file\n' >&2 + exit 1 + fi + + umask 077 + openssl rand -hex 32 > "$token_file" +fi + +runtime='' +if [ -n "${GIT_CRED_PROXY_RUNTIME:-}" ]; then + runtime="$GIT_CRED_PROXY_RUNTIME" +elif command -v bun >/dev/null 2>&1; then + runtime='bun' +elif command -v node >/dev/null 2>&1; then + runtime='node' +else + printf 'Either bun or node is required to start the proxy\n' >&2 + exit 1 +fi + +token=$(tr -d '\r\n' < "$token_file") + +cat > "$config_file" <>"$log_file" 2>&1 & + +pid=$! +printf '%s\n' "$pid" > "$pid_file" + +sleep 1 + +if ! kill -0 "$pid" 2>/dev/null; then + printf 'Proxy failed to start. Check %s\n' "$log_file" >&2 + exit 1 +fi + +printf 'Proxy started\n' +printf 'Host listen URL: http://%s:%s\n' "$host" "$port" +printf 'Container URL: %s\n' "$public_url" +printf 'Token file: %s\n' "$token_file" +printf 'Log file: %s\n' "$log_file" +printf 'Next: run /workspaces/host-git-cred-proxy/configure-container.sh inside the container\n' diff --git a/status.sh b/status.sh new file mode 100755 index 0000000..ecae8e8 --- /dev/null +++ b/status.sh @@ -0,0 +1,48 @@ +#!/bin/sh +set -eu + +script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +state_dir="$script_dir/state" +pid_file="$state_dir/server.pid" +log_file="$state_dir/server.log" +config_file="$state_dir/config.env" + +host='127.0.0.1' +port='18765' +public_url="http://host.docker.internal:${port}" + +if [ -f "$config_file" ]; then + . "$config_file" + host="${GIT_CRED_PROXY_HOST:-$host}" + port="${GIT_CRED_PROXY_PORT:-$port}" + public_url="${GIT_CRED_PROXY_PUBLIC_URL:-$public_url}" +fi + +printf 'Host listen URL: http://%s:%s\n' "$host" "$port" +printf 'Container URL: %s\n' "$public_url" + +if [ ! -f "$pid_file" ]; then + printf 'Status: stopped\n' + exit 1 +fi + +pid=$(cat "$pid_file" 2>/dev/null || true) + +if [ -z "${pid:-}" ] || ! kill -0 "$pid" 2>/dev/null; then + printf 'Status: stale pid file\n' + exit 1 +fi + +printf 'Status: running (pid=%s)\n' "$pid" + +if command -v curl >/dev/null 2>&1; then + if curl -fsS "http://${host}:${port}/healthz" >/dev/null 2>&1; then + printf 'Health: ok\n' + else + printf 'Health: check failed\n' + fi +fi + +if [ -f "$log_file" ]; then + printf 'Log file: %s\n' "$log_file" +fi diff --git a/stop.sh b/stop.sh new file mode 100755 index 0000000..9d59590 --- /dev/null +++ b/stop.sh @@ -0,0 +1,27 @@ +#!/bin/sh +set -eu + +script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +pid_file="$script_dir/state/server.pid" + +if [ ! -f "$pid_file" ]; then + printf 'Proxy is not running\n' + exit 0 +fi + +pid=$(cat "$pid_file" 2>/dev/null || true) + +if [ -z "${pid:-}" ]; then + rm -f "$pid_file" + printf 'Stale pid file removed\n' + exit 0 +fi + +if kill -0 "$pid" 2>/dev/null; then + kill "$pid" + printf 'Stopped proxy pid=%s\n' "$pid" +else + printf 'Proxy process was not running, removing stale pid file\n' +fi + +rm -f "$pid_file"