1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package main
import (
"crypto/tls"
"fmt"
"io"
"net/url"
"os"
"tildegit.org/tjp/sliderule"
"tildegit.org/tjp/sliderule/gemini"
)
const usage = `Resource fetcher for the small web.
Usage:
sw-fetch (-h | --help)
sw-fetch [-v | --verbose] [-o PATH | --output PATH] [-k | --keyfile PATH] [ -c | --certfile PATH ] [ -s | --skip-verify ] URL
Options:
-h --help Show this screen.
-v --verbose Display more diagnostic information on standard error.
-o --output PATH Send the fetched resource to PATH instead of standard out.
-k --keyfile PATH Path to the TLS key file to use.
-c --certfile PATH Path to the TLS certificate file to use.
-s --skip-verify Don't verify server TLS certificates.
`
func main() {
conf := configure()
cl := sliderule.NewClient(conf.clientTLS)
response, err := cl.Fetch(conf.url.String())
if err != nil {
fail(err.Error() + "\n")
}
defer func() {
_ = response.Close()
_ = conf.output.Close()
}()
_, _ = io.Copy(conf.output, response.Body)
}
type config struct {
verbose bool
output io.WriteCloser
url *url.URL
clientTLS *tls.Config
}
func configure() config {
if len(os.Args) == 1 {
fail(usage)
}
conf := config{output: os.Stdout}
key := ""
cert := ""
verify := true
for i := 1; i <= len(os.Args)-1; i += 1 {
switch os.Args[i] {
case "-h", "--help":
os.Stdout.WriteString(usage)
os.Exit(0)
case "-v", "--verbose":
conf.verbose = true
case "-o", "--output":
if i+1 == len(os.Args)-1 {
fail(usage)
}
out := os.Args[i+1]
if out != "-" {
output, err := os.OpenFile(out, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err.Error())
failf("'%s' is not a valid path\n", out)
}
conf.output = output
}
i += 1
case "-k", "--keyfile":
if i+1 == len(os.Args)-1 {
fail(usage)
}
i += 1
key = os.Args[i]
case "-c", "--certfile":
if i+1 == len(os.Args)-1 {
fail(usage)
}
i += 1
cert = os.Args[i]
case "-s", "--skip-verify":
verify = false
}
}
conf.clientTLS = &tls.Config{}
if key != "" || cert != "" {
if key == "" || cert == "" {
fail("-k|--keyfile and -c|--certfile must both be present, or neither\n")
}
tlsConf, err := gemini.FileTLS(cert, key)
if err != nil {
failf("failed to load TLS key pair")
}
conf.clientTLS = tlsConf
}
conf.clientTLS.InsecureSkipVerify = !verify
u, err := url.Parse(os.Args[len(os.Args)-1])
if err != nil || u.Scheme == "" {
fail(usage)
}
conf.url = u
return conf
}
func fail(msg string) {
os.Stderr.WriteString(msg)
os.Exit(1)
}
func failf(msg string, args ...any) {
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}
|