Hacker News new | ask | show | jobs
by keepamovin 462 days ago
Lol it is not hard to create a hidden service:

Prerequisites:

- Linux: You may need `sudo` privileges to install Tor and modify system files.

- macOS: Homebrew must be installed (`brew`) to manage Tor.

- A web server must already be running on the specified local port (e.g., 8080).

- Firewall: This function does not configure the firewall. Ensure that:

  - Tor’s default port (9050) is allowed.
  - Your web server’s port (e.g., 8080) is accessible locally.


  #!/usr/bin/env bash

  # Function to add a Tor hidden service for a local web server
  add_tor_hidden_service() {
    local local_port="${1:-8080}"  # Default to 8080 if no port is provided
    local torrc=""
    local tordir=""
    local sudo_cmd=""
    local os_type=""

    # Detect OS and set paths
    if [[ "$OSTYPE" == "linux-gnu"* ]]; then
      os_type="linux"
      torrc="/etc/tor/torrc"
      tordir="/var/lib/tor"
      sudo_cmd="sudo -n"
    elif [[ "$OSTYPE" == "darwin"* ]]; then
      os_type="macos"
      torrc="$(brew --prefix)/etc/tor/torrc"
      tordir="$(brew --prefix)/var/lib/tor"
    else
      echo "Error: Unsupported OS (Linux or macOS required)" >&2
      return 1
    fi

    # Install Tor if not already installed
    if ! command -v tor &>/dev/null; then
      echo "Installing Tor..." >&2
      if [[ "$os_type" == "linux" ]]; then
        if [ -f /etc/debian_version ]; then
          $sudo_cmd apt update && $sudo_cmd apt install -y tor
        elif [ -f /etc/redhat-release ]; then
          $sudo_cmd yum install -y tor || $sudo_cmd dnf install -y tor
        else
          echo "Error: Only Debian or RedHat-based Linux supported" >&2
          return 1
        fi
      elif [[ "$os_type" == "macos" ]]; then
        brew install tor
      fi
    fi

    # Ensure Tor is running
    if [[ "$os_type" == "linux" ]]; then
      $sudo_cmd systemctl start tor
    elif [[ "$os_type" == "macos" ]]; then
      brew services start tor
    fi

    # Configure hidden service
    local hidden_service_dir="$tordir/hidden_service_$local_port"
    local dir_line="HiddenServiceDir $hidden_service_dir"
    local port_line="HiddenServicePort 80 127.0.0.1:$local_port"

    if ! grep -qF -- "$dir_line" "$torrc"; then
      echo "Configuring Tor hidden service..." >&2
      echo "$dir_line" | $sudo_cmd tee -a "$torrc" >/dev/null
      echo "$port_line" | $sudo_cmd tee -a "$torrc" >/dev/null
    fi

    # Restart Tor to apply changes
    echo "Restarting Tor..." >&2
    if [[ "$os_type" == "linux" ]]; then
      $sudo_cmd systemctl restart tor
    elif [[ "$os_type" == "macos" ]]; then
      brew services restart tor
    fi

    # Wait for onion address to be generated
    local onion_address=""
    for attempt in {1..30}; do
      if [[ -f "$hidden_service_dir/hostname" ]]; then
        onion_address=$($sudo_cmd cat "$hidden_service_dir/hostname")
        break
      fi
      sleep 1
    done

    if [[ -z "$onion_address" ]]; then
      echo "Error: Failed to generate onion address" >&2
      return 1
    fi

    echo "Success! Your web server is now available on the Tor network." >&2
    echo "Onion address: $onion_address" >&2
    echo "$onion_address"
  }

  # Example usage
  # serve -p 8080
  # add_tor_hidden_service 8080
  add_tor_hidden_service "$@"
Tested on macOS, probably good in other OS listed above. So run a server on a port. Then run this function. Boom, you have a hidden service.

Mining for a nice vanity hostname might be more difficult tho! How would that be done?

1 comments

> Mining for a nice vanity hostname might be more difficult tho! How would that be done?

https://community.torproject.org/onion-services/advanced/van...

Nice, this is cool, too: Onionmine hahaha :)

https://onionservices.torproject.org/apps/web/onionmine/