2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-09-21 20:09:50 -06:00
|
|
|
|
|
|
|
import (
|
2020-09-23 22:30:21 -06:00
|
|
|
"bufio"
|
2020-12-17 15:16:55 -07:00
|
|
|
"context"
|
2020-09-21 20:09:50 -06:00
|
|
|
"crypto/tls"
|
2020-09-25 17:53:50 -06:00
|
|
|
"crypto/x509"
|
2020-10-27 17:21:33 -06:00
|
|
|
"net"
|
2020-10-28 11:40:25 -06:00
|
|
|
"strings"
|
2020-10-31 18:55:56 -06:00
|
|
|
"time"
|
2020-09-21 20:09:50 -06:00
|
|
|
)
|
|
|
|
|
2020-10-28 11:40:25 -06:00
|
|
|
// Client is a Gemini client.
|
2020-09-25 21:06:54 -06:00
|
|
|
type Client struct {
|
2020-12-17 17:50:26 -07:00
|
|
|
// TrustCertificate is called to determine whether the client
|
|
|
|
// should trust the certificate provided by the server.
|
|
|
|
// If TrustCertificate is nil, the client will accept any certificate.
|
|
|
|
// If the returned error is not nil, the certificate will not be trusted
|
|
|
|
// and the request will be aborted.
|
|
|
|
TrustCertificate func(hostname string, cert *x509.Certificate) error
|
2020-10-31 18:55:56 -06:00
|
|
|
|
2020-12-17 17:50:26 -07:00
|
|
|
// Timeout specifies a time limit for requests made by this
|
|
|
|
// Client. The timeout includes connection time and reading
|
|
|
|
// the response body. The timer remains running after
|
|
|
|
// Get and Do return and will interrupt reading of the Response.Body.
|
2020-10-31 21:05:31 -06:00
|
|
|
//
|
2020-12-17 17:50:26 -07:00
|
|
|
// A Timeout of zero means no timeout.
|
|
|
|
Timeout time.Duration
|
2020-09-25 17:53:50 -06:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:50:26 -07:00
|
|
|
// Get performs a Gemini request for the given URL.
|
2020-10-27 17:21:33 -06:00
|
|
|
func (c *Client) Get(url string) (*Response, error) {
|
|
|
|
req, err := NewRequest(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do performs a Gemini request and returns a Gemini response.
|
|
|
|
func (c *Client) Do(req *Request) (*Response, error) {
|
2020-11-27 15:45:15 -07:00
|
|
|
// Extract hostname
|
|
|
|
colonPos := strings.LastIndex(req.Host, ":")
|
|
|
|
if colonPos == -1 {
|
|
|
|
colonPos = len(req.Host)
|
|
|
|
}
|
|
|
|
hostname := req.Host[:colonPos]
|
|
|
|
|
2020-09-25 17:53:50 -06:00
|
|
|
// Connect to the host
|
|
|
|
config := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
2020-09-25 22:31:16 -06:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2020-10-28 11:40:25 -06:00
|
|
|
GetClientCertificate: func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
2020-12-17 14:46:16 -07:00
|
|
|
if req.Certificate != nil {
|
|
|
|
return req.Certificate, nil
|
|
|
|
}
|
|
|
|
return &tls.Certificate{}, nil
|
2020-09-26 13:14:34 -06:00
|
|
|
},
|
2020-10-13 14:44:46 -06:00
|
|
|
VerifyConnection: func(cs tls.ConnectionState) error {
|
2020-10-28 11:40:25 -06:00
|
|
|
return c.verifyConnection(req, cs)
|
2020-09-25 17:53:50 -06:00
|
|
|
},
|
2020-11-27 15:45:15 -07:00
|
|
|
ServerName: hostname,
|
2020-09-25 17:53:50 -06:00
|
|
|
}
|
2020-12-17 22:12:32 -07:00
|
|
|
// Set connection context
|
2020-12-17 15:16:55 -07:00
|
|
|
ctx := req.Context
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
netConn, err := (&net.Dialer{}).DialContext(ctx, "tcp", req.Host)
|
2020-09-25 17:53:50 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-25 22:42:25 -07:00
|
|
|
conn := tls.Client(netConn, config)
|
2020-10-31 18:55:56 -06:00
|
|
|
// Set connection deadline
|
2020-10-31 19:07:02 -06:00
|
|
|
if d := c.Timeout; d != 0 {
|
|
|
|
conn.SetDeadline(time.Now().Add(d))
|
2020-10-31 18:55:56 -06:00
|
|
|
}
|
2020-09-25 17:53:50 -06:00
|
|
|
|
|
|
|
// Write the request
|
|
|
|
w := bufio.NewWriter(conn)
|
|
|
|
req.write(w)
|
|
|
|
if err := w.Flush(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the response
|
|
|
|
resp := &Response{}
|
2020-10-27 17:16:55 -06:00
|
|
|
if err := resp.read(conn); err != nil {
|
2020-09-25 17:53:50 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-28 11:40:25 -06:00
|
|
|
// Store connection state
|
2020-09-27 17:56:33 -06:00
|
|
|
resp.TLS = conn.ConnectionState()
|
2020-09-27 21:58:45 -06:00
|
|
|
|
2020-09-25 17:53:50 -06:00
|
|
|
return resp, nil
|
2020-09-23 22:30:21 -06:00
|
|
|
}
|
2020-10-27 17:21:33 -06:00
|
|
|
|
2020-10-28 11:40:25 -06:00
|
|
|
func (c *Client) verifyConnection(req *Request, cs tls.ConnectionState) error {
|
|
|
|
// Verify the hostname
|
|
|
|
var hostname string
|
|
|
|
if host, _, err := net.SplitHostPort(req.Host); err == nil {
|
|
|
|
hostname = host
|
|
|
|
} else {
|
|
|
|
hostname = req.Host
|
|
|
|
}
|
|
|
|
cert := cs.PeerCertificates[0]
|
|
|
|
if err := verifyHostname(cert, hostname); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 13:27:12 -07:00
|
|
|
|
2020-12-17 17:50:26 -07:00
|
|
|
// See if the client trusts the certificate
|
|
|
|
if c.TrustCertificate != nil {
|
|
|
|
return c.TrustCertificate(hostname, cert)
|
2020-10-27 17:21:33 -06:00
|
|
|
}
|
2020-12-17 17:50:26 -07:00
|
|
|
return nil
|
2020-10-27 17:21:33 -06:00
|
|
|
}
|