-
-
Save zer4tul/f03755a13a4cfbe69389434d36172549 to your computer and use it in GitHub Desktop.
trippy.rs wrapper, with default configuration generate and geoip data downloading.
This file contains hidden or 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
| #!/bin/bash | |
| # --- Configuration --- | |
| TRIP_CMD="trippy" | |
| CONF_DIR="$HOME/.config/trippy" | |
| CONF_FILE="$CONF_DIR/trippy.toml" | |
| OLD_CONF_CHECK="$CONF_DIR/config.toml" | |
| GEOIP_FILE="$CONF_DIR/city.mmdb" | |
| GEOIP_URL="https://cdn.jsdelivr.net/npm/geolite2-city/GeoLite2-City.mmdb.gz" | |
| RELEASE_URL="https://github.com/fujiapple852/trippy/releases" | |
| INSTALL_PATH="/usr/local/bin/trippy" | |
| # --- Functions --- | |
| log_info() { | |
| echo -e "\033[0;32m[INFO]\033[0m $1" | |
| } | |
| log_warn() { | |
| echo -e "\033[0;33m[WARN]\033[0m $1" | |
| } | |
| install_trippy() { | |
| log_info "Trippy not found. Starting automatic installation..." | |
| # Detect OS and Architecture | |
| local OS=$(uname -s | tr '[:upper:]' '[:lower:]') | |
| local ARCH=$(uname -m) | |
| local SUFFIX="" | |
| # Map OS/Arch to specific asset patterns | |
| if [[ "$OS" == "linux" ]]; then | |
| if [[ "$ARCH" == "x86_64" ]]; then | |
| SUFFIX="x86_64-unknown-linux-musl" | |
| elif [[ "$ARCH" == "aarch64" ]]; then | |
| SUFFIX="aarch64-unknown-linux-gnu" | |
| fi | |
| elif [[ "$OS" == "darwin" ]]; then | |
| if [[ "$ARCH" == "x86_64" ]]; then | |
| SUFFIX="x86_64-apple-darwin" | |
| elif [[ "$ARCH" == "aarch64" ]]; then | |
| SUFFIX="aarch64-apple-darwin" | |
| fi | |
| fi | |
| if [[ -z "$SUFFIX" ]]; then | |
| echo "Error: Unsupported platform ($OS $ARCH). Please install manually: $RELEASE_URL" | |
| exit 1 | |
| fi | |
| # Fetch latest version tag from GitHub API | |
| log_info "Fetching latest version info..." | |
| local LATEST_TAG=$(curl -s https://api.github.com/repos/fujiapple852/trippy/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') | |
| if [[ -z "$LATEST_TAG" ]]; then | |
| log_warn "Failed to fetch latest version tag. Attempting manual install may be required." | |
| exit 1 | |
| fi | |
| # Construct download URL | |
| local VERSION=${LATEST_TAG#v} | |
| local ASSET_NAME="trippy-${VERSION}-${SUFFIX}.tar.gz" | |
| local DOWNLOAD_URL="https://github.com/fujiapple852/trippy/releases/download/${LATEST_TAG}/${ASSET_NAME}" | |
| log_info "Downloading ${ASSET_NAME}..." | |
| local TEMP_DIR=$(mktemp -d) | |
| if curl -fsSL "$DOWNLOAD_URL" -o "${TEMP_DIR}/${ASSET_NAME}"; then | |
| tar -xzf "${TEMP_DIR}/${ASSET_NAME}" -C "$TEMP_DIR" | |
| local EXTRACTED_BIN=$(find "$TEMP_DIR" -name "trip" -type f | head -n 1) | |
| if [[ -f "$EXTRACTED_BIN" ]]; then | |
| log_info "Installing binary to $INSTALL_PATH (requires sudo)..." | |
| sudo mv "$EXTRACTED_BIN" "$INSTALL_PATH" | |
| sudo chmod +x "$INSTALL_PATH" | |
| log_info "Installation successful." | |
| else | |
| log_warn "Could not find 'trip' binary in the downloaded archive." | |
| exit 1 | |
| fi | |
| else | |
| log_warn "Download failed." | |
| exit 1 | |
| fi | |
| rm -rf "$TEMP_DIR" | |
| } | |
| download_geoip() { | |
| log_info "Downloading and decompressing GeoIP database..." | |
| # Enable pipefail locally to catch curl errors in the pipeline | |
| set -o pipefail | |
| # -f: fail on server errors, -sS: show errors but stay silent otherwise, -L: follow redirects | |
| if curl -fsSL "$GEOIP_URL" | gunzip -c > "$GEOIP_FILE"; then | |
| log_info "GeoIP database updated successfully." | |
| set +o pipefail | |
| else | |
| log_warn "Failed to download or decompress GeoIP database." | |
| rm -f "$GEOIP_FILE" | |
| set +o pipefail | |
| return 1 | |
| fi | |
| } | |
| # 1. Check if 'trippy' exists in PATH, if not, install it | |
| if ! command -v "$TRIP_CMD" &> /dev/null; then | |
| install_trippy | |
| fi | |
| # Re-evaluate path after potential installation | |
| TRIP_PATH=$(which "$TRIP_CMD") | |
| # 2. Check for CAP_NET_RAW capability (Linux only) | |
| if [[ "$OSTYPE" == "linux-gnu"* ]]; then | |
| # Use a more inclusive grep pattern as getcap output format varies (=p vs +p) | |
| if ! getcap "$TRIP_PATH" | grep -q "cap_net_raw"; then | |
| log_warn "Running setcap for CAP_NET_RAW, should ask for password for sudo." | |
| sudo setcap cap_net_raw+p "$TRIP_PATH" | |
| # Immediate verification | |
| if ! getcap "$TRIP_PATH" | grep -q "cap_net_raw"; then | |
| log_warn "Failed to set capabilities. Please check if the filesystem supports xattrs." | |
| fi | |
| fi | |
| fi | |
| # 3. Configuration Management | |
| mkdir -p "$CONF_DIR" | |
| if [[ ! -f "$OLD_CONF_CHECK" && ! -f "$CONF_FILE" ]]; then | |
| log_info "Generating default config at $CONF_FILE" | |
| "$TRIP_CMD" --print-config-template > "$CONF_FILE" | |
| # Inject GeoIP path into config | |
| ESCAPED_GEOIP_PATH=$(echo "$GEOIP_FILE" | sed 's/\//\\\//g') | |
| sed -i "s/^# geoip-mmdb-file = .*/geoip-mmdb-file = \"$ESCAPED_GEOIP_PATH\"/" "$CONF_FILE" | |
| fi | |
| # 4 & 5. GeoIP Database Lifecycle | |
| if [[ ! -f "$GEOIP_FILE" ]]; then | |
| download_geoip | |
| else | |
| # Auto-update if older than 30 days | |
| if [[ -n $(find "$GEOIP_FILE" -mtime +30) ]]; then | |
| log_info "GeoIP database is older than 30 days. Refreshing..." | |
| download_geoip | |
| fi | |
| fi | |
| # Final execution | |
| exec "$TRIP_CMD" "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment