Skip to content

dpc-sdp/uri-rewriter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

uri-rewriter

A small Go command-line utility to rewrite the hostname component of a URI using DNS CNAME records.

Usage

uri-rewriter hostname-cname [--cname-level N] <uri>
  • --cname-level, -l: The step in the CNAME chain to use for replacement.
    • 1 = the first CNAME target (closest to the original hostname)
    • If the requested level exceeds the available CNAME chain length, the last available CNAME target is used.
    • If there is no CNAME for the hostname, the URI is output unchanged.

Examples

Assume DNS has the following CNAME chain:

www.example.com -> www.example.net -> origin.example.org
  • Level 1 (first CNAME):
uri-rewriter hostname-cname -l 1 "https://www.example.com/path?x=1"
# https://www.example.net/path?x=1
  • Level 2 (second CNAME):
uri-rewriter hostname-cname -l 2 "https://www.example.com/path?x=1"
# https://origin.example.org/path?x=1
  • With port preservation:
uri-rewriter hostname-cname -l 1 "https://www.example.com:8443/path"
# https://www.example.net:8443/path

Notes

  • Only the rewritten URI is printed to stdout. Errors (e.g., invalid URI, DNS failures) are written to stderr with a non-zero exit code.
  • The tool follows the system resolver configuration (/etc/resolv.conf) to query CNAME records. If it is unavailable, it falls back to public resolvers (Cloudflare 1.1.1.1 and Google 8.8.8.8).
  • Trailing dots in DNS names (e.g., example.com.) are removed in the output for URI compatibility.

Install

git clone https://github.com/dpc-sdp/uri-rewriter.git
cd uri-rewriter
go build .
# The binary will be at ./uri-rewriter

Or install directly with Go:

go install github.com/dpc-sdp/uri-rewriter@latest

License

MIT

About

Rewrites a uri string based on various flags.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages