diff options
| author | adnano <[email protected]> | 2020-09-26 16:38:26 -0400 |
|---|---|---|
| committer | adnano <[email protected]> | 2020-09-26 16:38:26 -0400 |
| commit | 92a1dbbc0c6af06f8e463fce53adb9de7f6b0aea (patch) | |
| tree | d9bef41efef59fb6377a7af92d6ef0b92eea3782 /examples/client | |
| parent | Add preliminary CertificateStore API (diff) | |
| download | go-gemini-92a1dbbc0c6af06f8e463fce53adb9de7f6b0aea.tar.xz go-gemini-92a1dbbc0c6af06f8e463fce53adb9de7f6b0aea.zip | |
Implement file server
Diffstat (limited to 'examples/client')
| -rw-r--r-- | examples/client/client.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/client/client.go b/examples/client/client.go index 687068a..5ac68ed 100644 --- a/examples/client/client.go +++ b/examples/client/client.go @@ -5,6 +5,7 @@ package main import ( "bufio" "crypto/tls" + "crypto/x509" "fmt" "log" "os" @@ -28,6 +29,29 @@ func init() { KnownHosts: knownHosts, } + client.TrustCertificate = func(cert *x509.Certificate, knownHosts *gemini.KnownHosts) error { + err := knownHosts.Lookup(cert) + if err != nil { + switch err { + case gemini.ErrCertificateNotTrusted: + // Alert the user that the certificate is not trusted + fmt.Println("error: certificate is not trusted!") + fmt.Println("This could indicate a Man-in-the-Middle attack.") + case gemini.ErrCertificateUnknown: + // Prompt the user to trust the certificate + if userTrustsCertificateTemporarily() { + // Temporarily trust the certificate + return nil + } else if userTrustsCertificatePermanently() { + // Add the certificate to the known hosts file + knownHosts.Add(cert) + return nil + } + } + } + return err + } + // Configure a client side certificate. // To generate a certificate, run: // @@ -81,6 +105,20 @@ func makeRequest(url string) { } } +func userTrustsCertificateTemporarily() bool { + fmt.Println("Do you want to trust the certificate temporarily? (y/n)") + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + return scanner.Text() == "y" +} + +func userTrustsCertificatePermanently() bool { + fmt.Println("How about permanently? (y/n)") + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + return scanner.Text() == "y" +} + func main() { if len(os.Args) < 2 { log.Fatalf("usage: %s gemini://...", os.Args[0]) |