package main import ( "bufio" "crypto/tls" "flag" "fmt" "gemini" "log" "os" ) func usage() { fmt.Printf("Usage: %s [options] url\n", os.Args[0]) flag.PrintDefaults() } func create_output(path string) (*os.File, error) { if path == "" { return os.Stdout, nil } return os.Create(path) } func read_requests(path string) ([]string, error) { if path == "" { return nil, nil } file, err := os.Open(path) if err != nil { return nil, err } defer file.Close() var lines []string scanner := bufio.NewScanner(file) for scanner.Scan() { lines = append(lines, scanner.Text()) } return lines, scanner.Err() } func main() { flags := struct { host string port int input string output string }{ host: "", port: gemini.GeminiPort, input: "", output: "", } flag.Usage = usage flag.StringVar(&flags.host, "host", flags.host, "server host name") flag.IntVar(&flags.port, "port", flags.port, "server port number") flag.StringVar(&flags.input, "i", flags.input, "read urls from file") flag.StringVar(&flags.output, "o", flags.output, "write output to path") flag.Parse() requests, err := read_requests(flags.input) if err != nil { log.Panic(err) } requests = append(requests, flag.Args()...) if len(requests) == 0 { flag.Usage() os.Exit(1) } output, err := create_output(flags.output) if err != nil { log.Fatal(err) } conf := tls.Config{ MinVersion: tls.VersionTLS12, InsecureSkipVerify: true, } log.Printf("Got %d requests\n", len(requests)) for _, request := range requests { log.Println(request) if conn, err := gemini.Parse(request, flags.host, flags.port); err != nil { log.Print(err) } else if err := conn.Get(&conf, output); err != nil { log.Print(err) } } }