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

168 lines
3.8 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-12-17 23:16:55 +01:00
"context"
2020-09-22 04:09:50 +02:00
"crypto/tls"
2020-09-26 01:53:50 +02:00
"crypto/x509"
2020-12-19 19:43:47 +01:00
"errors"
"fmt"
2020-10-28 00:21:33 +01:00
"net"
2020-11-01 01:55:56 +01:00
"time"
2020-09-22 04:09:50 +02:00
)
2020-10-28 18:40:25 +01:00
// Client is a Gemini client.
2020-09-26 05:06:54 +02:00
type Client struct {
2020-12-18 01:50:26 +01: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.
2021-01-15 04:34:12 +01:00
//
2021-01-25 18:02:09 +01:00
// See the tofu submodule for an implementation of trust on first use.
2020-12-18 01:50:26 +01:00
TrustCertificate func(hostname string, cert *x509.Certificate) error
2020-11-01 01:55:56 +01:00
2020-12-18 01:50:26 +01: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-11-01 04:05:31 +01:00
//
2020-12-18 01:50:26 +01:00
// A Timeout of zero means no timeout.
Timeout time.Duration
2020-09-26 01:53:50 +02:00
}
2020-12-18 01:50:26 +01:00
// Get performs a Gemini request for the given URL.
2020-10-28 00:21:33 +01: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) {
2021-02-09 22:55:14 +01:00
// Punycode request URL
if punycode, err := punycodeHost(req.URL.Host); err != nil {
return nil, err
} else {
// Make a copy of the request
_req := *req
req = &_req
_url := *req.URL
req.URL = &_url
// Set the host
req.URL.Host = punycode
}
// Extract hostname and punycode it
hostname, port, err := net.SplitHostPort(req.Host)
if err != nil {
return nil, err
}
punycode, err := punycodeHostname(hostname)
if err != nil {
return nil, err
2020-11-27 23:45:15 +01:00
}
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-10-28 18:40:25 +01:00
GetClientCertificate: func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) {
2020-12-17 22:46:16 +01:00
if req.Certificate != nil {
return req.Certificate, nil
}
return &tls.Certificate{}, nil
2020-09-26 21:14:34 +02:00
},
VerifyConnection: func(cs tls.ConnectionState) error {
return c.verifyConnection(hostname, punycode, cs)
2020-09-26 01:53:50 +02:00
},
ServerName: punycode,
2020-09-26 01:53:50 +02:00
}
2020-12-17 23:16:55 +01:00
ctx := req.Context
if ctx == nil {
ctx = context.Background()
}
start := time.Now()
dialer := net.Dialer{
Timeout: c.Timeout,
}
address := net.JoinHostPort(punycode, port)
netConn, err := dialer.DialContext(ctx, "tcp", address)
2020-09-26 01:53:50 +02:00
if err != nil {
return nil, err
}
2020-11-26 06:42:25 +01:00
conn := tls.Client(netConn, config)
2020-11-01 01:55:56 +01:00
// Set connection deadline
2020-12-18 06:35:08 +01:00
if c.Timeout != 0 {
err := conn.SetDeadline(start.Add(c.Timeout))
if err != nil {
2021-01-25 18:02:09 +01:00
return nil, fmt.Errorf("failed to set connection deadline: %w", err)
}
2020-11-01 01:55:56 +01:00
}
2020-09-26 01:53:50 +02:00
resp, err := c.do(conn, req)
if err != nil {
// If we fail to perform the request/response we have
// to take responsibility for closing the connection.
_ = conn.Close()
return nil, err
}
// Store connection state
state := conn.ConnectionState()
resp.TLS = &state
return resp, nil
}
func (c *Client) do(conn *tls.Conn, req *Request) (*Response, error) {
2020-09-26 01:53:50 +02:00
// Write the request
w := bufio.NewWriter(conn)
err := req.Write(w)
if err != nil {
2021-01-25 18:02:09 +01:00
return nil, fmt.Errorf("failed to write request: %w", err)
}
2020-09-26 01:53:50 +02:00
if err := w.Flush(); err != nil {
return nil, err
}
// Read the response
resp, err := ReadResponse(conn)
if err != nil {
2020-09-26 01:53:50 +02:00
return nil, err
}
2020-09-26 01:53:50 +02:00
return resp, nil
2020-09-24 06:30:21 +02:00
}
2020-10-28 00:21:33 +01:00
func (c *Client) verifyConnection(hostname, punycode string, cs tls.ConnectionState) error {
2020-10-28 18:40:25 +01:00
cert := cs.PeerCertificates[0]
// Try punycode and then hostname
if err := verifyHostname(cert, punycode); err != nil {
if err := verifyHostname(cert, hostname); err != nil {
return err
}
2020-10-28 18:40:25 +01:00
}
2020-12-19 19:43:47 +01:00
// Check expiration date
if !time.Now().Before(cert.NotAfter) {
return errors.New("gemini: certificate expired")
}
2020-11-05 21:27:12 +01:00
2020-12-18 01:50:26 +01:00
// See if the client trusts the certificate
if c.TrustCertificate != nil {
return c.TrustCertificate(hostname, cert)
2020-10-28 00:21:33 +01:00
}
2020-12-18 01:50:26 +01:00
return nil
2020-10-28 00:21:33 +01:00
}