100 lines
3.1 KiB
Go
100 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Posting is the raw material handed to Claude.
|
|
type Posting struct {
|
|
URL string
|
|
Text string
|
|
}
|
|
|
|
// errUnreadable means the page could not be turned into posting text: the
|
|
// site refused a server (Indeed, Glassdoor), needs JavaScript, or is empty.
|
|
// The bot answers it by asking for the text to be pasted instead.
|
|
var errUnreadable = errors.New("page could not be read")
|
|
|
|
const (
|
|
maxPageBytes = 4 << 20
|
|
userAgent = "Mozilla/5.0 (X11; Linux x86_64) jobhunt/0.1 (personal job posting reader)"
|
|
)
|
|
|
|
var cgnat = netip.MustParsePrefix("100.64.0.0/10") // Tailscale addresses
|
|
|
|
func isPublic(ip netip.Addr) bool {
|
|
ip = ip.Unmap()
|
|
return ip.IsValid() && !ip.IsLoopback() && !ip.IsPrivate() && !ip.IsUnspecified() &&
|
|
!ip.IsLinkLocalUnicast() && !ip.IsLinkLocalMulticast() && !ip.IsMulticast() &&
|
|
!cgnat.Contains(ip)
|
|
}
|
|
|
|
// newHTTPClient returns a client that refuses to connect to anything but
|
|
// public addresses, so a pasted link can never reach services on this box or
|
|
// on the tailnet. The check runs on the resolved address of every connection,
|
|
// redirects included. Tests pass allowPrivate to reach httptest servers.
|
|
func newHTTPClient(allowPrivate bool) *http.Client {
|
|
dialer := &net.Dialer{Timeout: 10 * time.Second}
|
|
if !allowPrivate {
|
|
dialer.Control = func(_, address string, _ syscall.RawConn) error {
|
|
ap, err := netip.ParseAddrPort(address)
|
|
if err != nil || !isPublic(ap.Addr()) {
|
|
return fmt.Errorf("refusing to connect to non-public address %s", address)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
return &http.Client{
|
|
Timeout: 30 * time.Second,
|
|
Transport: &http.Transport{DialContext: dialer.DialContext, Proxy: nil},
|
|
CheckRedirect: func(_ *http.Request, via []*http.Request) error {
|
|
if len(via) >= 5 {
|
|
return errors.New("too many redirects")
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func httpGet(ctx context.Context, client *http.Client, url string, limit int64) (int, []byte, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
req.Header.Set("User-Agent", userAgent)
|
|
req.Header.Set("Accept", "text/html,application/xhtml+xml,text/plain;q=0.9,*/*;q=0.5")
|
|
req.Header.Set("Accept-Language", "en,nl;q=0.8")
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(io.LimitReader(resp.Body, limit))
|
|
return resp.StatusCode, body, err
|
|
}
|
|
|
|
// fetchPosting downloads a posting page and reduces it to text. It makes one
|
|
// plain request; it does not try to get past sites that block servers.
|
|
func fetchPosting(ctx context.Context, client *http.Client, url string) (Posting, error) {
|
|
status, body, err := httpGet(ctx, client, url, maxPageBytes)
|
|
if err != nil {
|
|
return Posting{}, fmt.Errorf("%w: %v", errUnreadable, err)
|
|
}
|
|
if status >= 400 {
|
|
return Posting{}, fmt.Errorf("%w: the site answered HTTP %d", errUnreadable, status)
|
|
}
|
|
text, ok := extractPosting(string(body))
|
|
if !ok {
|
|
return Posting{}, fmt.Errorf("%w: no posting text on the page (it probably needs JavaScript)", errUnreadable)
|
|
}
|
|
return Posting{URL: url, Text: text}, nil
|
|
}
|