Timmy Welch 9363450cdc v0.0.1
Add pre-commit config
Update default consensus with more sources
Move IP version spec to Source so that dual-stack sites can be used
Add tests for HTTPSource and consensus
2022-07-07 18:40:35 -07:00

64 lines
1.3 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"git.narnian.us/lordwelch/externalip"
)
// CLI Flags
var (
timeout = flag.Duration("t", time.Second*5, "consensus's voting timeout")
verbose = flag.Bool("v", false, "log errors to STDERR, when defined")
)
func main() {
// configure the consensus
cfg := externalip.DefaultConsensusConfig()
if timeout != nil {
cfg.WithTimeout(*timeout)
}
// optionally create the logger,
// if no logger is defined, all logs will be discarded.
var logger *log.Logger
if verbose != nil && *verbose {
logger = externalip.NewLogger(os.Stderr)
}
// create the consensus
consensus := externalip.DefaultConsensus(cfg, logger)
// retrieve the external ip
ip4, _ := consensus.ExternalIP(4)
ip6, err := consensus.ExternalIP(6)
// simple error handling
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// success, simply output the IP in string format
fmt.Println(ip4.String())
fmt.Println(ip6.String())
}
func init() {
// Define customized usage output
flag.Usage = func() {
fmt.Fprint(os.Stderr, "Retrieve your external IP.\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n %s [flags]\n\n", os.Args[0])
fmt.Fprintln(os.Stderr, "Flags:")
fmt.Fprintf(os.Stderr, " -h help\n \tshow this usage message\n")
flag.PrintDefaults()
}
// Parse CLI Flags
flag.Parse()
}