Compare commits

..

No commits in common. "280703edd8e27fdd971604a85a04a91938ba608c" and "16658e845df588ba58a73c9950a7cc45dd7be7e4" have entirely different histories.

1 changed files with 9 additions and 204 deletions

213
main.go
View File

@ -6,10 +6,8 @@ import (
"log"
"sort"
"time"
"bufio"
"errors"
"context"
"strconv"
"strings"
"net/url"
"crypto/x509"
@ -23,8 +21,6 @@ import (
var (
hosts tofu.KnownHosts
hostsfile *tofu.HostWriter
predirs map[string]*url.URL
perrors map[string]PError
)
func xdgDataHome() string {
@ -45,154 +41,6 @@ func init() {
if err != nil {
log.Fatal(err)
}
err = populatePRedirs()
if err != nil {
log.Fatal(err)
}
err = populatePErrors()
if err != nil {
log.Fatal(err)
}
}
func populatePRedirs() error {
file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "predirs"), os.O_RDONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
predirs = make(map[string]*url.URL)
for scanner.Scan() {
urls := strings.Split(scanner.Text(), " ")
if len(urls) != 2 {
continue
}
url0, err := url.Parse(urls[0])
if err != nil {
continue
}
if url0.Scheme != "gemini" {
continue
}
url1, err := url.Parse(urls[1])
if err != nil {
continue
}
if url1.Scheme != "gemini" {
continue
}
predirs[url0.String()] = url1
}
return nil
}
func savePRedirs() error {
file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "predirs"), os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
err = file.Truncate(0)
if err != nil {
return err
}
writer := bufio.NewWriter(file)
for url0, url1 := range predirs {
_, err = writer.WriteString(url0)
if err != nil {
return err
}
_, err = writer.WriteRune(' ')
if err != nil {
return err
}
_, err = writer.WriteString(url1.String())
if err != nil {
return err
}
_, err = writer.WriteRune('\n')
if err != nil {
return err
}
}
return writer.Flush()
}
func populatePErrors() error {
file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "perrors"), os.O_RDONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
perrors = make(map[string]PError)
unescaper := strings.NewReplacer("\\n", "\n", "\\\\", "\\")
for scanner.Scan() {
values := strings.SplitN(scanner.Text(), " ", 3)
if len(values) != 3 {
continue
}
url, err := url.Parse(values[0])
if err != nil {
continue
}
if url.Scheme != "gemini" {
continue
}
code, err := strconv.Atoi(values[1])
if err != nil {
continue
}
perrors[url.String()] = PError{
code: gemini.Status(code),
message: unescaper.Replace(values[2]),
}
}
return nil
}
func savePErrors() error {
file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "perrors"), os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
err = file.Truncate(0)
if err != nil {
return err
}
writer := bufio.NewWriter(file)
escaper := strings.NewReplacer("\\", "\\\\", "\n", "\\n")
for url, perror := range perrors {
_, err = writer.WriteString(url)
if err != nil {
return err
}
_, err = writer.WriteRune(' ')
if err != nil {
return err
}
_, err = writer.WriteString(strconv.Itoa(int(perror.code)))
if err != nil {
return err
}
_, err = writer.WriteRune(' ')
if err != nil {
return err
}
_, err = escaper.WriteString(writer, perror.message)
if err != nil {
return err
}
_, err = writer.WriteRune('\n')
if err != nil {
return err
}
}
return writer.Flush()
}
func trustCertificate(hostname string, cert *x509.Certificate) error {
@ -211,50 +59,16 @@ func trustCertificate(hostname string, cert *x509.Certificate) error {
return nil
}
func do(client gemini.Client, ctx context.Context, req *gemini.Request, via []*gemini.Request) (*gemini.Response, *gemini.Request, error) {
if target, exists := predirs[req.URL.String()]; exists {
via = append(via, req)
if len(via) > 5 {
return nil, req, errors.New("too many redirects")
}
redirect := *req
redirect.URL = target
return do(client, ctx, &redirect, via)
}
if perror, exists := perrors[req.URL.String()]; exists {
return nil, req, errors.New(fmt.Sprintf("%d %s", perror.code, perror.message))
func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, *gemini.Request, error) {
client := gemini.Client{
TrustCertificate: trustCertificate,
}
ctx := context.Background()
resp, err := client.Do(ctx, req)
if err != nil {
return resp, req, err
}
if resp.Status == gemini.StatusPermanentRedirect {
target, err := url.Parse(resp.Meta)
if err != nil {
return resp, req, err
}
if target.Scheme != "gemini" && target.Scheme != "" {
return resp, req, errors.New(fmt.Sprintf("tried to redirect to scheme %s", target.Scheme))
}
target = req.URL.ResolveReference(target)
predirs[req.URL.String()] = target
err = savePRedirs()
if err != nil {
return resp, req, err
}
}
if resp.Status.Class() == gemini.StatusPermanentFailure {
perrors[req.URL.String()] = PError{
code: resp.Status,
message: resp.Meta,
}
err = savePErrors()
if err != nil {
return resp, req, err
}
}
if resp.Status.Class() == gemini.StatusRedirect {
via = append(via, req)
if len(via) > 5 {
@ -271,10 +85,7 @@ func do(client gemini.Client, ctx context.Context, req *gemini.Request, via []*g
target = req.URL.ResolveReference(target)
redirect := *req
redirect.URL = target
return do(client, ctx, &redirect, via)
}
if resp.Status.Class() != gemini.StatusSuccess {
return resp, req, errors.New(fmt.Sprintf("%d %s", resp.Status, resp.Meta))
return do(&redirect, via)
}
return resp, req, err
@ -295,16 +106,15 @@ func main() {
log.Fatal(err)
}
client := gemini.Client{
TrustCertificate: trustCertificate,
}
ctx := context.Background()
resp, req, err := do(client, ctx, req, nil)
resp, req, err := do(req, nil)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.Status.Class() != gemini.StatusSuccess {
log.Fatalf("%d %s", resp.Status, resp.Meta)
}
if resp.Meta != "text/gemini" && !strings.HasPrefix(resp.Meta, "text/gemini;") {
log.Fatal("mime type is not text/gemini")
}
@ -398,11 +208,6 @@ func (a ByTime) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
type PError struct {
code gemini.Status
message string
}
type AtomWriter struct {
Title string
Items []FeedItem