Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b17f3d8eb | ||
|
|
f36a1c5c87 | ||
|
|
af61c1b60a | ||
|
|
ad18ae601c | ||
|
|
8473f3b9d4 | ||
|
|
06c53cc5b1 | ||
|
|
4b643523fb | ||
|
|
79a4dfd43f | ||
|
|
14d89f304a | ||
|
|
7a00539f75 | ||
|
|
a0adc42c95 | ||
|
|
da8af5dbcb | ||
|
|
ced6b06d76 | ||
|
|
4a0f8e5e73 | ||
|
|
e701ceff71 | ||
|
|
1a3974b3a3 | ||
|
|
3fd55c5cee | ||
|
|
6f11910dff | ||
|
|
da3e9ac0fe | ||
|
|
9fe837ffac | ||
|
|
4b8bb16a3d | ||
|
|
95aff9c573 | ||
|
|
de042e4724 | ||
|
|
d78052ce08 | ||
|
|
1f2888c54a | ||
|
|
41d5f8d31b | ||
|
|
24026422b2 | ||
|
|
5e977250ec | ||
|
|
d8c5da1c7c | ||
|
|
d01d50ff1a | ||
|
|
3ed39e62d8 | ||
|
|
f2921a396f | ||
|
|
efef44c2f9 | ||
|
|
c8626bae17 |
@@ -1,6 +1,6 @@
|
|||||||
# go-gemini
|
# go-gemini
|
||||||
|
|
||||||
[](https://godoc.org/git.sr.ht/~adnano/go-gemini)
|
[](https://godocs.io/git.sr.ht/~adnano/go-gemini)
|
||||||
|
|
||||||
Package gemini implements the [Gemini protocol](https://gemini.circumlunar.space) in Go.
|
Package gemini implements the [Gemini protocol](https://gemini.circumlunar.space) in Go.
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package gemini
|
// Package certificate provides utility functions for TLS certificates.
|
||||||
|
package certificate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto"
|
"crypto"
|
||||||
@@ -19,27 +20,23 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CertificateDir maps certificate scopes to certificates.
|
// Dir represents a directory of certificates.
|
||||||
type CertificateStore map[string]tls.Certificate
|
// The zero value for Dir is an empty directory ready to use.
|
||||||
|
|
||||||
// CertificateDir represents a certificate store optionally loaded from a directory.
|
|
||||||
// The zero value of CertificateDir is an empty store ready to use.
|
|
||||||
//
|
//
|
||||||
// CertificateDir is safe for concurrent use by multiple goroutines.
|
// Dir is safe for concurrent use by multiple goroutines.
|
||||||
type CertificateDir struct {
|
type Dir struct {
|
||||||
CertificateStore
|
certs map[string]tls.Certificate
|
||||||
dir bool
|
path *string
|
||||||
path string
|
mu sync.RWMutex
|
||||||
mu sync.RWMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add adds a certificate for the given scope to the store.
|
// Add adds a certificate for the given scope to the directory.
|
||||||
// It tries to parse the certificate if it is not already parsed.
|
// It tries to parse the certificate if it is not already parsed.
|
||||||
func (c *CertificateDir) Add(scope string, cert tls.Certificate) {
|
func (d *Dir) Add(scope string, cert tls.Certificate) error {
|
||||||
c.mu.Lock()
|
d.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer d.mu.Unlock()
|
||||||
if c.CertificateStore == nil {
|
if d.certs == nil {
|
||||||
c.CertificateStore = CertificateStore{}
|
d.certs = map[string]tls.Certificate{}
|
||||||
}
|
}
|
||||||
// Parse certificate if not already parsed
|
// Parse certificate if not already parsed
|
||||||
if cert.Leaf == nil {
|
if cert.Leaf == nil {
|
||||||
@@ -48,40 +45,45 @@ func (c *CertificateDir) Add(scope string, cert tls.Certificate) {
|
|||||||
cert.Leaf = parsed
|
cert.Leaf = parsed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.CertificateStore[scope] = cert
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write writes the provided certificate to the certificate directory.
|
if d.path != nil {
|
||||||
func (c *CertificateDir) Write(scope string, cert tls.Certificate) error {
|
|
||||||
c.mu.RLock()
|
|
||||||
defer c.mu.RUnlock()
|
|
||||||
if c.dir {
|
|
||||||
// Escape slash character
|
// Escape slash character
|
||||||
scope = strings.ReplaceAll(scope, "/", ":")
|
scope = strings.ReplaceAll(scope, "/", ":")
|
||||||
certPath := filepath.Join(c.path, scope+".crt")
|
certPath := filepath.Join(*d.path, scope+".crt")
|
||||||
keyPath := filepath.Join(c.path, scope+".key")
|
keyPath := filepath.Join(*d.path, scope+".key")
|
||||||
if err := WriteCertificate(cert, certPath, keyPath); err != nil {
|
if err := Write(cert, certPath, keyPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
d.certs[scope] = cert
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup returns the certificate for the given scope.
|
// Lookup returns the certificate for the provided scope.
|
||||||
func (c *CertificateDir) Lookup(scope string) (tls.Certificate, bool) {
|
func (d *Dir) Lookup(scope string) (tls.Certificate, bool) {
|
||||||
c.mu.RLock()
|
d.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
defer d.mu.RUnlock()
|
||||||
cert, ok := c.CertificateStore[scope]
|
cert, ok := d.certs[scope]
|
||||||
return cert, ok
|
return cert, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load loads certificates from the given path.
|
// Entries returns a map of hostnames to certificates.
|
||||||
// The path should lead to a directory containing certificates and private keys
|
func (d *Dir) Entries() map[string]tls.Certificate {
|
||||||
// in the form scope.crt and scope.key.
|
certs := map[string]tls.Certificate{}
|
||||||
// For example, the hostname "localhost" would have the corresponding files
|
for key := range d.certs {
|
||||||
// localhost.crt (certificate) and localhost.key (private key).
|
certs[key] = d.certs[key]
|
||||||
// New certificates will be written to this directory.
|
}
|
||||||
func (c *CertificateDir) Load(path string) error {
|
return certs
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load loads certificates from the provided path.
|
||||||
|
// Add will write certificates to this path.
|
||||||
|
//
|
||||||
|
// The directory should contain certificates and private keys
|
||||||
|
// named scope.crt and scope.key respectively, where scope is
|
||||||
|
// the scope of the certificate.
|
||||||
|
func (d *Dir) Load(path string) error {
|
||||||
matches, err := filepath.Glob(filepath.Join(path, "*.crt"))
|
matches, err := filepath.Glob(filepath.Join(path, "*.crt"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -95,31 +97,31 @@ func (c *CertificateDir) Load(path string) error {
|
|||||||
scope := strings.TrimSuffix(filepath.Base(crtPath), ".crt")
|
scope := strings.TrimSuffix(filepath.Base(crtPath), ".crt")
|
||||||
// Unescape slash character
|
// Unescape slash character
|
||||||
scope = strings.ReplaceAll(scope, ":", "/")
|
scope = strings.ReplaceAll(scope, ":", "/")
|
||||||
c.Add(scope, cert)
|
d.Add(scope, cert)
|
||||||
}
|
}
|
||||||
c.SetDir(path)
|
d.SetPath(path)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDir sets the directory that new certificates will be written to.
|
// SetPath sets the directory path.
|
||||||
func (c *CertificateDir) SetDir(path string) {
|
// Add will write certificates to this path.
|
||||||
c.mu.Lock()
|
func (d *Dir) SetPath(path string) {
|
||||||
defer c.mu.Unlock()
|
d.mu.Lock()
|
||||||
c.dir = true
|
defer d.mu.Unlock()
|
||||||
c.path = path
|
d.path = &path
|
||||||
}
|
}
|
||||||
|
|
||||||
// CertificateOptions configures the creation of a certificate.
|
// CreateOptions configures the creation of a TLS certificate.
|
||||||
type CertificateOptions struct {
|
type CreateOptions struct {
|
||||||
// Subject Alternate Name values.
|
|
||||||
// Should contain the IP addresses that the certificate is valid for.
|
|
||||||
IPAddresses []net.IP
|
|
||||||
|
|
||||||
// Subject Alternate Name values.
|
// Subject Alternate Name values.
|
||||||
// Should contain the DNS names that this certificate is valid for.
|
// Should contain the DNS names that this certificate is valid for.
|
||||||
// E.g. example.com, *.example.com
|
// E.g. example.com, *.example.com
|
||||||
DNSNames []string
|
DNSNames []string
|
||||||
|
|
||||||
|
// Subject Alternate Name values.
|
||||||
|
// Should contain the IP addresses that the certificate is valid for.
|
||||||
|
IPAddresses []net.IP
|
||||||
|
|
||||||
// Subject specifies the certificate Subject.
|
// Subject specifies the certificate Subject.
|
||||||
//
|
//
|
||||||
// Subject.CommonName can contain the DNS name that this certificate
|
// Subject.CommonName can contain the DNS name that this certificate
|
||||||
@@ -136,8 +138,8 @@ type CertificateOptions struct {
|
|||||||
Ed25519 bool
|
Ed25519 bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateCertificate creates a new TLS certificate.
|
// Create creates a new TLS certificate.
|
||||||
func CreateCertificate(options CertificateOptions) (tls.Certificate, error) {
|
func Create(options CreateOptions) (tls.Certificate, error) {
|
||||||
crt, priv, err := newX509KeyPair(options)
|
crt, priv, err := newX509KeyPair(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tls.Certificate{}, err
|
return tls.Certificate{}, err
|
||||||
@@ -150,7 +152,7 @@ func CreateCertificate(options CertificateOptions) (tls.Certificate, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newX509KeyPair creates and returns a new certificate and private key.
|
// newX509KeyPair creates and returns a new certificate and private key.
|
||||||
func newX509KeyPair(options CertificateOptions) (*x509.Certificate, crypto.PrivateKey, error) {
|
func newX509KeyPair(options CreateOptions) (*x509.Certificate, crypto.PrivateKey, error) {
|
||||||
var pub crypto.PublicKey
|
var pub crypto.PublicKey
|
||||||
var priv crypto.PrivateKey
|
var priv crypto.PrivateKey
|
||||||
if options.Ed25519 {
|
if options.Ed25519 {
|
||||||
@@ -206,9 +208,9 @@ func newX509KeyPair(options CertificateOptions) (*x509.Certificate, crypto.Priva
|
|||||||
return cert, priv, nil
|
return cert, priv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteCertificate writes the provided certificate and private key
|
// Write writes the provided certificate and its private key
|
||||||
// to certPath and keyPath respectively.
|
// to certPath and keyPath respectively.
|
||||||
func WriteCertificate(cert tls.Certificate, certPath, keyPath string) error {
|
func Write(cert tls.Certificate, certPath, keyPath string) error {
|
||||||
certOut, err := os.OpenFile(certPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
certOut, err := os.OpenFile(certPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
46
client.go
46
client.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,6 +19,9 @@ type Client struct {
|
|||||||
// If TrustCertificate is nil, the client will accept any certificate.
|
// If TrustCertificate is nil, the client will accept any certificate.
|
||||||
// If the returned error is not nil, the certificate will not be trusted
|
// If the returned error is not nil, the certificate will not be trusted
|
||||||
// and the request will be aborted.
|
// and the request will be aborted.
|
||||||
|
//
|
||||||
|
// For a basic trust on first use implementation, see (*KnownHosts).TOFU
|
||||||
|
// in the tofu submodule.
|
||||||
TrustCertificate func(hostname string, cert *x509.Certificate) error
|
TrustCertificate func(hostname string, cert *x509.Certificate) error
|
||||||
|
|
||||||
// Timeout specifies a time limit for requests made by this
|
// Timeout specifies a time limit for requests made by this
|
||||||
@@ -67,19 +71,53 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
|||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
netConn, err := (&net.Dialer{}).DialContext(ctx, "tcp", req.Host)
|
|
||||||
|
start := time.Now()
|
||||||
|
dialer := net.Dialer{
|
||||||
|
Timeout: c.Timeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
netConn, err := dialer.DialContext(ctx, "tcp", req.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn := tls.Client(netConn, config)
|
conn := tls.Client(netConn, config)
|
||||||
|
|
||||||
// Set connection deadline
|
// Set connection deadline
|
||||||
if c.Timeout != 0 {
|
if c.Timeout != 0 {
|
||||||
conn.SetDeadline(time.Now().Add(c.Timeout))
|
err := conn.SetDeadline(start.Add(c.Timeout))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"failed to set connection deadline: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
resp.TLS = conn.ConnectionState()
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) do(conn *tls.Conn, req *Request) (*Response, error) {
|
||||||
// Write the request
|
// Write the request
|
||||||
w := bufio.NewWriter(conn)
|
w := bufio.NewWriter(conn)
|
||||||
req.Write(w)
|
|
||||||
|
err := req.Write(w)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"failed to write request data: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := w.Flush(); err != nil {
|
if err := w.Flush(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -89,8 +127,6 @@ func (c *Client) Do(req *Request) (*Response, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Store connection state
|
|
||||||
resp.TLS = conn.ConnectionState()
|
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|||||||
50
doc.go
Normal file
50
doc.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
Package gemini implements the Gemini protocol.
|
||||||
|
|
||||||
|
Client is a Gemini client.
|
||||||
|
|
||||||
|
client := &gemini.Client{}
|
||||||
|
resp, err := client.Get("gemini://example.com")
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
if resp.Body != nil {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
|
||||||
|
Server is a Gemini server.
|
||||||
|
|
||||||
|
server := &gemini.Server{
|
||||||
|
ReadTimeout: 10 * time.Second,
|
||||||
|
WriteTimeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
Servers should be configured with certificates:
|
||||||
|
|
||||||
|
err := server.Certificates.Load("/var/lib/gemini/certs")
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
|
||||||
|
Servers can accept requests for multiple hosts and schemes:
|
||||||
|
|
||||||
|
server.RegisterFunc("example.com", func(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
|
fmt.Fprint(w, "Welcome to example.com")
|
||||||
|
})
|
||||||
|
server.RegisterFunc("example.org", func(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
|
fmt.Fprint(w, "Welcome to example.org")
|
||||||
|
})
|
||||||
|
server.RegisterFunc("http://example.net", func(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
|
fmt.Fprint(w, "Proxied content from http://example.net")
|
||||||
|
})
|
||||||
|
|
||||||
|
To start the server, call ListenAndServe:
|
||||||
|
|
||||||
|
err := server.ListenAndServe()
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
package gemini
|
||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.sr.ht/~adnano/go-gemini"
|
"git.sr.ht/~adnano/go-gemini"
|
||||||
|
"git.sr.ht/~adnano/go-gemini/certificate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@@ -33,7 +34,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
|
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
|
||||||
return gemini.CreateCertificate(gemini.CertificateOptions{
|
return certificate.Create(certificate.CreateOptions{
|
||||||
Subject: pkix.Name{
|
Subject: pkix.Name{
|
||||||
CommonName: hostname,
|
CommonName: hostname,
|
||||||
},
|
},
|
||||||
@@ -41,7 +42,7 @@ func main() {
|
|||||||
Duration: time.Hour,
|
Duration: time.Hour,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
server.Register("localhost", &mux)
|
server.Handle("localhost", &mux)
|
||||||
|
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -55,7 +56,7 @@ func fingerprint(cert *x509.Certificate) string {
|
|||||||
|
|
||||||
func profile(w *gemini.ResponseWriter, r *gemini.Request) {
|
func profile(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
if r.Certificate == nil {
|
if r.Certificate == nil {
|
||||||
w.WriteStatus(gemini.StatusCertificateRequired)
|
w.Status(gemini.StatusCertificateRequired)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fingerprint := fingerprint(r.Certificate.Leaf)
|
fingerprint := fingerprint(r.Certificate.Leaf)
|
||||||
@@ -70,13 +71,13 @@ func profile(w *gemini.ResponseWriter, r *gemini.Request) {
|
|||||||
|
|
||||||
func changeUsername(w *gemini.ResponseWriter, r *gemini.Request) {
|
func changeUsername(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
if r.Certificate == nil {
|
if r.Certificate == nil {
|
||||||
w.WriteStatus(gemini.StatusCertificateRequired)
|
w.Status(gemini.StatusCertificateRequired)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
username, err := gemini.QueryUnescape(r.URL.RawQuery)
|
username, err := gemini.QueryUnescape(r.URL.RawQuery)
|
||||||
if err != nil || username == "" {
|
if err != nil || username == "" {
|
||||||
w.WriteHeader(gemini.StatusInput, "Username")
|
w.Header(gemini.StatusInput, "Username")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fingerprint := fingerprint(r.Certificate.Leaf)
|
fingerprint := fingerprint(r.Certificate.Leaf)
|
||||||
@@ -86,5 +87,5 @@ func changeUsername(w *gemini.ResponseWriter, r *gemini.Request) {
|
|||||||
users[fingerprint] = user
|
users[fingerprint] = user
|
||||||
}
|
}
|
||||||
user.Name = username
|
user.Name = username
|
||||||
w.WriteHeader(gemini.StatusRedirect, "/")
|
w.Header(gemini.StatusRedirect, "/")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.sr.ht/~adnano/go-gemini"
|
"git.sr.ht/~adnano/go-gemini/certificate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -24,20 +24,20 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
options := gemini.CertificateOptions{
|
options := certificate.CreateOptions{
|
||||||
Subject: pkix.Name{
|
Subject: pkix.Name{
|
||||||
CommonName: host,
|
CommonName: host,
|
||||||
},
|
},
|
||||||
DNSNames: []string{host},
|
DNSNames: []string{host},
|
||||||
Duration: duration,
|
Duration: duration,
|
||||||
}
|
}
|
||||||
cert, err := gemini.CreateCertificate(options)
|
cert, err := certificate.Create(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
certPath := host + ".crt"
|
certPath := host + ".crt"
|
||||||
keyPath := host + ".key"
|
keyPath := host + ".key"
|
||||||
if err := gemini.WriteCertificate(cert, certPath, keyPath); err != nil {
|
if err := certificate.Write(cert, certPath, keyPath); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -17,12 +18,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.sr.ht/~adnano/go-gemini"
|
"git.sr.ht/~adnano/go-gemini"
|
||||||
|
"git.sr.ht/~adnano/go-gemini/tofu"
|
||||||
"git.sr.ht/~adnano/go-xdg"
|
"git.sr.ht/~adnano/go-xdg"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
hosts gemini.KnownHostsFile
|
hosts tofu.KnownHosts
|
||||||
scanner *bufio.Scanner
|
hostsfile *tofu.HostWriter
|
||||||
|
scanner *bufio.Scanner
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -30,7 +33,12 @@ func init() {
|
|||||||
path := filepath.Join(xdg.DataHome(), "gemini", "known_hosts")
|
path := filepath.Join(xdg.DataHome(), "gemini", "known_hosts")
|
||||||
err := hosts.Load(path)
|
err := hosts.Load(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
hostsfile, err = tofu.NewHostsFile(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
scanner = bufio.NewScanner(os.Stdin)
|
scanner = bufio.NewScanner(os.Stdin)
|
||||||
@@ -46,25 +54,26 @@ Otherwise, this should be safe to trust.
|
|||||||
=> `
|
=> `
|
||||||
|
|
||||||
func trustCertificate(hostname string, cert *x509.Certificate) error {
|
func trustCertificate(hostname string, cert *x509.Certificate) error {
|
||||||
fingerprint := gemini.NewFingerprint(cert.Raw, cert.NotAfter)
|
host := tofu.NewHost(hostname, cert.Raw, cert.NotAfter)
|
||||||
|
|
||||||
knownHost, ok := hosts.Lookup(hostname)
|
knownHost, ok := hosts.Lookup(hostname)
|
||||||
if ok && time.Now().Before(knownHost.Expires) {
|
if ok && time.Now().Before(knownHost.Expires) {
|
||||||
// Check fingerprint
|
// Check fingerprint
|
||||||
if knownHost.Hex == fingerprint.Hex {
|
if bytes.Equal(knownHost.Fingerprint, host.Fingerprint) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("error: fingerprint does not match!")
|
return errors.New("error: fingerprint does not match!")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(trustPrompt, hostname, fingerprint.Hex)
|
fmt.Printf(trustPrompt, hostname, host.Fingerprint)
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
switch scanner.Text() {
|
switch scanner.Text() {
|
||||||
case "t":
|
case "t":
|
||||||
hosts.Add(hostname, fingerprint)
|
hosts.Add(host)
|
||||||
hosts.Write(hostname, fingerprint)
|
hostsfile.WriteHost(host)
|
||||||
return nil
|
return nil
|
||||||
case "o":
|
case "o":
|
||||||
hosts.Add(hostname, fingerprint)
|
hosts.Add(host)
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return errors.New("certificate not trusted")
|
return errors.New("certificate not trusted")
|
||||||
@@ -146,7 +155,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Print(string(body))
|
fmt.Print(string(body))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("%d %s: %s\n", resp.Status, resp.Status.Message(), resp.Meta)
|
fmt.Printf("%d %s\n", resp.Status, resp.Meta)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.sr.ht/~adnano/go-gemini"
|
"git.sr.ht/~adnano/go-gemini"
|
||||||
|
"git.sr.ht/~adnano/go-gemini/certificate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -21,7 +22,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
|
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
|
||||||
return gemini.CreateCertificate(gemini.CertificateOptions{
|
return certificate.Create(certificate.CreateOptions{
|
||||||
Subject: pkix.Name{
|
Subject: pkix.Name{
|
||||||
CommonName: hostname,
|
CommonName: hostname,
|
||||||
},
|
},
|
||||||
@@ -33,7 +34,7 @@ func main() {
|
|||||||
var mux gemini.ServeMux
|
var mux gemini.ServeMux
|
||||||
mux.Handle("/", gemini.FileServer(gemini.Dir("/var/www")))
|
mux.Handle("/", gemini.FileServer(gemini.Dir("/var/www")))
|
||||||
|
|
||||||
server.Register("localhost", &mux)
|
server.Handle("localhost", &mux)
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509/pkix"
|
"crypto/x509/pkix"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -12,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.sr.ht/~adnano/go-gemini"
|
"git.sr.ht/~adnano/go-gemini"
|
||||||
|
"git.sr.ht/~adnano/go-gemini/certificate"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -20,7 +22,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
|
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
|
||||||
return gemini.CreateCertificate(gemini.CertificateOptions{
|
return certificate.Create(certificate.CreateOptions{
|
||||||
Subject: pkix.Name{
|
Subject: pkix.Name{
|
||||||
CommonName: hostname,
|
CommonName: hostname,
|
||||||
},
|
},
|
||||||
@@ -29,16 +31,41 @@ func main() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
server.RegisterFunc("localhost", stream)
|
server.HandleFunc("localhost", stream)
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stream writes an infinite stream to w.
|
||||||
func stream(w *gemini.ResponseWriter, r *gemini.Request) {
|
func stream(w *gemini.ResponseWriter, r *gemini.Request) {
|
||||||
|
ch := make(chan string)
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
go func(ctx context.Context) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
ch <- fmt.Sprint(time.Now().UTC())
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
// Close channel when finished.
|
||||||
|
// In this example this will never be reached.
|
||||||
|
close(ch)
|
||||||
|
}(ctx)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
fmt.Fprintln(w, time.Now().UTC())
|
s, ok := <-ch
|
||||||
w.Flush()
|
if !ok {
|
||||||
time.Sleep(time.Second)
|
break
|
||||||
|
}
|
||||||
|
fmt.Fprintln(w, s)
|
||||||
|
if err := w.Flush(); err != nil {
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
fs.go
27
fs.go
@@ -14,7 +14,9 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FileServer takes a filesystem and returns a Responder which uses that filesystem.
|
// FileServer takes a filesystem and returns a Responder which uses that filesystem.
|
||||||
// The returned Responder sanitizes paths before handling them.
|
// The returned Responder cleans paths before handling them.
|
||||||
|
//
|
||||||
|
// TODO: Use io/fs.FS when available.
|
||||||
func FileServer(fsys FS) Responder {
|
func FileServer(fsys FS) Responder {
|
||||||
return fsHandler{fsys}
|
return fsHandler{fsys}
|
||||||
}
|
}
|
||||||
@@ -27,23 +29,27 @@ func (fsh fsHandler) Respond(w *ResponseWriter, r *Request) {
|
|||||||
p := path.Clean(r.URL.Path)
|
p := path.Clean(r.URL.Path)
|
||||||
f, err := fsh.Open(p)
|
f, err := fsh.Open(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteStatus(StatusNotFound)
|
w.Status(StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Detect mimetype
|
// Detect mimetype
|
||||||
ext := path.Ext(p)
|
ext := path.Ext(p)
|
||||||
mimetype := mime.TypeByExtension(ext)
|
mimetype := mime.TypeByExtension(ext)
|
||||||
w.SetMediaType(mimetype)
|
w.Meta(mimetype)
|
||||||
// Copy file to response writer
|
// Copy file to response writer
|
||||||
io.Copy(w, f)
|
_, _ = io.Copy(w, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: replace with io/fs.FS when available
|
// FS represents a filesystem.
|
||||||
|
//
|
||||||
|
// TODO: Replace with io/fs.FS when available.
|
||||||
type FS interface {
|
type FS interface {
|
||||||
Open(name string) (File, error)
|
Open(name string) (File, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: replace with io/fs.File when available
|
// File represents a file.
|
||||||
|
//
|
||||||
|
// TODO: Replace with io/fs.File when available.
|
||||||
type File interface {
|
type File interface {
|
||||||
Stat() (os.FileInfo, error)
|
Stat() (os.FileInfo, error)
|
||||||
Read([]byte) (int, error)
|
Read([]byte) (int, error)
|
||||||
@@ -51,6 +57,8 @@ type File interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dir implements FS using the native filesystem restricted to a specific directory.
|
// Dir implements FS using the native filesystem restricted to a specific directory.
|
||||||
|
//
|
||||||
|
// TODO: replace with os.DirFS when available.
|
||||||
type Dir string
|
type Dir string
|
||||||
|
|
||||||
// Open tries to open the file with the given name.
|
// Open tries to open the file with the given name.
|
||||||
@@ -62,19 +70,20 @@ func (d Dir) Open(name string) (File, error) {
|
|||||||
|
|
||||||
// ServeFile responds to the request with the contents of the named file
|
// ServeFile responds to the request with the contents of the named file
|
||||||
// or directory.
|
// or directory.
|
||||||
|
//
|
||||||
// TODO: Use io/fs.FS when available.
|
// TODO: Use io/fs.FS when available.
|
||||||
func ServeFile(w *ResponseWriter, fs FS, name string) {
|
func ServeFile(w *ResponseWriter, fs FS, name string) {
|
||||||
f, err := fs.Open(name)
|
f, err := fs.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteStatus(StatusNotFound)
|
w.Status(StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Detect mimetype
|
// Detect mimetype
|
||||||
ext := path.Ext(name)
|
ext := path.Ext(name)
|
||||||
mimetype := mime.TypeByExtension(ext)
|
mimetype := mime.TypeByExtension(ext)
|
||||||
w.SetMediaType(mimetype)
|
w.Meta(mimetype)
|
||||||
// Copy file to response writer
|
// Copy file to response writer
|
||||||
io.Copy(w, f)
|
_, _ = io.Copy(w, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func openFile(p string) (File, error) {
|
func openFile(p string) (File, error) {
|
||||||
|
|||||||
49
gemini.go
49
gemini.go
@@ -1,52 +1,3 @@
|
|||||||
/*
|
|
||||||
Package gemini implements the Gemini protocol.
|
|
||||||
|
|
||||||
Client is a Gemini client.
|
|
||||||
|
|
||||||
client := &gemini.Client{}
|
|
||||||
resp, err := client.Get("gemini://example.com")
|
|
||||||
if err != nil {
|
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
if resp.Status.Class() == gemini.StatusClassSucess {
|
|
||||||
defer resp.Body.Close()
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
// ...
|
|
||||||
|
|
||||||
Server is a Gemini server.
|
|
||||||
|
|
||||||
server := &gemini.Server{
|
|
||||||
ReadTimeout: 10 * time.Second,
|
|
||||||
WriteTimeout: 10 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
Servers should be configured with certificates:
|
|
||||||
|
|
||||||
err := server.Certificates.Load("/var/lib/gemini/certs")
|
|
||||||
if err != nil {
|
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
|
|
||||||
Servers can accept requests for multiple hosts and schemes:
|
|
||||||
|
|
||||||
server.RegisterFunc("example.com", func(w *gemini.ResponseWriter, r *gemini.Request) {
|
|
||||||
fmt.Fprint(w, "Welcome to example.com")
|
|
||||||
})
|
|
||||||
server.RegisterFunc("example.org", func(w *gemini.ResponseWriter, r *gemini.Request) {
|
|
||||||
fmt.Fprint(w, "Welcome to example.org")
|
|
||||||
})
|
|
||||||
server.RegisterFunc("http://example.net", func(w *gemini.ResponseWriter, r *gemini.Request) {
|
|
||||||
fmt.Fprint(w, "Proxied content from http://example.net")
|
|
||||||
})
|
|
||||||
|
|
||||||
To start the server, call ListenAndServe:
|
|
||||||
|
|
||||||
err := server.ListenAndServe()
|
|
||||||
if err != nil {
|
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
package gemini
|
package gemini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
6
mux.go
6
mux.go
@@ -138,14 +138,14 @@ func (mux *ServeMux) Respond(w *ResponseWriter, r *Request) {
|
|||||||
// If the given path is /tree and its handler is not registered,
|
// If the given path is /tree and its handler is not registered,
|
||||||
// redirect for /tree/.
|
// redirect for /tree/.
|
||||||
if u, ok := mux.redirectToPathSlash(path, r.URL); ok {
|
if u, ok := mux.redirectToPathSlash(path, r.URL); ok {
|
||||||
w.WriteHeader(StatusRedirect, u.String())
|
w.Header(StatusRedirect, u.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if path != r.URL.Path {
|
if path != r.URL.Path {
|
||||||
u := *r.URL
|
u := *r.URL
|
||||||
u.Path = path
|
u.Path = path
|
||||||
w.WriteHeader(StatusRedirect, u.String())
|
w.Header(StatusRedirect, u.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +154,7 @@ func (mux *ServeMux) Respond(w *ResponseWriter, r *Request) {
|
|||||||
|
|
||||||
resp := mux.match(path)
|
resp := mux.match(path)
|
||||||
if resp == nil {
|
if resp == nil {
|
||||||
w.WriteStatus(StatusNotFound)
|
w.Status(StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp.Respond(w, r)
|
resp.Respond(w, r)
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ type Request struct {
|
|||||||
Host string
|
Host string
|
||||||
|
|
||||||
// Certificate specifies the TLS certificate to use for the request.
|
// Certificate specifies the TLS certificate to use for the request.
|
||||||
// Request certificates take precedence over client certificates.
|
|
||||||
//
|
//
|
||||||
// On the server side, if the client provided a certificate then
|
// On the server side, if the client provided a certificate then
|
||||||
// Certificate.Leaf is guaranteed to be non-nil.
|
// Certificate.Leaf is guaranteed to be non-nil.
|
||||||
|
|||||||
93
response.go
93
response.go
@@ -13,7 +13,7 @@ type Response struct {
|
|||||||
Status Status
|
Status Status
|
||||||
|
|
||||||
// Meta contains more information related to the response status.
|
// Meta contains more information related to the response status.
|
||||||
// For successful responses, Meta should contain the mimetype of the response.
|
// For successful responses, Meta should contain the media type of the response.
|
||||||
// For failure responses, Meta should contain a short description of the failure.
|
// For failure responses, Meta should contain a short description of the failure.
|
||||||
// Meta should not be longer than 1024 bytes.
|
// Meta should not be longer than 1024 bytes.
|
||||||
Meta string
|
Meta string
|
||||||
@@ -82,6 +82,8 @@ func ReadResponse(rc io.ReadCloser) (*Response, error) {
|
|||||||
|
|
||||||
if resp.Status.Class() == StatusClassSuccess {
|
if resp.Status.Class() == StatusClassSuccess {
|
||||||
resp.Body = newReadCloserBody(br, rc)
|
resp.Body = newReadCloserBody(br, rc)
|
||||||
|
} else {
|
||||||
|
rc.Close()
|
||||||
}
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
@@ -112,3 +114,92 @@ func (b *readCloserBody) Read(p []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
return b.ReadCloser.Read(p)
|
return b.ReadCloser.Read(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResponseWriter is used to construct a Gemini response.
|
||||||
|
type ResponseWriter struct {
|
||||||
|
b *bufio.Writer
|
||||||
|
status Status
|
||||||
|
meta string
|
||||||
|
setHeader bool
|
||||||
|
wroteHeader bool
|
||||||
|
bodyAllowed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewResponseWriter returns a ResponseWriter that uses the provided io.Writer.
|
||||||
|
func NewResponseWriter(w io.Writer) *ResponseWriter {
|
||||||
|
return &ResponseWriter{
|
||||||
|
b: bufio.NewWriter(w),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header sets the response header.
|
||||||
|
func (w *ResponseWriter) Header(status Status, meta string) {
|
||||||
|
w.status = status
|
||||||
|
w.meta = meta
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status sets the response status code.
|
||||||
|
// It also sets the response meta to status.Meta().
|
||||||
|
func (w *ResponseWriter) Status(status Status) {
|
||||||
|
w.status = status
|
||||||
|
w.meta = status.Meta()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Meta sets the response meta.
|
||||||
|
//
|
||||||
|
// For successful responses, meta should contain the media type of the response.
|
||||||
|
// For failure responses, meta should contain a short description of the failure.
|
||||||
|
// The response meta should not be greater than 1024 bytes.
|
||||||
|
func (w *ResponseWriter) Meta(meta string) {
|
||||||
|
w.meta = meta
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write writes data to the connection as part of the response body.
|
||||||
|
// If the response status does not allow for a response body, Write returns
|
||||||
|
// ErrBodyNotAllowed.
|
||||||
|
//
|
||||||
|
// Write writes the response header if it has not already been written.
|
||||||
|
// It writes a successful status code if one is not set.
|
||||||
|
func (w *ResponseWriter) Write(b []byte) (int, error) {
|
||||||
|
if !w.wroteHeader {
|
||||||
|
w.writeHeader(StatusSuccess)
|
||||||
|
}
|
||||||
|
if !w.bodyAllowed {
|
||||||
|
return 0, ErrBodyNotAllowed
|
||||||
|
}
|
||||||
|
return w.b.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResponseWriter) writeHeader(defaultStatus Status) {
|
||||||
|
status := w.status
|
||||||
|
if status == 0 {
|
||||||
|
status = defaultStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
meta := w.meta
|
||||||
|
if status.Class() == StatusClassSuccess {
|
||||||
|
w.bodyAllowed = true
|
||||||
|
|
||||||
|
if meta == "" {
|
||||||
|
meta = "text/gemini"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.b.WriteString(strconv.Itoa(int(status)))
|
||||||
|
w.b.WriteByte(' ')
|
||||||
|
w.b.WriteString(meta)
|
||||||
|
w.b.Write(crlf)
|
||||||
|
w.wroteHeader = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush writes any buffered data to the underlying io.Writer.
|
||||||
|
//
|
||||||
|
// Flush writes the response header if it has not already been written.
|
||||||
|
// It writes a failure status code if one is not set.
|
||||||
|
func (w *ResponseWriter) Flush() error {
|
||||||
|
if !w.wroteHeader {
|
||||||
|
w.writeHeader(StatusTemporaryFailure)
|
||||||
|
}
|
||||||
|
// Write errors from writeHeader will be returned here.
|
||||||
|
return w.b.Flush()
|
||||||
|
}
|
||||||
|
|||||||
140
server.go
140
server.go
@@ -1,15 +1,14 @@
|
|||||||
package gemini
|
package gemini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.sr.ht/~adnano/go-gemini/certificate"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server is a Gemini server.
|
// Server is a Gemini server.
|
||||||
@@ -26,7 +25,7 @@ type Server struct {
|
|||||||
WriteTimeout time.Duration
|
WriteTimeout time.Duration
|
||||||
|
|
||||||
// Certificates contains the certificates used by the server.
|
// Certificates contains the certificates used by the server.
|
||||||
Certificates CertificateDir
|
Certificates certificate.Dir
|
||||||
|
|
||||||
// CreateCertificate, if not nil, will be called to create a new certificate
|
// CreateCertificate, if not nil, will be called to create a new certificate
|
||||||
// if the current one is expired or missing.
|
// if the current one is expired or missing.
|
||||||
@@ -47,12 +46,12 @@ type responderKey struct {
|
|||||||
hostname string
|
hostname string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register registers a responder for the given pattern.
|
// Handle registers a responder for the given pattern.
|
||||||
//
|
//
|
||||||
// Patterns must be in the form of "hostname" or "scheme://hostname".
|
// The pattern must be in the form of "hostname" or "scheme://hostname".
|
||||||
// If no scheme is specified, a scheme of "gemini://" is implied.
|
// If no scheme is specified, a scheme of "gemini://" is implied.
|
||||||
// Wildcard patterns are supported (e.g. "*.example.com").
|
// Wildcard patterns are supported (e.g. "*.example.com").
|
||||||
func (s *Server) Register(pattern string, responder Responder) {
|
func (s *Server) Handle(pattern string, responder Responder) {
|
||||||
if pattern == "" {
|
if pattern == "" {
|
||||||
panic("gemini: invalid pattern")
|
panic("gemini: invalid pattern")
|
||||||
}
|
}
|
||||||
@@ -81,9 +80,9 @@ func (s *Server) Register(pattern string, responder Responder) {
|
|||||||
s.hosts[key.hostname] = true
|
s.hosts[key.hostname] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterFunc registers a responder function for the given pattern.
|
// HandleFunc registers a responder function for the given pattern.
|
||||||
func (s *Server) RegisterFunc(pattern string, responder func(*ResponseWriter, *Request)) {
|
func (s *Server) HandleFunc(pattern string, responder func(*ResponseWriter, *Request)) {
|
||||||
s.Register(pattern, ResponderFunc(responder))
|
s.Handle(pattern, ResponderFunc(responder))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenAndServe listens for requests at the server's configured address.
|
// ListenAndServe listens for requests at the server's configured address.
|
||||||
@@ -160,8 +159,7 @@ func (s *Server) getCertificateFor(hostname string) (*tls.Certificate, error) {
|
|||||||
if s.CreateCertificate != nil {
|
if s.CreateCertificate != nil {
|
||||||
cert, err := s.CreateCertificate(hostname)
|
cert, err := s.CreateCertificate(hostname)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
s.Certificates.Add(hostname, cert)
|
if err := s.Certificates.Add(hostname, cert); err != nil {
|
||||||
if err := s.Certificates.Write(hostname, cert); err != nil {
|
|
||||||
s.logf("gemini: Failed to write new certificate for %s: %s", hostname, err)
|
s.logf("gemini: Failed to write new certificate for %s: %s", hostname, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -176,39 +174,43 @@ func (s *Server) getCertificateFor(hostname string) (*tls.Certificate, error) {
|
|||||||
func (s *Server) respond(conn net.Conn) {
|
func (s *Server) respond(conn net.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
if d := s.ReadTimeout; d != 0 {
|
if d := s.ReadTimeout; d != 0 {
|
||||||
conn.SetReadDeadline(time.Now().Add(d))
|
_ = conn.SetReadDeadline(time.Now().Add(d))
|
||||||
}
|
}
|
||||||
if d := s.WriteTimeout; d != 0 {
|
if d := s.WriteTimeout; d != 0 {
|
||||||
conn.SetWriteDeadline(time.Now().Add(d))
|
_ = conn.SetWriteDeadline(time.Now().Add(d))
|
||||||
}
|
}
|
||||||
|
|
||||||
w := NewResponseWriter(conn)
|
w := NewResponseWriter(conn)
|
||||||
defer w.b.Flush()
|
defer func() {
|
||||||
|
_ = w.Flush()
|
||||||
|
}()
|
||||||
|
|
||||||
req, err := ReadRequest(conn)
|
req, err := ReadRequest(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteStatus(StatusBadRequest)
|
w.Status(StatusBadRequest)
|
||||||
} else {
|
return
|
||||||
// Store information about the TLS connection
|
}
|
||||||
if tlsConn, ok := conn.(*tls.Conn); ok {
|
|
||||||
req.TLS = tlsConn.ConnectionState()
|
// Store information about the TLS connection
|
||||||
if len(req.TLS.PeerCertificates) > 0 {
|
if tlsConn, ok := conn.(*tls.Conn); ok {
|
||||||
peerCert := req.TLS.PeerCertificates[0]
|
req.TLS = tlsConn.ConnectionState()
|
||||||
// Store the TLS certificate
|
if len(req.TLS.PeerCertificates) > 0 {
|
||||||
req.Certificate = &tls.Certificate{
|
peerCert := req.TLS.PeerCertificates[0]
|
||||||
Certificate: [][]byte{peerCert.Raw},
|
// Store the TLS certificate
|
||||||
Leaf: peerCert,
|
req.Certificate = &tls.Certificate{
|
||||||
}
|
Certificate: [][]byte{peerCert.Raw},
|
||||||
|
Leaf: peerCert,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := s.responder(req)
|
resp := s.responder(req)
|
||||||
if resp != nil {
|
if resp == nil {
|
||||||
resp.Respond(w, req)
|
w.Status(StatusNotFound)
|
||||||
} else {
|
return
|
||||||
w.WriteStatus(StatusNotFound)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resp.Respond(w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) responder(r *Request) Responder {
|
func (s *Server) responder(r *Request) Responder {
|
||||||
@@ -232,82 +234,6 @@ func (s *Server) logf(format string, args ...interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResponseWriter is used by a Gemini handler to construct a Gemini response.
|
|
||||||
type ResponseWriter struct {
|
|
||||||
b *bufio.Writer
|
|
||||||
bodyAllowed bool
|
|
||||||
wroteHeader bool
|
|
||||||
mediatype string
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewResponseWriter returns a ResponseWriter that uses the provided io.Writer.
|
|
||||||
func NewResponseWriter(w io.Writer) *ResponseWriter {
|
|
||||||
return &ResponseWriter{
|
|
||||||
b: bufio.NewWriter(w),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteHeader writes the response header.
|
|
||||||
// If the header has already been written, WriteHeader does nothing.
|
|
||||||
//
|
|
||||||
// Meta contains more information related to the response status.
|
|
||||||
// For successful responses, Meta should contain the mimetype of the response.
|
|
||||||
// For failure responses, Meta should contain a short description of the failure.
|
|
||||||
// Meta should not be longer than 1024 bytes.
|
|
||||||
func (w *ResponseWriter) WriteHeader(status Status, meta string) {
|
|
||||||
if w.wroteHeader {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.b.WriteString(strconv.Itoa(int(status)))
|
|
||||||
w.b.WriteByte(' ')
|
|
||||||
w.b.WriteString(meta)
|
|
||||||
w.b.Write(crlf)
|
|
||||||
|
|
||||||
// Only allow body to be written on successful status codes.
|
|
||||||
if status.Class() == StatusClassSuccess {
|
|
||||||
w.bodyAllowed = true
|
|
||||||
}
|
|
||||||
w.wroteHeader = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteStatus writes the response header with the given status code.
|
|
||||||
//
|
|
||||||
// WriteStatus is equivalent to WriteHeader(status, status.Message())
|
|
||||||
func (w *ResponseWriter) WriteStatus(status Status) {
|
|
||||||
w.WriteHeader(status, status.Message())
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetMediaType sets the media type that will be written for a successful response.
|
|
||||||
// If the mimetype is not set, it will default to "text/gemini".
|
|
||||||
func (w *ResponseWriter) SetMediaType(mediatype string) {
|
|
||||||
w.mediatype = mediatype
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write writes data to the connection as part of the response body.
|
|
||||||
// If the response status does not allow for a response body, Write returns
|
|
||||||
// ErrBodyNotAllowed.
|
|
||||||
//
|
|
||||||
// If the response header has not yet been written, Write calls WriteHeader
|
|
||||||
// with StatusSuccess and the mimetype set in SetMimetype.
|
|
||||||
func (w *ResponseWriter) Write(b []byte) (int, error) {
|
|
||||||
if !w.wroteHeader {
|
|
||||||
mediatype := w.mediatype
|
|
||||||
if mediatype == "" {
|
|
||||||
mediatype = "text/gemini"
|
|
||||||
}
|
|
||||||
w.WriteHeader(StatusSuccess, mediatype)
|
|
||||||
}
|
|
||||||
if !w.bodyAllowed {
|
|
||||||
return 0, ErrBodyNotAllowed
|
|
||||||
}
|
|
||||||
return w.b.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Flush writes any buffered data to the underlying io.Writer.
|
|
||||||
func (w *ResponseWriter) Flush() error {
|
|
||||||
return w.b.Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
// A Responder responds to a Gemini request.
|
// A Responder responds to a Gemini request.
|
||||||
type Responder interface {
|
type Responder interface {
|
||||||
// Respond accepts a Request and constructs a Response.
|
// Respond accepts a Request and constructs a Response.
|
||||||
|
|||||||
16
status.go
16
status.go
@@ -41,19 +41,11 @@ func (s Status) Class() StatusClass {
|
|||||||
return StatusClass(s / 10)
|
return StatusClass(s / 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message returns a status message corresponding to this status code.
|
// Meta returns a description of the status code appropriate for use in a response.
|
||||||
func (s Status) Message() string {
|
//
|
||||||
|
// Meta returns an empty string for input, success, and redirect status codes.
|
||||||
|
func (s Status) Meta() string {
|
||||||
switch s {
|
switch s {
|
||||||
case StatusInput:
|
|
||||||
return "Input"
|
|
||||||
case StatusSensitiveInput:
|
|
||||||
return "Sensitive input"
|
|
||||||
case StatusSuccess:
|
|
||||||
return "Success"
|
|
||||||
case StatusRedirect:
|
|
||||||
return "Redirect"
|
|
||||||
case StatusPermanentRedirect:
|
|
||||||
return "Permanent redirect"
|
|
||||||
case StatusTemporaryFailure:
|
case StatusTemporaryFailure:
|
||||||
return "Temporary failure"
|
return "Temporary failure"
|
||||||
case StatusServerUnavailable:
|
case StatusServerUnavailable:
|
||||||
|
|||||||
10
text.go
10
text.go
@@ -88,17 +88,17 @@ func (l LineText) line() {}
|
|||||||
type Text []Line
|
type Text []Line
|
||||||
|
|
||||||
// ParseText parses Gemini text from the provided io.Reader.
|
// ParseText parses Gemini text from the provided io.Reader.
|
||||||
func ParseText(r io.Reader) Text {
|
func ParseText(r io.Reader) (Text, error) {
|
||||||
var t Text
|
var t Text
|
||||||
ParseLines(r, func(line Line) {
|
err := ParseLines(r, func(line Line) {
|
||||||
t = append(t, line)
|
t = append(t, line)
|
||||||
})
|
})
|
||||||
return t
|
return t, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseLines parses Gemini text from the provided io.Reader.
|
// ParseLines parses Gemini text from the provided io.Reader.
|
||||||
// It calls handler with each line that it parses.
|
// It calls handler with each line that it parses.
|
||||||
func ParseLines(r io.Reader, handler func(Line)) {
|
func ParseLines(r io.Reader, handler func(Line)) error {
|
||||||
const spacetab = " \t"
|
const spacetab = " \t"
|
||||||
var pre bool
|
var pre bool
|
||||||
scanner := bufio.NewScanner(r)
|
scanner := bufio.NewScanner(r)
|
||||||
@@ -149,6 +149,8 @@ func ParseLines(r io.Reader, handler func(Line)) {
|
|||||||
}
|
}
|
||||||
handler(line)
|
handler(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// String writes the Gemini text response to a string and returns it.
|
// String writes the Gemini text response to a string and returns it.
|
||||||
|
|||||||
152
tofu.go
152
tofu.go
@@ -1,152 +0,0 @@
|
|||||||
package gemini
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"crypto/sha512"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// KnownHosts maps hosts to fingerprints.
|
|
||||||
type KnownHosts map[string]Fingerprint
|
|
||||||
|
|
||||||
// KnownHostsFile represents a list of known hosts optionally loaded from a file.
|
|
||||||
// The zero value for KnownHostsFile represents an empty list ready to use.
|
|
||||||
//
|
|
||||||
// KnownHostsFile is safe for concurrent use by multiple goroutines.
|
|
||||||
type KnownHostsFile struct {
|
|
||||||
KnownHosts
|
|
||||||
out io.Writer
|
|
||||||
mu sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetOutput sets the output to which new known hosts will be written to.
|
|
||||||
func (k *KnownHostsFile) SetOutput(w io.Writer) {
|
|
||||||
k.mu.Lock()
|
|
||||||
defer k.mu.Unlock()
|
|
||||||
k.out = w
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add adds a known host to the list of known hosts.
|
|
||||||
func (k *KnownHostsFile) Add(hostname string, fingerprint Fingerprint) {
|
|
||||||
k.mu.Lock()
|
|
||||||
defer k.mu.Unlock()
|
|
||||||
if k.KnownHosts == nil {
|
|
||||||
k.KnownHosts = KnownHosts{}
|
|
||||||
}
|
|
||||||
k.KnownHosts[hostname] = fingerprint
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lookup returns the fingerprint of the certificate corresponding to
|
|
||||||
// the given hostname.
|
|
||||||
func (k *KnownHostsFile) Lookup(hostname string) (Fingerprint, bool) {
|
|
||||||
k.mu.RLock()
|
|
||||||
defer k.mu.RUnlock()
|
|
||||||
c, ok := k.KnownHosts[hostname]
|
|
||||||
return c, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write writes a known hosts entry to the configured output.
|
|
||||||
func (k *KnownHostsFile) Write(hostname string, fingerprint Fingerprint) {
|
|
||||||
k.mu.RLock()
|
|
||||||
defer k.mu.RUnlock()
|
|
||||||
if k.out != nil {
|
|
||||||
k.writeKnownHost(k.out, hostname, fingerprint)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteAll writes all of the known hosts to the provided io.Writer.
|
|
||||||
func (k *KnownHostsFile) WriteAll(w io.Writer) error {
|
|
||||||
k.mu.RLock()
|
|
||||||
defer k.mu.RUnlock()
|
|
||||||
for h, c := range k.KnownHosts {
|
|
||||||
if _, err := k.writeKnownHost(w, h, c); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeKnownHost writes a known host to the provided io.Writer.
|
|
||||||
func (k *KnownHostsFile) writeKnownHost(w io.Writer, hostname string, f Fingerprint) (int, error) {
|
|
||||||
return fmt.Fprintf(w, "%s %s %s %d\n", hostname, f.Algorithm, f.Hex, f.Expires.Unix())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load loads the known hosts from the provided path.
|
|
||||||
// It creates the file if it does not exist.
|
|
||||||
// New known hosts will be appended to the file.
|
|
||||||
func (k *KnownHostsFile) Load(path string) error {
|
|
||||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
k.Parse(f)
|
|
||||||
k.SetOutput(f)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse parses the provided reader and adds the parsed known hosts to the list.
|
|
||||||
// Invalid entries are ignored.
|
|
||||||
func (k *KnownHostsFile) Parse(r io.Reader) {
|
|
||||||
k.mu.Lock()
|
|
||||||
defer k.mu.Unlock()
|
|
||||||
if k.KnownHosts == nil {
|
|
||||||
k.KnownHosts = map[string]Fingerprint{}
|
|
||||||
}
|
|
||||||
scanner := bufio.NewScanner(r)
|
|
||||||
for scanner.Scan() {
|
|
||||||
text := scanner.Text()
|
|
||||||
parts := strings.Split(text, " ")
|
|
||||||
if len(parts) < 4 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
hostname := parts[0]
|
|
||||||
algorithm := parts[1]
|
|
||||||
if algorithm != "SHA-512" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
hex := parts[2]
|
|
||||||
|
|
||||||
unix, err := strconv.ParseInt(parts[3], 10, 0)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
expires := time.Unix(unix, 0)
|
|
||||||
|
|
||||||
k.KnownHosts[hostname] = Fingerprint{
|
|
||||||
Algorithm: algorithm,
|
|
||||||
Hex: hex,
|
|
||||||
Expires: expires,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fingerprint represents a fingerprint using a certain algorithm.
|
|
||||||
type Fingerprint struct {
|
|
||||||
Algorithm string // fingerprint algorithm e.g. SHA-512
|
|
||||||
Hex string // fingerprint in hexadecimal, with ':' between each octet
|
|
||||||
Expires time.Time // unix time of the fingerprint expiration date
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFingerprint returns the SHA-512 fingerprint of the provided raw data.
|
|
||||||
func NewFingerprint(raw []byte, expires time.Time) Fingerprint {
|
|
||||||
sum512 := sha512.Sum512(raw)
|
|
||||||
var b strings.Builder
|
|
||||||
for i, f := range sum512 {
|
|
||||||
if i > 0 {
|
|
||||||
b.WriteByte(':')
|
|
||||||
}
|
|
||||||
fmt.Fprintf(&b, "%02X", f)
|
|
||||||
}
|
|
||||||
return Fingerprint{
|
|
||||||
Algorithm: "SHA-512",
|
|
||||||
Hex: b.String(),
|
|
||||||
Expires: expires,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
344
tofu/tofu.go
Normal file
344
tofu/tofu.go
Normal file
@@ -0,0 +1,344 @@
|
|||||||
|
// Package tofu implements trust on first use using hosts and fingerprints.
|
||||||
|
package tofu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"crypto/sha512"
|
||||||
|
"crypto/x509"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// KnownHosts represents a list of known hosts.
|
||||||
|
// The zero value for KnownHosts represents an empty list ready to use.
|
||||||
|
//
|
||||||
|
// KnownHosts is safe for concurrent use by multiple goroutines.
|
||||||
|
type KnownHosts struct {
|
||||||
|
hosts map[string]Host
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add adds a host to the list of known hosts.
|
||||||
|
func (k *KnownHosts) Add(h Host) error {
|
||||||
|
k.mu.Lock()
|
||||||
|
defer k.mu.Unlock()
|
||||||
|
if k.hosts == nil {
|
||||||
|
k.hosts = map[string]Host{}
|
||||||
|
}
|
||||||
|
|
||||||
|
k.hosts[h.Hostname] = h
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup returns the known host entry corresponding to the given hostname.
|
||||||
|
func (k *KnownHosts) Lookup(hostname string) (Host, bool) {
|
||||||
|
k.mu.RLock()
|
||||||
|
defer k.mu.RUnlock()
|
||||||
|
c, ok := k.hosts[hostname]
|
||||||
|
return c, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entries returns the known host entries sorted by hostname.
|
||||||
|
func (k *KnownHosts) Entries() []Host {
|
||||||
|
keys := make([]string, 0, len(k.hosts))
|
||||||
|
for key := range k.hosts {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
hosts := make([]Host, 0, len(k.hosts))
|
||||||
|
for _, key := range keys {
|
||||||
|
hosts = append(hosts, k.hosts[key])
|
||||||
|
}
|
||||||
|
return hosts
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo writes the list of known hosts to the provided io.Writer.
|
||||||
|
func (k *KnownHosts) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
k.mu.RLock()
|
||||||
|
defer k.mu.RUnlock()
|
||||||
|
|
||||||
|
var written int
|
||||||
|
|
||||||
|
bw := bufio.NewWriter(w)
|
||||||
|
for _, h := range k.hosts {
|
||||||
|
n, err := bw.WriteString(h.String())
|
||||||
|
written += n
|
||||||
|
if err != nil {
|
||||||
|
return int64(written), err
|
||||||
|
}
|
||||||
|
|
||||||
|
bw.WriteByte('\n')
|
||||||
|
written += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64(written), bw.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load loads the known hosts entries from the provided path.
|
||||||
|
func (k *KnownHosts) Load(path string) error {
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
return k.Parse(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse parses the provided io.Reader and adds the parsed hosts to the list.
|
||||||
|
// Invalid entries are ignored.
|
||||||
|
//
|
||||||
|
// For more control over errors encountered during parsing, use bufio.Scanner
|
||||||
|
// in combination with ParseHost. For example:
|
||||||
|
//
|
||||||
|
// var knownHosts tofu.KnownHosts
|
||||||
|
// scanner := bufio.NewScanner(r)
|
||||||
|
// for scanner.Scan() {
|
||||||
|
// host, err := tofu.ParseHost(scanner.Bytes())
|
||||||
|
// if err != nil {
|
||||||
|
// // handle error
|
||||||
|
// } else {
|
||||||
|
// knownHosts.Add(host)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// err := scanner.Err()
|
||||||
|
// if err != nil {
|
||||||
|
// // handle error
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
func (k *KnownHosts) Parse(r io.Reader) error {
|
||||||
|
k.mu.Lock()
|
||||||
|
defer k.mu.Unlock()
|
||||||
|
|
||||||
|
if k.hosts == nil {
|
||||||
|
k.hosts = map[string]Host{}
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
for scanner.Scan() {
|
||||||
|
text := scanner.Bytes()
|
||||||
|
if len(text) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
h, err := ParseHost(text)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
k.hosts[h.Hostname] = h
|
||||||
|
}
|
||||||
|
|
||||||
|
return scanner.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TOFU implements basic trust on first use.
|
||||||
|
//
|
||||||
|
// If the host is not on file, it is added to the list.
|
||||||
|
// If the host on file is expired, it is replaced with the provided host.
|
||||||
|
// If the fingerprint does not match the one on file, an error is returned.
|
||||||
|
func (k *KnownHosts) TOFU(hostname string, cert *x509.Certificate) error {
|
||||||
|
host := NewHost(hostname, cert.Raw, cert.NotAfter)
|
||||||
|
|
||||||
|
knownHost, ok := k.Lookup(hostname)
|
||||||
|
if !ok || time.Now().After(knownHost.Expires) {
|
||||||
|
k.Add(host)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check fingerprint
|
||||||
|
if !bytes.Equal(knownHost.Fingerprint, host.Fingerprint) {
|
||||||
|
return fmt.Errorf("fingerprint for %q does not match", hostname)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HostWriter writes host entries to an io.WriteCloser.
|
||||||
|
//
|
||||||
|
// HostWriter is safe for concurrent use by multiple goroutines.
|
||||||
|
type HostWriter struct {
|
||||||
|
bw *bufio.Writer
|
||||||
|
cl io.Closer
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHostWriter returns a new host writer that writes to
|
||||||
|
// the provided io.WriteCloser.
|
||||||
|
func NewHostWriter(w io.WriteCloser) *HostWriter {
|
||||||
|
return &HostWriter{
|
||||||
|
bw: bufio.NewWriter(w),
|
||||||
|
cl: w,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHostsFile returns a new host writer that appends to the file at the given path.
|
||||||
|
// The file is created if it does not exist.
|
||||||
|
func NewHostsFile(path string) (*HostWriter, error) {
|
||||||
|
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewHostWriter(f), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteHost writes the host to the underlying io.Writer.
|
||||||
|
func (h *HostWriter) WriteHost(host Host) error {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
|
h.bw.WriteString(host.String())
|
||||||
|
h.bw.WriteByte('\n')
|
||||||
|
|
||||||
|
if err := h.bw.Flush(); err != nil {
|
||||||
|
return fmt.Errorf("failed to write host: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the underlying io.Closer.
|
||||||
|
func (h *HostWriter) Close() error {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
return h.cl.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host represents a host entry with a fingerprint using a certain algorithm.
|
||||||
|
type Host struct {
|
||||||
|
Hostname string // hostname
|
||||||
|
Algorithm string // fingerprint algorithm e.g. SHA-512
|
||||||
|
Fingerprint Fingerprint // fingerprint
|
||||||
|
Expires time.Time // unix time of the fingerprint expiration date
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHost returns a new host with a SHA-512 fingerprint of
|
||||||
|
// the provided raw data.
|
||||||
|
func NewHost(hostname string, raw []byte, expires time.Time) Host {
|
||||||
|
sum := sha512.Sum512(raw)
|
||||||
|
|
||||||
|
return Host{
|
||||||
|
Hostname: hostname,
|
||||||
|
Algorithm: "SHA-512",
|
||||||
|
Fingerprint: sum[:],
|
||||||
|
Expires: expires,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseHost parses a host from the provided text.
|
||||||
|
func ParseHost(text []byte) (Host, error) {
|
||||||
|
var h Host
|
||||||
|
err := h.UnmarshalText(text)
|
||||||
|
return h, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns a string representation of the host.
|
||||||
|
func (h Host) String() string {
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString(h.Hostname)
|
||||||
|
b.WriteByte(' ')
|
||||||
|
b.WriteString(h.Algorithm)
|
||||||
|
b.WriteByte(' ')
|
||||||
|
b.WriteString(h.Fingerprint.String())
|
||||||
|
b.WriteByte(' ')
|
||||||
|
b.WriteString(strconv.FormatInt(h.Expires.Unix(), 10))
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText unmarshals the host from the provided text.
|
||||||
|
func (h *Host) UnmarshalText(text []byte) error {
|
||||||
|
const format = "hostname algorithm hex-fingerprint expiry-unix-ts"
|
||||||
|
|
||||||
|
parts := bytes.Split(text, []byte(" "))
|
||||||
|
if len(parts) != 4 {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"expected the format %q", format)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(parts[0]) == 0 {
|
||||||
|
return errors.New("empty hostname")
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Hostname = string(parts[0])
|
||||||
|
|
||||||
|
algorithm := string(parts[1])
|
||||||
|
if algorithm != "SHA-512" {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"unsupported algorithm %q", algorithm)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Algorithm = algorithm
|
||||||
|
|
||||||
|
fingerprint := make([]byte, 0, sha512.Size)
|
||||||
|
scanner := bufio.NewScanner(bytes.NewReader(parts[2]))
|
||||||
|
scanner.Split(scanFingerprint)
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
b, err := strconv.ParseUint(scanner.Text(), 16, 8)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse fingerprint hash: %w", err)
|
||||||
|
}
|
||||||
|
fingerprint = append(fingerprint, byte(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fingerprint) != sha512.Size {
|
||||||
|
return fmt.Errorf("invalid fingerprint size %d, expected %d",
|
||||||
|
len(fingerprint), sha512.Size)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Fingerprint = fingerprint
|
||||||
|
|
||||||
|
unix, err := strconv.ParseInt(string(parts[3]), 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"invalid unix timestamp: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Expires = time.Unix(unix, 0)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func scanFingerprint(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||||
|
if atEOF && len(data) == 0 {
|
||||||
|
return 0, nil, nil
|
||||||
|
}
|
||||||
|
if i := bytes.IndexByte(data, ':'); i >= 0 {
|
||||||
|
// We have a full newline-terminated line.
|
||||||
|
return i + 1, data[0:i], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we're at EOF, we have a final, non-terminated hex byte
|
||||||
|
if atEOF {
|
||||||
|
return len(data), data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request more data.
|
||||||
|
return 0, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fingerprint represents a fingerprint.
|
||||||
|
type Fingerprint []byte
|
||||||
|
|
||||||
|
// String returns a string representation of the fingerprint.
|
||||||
|
func (f Fingerprint) String() string {
|
||||||
|
var sb strings.Builder
|
||||||
|
|
||||||
|
for i, b := range f {
|
||||||
|
if i > 0 {
|
||||||
|
sb.WriteByte(':')
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(&sb, "%02X", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user