mirror of
https://gitgud.io/yats/libkiwi.git
synced 2026-05-02 02:32:05 -04:00
Update firebird version
Ditch kiwijar
This commit is contained in:
79
libkiwi.go
79
libkiwi.go
@@ -2,13 +2,14 @@ package libkiwi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/y-a-t-s/firebird"
|
||||
"github.com/y-a-t-s/kiwijar"
|
||||
)
|
||||
|
||||
type KF struct {
|
||||
@@ -23,9 +24,18 @@ func NewKF(hc http.Client, host string, cookies string) (kf *KF, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
jar := kiwijar.KiwiJar{}
|
||||
jar.ParseString(u, cookies)
|
||||
hc.Jar = &jar
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cs, err := parseCookieString(u, cookies)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
jar.SetCookies(u, cs)
|
||||
hc.Jar = jar
|
||||
|
||||
kf = &KF{
|
||||
Client: hc,
|
||||
@@ -47,19 +57,19 @@ func NewKF(hc http.Client, host string, cookies string) (kf *KF, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (kf *KF) GetPage(ctx context.Context, u *url.URL) (resp *http.Response, err error) {
|
||||
func (kf *KF) GetPage(ctx context.Context, u *url.URL) (*http.Response, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err = kf.Client.Do(req)
|
||||
resp, err := kf.Client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
hn := resp.Request.URL.Hostname()
|
||||
if hn != kf.domain.Hostname() {
|
||||
jar := kf.Client.Jar.(*kiwijar.KiwiJar)
|
||||
jar := kf.Client.Jar
|
||||
jar.SetCookies(resp.Request.URL, jar.Cookies(kf.domain))
|
||||
kf.domain.Host = hn
|
||||
}
|
||||
@@ -68,30 +78,67 @@ func (kf *KF) GetPage(ctx context.Context, u *url.URL) (resp *http.Response, err
|
||||
if resp.StatusCode == 203 {
|
||||
err = kf.solveKiwiFlare(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
// Try fetching the page again now that we're authed.
|
||||
return kf.GetPage(ctx, u)
|
||||
}
|
||||
|
||||
return
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (kf *KF) RefreshSession(ctx context.Context) (tk string, err error) {
|
||||
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
|
||||
}
|
||||
|
||||
func setCookie(jar http.CookieJar, u *url.URL, cookie *http.Cookie) {
|
||||
cookies := jar.Cookies(u)
|
||||
found := false
|
||||
for i, c := range cookies {
|
||||
if c.Name == cookie.Name {
|
||||
cookies[i] = cookie
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
cookies = append(cookies, cookie)
|
||||
}
|
||||
|
||||
jar.SetCookies(u, cookies)
|
||||
}
|
||||
|
||||
func (kf *KF) RefreshSession(ctx context.Context) (string, error) {
|
||||
// Clear any existing session token to request a new one.
|
||||
kf.Client.Jar.(*kiwijar.KiwiJar).SetCookie(kf.domain, &http.Cookie{
|
||||
setCookie(kf.Client.Jar, kf.domain, &http.Cookie{
|
||||
Name: "xf_session",
|
||||
Value: "",
|
||||
})
|
||||
|
||||
resp, err := kf.GetPage(ctx, kf.domain)
|
||||
if err != nil {
|
||||
return
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
tk = regexp.MustCompile(`xf_session=([^;]*)`).FindString(resp.Header.Get("Set-Cookie"))
|
||||
return
|
||||
return regexp.MustCompile(`xf_session=([^;]*)`).FindString(resp.Header.Get("Set-Cookie")), nil
|
||||
}
|
||||
|
||||
func (kf *KF) solveKiwiFlare(ctx context.Context) error {
|
||||
|
||||
Reference in New Issue
Block a user