localias

repository·main·Indexed 23 days ago

https://github.com/peterldowns/localias

A developer tool for securely managing local aliases for development servers. It allows the use of TLS-secured domain names (e.g., https://server.test) that redirect to local ports by automatically managing /etc/hosts and TLS certificates via a Caddy-based proxy server. It supports foreground and daemon modes, mDNS for .local domains, and configuration via YAML files.

Tokens
4.9K
Snippets
17
Records
38
Agent score
78%

What's inside localias

  1. How localias works: Configuration and Proxy

    main

    Localias consists of two main components:

    1. The Configuration File: A YAML file that maps aliases to local ports. This file is the source of truth for which URLs should be redirected and where.
    2. The Proxy Server: A Caddy-based server that reads the configuration, updates /etc/hosts, provisions TLS certificates, and proxies incoming requests to the correct local ports.

    Configuration File Discovery

    Localias searches for a configuration file in the following order, using the first one it finds:

    • The path provided via the --configfile <path> flag.
    • The path provided via the LOCALIAS_CONFIGFILE=<path> environment variable.
    • A .localias.yaml file in the current working directory.
    • A .localias.yaml file at the root of the current Git repository.
    • The default system location: $XDG_CONFIG_HOME/localias.yaml (on MacOS: ~/Library/Application Support/localias.yaml; on Linux/WSL: ~/.config/localias.yaml).
  2. Configure Firefox to trust the system certificate store

    main

    Firefox does not trust the system certificate store by default on MacOS/Linux, which can cause certificate warnings for secure Localias aliases. You can enable system trust by following these steps:

    1. Open Firefox and visit about:config.
    2. Search for security.enterprise_roots.enabled.
    3. Set its value to true.
    4. Restart Firefox.

    If you are using Firefox on Windows to browse to a server running in WSL, you must manually import the Localias root certificate via Settings > Privacy & Security > Security > Certificates > View Certificates > Authorities > Import... using the path provided by localias debug cert.

    security.enterprise_roots.enabled = true
  3. Install the Localias root certificate on Windows (WSL users)

    main

    When running Localias inside WSL, the certificates are installed in the Linux VM's trust store but not the Windows host. To avoid certificate warnings in Windows browsers, you must manually install the Localias root certificate into the Windows certificate store using the debug cert --install command. This command requires administrator privileges.

    localias debug cert --install
  4. Use .local domains for network-wide testing

    main

    Localias supports mDNS (multicast DNS) for domains ending in .local. This allows you to broadcast your local aliases to your entire network, making it easy to test responsive websites from other devices like mobile phones.

    To create a network-accessible alias, use the localias add command with a .local TLD. Note that when visiting a secure .local alias from another device, you may need to accept a certificate warning.

    $ localias add frontend.local 8080
    [added] frontend.local -> 8080
    $ localias add http://insecure.local 8080
    [added] http://insecure.local 8080
  5. Grant Localias permission to bind to privileged ports on Linux

    main

    Localias acts as a proxy on ports 80 and 443. On Linux, you may encounter a permission denied error when trying to bind to these ports. To fix this, grant the localias binary the CAP_NET_BIND_SERVICE capability using setcap.

    sudo setcap CAP_NET_BIND_SERVICE=+eip $(which localias)
  6. Install localias

    main

    You can install localias using Homebrew, Go, or Nix. Alternatively, you can download the binaries manually from GitHub releases.

    Homebrew

    brew install peterldowns/tap/localias

    Golang

    To run without installing:

    go run github.com/peterldowns/localias/cmd/localias@latest --help

    To install:

    go install github.com/peterldowns/localias/cmd/localias@latest

    Nix (flakes)

    To run without installing:

    nix run github:peterldowns/localias -- --help

    To install:

    nix profile install --refresh github:peterldowns/localias

    Manual Download

    Download the appropriate binary for your architecture from the GitHub releases page.

    brew install peterldowns/tap/localias
  7. Using Localias in GitHub Actions

    main

    Localias can be run in GitHub Actions CI environments to test with URLs that mirror production setups.

    Important Note on SSL: When running on Ubuntu-based GitHub Actions runners, you may encounter issues with self-signed SSL certificate handling due to how NSS databases and certificate stores are managed.

    For a production-ready implementation that includes certificate warming, chromium testing with backoff mechanisms, and handling of NSS database edge cases, it is recommended to use the community-maintained GitHub Action: iloveitaly/github-action-localias.

  8. Run the localias proxy server

    main

    To start proxying requests, you must run the proxy server. Note that running the server requires sudo privileges to edit /etc/hosts, install TLS root certificates, and bind to ports 80/443. Localias will prompt for your password via a subshell when needed.

    Foreground Mode

    Use run to start the server in the foreground. This is useful for seeing logs and stopping the server with Ctrl+C.

    localias run

    Daemon Mode (Background)

    Use start to run the proxy server as a background daemon process.

    localias start

    Daemon Management

    If running in daemon mode, use these commands to manage the process:

    • localias status: Show the status of the daemon.
    • localias reload: Apply the latest configuration changes to the running daemon.
    • localias stop: Stop the daemon process.
    localias run
  9. Use the localias CLI

    main

    The localias CLI is used to securely manage local aliases for development servers. It allows you to map secure hostnames (e.g., https://secure.test) to local development ports (e.g., http://127.0.0.1:9000).

    Common tasks include managing individual aliases, controlling the proxy server daemon, and importing configurations.

    # Add an alias forwarding https://secure.test to http://127.0.0.1:9000
    localias set secure.test 9000
    
    # List all aliases
    localias list
    
    # Start the proxy server as a daemon process
    localias start
    
    # Run the proxy server in the foreground
    localias run
  10. Troubleshoot port 80/443 binding conflicts

    main

    If you receive the error localias could not start successfully or if localias status shows the daemon is not running, another process is likely already using ports 80 or 443.

    Common causes include:

    • Another instance of localias running in a different terminal.
    • A proxy server like Caddy, Nginx, or Apache running.

    Use ps aux | grep -i localias to check for other Localias instances and lsof -Pn | grep -E '\*:443|\*:80' to identify which services are listening on those ports. You must stop the conflicting process before Localias can start.

    $ ps aux | grep -i localias
    $ lsof -Pn | grep -E '\*:443|\*:80'
  11. Get the Localias root certificate path

    main

    To manually import the Localias root certificate (e.g., into Firefox on Windows/WSL), use the debug cert command to print the absolute path to the certificate file.

    $ localias debug cert
    /Users/pd/Library/Application Support/localias/caddy/pki/authorities/local/root.crt
  12. Manage aliases via the CLI

    main

    Use the following commands to interact directly with your configuration file:

    CommandDescription
    localias set <alias> <port>Add a new alias or update an existing one
    localias listList all currently configured aliases
    localias remove <alias>Remove a specific alias
    localias clearRemove all aliases from the configuration
    localias import <path>Import aliases from a specific YAML file

    Configuration Format

    The configuration file is a simple YAML map of <alias>: <port>. You can specify protocol preferences:

    # Serves over both https and http
    bareTLD: 9003
    
    # Serves over both https and http
    implicitly_secure.test: 9002
    
    # Explicitly secure (https and http)
    https://explicit_secure.test: 9000
    
    # Explicitly insecure (http only)
    http://explicit_insecure.test: 9001
    localias set frontend.test 3000