refactoring

This commit is contained in:
y a t s
2026-06-19 10:42:39 -04:00
parent 74b923f06f
commit c7ccfc65bf
3 changed files with 25 additions and 33 deletions
+13 -21
View File
@@ -29,6 +29,16 @@ func loginForm(xfToken string, creds Credentials) url.Values {
} }
} }
func newFormRequest(ctx context.Context, reqURL string, form url.Values) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, "POST", reqURL, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}
func (kf *KF) newLoginRequest(ctx context.Context, creds Credentials) (*http.Request, error) { func (kf *KF) newLoginRequest(ctx context.Context, creds Credentials) (*http.Request, error) {
u := kf.urlFromPath("login") u := kf.urlFromPath("login")
@@ -43,17 +53,8 @@ func (kf *KF) newLoginRequest(ctx context.Context, creds Credentials) (*http.Req
return nil, err return nil, err
} }
form := loginForm(xfToken, creds) // e.g. https://kiwifarms.net/login/login
return newFormRequest(ctx, fmt.Sprintf("%s/login", u.String()), loginForm(xfToken, creds))
// i.e. https://kiwifarms.net/login/login
reqURL := fmt.Sprintf("%s/login", u.String())
req, err := http.NewRequestWithContext(ctx, "POST", reqURL, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
} }
func (kf *KF) Login(ctx context.Context, creds Credentials) (*http.Response, error) { func (kf *KF) Login(ctx context.Context, creds Credentials) (*http.Response, error) {
@@ -101,16 +102,7 @@ func (kf *KF) newTwoFactorRequest(ctx context.Context, resp *http.Response, code
return nil, err return nil, err
} }
form := twoFactorForm(xfToken, code, u) return newFormRequest(ctx, fmt.Sprintf("https://%s/login/two-step", u.Hostname()), twoFactorForm(xfToken, code, u))
reqURL := fmt.Sprintf("https://%s/login/two-step", u.Hostname())
req, err := http.NewRequestWithContext(ctx, "POST", reqURL, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
} }
func (kf *KF) TwoFactorAuth(ctx context.Context, resp *http.Response, code uint32) (*http.Response, error) { func (kf *KF) TwoFactorAuth(ctx context.Context, resp *http.Response, code uint32) (*http.Response, error) {
+10 -8
View File
@@ -6,12 +6,13 @@ import (
) )
// http.CookieJar interface wrapper to add convenience functions. // http.CookieJar interface wrapper to add convenience functions.
type jar struct { type cookieJar struct {
http.CookieJar jar *http.CookieJar
} }
func (j *jar) GetCookie(u *url.URL, name string) *http.Cookie { func (j *cookieJar) GetCookie(u *url.URL, name string) *http.Cookie {
cookies := j.Cookies(u) jar := (*j.jar)
cookies := jar.Cookies(u)
for _, c := range cookies { for _, c := range cookies {
if c.Name == name { if c.Name == name {
return c return c
@@ -21,18 +22,19 @@ func (j *jar) GetCookie(u *url.URL, name string) *http.Cookie {
return nil return nil
} }
func (j *jar) SetCookie(u *url.URL, newCookie *http.Cookie) { func (j *cookieJar) SetCookie(u *url.URL, newCookie *http.Cookie) {
cookies := j.Cookies(u) jar := (*j.jar)
cookies := jar.Cookies(u)
for i, c := range cookies { for i, c := range cookies {
if c.Name == newCookie.Name { if c.Name == newCookie.Name {
cookies[i] = newCookie cookies[i] = newCookie
j.SetCookies(u, cookies) jar.SetCookies(u, cookies)
return return
} }
} }
// Append if not already existing. // Append if not already existing.
cookies = append(cookies, newCookie) cookies = append(cookies, newCookie)
j.SetCookies(u, cookies) jar.SetCookies(u, cookies)
} }
+2 -4
View File
@@ -13,13 +13,11 @@ import (
gq "github.com/PuerkitoBio/goquery" gq "github.com/PuerkitoBio/goquery"
) )
const _USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
type KF struct { type KF struct {
client http.Client client http.Client
domain *url.URL domain *url.URL
Cookies jar Cookies cookieJar
} }
// Supply your own http.Client to route through any proxies. // Supply your own http.Client to route through any proxies.
@@ -41,7 +39,7 @@ func NewKF(hc http.Client, host *url.URL) (KF, error) {
return KF{ return KF{
client: hc, client: hc,
domain: u, domain: u,
Cookies: jar{hc.Jar}, Cookies: cookieJar{&hc.Jar},
}, nil }, nil
} }