1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-05-22 16:16:04 +02:00
go-gemini/client.go

103 lines
3.0 KiB
Go
Raw Normal View History

2020-10-24 21:15:32 +02:00
package gemini
2020-09-22 04:09:50 +02:00
import (
2020-09-24 06:30:21 +02:00
"bufio"
2020-09-22 04:09:50 +02:00
"crypto/tls"
2020-09-26 01:53:50 +02:00
"crypto/x509"
2020-09-22 04:09:50 +02:00
)
2020-09-26 01:53:50 +02:00
// Client represents a Gemini client.
2020-09-26 05:06:54 +02:00
type Client struct {
// KnownHosts is a list of known hosts that the client trusts.
KnownHosts KnownHosts
2020-09-26 05:06:54 +02:00
2020-09-28 08:16:49 +02:00
// CertificateStore maps hostnames to certificates.
// It is used to determine which certificate to use when the server requests
// a certificate.
2020-09-28 05:49:41 +02:00
CertificateStore CertificateStore
2020-09-26 21:14:34 +02:00
2020-09-28 06:29:11 +02:00
// GetCertificate, if not nil, will be called when a server requests a certificate.
// The returned certificate will be used when sending the request again.
// If the certificate is nil, the request will not be sent again and
// the response will be returned.
2020-10-12 22:34:52 +02:00
GetCertificate func(hostname string, store *CertificateStore) *tls.Certificate
2020-09-26 21:14:34 +02:00
2020-09-26 05:06:54 +02:00
// TrustCertificate, if not nil, will be called to determine whether the
// client should trust the given certificate.
// If error is not nil, the connection will be aborted.
TrustCertificate func(hostname string, cert *x509.Certificate, knownHosts *KnownHosts) error
2020-09-26 01:53:50 +02:00
}
// Send sends a Gemini request and returns a Gemini response.
2020-09-26 05:06:54 +02:00
func (c *Client) Send(req *Request) (*Response, error) {
2020-09-26 01:53:50 +02:00
// Connect to the host
config := &tls.Config{
InsecureSkipVerify: true,
2020-09-26 06:31:16 +02:00
MinVersion: tls.VersionTLS12,
2020-09-26 21:14:34 +02:00
GetClientCertificate: func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
2020-09-28 06:29:11 +02:00
// Request certificates take precedence over client certificates
2020-09-28 05:49:41 +02:00
if req.Certificate != nil {
return req.Certificate, nil
2020-09-28 01:45:48 +02:00
}
// If we have already stored the certificate, return it
2020-10-13 19:31:50 +02:00
if cert, err := c.CertificateStore.Lookup(hostname(req.Host)); err == nil {
2020-10-12 05:48:18 +02:00
return cert, nil
}
2020-09-28 05:49:41 +02:00
return &tls.Certificate{}, nil
2020-09-26 21:14:34 +02:00
},
VerifyConnection: func(cs tls.ConnectionState) error {
cert := cs.PeerCertificates[0]
// Verify the hostname
2020-10-13 19:31:50 +02:00
if err := verifyHostname(cert, hostname(req.Host)); err != nil {
2020-09-27 19:50:48 +02:00
return err
2020-09-26 05:06:54 +02:00
}
// Check that the client trusts the certificate
if c.TrustCertificate == nil {
2020-10-13 19:31:50 +02:00
if err := c.KnownHosts.Lookup(hostname(req.Host), cert); err != nil {
2020-09-26 19:29:29 +02:00
return err
2020-09-26 05:06:54 +02:00
}
2020-10-13 19:31:50 +02:00
} else if err := c.TrustCertificate(hostname(req.Host), cert, &c.KnownHosts); err != nil {
return err
2020-09-26 05:06:54 +02:00
}
return nil
2020-09-26 01:53:50 +02:00
},
}
conn, err := tls.Dial("tcp", req.Host, config)
if err != nil {
return nil, err
}
defer conn.Close()
// Write the request
w := bufio.NewWriter(conn)
req.write(w)
if err := w.Flush(); err != nil {
return nil, err
}
// Read the response
resp := &Response{}
r := bufio.NewReader(conn)
if err := resp.read(r); err != nil {
return nil, err
}
2020-09-28 01:56:33 +02:00
// Store connection information
resp.TLS = conn.ConnectionState()
// Resend the request with a certificate if the server responded
// with CertificateRequired
if resp.Status == StatusCertificateRequired {
// Check to see if a certificate was already provided to prevent an infinite loop
if req.Certificate != nil {
return resp, nil
}
if c.GetCertificate != nil {
2020-10-13 19:31:50 +02:00
if cert := c.GetCertificate(hostname(req.Host), &c.CertificateStore); cert != nil {
req.Certificate = cert
return c.Send(req)
}
}
}
2020-09-26 01:53:50 +02:00
return resp, nil
2020-09-24 06:30:21 +02:00
}