2020-06-18 23:54:48 +03:00
|
|
|
// Package client retrieves data over Gemini and implements a TOFU system.
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-09-01 23:22:44 +03:00
|
|
|
"net"
|
2020-06-20 06:44:04 +03:00
|
|
|
"net/url"
|
|
|
|
|
2020-06-18 23:54:48 +03:00
|
|
|
"github.com/makeworld-the-better-one/go-gemini"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Fetch returns response data and an error.
|
|
|
|
// The error text is human friendly and should be displayed.
|
2020-06-20 06:44:04 +03:00
|
|
|
func Fetch(u string) (*gemini.Response, error) {
|
2020-08-28 05:40:40 +03:00
|
|
|
|
2020-09-01 23:22:44 +03:00
|
|
|
res, err := gemini.Fetch(u)
|
2020-06-18 23:54:48 +03:00
|
|
|
if err != nil {
|
2020-06-19 21:05:05 +03:00
|
|
|
return nil, err
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
2020-06-20 06:44:04 +03:00
|
|
|
|
|
|
|
parsed, _ := url.Parse(u)
|
2020-09-01 23:22:44 +03:00
|
|
|
|
2020-06-24 20:31:01 +03:00
|
|
|
ok := handleTofu(parsed.Hostname(), parsed.Port(), res.Cert)
|
2020-06-18 23:54:48 +03:00
|
|
|
if !ok {
|
2020-06-24 20:31:01 +03:00
|
|
|
return res, ErrTofu
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
2020-09-01 23:22:44 +03:00
|
|
|
|
2020-06-24 20:31:01 +03:00
|
|
|
return res, err
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
2020-09-01 23:22:44 +03:00
|
|
|
|
|
|
|
// FetchWithProxy is the same as Fetch, but uses a proxy.
|
|
|
|
func FetchWithProxy(proxyHostname, proxyPort, u string) (*gemini.Response, error) {
|
|
|
|
res, err := gemini.FetchWithHost(net.JoinHostPort(proxyHostname, proxyPort), u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only associate the returned cert with the proxy
|
|
|
|
ok := handleTofu(proxyHostname, proxyPort, res.Cert)
|
|
|
|
if !ok {
|
|
|
|
return res, ErrTofu
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|