aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authoradnano <[email protected]>2020-09-26 16:38:26 -0400
committeradnano <[email protected]>2020-09-26 16:38:26 -0400
commit92a1dbbc0c6af06f8e463fce53adb9de7f6b0aea (patch)
treed9bef41efef59fb6377a7af92d6ef0b92eea3782 /examples
parentAdd preliminary CertificateStore API (diff)
downloadgo-gemini-92a1dbbc0c6af06f8e463fce53adb9de7f6b0aea.tar.xz
go-gemini-92a1dbbc0c6af06f8e463fce53adb9de7f6b0aea.zip
Implement file server
Diffstat (limited to 'examples')
-rw-r--r--examples/client/client.go38
-rw-r--r--examples/server/server.go6
2 files changed, 39 insertions, 5 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])
diff --git a/examples/server/server.go b/examples/server/server.go
index f99c6cd..03dfc2b 100644
--- a/examples/server/server.go
+++ b/examples/server/server.go
@@ -23,11 +23,7 @@ func main() {
}
mux := &gemini.ServeMux{}
- mux.HandleFunc("/", func(rw *gemini.ResponseWriter, req *gemini.Request) {
- rw.WriteHeader(gemini.StatusSuccess, "text/gemini")
- rw.Write([]byte("You requested " + req.URL.String()))
- log.Printf("Request from %s for %s", req.RemoteAddr.String(), req.URL)
- })
+ mux.Handle("/", gemini.FileServer(gemini.Dir("/var/www")))
server := gemini.Server{
Handler: mux,