2020-06-18 16:54:48 -04:00
|
|
|
// Package client retrieves data over Gemini and implements a TOFU system.
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-11-04 16:37:00 -05:00
|
|
|
"io/ioutil"
|
2020-09-01 16:22:44 -04:00
|
|
|
"net"
|
2020-06-19 23:44:04 -04:00
|
|
|
"net/url"
|
2020-11-18 19:29:22 -05:00
|
|
|
"sync"
|
2020-11-23 21:09:48 -05:00
|
|
|
"time"
|
2020-06-19 23:44:04 -04:00
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
"github.com/makeworld-the-better-one/go-gemini"
|
2020-11-04 16:37:00 -05:00
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/spf13/viper"
|
2020-06-18 16:54:48 -04:00
|
|
|
)
|
|
|
|
|
2020-11-23 21:09:48 -05:00
|
|
|
var (
|
|
|
|
certCache = make(map[string][][]byte)
|
2020-11-23 21:11:51 -05:00
|
|
|
certCacheMu = &sync.RWMutex{}
|
|
|
|
|
2020-11-23 21:09:48 -05:00
|
|
|
fetchClient *gemini.Client
|
|
|
|
dlClient *gemini.Client // For downloading
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
fetchClient = &gemini.Client{
|
|
|
|
ConnectTimeout: 10 * time.Second, // Default is 15
|
|
|
|
ReadTimeout: time.Duration(viper.GetInt("a-general.page_max_time")) * time.Second,
|
|
|
|
}
|
|
|
|
dlClient = &gemini.Client{
|
|
|
|
ConnectTimeout: 10 * time.Second, // Default is 15
|
|
|
|
// No read timeout, download can take as long as it needs
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 16:37:00 -05:00
|
|
|
|
|
|
|
func clientCert(host string) ([]byte, []byte) {
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.RLock()
|
|
|
|
pair, ok := certCache[host]
|
|
|
|
certCacheMu.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return pair[0], pair[1]
|
2020-11-04 16:37:00 -05:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:45:34 -05:00
|
|
|
// Expand paths starting with ~/
|
2020-11-04 16:37:00 -05:00
|
|
|
certPath, err := homedir.Expand(viper.GetString("auth.certs." + host))
|
|
|
|
if err != nil {
|
|
|
|
certPath = viper.GetString("auth.certs." + host)
|
|
|
|
}
|
|
|
|
keyPath, err := homedir.Expand(viper.GetString("auth.keys." + host))
|
|
|
|
if err != nil {
|
|
|
|
keyPath = viper.GetString("auth.keys." + host)
|
|
|
|
}
|
|
|
|
if certPath == "" && keyPath == "" {
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.Lock()
|
2020-11-04 16:37:00 -05:00
|
|
|
certCache[host] = [][]byte{nil, nil}
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.Unlock()
|
2020-11-04 16:37:00 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := ioutil.ReadFile(certPath)
|
|
|
|
if err != nil {
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.Lock()
|
2020-11-04 16:37:00 -05:00
|
|
|
certCache[host] = [][]byte{nil, nil}
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.Unlock()
|
2020-11-04 16:37:00 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
key, err := ioutil.ReadFile(keyPath)
|
|
|
|
if err != nil {
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.Lock()
|
2020-11-04 16:37:00 -05:00
|
|
|
certCache[host] = [][]byte{nil, nil}
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.Unlock()
|
2020-11-04 16:37:00 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.Lock()
|
2020-11-04 16:37:00 -05:00
|
|
|
certCache[host] = [][]byte{cert, key}
|
2020-11-18 19:29:22 -05:00
|
|
|
certCacheMu.Unlock()
|
2020-11-04 16:37:00 -05:00
|
|
|
return cert, key
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasClientCert returns whether or not a client certificate exists for a host.
|
|
|
|
func HasClientCert(host string) bool {
|
|
|
|
cert, _ := clientCert(host)
|
|
|
|
return cert != nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 21:09:48 -05:00
|
|
|
func fetch(u string, c *gemini.Client) (*gemini.Response, error) {
|
2020-11-04 16:37:00 -05:00
|
|
|
parsed, _ := url.Parse(u)
|
|
|
|
cert, key := clientCert(parsed.Host)
|
2020-08-27 22:40:40 -04:00
|
|
|
|
2020-11-04 16:37:00 -05:00
|
|
|
var res *gemini.Response
|
|
|
|
var err error
|
|
|
|
if cert != nil {
|
2020-11-23 21:09:48 -05:00
|
|
|
res, err = c.FetchWithCert(u, cert, key)
|
2020-11-04 16:37:00 -05:00
|
|
|
} else {
|
2020-11-23 21:09:48 -05:00
|
|
|
res, err = c.Fetch(u)
|
2020-11-04 16:37:00 -05:00
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
if err != nil {
|
2020-06-19 14:05:05 -04:00
|
|
|
return nil, err
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
2020-06-19 23:44:04 -04:00
|
|
|
|
2020-06-24 13:31:01 -04:00
|
|
|
ok := handleTofu(parsed.Hostname(), parsed.Port(), res.Cert)
|
2020-06-18 16:54:48 -04:00
|
|
|
if !ok {
|
2020-06-24 13:31:01 -04:00
|
|
|
return res, ErrTofu
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
2020-09-01 16:22:44 -04:00
|
|
|
|
2020-06-24 13:31:01 -04:00
|
|
|
return res, err
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
2020-09-01 16:22:44 -04:00
|
|
|
|
2020-11-23 21:09:48 -05:00
|
|
|
// Fetch returns response data and an error.
|
|
|
|
// The error text is human friendly and should be displayed.
|
|
|
|
func Fetch(u string) (*gemini.Response, error) {
|
|
|
|
return fetch(u, fetchClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download is the same as Fetch but with no read timeout.
|
|
|
|
func Download(u string) (*gemini.Response, error) {
|
|
|
|
return fetch(u, dlClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchWithProxy(proxyHostname, proxyPort, u string, c *gemini.Client) (*gemini.Response, error) {
|
2020-11-04 16:37:00 -05:00
|
|
|
parsed, _ := url.Parse(u)
|
|
|
|
cert, key := clientCert(parsed.Host)
|
|
|
|
|
|
|
|
var res *gemini.Response
|
|
|
|
var err error
|
|
|
|
if cert != nil {
|
2020-11-23 21:09:48 -05:00
|
|
|
res, err = c.FetchWithHostAndCert(net.JoinHostPort(proxyHostname, proxyPort), u, cert, key)
|
2020-11-04 16:37:00 -05:00
|
|
|
} else {
|
2020-11-23 21:09:48 -05:00
|
|
|
res, err = c.FetchWithHost(net.JoinHostPort(proxyHostname, proxyPort), u)
|
2020-11-04 16:37:00 -05:00
|
|
|
}
|
2020-09-01 16:22:44 -04:00
|
|
|
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
|
|
|
|
}
|
2020-11-23 21:09:48 -05:00
|
|
|
|
|
|
|
// FetchWithProxy is the same as Fetch, but uses a proxy.
|
|
|
|
func FetchWithProxy(proxyHostname, proxyPort, u string) (*gemini.Response, error) {
|
|
|
|
return fetchWithProxy(proxyHostname, proxyPort, u, fetchClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadWithProxy is the same as FetchWithProxy but with no read timeout.
|
|
|
|
func DownloadWithProxy(proxyHostname, proxyPort, u string) (*gemini.Response, error) {
|
|
|
|
return fetchWithProxy(proxyHostname, proxyPort, u, dlClient)
|
|
|
|
}
|