2020-09-21 13:48:42 -06:00
|
|
|
package gemini
|
|
|
|
|
|
|
|
import (
|
2020-09-21 15:58:36 -06:00
|
|
|
"bufio"
|
2020-09-21 13:48:42 -06:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-09-21 15:23:51 -06:00
|
|
|
ErrProtocol = errors.New("Protocol error")
|
2020-09-21 13:48:42 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a Gemini client.
|
2020-09-21 17:17:10 -06:00
|
|
|
type Client struct{}
|
2020-09-21 13:48:42 -06:00
|
|
|
|
2020-09-21 15:23:51 -06:00
|
|
|
// Request makes a request for the provided URL. The host is inferred from the URL.
|
|
|
|
func (c *Client) Request(url string) (*Response, error) {
|
2020-09-21 13:48:42 -06:00
|
|
|
req, err := NewRequest(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.Do(req)
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:23:51 -06:00
|
|
|
// ProxyRequest requests the provided URL from the provided host.
|
|
|
|
func (c *Client) ProxyRequest(host, url string) (*Response, error) {
|
2020-09-21 13:48:42 -06:00
|
|
|
req, err := NewProxyRequest(host, url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request is a Gemini request.
|
2020-09-21 17:17:10 -06:00
|
|
|
//
|
|
|
|
// A Request can optionally be configured with a client certificate. Example:
|
|
|
|
//
|
|
|
|
// req := NewRequest(url)
|
|
|
|
// cert, err := tls.LoadX509KeyPair("client.crt", "client.key")
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// req.Certificates = append(req.Certificates, cert)
|
|
|
|
//
|
2020-09-21 13:48:42 -06:00
|
|
|
type Request struct {
|
2020-09-21 17:17:10 -06:00
|
|
|
Host string // host or host:port
|
|
|
|
URL *url.URL // the requested URL
|
|
|
|
Certificates []tls.Certificate // client certificates
|
2020-09-21 13:48:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRequest returns a new request. The host is inferred from the provided url.
|
|
|
|
func NewRequest(rawurl string) (*Request, error) {
|
|
|
|
u, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore UserInfo if present
|
|
|
|
u.User = nil
|
|
|
|
|
|
|
|
return &Request{
|
|
|
|
Host: u.Host,
|
|
|
|
URL: u,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProxyRequest makes a new request using the provided host.
|
|
|
|
func NewProxyRequest(host, rawurl string) (*Request, error) {
|
|
|
|
u, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore UserInfo if present
|
|
|
|
u.User = nil
|
|
|
|
|
|
|
|
return &Request{
|
|
|
|
Host: host,
|
|
|
|
URL: u,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:36:09 -06:00
|
|
|
// Do sends a Gemini request and returns a Gemini response.
|
2020-09-21 13:48:42 -06:00
|
|
|
func (c *Client) Do(req *Request) (*Response, error) {
|
|
|
|
host := req.Host
|
|
|
|
if strings.LastIndex(host, ":") == -1 {
|
|
|
|
// The default port is 1965
|
|
|
|
host += ":1965"
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:17:10 -06:00
|
|
|
config := tls.Config{}
|
2020-09-21 16:21:42 -06:00
|
|
|
// Allow self signed certificates
|
|
|
|
config.InsecureSkipVerify = true
|
2020-09-21 17:17:10 -06:00
|
|
|
config.Certificates = req.Certificates
|
2020-09-21 16:21:42 -06:00
|
|
|
|
|
|
|
conn, err := tls.Dial("tcp", host, &config)
|
2020-09-21 13:48:42 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Write the request
|
|
|
|
request := req.URL.String() + "\r\n"
|
|
|
|
if _, err := conn.Write([]byte(request)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:58:36 -06:00
|
|
|
buf := bufio.NewReader(conn)
|
|
|
|
|
2020-09-21 13:48:42 -06:00
|
|
|
// Read the response header
|
|
|
|
code := make([]byte, 2)
|
2020-09-21 15:58:36 -06:00
|
|
|
if _, err := buf.Read(code); err != nil {
|
2020-09-21 13:48:42 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
status, err := strconv.Atoi(string(code))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read one space
|
2020-09-21 15:58:36 -06:00
|
|
|
if space, err := buf.ReadByte(); err != nil {
|
2020-09-21 13:48:42 -06:00
|
|
|
return nil, err
|
2020-09-21 15:58:36 -06:00
|
|
|
} else if space != ' ' {
|
2020-09-21 15:23:51 -06:00
|
|
|
return nil, ErrProtocol
|
2020-09-21 13:48:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the meta
|
2020-09-21 15:58:36 -06:00
|
|
|
meta, err := readLine(buf)
|
2020-09-21 15:23:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-21 13:48:42 -06:00
|
|
|
|
|
|
|
// Read the response body
|
2020-09-21 15:58:36 -06:00
|
|
|
body, err := ioutil.ReadAll(buf)
|
2020-09-21 13:48:42 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Response{
|
|
|
|
Status: status,
|
|
|
|
Meta: meta,
|
|
|
|
Body: body,
|
|
|
|
}, nil
|
|
|
|
}
|