client: Follow redirects

This commit is contained in:
Adnan Maolood 2020-10-27 22:12:10 -04:00
parent b84811668c
commit fc72224ce9
3 changed files with 56 additions and 30 deletions

View File

@ -5,6 +5,7 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"net" "net"
"net/url"
) )
// Client represents a Gemini client. // Client represents a Gemini client.
@ -17,6 +18,12 @@ type Client struct {
// a certificate. // a certificate.
CertificateStore CertificateStore CertificateStore CertificateStore
// CheckRedirect, if not nil, will be called to determine whether
// to follow a redirect.
// If CheckRedirect is nil, a default policy of no more than 5 consecutive
// redirects will be enforced.
CheckRedirect func(req *Request, via []*Request) error
// GetCertificate, if not nil, will be called when a server requests a certificate. // GetCertificate, if not nil, will be called when a server requests a certificate.
// The returned certificate will be used when sending the request again. // The returned certificate will be used when sending the request again.
// If the certificate is nil, the request will not be sent again and // If the certificate is nil, the request will not be sent again and
@ -40,6 +47,10 @@ func (c *Client) Get(url string) (*Response, error) {
// Do performs a Gemini request and returns a Gemini response. // Do performs a Gemini request and returns a Gemini response.
func (c *Client) Do(req *Request) (*Response, error) { func (c *Client) Do(req *Request) (*Response, error) {
return c.do(req, nil)
}
func (c *Client) do(req *Request, via []*Request) (*Response, error) {
// Connect to the host // Connect to the host
config := &tls.Config{ config := &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,
@ -105,6 +116,31 @@ func (c *Client) Do(req *Request) (*Response, error) {
return c.Do(req) return c.Do(req)
} }
} }
} else if resp.Status.Class() == StatusClassRedirect {
if via == nil {
via = []*Request{}
}
via = append(via, req)
target, err := url.Parse(resp.Meta)
if err != nil {
return resp, err
}
target = req.URL.ResolveReference(target)
redirect, err := NewRequestFromURL(target)
if err != nil {
return resp, err
}
if c.CheckRedirect != nil {
if err := c.CheckRedirect(redirect, via); err != nil {
return resp, err
}
} else if len(via) > 5 {
// Default policy of no more than 5 redirects
return resp, ErrTooManyRedirects
}
return c.do(redirect, via)
} }
return resp, nil return resp, nil
} }

View File

@ -12,26 +12,25 @@ import (
"os" "os"
"time" "time"
gmi "git.sr.ht/~adnano/go-gemini" "git.sr.ht/~adnano/go-gemini"
) )
var ( var (
scanner = bufio.NewScanner(os.Stdin) scanner = bufio.NewScanner(os.Stdin)
client = &gmi.Client{} client = &gemini.Client{}
) )
func init() { func init() {
// Initialize the client client.KnownHosts.LoadDefault()
client.KnownHosts.LoadDefault() // Load known hosts client.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
client.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *gmi.KnownHosts) error {
err := knownHosts.Lookup(hostname, cert) err := knownHosts.Lookup(hostname, cert)
if err != nil { if err != nil {
switch err { switch err {
case gmi.ErrCertificateNotTrusted: case gemini.ErrCertificateNotTrusted:
// Alert the user that the certificate is not trusted // Alert the user that the certificate is not trusted
fmt.Printf("Warning: Certificate for %s is not trusted!\n", hostname) fmt.Printf("Warning: Certificate for %s is not trusted!\n", hostname)
fmt.Println("This could indicate a Man-in-the-Middle attack.") fmt.Println("This could indicate a Man-in-the-Middle attack.")
case gmi.ErrCertificateUnknown: case gemini.ErrCertificateUnknown:
// Prompt the user to trust the certificate // Prompt the user to trust the certificate
trust := trustCertificate(cert) trust := trustCertificate(cert)
switch trust { switch trust {
@ -48,7 +47,7 @@ func init() {
} }
return err return err
} }
client.GetCertificate = func(hostname string, store *gmi.CertificateStore) *tls.Certificate { client.GetCertificate = func(hostname string, store *gemini.CertificateStore) *tls.Certificate {
// If the certificate is in the store, return it // If the certificate is in the store, return it
if cert, err := store.Lookup(hostname); err == nil { if cert, err := store.Lookup(hostname); err == nil {
return cert return cert
@ -56,7 +55,7 @@ func init() {
// Otherwise, generate a certificate // Otherwise, generate a certificate
fmt.Println("Generating client certificate for", hostname) fmt.Println("Generating client certificate for", hostname)
duration := time.Hour duration := time.Hour
cert, err := gmi.NewCertificate(hostname, duration) cert, err := gemini.NewCertificate(hostname, duration)
if err != nil { if err != nil {
return nil return nil
} }
@ -67,20 +66,19 @@ func init() {
} }
// sendRequest sends a request to the given URL. // sendRequest sends a request to the given URL.
func sendRequest(req *gmi.Request) error { func sendRequest(req *gemini.Request) error {
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return err return err
} }
// TODO: More fine-grained analysis of the status code.
switch resp.Status.Class() { switch resp.Status.Class() {
case gmi.StatusClassInput: case gemini.StatusClassInput:
fmt.Printf("%s: ", resp.Meta) fmt.Printf("%s: ", resp.Meta)
scanner.Scan() scanner.Scan()
req.URL.RawQuery = url.QueryEscape(scanner.Text()) req.URL.RawQuery = url.QueryEscape(scanner.Text())
return sendRequest(req) return sendRequest(req)
case gmi.StatusClassSuccess: case gemini.StatusClassSuccess:
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
@ -88,23 +86,14 @@ func sendRequest(req *gmi.Request) error {
} }
fmt.Print(string(body)) fmt.Print(string(body))
return nil return nil
case gmi.StatusClassRedirect: case gemini.StatusClassRedirect:
fmt.Println("Redirecting to", resp.Meta) // This should not happen unless CheckRedirect returns false.
target, err := url.Parse(resp.Meta) return fmt.Errorf("Failed to redirect to %s", resp.Meta)
if err != nil { case gemini.StatusClassTemporaryFailure:
return err
}
// TODO: Prompt the user if the redirect is to another domain.
redirect, err := gmi.NewRequestFromURL(req.URL.ResolveReference(target))
if err != nil {
return err
}
return sendRequest(redirect)
case gmi.StatusClassTemporaryFailure:
return fmt.Errorf("Temporary failure: %s", resp.Meta) return fmt.Errorf("Temporary failure: %s", resp.Meta)
case gmi.StatusClassPermanentFailure: case gemini.StatusClassPermanentFailure:
return fmt.Errorf("Permanent failure: %s", resp.Meta) return fmt.Errorf("Permanent failure: %s", resp.Meta)
case gmi.StatusClassCertificateRequired: case gemini.StatusClassCertificateRequired:
// Note that this should not happen unless the server responds with // Note that this should not happen unless the server responds with
// CertificateRequired even after we send a certificate. // CertificateRequired even after we send a certificate.
// CertificateNotAuthorized and CertificateNotValid are handled here. // CertificateNotAuthorized and CertificateNotValid are handled here.
@ -131,7 +120,7 @@ Otherwise, this should be safe to trust.
=> ` => `
func trustCertificate(cert *x509.Certificate) trust { func trustCertificate(cert *x509.Certificate) trust {
fmt.Printf(trustPrompt, gmi.Fingerprint(cert)) fmt.Printf(trustPrompt, gemini.Fingerprint(cert))
scanner.Scan() scanner.Scan()
switch scanner.Text() { switch scanner.Text() {
case "t": case "t":
@ -150,7 +139,7 @@ func main() {
} }
url := os.Args[1] url := os.Args[1]
req, err := gmi.NewRequest(url) req, err := gemini.NewRequest(url)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)

View File

@ -20,6 +20,7 @@ var (
ErrNotAFile = errors.New("gemini: not a file") ErrNotAFile = errors.New("gemini: not a file")
ErrNotAGeminiURL = errors.New("gemini: not a Gemini URL") ErrNotAGeminiURL = errors.New("gemini: not a Gemini URL")
ErrBodyNotAllowed = errors.New("gemini: response status code does not allow for body") ErrBodyNotAllowed = errors.New("gemini: response status code does not allow for body")
ErrTooManyRedirects = errors.New("gemini: too many redirects")
) )
// DefaultClient is the default client. It is used by Get and Do. // DefaultClient is the default client. It is used by Get and Do.