From f66dea4fb5f039dcaa0f4dc88f0620b6ad045d23 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Fri, 9 Feb 2024 07:39:30 -0600 Subject: [PATCH] Added contrib/tlstunnel-check program to verify a domains validity for us with tlstunnel. Implements: https://todo.code.netlandish.com/~netlandish/links/46 --- contrib/tlstunnel-check/main.go | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 contrib/tlstunnel-check/main.go diff --git a/contrib/tlstunnel-check/main.go b/contrib/tlstunnel-check/main.go new file mode 100644 index 0000000..8baf7a9 --- /dev/null +++ b/contrib/tlstunnel-check/main.go @@ -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) +} -- 2.45.2