~netlandish/links

f66dea4fb5f039dcaa0f4dc88f0620b6ad045d23 — Peter Sanchez 9 months ago 4969ca7
Added contrib/tlstunnel-check program to verify a domains validity for
us with tlstunnel.

Implements: https://todo.code.netlandish.com/~netlandish/links/46
1 files changed, 60 insertions(+), 0 deletions(-)

A contrib/tlstunnel-check/main.go
A contrib/tlstunnel-check/main.go => contrib/tlstunnel-check/main.go +60 -0
@@ 0,0 1,60 @@
package main

import (
	"flag"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"time"
)

var (
	domainsURL = "http://localhost:8004/_check/domain"
	timeout    = 10
	debug      bool
)

func main() {
	domain := os.Getenv("TLSTUNNEL_NAME")
	if domain == "" {
		fmt.Fprintf(os.Stderr, "No value set for TLSTUNNEL_NAME\n")
		os.Exit(1)
	}

	flag.StringVar(&domainsURL, "url", domainsURL, "url to links-domains service")
	flag.IntVar(&timeout, "timeout", timeout, "http request timeout value")
	flag.BoolVar(&debug, "debug", false, "enable debug logging")
	flag.Parse()

	parts, err := url.Parse(domainsURL)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Invalid domain URL: %v\n", err)
		os.Exit(1)
	}
	qs := parts.Query()
	qs.Set("domain", domain)
	parts.RawQuery = qs.Encode()

	httpClient := &http.Client{
		Timeout: time.Duration(timeout) * time.Second,
	}
	req, err := http.NewRequest("GET", parts.String(), nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		os.Exit(1)
	}
	resp, err := httpClient.Do(req)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		os.Exit(1)
	}
	defer resp.Body.Close()

	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		// Invalid domain
		os.Exit(1)
	}
	// Valid domain, explicit
	os.Exit(0)
}