This Go service is designed to dynamically handle HTTP requests based on CNAME DNS records. It performs DNS lookups to retrieve CNAME records and redirects the request based on specific suffixes defined via environment variables. The redirect status code (e.g., 301, 302, etc.) is also determined dynamically, making the service flexible for a wide variety of use cases.
- Redirects requests based on CNAME records.
- Supports multiple types of HTTP status codes (301, 302, 303, 307, 308).
- DNS configuration can be customized using
/etc/resolv.conf
or overridden via an environment variable. - Uses Google Public DNS as a fallback if system DNS configuration fails.
- Go 1.16+
- Network connectivity to perform DNS lookups.
The service uses environment variables to determine which domains to redirect and with which HTTP status codes. The following environment variables are supported:
REDIRECT_DOMAIN_301
: Domain suffix to be redirected with HTTP status code 301 (Moved Permanently).REDIRECT_DOMAIN_302
: Domain suffix to be redirected with HTTP status code 302 (Found).REDIRECT_DOMAIN_303
: Domain suffix to be redirected with HTTP status code 303 (See Other).REDIRECT_DOMAIN_307
: Domain suffix to be redirected with HTTP status code 307 (Temporary Redirect).REDIRECT_DOMAIN_308
: Domain suffix to be redirected with HTTP status code 308 (Permanent Redirect).
Note: At least one of the
REDIRECT_DOMAIN_
environment variables must be defined, or the service will exit with an error.
DNS_CONFIG_FILE
: Path to the DNS configuration file (default:/etc/resolv.conf
).CACHE_DNS
: Whether to cache DNS lookups (default:false
).HTTP_PORT
: Port on which the service listens for incoming HTTP requests (default:80
).HTTPS_PORT
: Port on which the service listens for incoming HTTPS requests (default:443
).
- The service listens on port
80
and handles incoming HTTP requests and port443
for HTTPS requests. - For each request, it performs a CNAME lookup for the hostname.
- Based on the CNAME result, it determines if the CNAME ends with one of the configured suffixes (e.g.,
example.com
). - If a match is found, the service removes the suffix and redirects the request to the target domain with the appropriate status code.
- For HTTPS requests an SSL certificate is requested from Let's Encrypt (on the first request of that domain).
- The scheme (
http
orhttps
) is preserved from the original request.
To run the service:
- Clone the repository and navigate to the project directory.
- Set the required environment variables:
export REDIRECT_DOMAIN_301="permanent-redirect.com" export REDIRECT_DOMAIN_307="temporary-redirect.com"
- Build and run the service:
go build -o dns-redirect ./dns-redirect
- Suppose you have a CNAME record like
foo.example.com
that points tobar.example.net.permanent-redirect.com
. - If
REDIRECT_DOMAIN_301="permanent-redirect.com"
is set, and the CNAME matches, the service will remove.permanent-redirect.com
and redirectfoo.example.com
tobar.example.net
using a301 Moved Permanently
status code.
- The service logs all redirects, including the source URL, the target URL, and the HTTP status code used.
- Errors related to DNS lookups or unexpected CNAME formats are also logged.
- If no CNAME is found or if the format of the CNAME is not as expected, the service returns an
HTTP 500 Internal Server Error
response. - If no
REDIRECT_DOMAIN_
environment variables are defined, the service exits with an error.
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! Feel free to submit a pull request or open an issue for feature requests and bug reports.
Arnold Daniels - Jasny