Add user+pass login and 2fa support

This commit is contained in:
y a t s
2026-06-05 17:30:22 -04:00
parent 8e0720d587
commit 412f3108e1
6 changed files with 519 additions and 129 deletions
+101 -100
View File
@@ -4,60 +4,53 @@ import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strconv"
"strings"
"gitgud.io/yats/cerberus"
gq "github.com/PuerkitoBio/goquery"
)
type KF struct {
Client http.Client
client http.Client
domain *url.URL
}
// Supply your own http.Client to route through any proxies.
func NewKF(hc http.Client, host string, cookies string) (KF, error) {
u, err := parseHost(host)
func NewKF(hc http.Client, host *url.URL) (*KF, error) {
u, err := url.Parse(fmt.Sprintf("https://%s", host.Hostname()))
if err != nil {
return KF{}, err
return nil, err
}
if hc.Jar == nil {
jar, err := cookiejar.New(nil)
if err != nil {
return KF{}, err
}
hc.Jar = jar
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
if cookies != "" {
cs, err := parseCookieString(u, cookies)
if err != nil {
return KF{}, err
}
hc.Jar = jar
hc.Jar.SetCookies(u, cs)
kf := &KF{
client: hc,
domain: u,
}
// Update host url in case we get redirected across domains.
hc.CheckRedirect = func(req *http.Request, via []*http.Request) error {
hn := req.URL.Hostname()
if hn != u.Hostname() {
reqHost := req.URL.Hostname()
if reqHost != u.Hostname() {
// Deliberately set to Hostname() and not Host.
// This excludes any extra shit like ports.
u.Host = hn
u.Host = reqHost
}
return nil
}
return KF{
Client: hc,
domain: u,
}, nil
return kf, nil
}
func (kf *KF) GetPage(ctx context.Context, u *url.URL) (*http.Response, error) {
@@ -65,24 +58,20 @@ func (kf *KF) GetPage(ctx context.Context, u *url.URL) (*http.Response, error) {
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0")
resp, err := kf.Client.Do(req)
resp, err := kf.client.Do(req)
if err != nil {
return nil, err
}
hn := resp.Request.URL.Hostname()
if hn != kf.domain.Hostname() {
jar := kf.Client.Jar
jar.SetCookies(resp.Request.URL, jar.Cookies(kf.domain))
kf.domain.Host = hn
}
// KiwiFlare redirect is signaled by 203 status.
if resp.StatusCode == 203 {
err = kf.SolveKiwiFlare(ctx)
err = kf.solveKiwiFlare(ctx)
if err != nil {
return nil, err
}
// Try fetching the page again now that we're authed.
return kf.GetPage(ctx, u)
}
@@ -90,90 +79,102 @@ func (kf *KF) GetPage(ctx context.Context, u *url.URL) (*http.Response, error) {
return resp, nil
}
func parseCookieString(u *url.URL, cookies string) ([]*http.Cookie, error) {
sp := strings.Split(cookies, "; ")
cs := make([]*http.Cookie, len(sp))
for i, c := range sp {
kv := strings.Split(c, "=")
if len(kv) != 2 {
return nil, errors.New("Invalid cookie string: " + cookies)
}
cs[i] = &http.Cookie{
Domain: u.Hostname(),
Name: kv[0],
Path: "/",
Value: kv[1],
}
}
return cs, nil
type User struct {
ID uint32
Name string
Title string
URL *url.URL
}
func setCookie(jar http.CookieJar, u *url.URL, cookie *http.Cookie) {
cookies := jar.Cookies(u)
func parsePostAuthor(article *gq.Selection) (User, error) {
userBlock := article.Find("section.message-user")
for i, c := range cookies {
if c.Name == cookie.Name {
cookies[i] = cookie
// Store changes and stop looping since it's found.
jar.SetCookies(u, cookies)
return
}
idStr, ok := userBlock.Attr("data-user-id")
if !ok {
// TODO: Proper error types.
return User{}, errors.New("Failed to parse post author attr.")
}
// Append new cookie if existing one not found.
cookies = append(cookies, cookie)
jar.SetCookies(u, cookies)
}
func (kf *KF) CookieString() string {
var b strings.Builder
for _, c := range kf.Client.Jar.Cookies(kf.domain) {
fmt.Fprintf(&b, "%s: %s; ", c.Name, c.Value)
}
return strings.TrimSuffix(b.String(), "; ")
}
func (kf *KF) RefreshSession(ctx context.Context) (string, error) {
// Clear any existing session token to request a new one.
setCookie(kf.Client.Jar, kf.domain, &http.Cookie{
Name: "xf_session",
Value: "",
})
resp, err := kf.GetPage(ctx, kf.domain)
id, err := strconv.Atoi(idStr)
if err != nil {
return "", err
return User{}, err
}
name, ok := article.Attr("data-author")
if !ok {
return User{}, errors.New("Failed to parse post author attr.")
}
title := userBlock.Find(".message-userTitle").Text()
urlStr, ok := userBlock.Attr("itemid")
if !ok {
return User{}, errors.New("Failed to parse post author attr.")
}
u, err := url.Parse(urlStr)
if err != nil {
return User{}, err
}
user := User{
ID: uint32(id),
Name: name,
Title: title,
URL: u,
}
return user, nil
}
type Post struct {
Author User
Body io.Reader
}
func (kf *KF) GetPost(ctx context.Context, postID uint32) (Post, error) {
// Example post goto link: https://kiwifarms.st/goto/post?id=22058462
gtl := fmt.Sprintf("%s/goto/post?id=%d", kf.domain.String(), postID)
u, err := url.Parse(gtl)
if err != nil {
return Post{}, err
}
resp, err := kf.GetPage(ctx, u)
if err != nil {
return Post{}, err
}
defer resp.Body.Close()
return regexp.MustCompile(`xf_session=([^;]*)`).FindString(resp.Header.Get("Set-Cookie")), nil
}
doc, err := gq.NewDocumentFromReader(resp.Body)
if err != nil {
return Post{}, err
}
func (kf *KF) SolveKiwiFlare(ctx context.Context) error {
c, err := cerberus.NewChallenge(ctx, kf.Client, kf.domain.String())
if err != nil {
return err
// Selector: #js-post-22058462
article := doc.Find(fmt.Sprintf("article#js-post-%d", postID))
body := article.Find("div.message-content article.message-body")
if body.Length() == 0 {
return Post{}, errors.New("Failed to parse post message body.")
}
s, err := cerberus.Solve(ctx, c)
bh, err := body.Html()
if err != nil {
return err
return Post{}, err
}
resp, err := cerberus.Submit(ctx, kf.Client, s, "")
r := strings.NewReader(bh)
author, err := parsePostAuthor(article)
if err != nil {
return err
return Post{}, err
}
defer resp.Body.Close()
return nil
}
func parseHost(host string) (*url.URL, error) {
// Try prepending protocol if it seems to be missing.
if !strings.Contains(host, "://") {
host = "https://" + host
post := Post{
Author: author,
Body: r,
}
return url.Parse(host)
return post, nil
}