Remove (*KnownHosts).LoadDefault function
This commit is contained in:
parent
5332dc6280
commit
76dfe257f1
@ -47,7 +47,7 @@ type Client struct {
|
|||||||
// the request of a server.
|
// the request of a server.
|
||||||
// If CreateCertificate is nil or the returned error is not nil,
|
// If CreateCertificate is nil or the returned error is not nil,
|
||||||
// the request will not be sent again and the response will be returned.
|
// the request will not be sent again and the response will be returned.
|
||||||
CreateCertificate func(hostname, path string) (tls.Certificate, error)
|
CreateCertificate func(scope, path string) (tls.Certificate, error)
|
||||||
|
|
||||||
// TrustCertificate is called to determine whether the client
|
// TrustCertificate is called to determine whether the client
|
||||||
// should trust a certificate it has not seen before.
|
// should trust a certificate it has not seen before.
|
||||||
|
52
tofu.go
52
tofu.go
@ -7,7 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -28,28 +27,9 @@ type KnownHosts struct {
|
|||||||
file *os.File
|
file *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadDefault loads the known hosts from the default known hosts path, which is
|
|
||||||
// $XDG_DATA_HOME/gemini/known_hosts.
|
|
||||||
// It creates the path and any of its parent directories if they do not exist.
|
|
||||||
// KnownHosts will append to the file whenever a certificate is added.
|
|
||||||
func (k *KnownHosts) LoadDefault() error {
|
|
||||||
path, err := defaultKnownHostsPath()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return k.Load(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load loads the known hosts from the provided path.
|
// Load loads the known hosts from the provided path.
|
||||||
// It creates the path and any of its parent directories if they do not exist.
|
// New known hosts will be appended to the file.
|
||||||
// KnownHosts will append to the file whenever a certificate is added.
|
|
||||||
func (k *KnownHosts) Load(path string) error {
|
func (k *KnownHosts) Load(path string) error {
|
||||||
if dir := filepath.Dir(path); dir != "." {
|
|
||||||
err := os.MkdirAll(dir, 0755)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0644)
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -71,12 +51,6 @@ func (k *KnownHosts) Add(hostname string, cert *x509.Certificate) {
|
|||||||
k.add(hostname, cert, true)
|
k.add(hostname, cert, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddTemporary adds a certificate to the list of known hosts
|
|
||||||
// without writing it to the known hosts file.
|
|
||||||
func (k *KnownHosts) AddTemporary(hostname string, cert *x509.Certificate) {
|
|
||||||
k.add(hostname, cert, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *KnownHosts) add(hostname string, cert *x509.Certificate, write bool) {
|
func (k *KnownHosts) add(hostname string, cert *x509.Certificate, write bool) {
|
||||||
if k.hosts == nil {
|
if k.hosts == nil {
|
||||||
k.hosts = map[string]Fingerprint{}
|
k.hosts = map[string]Fingerprint{}
|
||||||
@ -164,27 +138,3 @@ func NewFingerprint(cert *x509.Certificate) Fingerprint {
|
|||||||
Expires: cert.NotAfter.Unix(),
|
Expires: cert.NotAfter.Unix(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultKnownHostsPath returns the default known_hosts path.
|
|
||||||
// The default path is $XDG_DATA_HOME/gemini/known_hosts
|
|
||||||
func defaultKnownHostsPath() (string, error) {
|
|
||||||
dataDir, err := userDataDir()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return filepath.Join(dataDir, "gemini", "known_hosts"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// userDataDir returns the user data directory.
|
|
||||||
func userDataDir() (string, error) {
|
|
||||||
dataDir, ok := os.LookupEnv("XDG_DATA_HOME")
|
|
||||||
if ok {
|
|
||||||
return dataDir, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
home, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return filepath.Join(home, ".local", "share"), nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user