Update examples/client.go
This commit is contained in:
parent
3fd55c5cee
commit
1a3974b3a3
|
@ -23,16 +23,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
hosts tofu.KnownHostsFile
|
hosts tofu.KnownHosts
|
||||||
|
hostsfile *tofu.HostWriter
|
||||||
scanner *bufio.Scanner
|
scanner *bufio.Scanner
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Load known hosts file
|
// Load known hosts file
|
||||||
path := filepath.Join(xdg.DataHome(), "gemini", "known_hosts")
|
path := filepath.Join(xdg.DataHome(), "gemini", "known_hosts")
|
||||||
err := hosts.Open(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)
|
||||||
|
@ -48,7 +54,7 @@ Otherwise, this should be safe to trust.
|
||||||
=> `
|
=> `
|
||||||
|
|
||||||
func trustCertificate(hostname string, cert *x509.Certificate) error {
|
func trustCertificate(hostname string, cert *x509.Certificate) error {
|
||||||
host := tofu.NewKnownHost(hostname, 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) {
|
||||||
|
@ -64,8 +70,10 @@ func trustCertificate(hostname string, cert *x509.Certificate) error {
|
||||||
switch scanner.Text() {
|
switch scanner.Text() {
|
||||||
case "t":
|
case "t":
|
||||||
hosts.Add(host)
|
hosts.Add(host)
|
||||||
|
hostsfile.WriteHost(host)
|
||||||
return nil
|
return nil
|
||||||
case "o":
|
case "o":
|
||||||
|
hosts.Add(host)
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return errors.New("certificate not trusted")
|
return errors.New("certificate not trusted")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user