2020-10-12 20:34:52 +00:00
|
|
|
/*
|
|
|
|
Package gmi implements the Gemini protocol.
|
|
|
|
|
2020-10-12 20:56:26 +00:00
|
|
|
Send makes a Gemini request with the default client:
|
2020-10-12 20:34:52 +00:00
|
|
|
|
|
|
|
req := gmi.NewRequest("gemini://example.com")
|
2020-10-12 20:49:35 +00:00
|
|
|
resp, err := gmi.Send(req)
|
2020-10-12 20:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
}
|
2020-10-12 20:49:35 +00:00
|
|
|
// ...
|
2020-10-12 20:34:52 +00:00
|
|
|
|
2020-10-12 20:56:26 +00:00
|
|
|
For control over client behavior, create a custom Client:
|
2020-10-12 20:34:52 +00:00
|
|
|
|
|
|
|
var client gmi.Client
|
2020-10-12 20:56:26 +00:00
|
|
|
resp, err := client.Send(req)
|
2020-10-12 20:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
}
|
2020-10-12 20:56:26 +00:00
|
|
|
// ...
|
2020-10-12 20:34:52 +00:00
|
|
|
|
|
|
|
The default client loads known hosts from "$XDG_DATA_HOME/gemini/known_hosts".
|
|
|
|
Custom clients can load their own list of known hosts:
|
|
|
|
|
2020-10-12 20:56:26 +00:00
|
|
|
err := client.KnownHosts.Load("path/to/my/known_hosts")
|
2020-10-12 20:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
}
|
|
|
|
|
|
|
|
Clients can control when to trust certificates with TrustCertificate:
|
|
|
|
|
|
|
|
client.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *gmi.KnownHosts) error {
|
|
|
|
return knownHosts.Lookup(hostname, cert)
|
|
|
|
}
|
|
|
|
|
2020-10-12 20:49:35 +00:00
|
|
|
If a server responds with StatusCertificateRequired, the default client will generate a certificate and resend the request with it. Custom clients can do so in GetCertificate:
|
2020-10-12 20:34:52 +00:00
|
|
|
|
|
|
|
client.GetCertificate = func(hostname string, store *gmi.CertificateStore) *tls.Certificate {
|
|
|
|
// If the certificate is in the store, return it
|
|
|
|
if cert, err := store.Lookup(hostname); err == nil {
|
|
|
|
return &cert
|
|
|
|
}
|
|
|
|
// Otherwise, generate a certificate
|
|
|
|
duration := time.Hour
|
|
|
|
cert, err := gmi.NewCertificate(hostname, duration)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Store and return the certificate
|
|
|
|
store.Add(hostname, cert)
|
|
|
|
return &cert
|
|
|
|
}
|
|
|
|
|
|
|
|
Server is a Gemini server.
|
|
|
|
|
|
|
|
var server gmi.Server
|
|
|
|
|
|
|
|
Servers must be configured with certificates:
|
|
|
|
|
2020-10-12 20:56:26 +00:00
|
|
|
err := server.CertificateStore.Load("/var/lib/gemini/certs")
|
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
}
|
2020-10-12 20:34:52 +00:00
|
|
|
|
|
|
|
Servers can accept requests for multiple hosts and schemes:
|
|
|
|
|
2020-10-14 00:22:12 +00:00
|
|
|
server.HandleFunc("example.com", func(w *gmi.ResponseWriter, r *gmi.Request) {
|
|
|
|
fmt.Fprint(w, "Welcome to example.com")
|
2020-10-12 20:34:52 +00:00
|
|
|
})
|
2020-10-14 00:22:12 +00:00
|
|
|
server.HandleFunc("example.org", func(w *gmi.ResponseWriter, r *gmi.Request) {
|
|
|
|
fmt.Fprint(w, "Welcome to example.org")
|
2020-10-12 20:34:52 +00:00
|
|
|
})
|
2020-10-14 00:22:12 +00:00
|
|
|
server.HandleSchemeFunc("http", "example.net", func(w *gmi.ResponseWriter, r *gmi.Request) {
|
|
|
|
fmt.Fprint(w, "Proxied content from example.net")
|
2020-10-12 20:34:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
To start the server, call ListenAndServe:
|
|
|
|
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
package gmi
|