Fix string bounds issue

Better hostname handling
This commit is contained in:
y a t s
2024-09-02 11:08:50 -04:00
parent 01b6837eea
commit 0bfe254d05
5 changed files with 37 additions and 22 deletions

2
go.mod
View File

@@ -2,6 +2,6 @@ module github.com/y-a-t-s/libkiwi
go 1.23.0
require github.com/y-a-t-s/firebird v0.0.0-20240827160906-63b3ab4765b6
require github.com/y-a-t-s/firebird v0.0.0-20240902150251-4943d1802500
require golang.org/x/net v0.28.0 // indirect

4
go.sum
View File

@@ -1,4 +1,4 @@
github.com/y-a-t-s/firebird v0.0.0-20240827160906-63b3ab4765b6 h1:V720EMlceDtw5k7CKbYzvQjseyZuCB6Evssp6m9dXkE=
github.com/y-a-t-s/firebird v0.0.0-20240827160906-63b3ab4765b6/go.mod h1:iaOF6bre/MgFaBH513kb3RxcJ+x8vSPPAeOjN7RNkn8=
github.com/y-a-t-s/firebird v0.0.0-20240902150251-4943d1802500 h1:D7egbhaHIVqiYF/aOPS89xYurCTHwqDgxivKpoYr8/o=
github.com/y-a-t-s/firebird v0.0.0-20240902150251-4943d1802500/go.mod h1:iaOF6bre/MgFaBH513kb3RxcJ+x8vSPPAeOjN7RNkn8=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=

19
jar.go
View File

@@ -39,7 +39,7 @@ func NewKiwiJar(domain *url.URL, cookies string) (kj *KiwiJar, err error) {
}
func (kj *KiwiJar) checkAlloc(u *url.URL) {
if kj.cookieMap == nil || kj.cookieMap[u.Host] == nil {
if kj.cookieMap == nil || kj.cookieMap[u.Hostname()] == nil {
kj.newDomain(u)
}
}
@@ -55,16 +55,17 @@ func (kj *KiwiJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
func (kj *KiwiJar) Cookies(u *url.URL) []*http.Cookie {
kj.checkAlloc(u)
hn := u.Hostname()
res := make(chan []*http.Cookie, 1)
go func() {
kj.mutex.Lock()
defer kj.mutex.Unlock()
cl := len(kj.cookieMap[u.Host])
cl := len(kj.cookieMap[hn])
cs := make([]*http.Cookie, cl)
i := 0
for _, c := range kj.cookieMap[u.Host] {
for _, c := range kj.cookieMap[hn] {
if i >= cl {
break
}
@@ -84,8 +85,10 @@ func (kj *KiwiJar) CookieString(u *url.URL) (cookies string) {
for _, c := range cs {
cookies += fmt.Sprintf("; %s=%s", c.Name, c.Value)
}
// Remove leading semicolon+space.
cookies = cookies[2:]
if len(cookies) > 2 {
// Remove leading semicolon+space.
cookies = cookies[2:]
}
return
}
@@ -99,7 +102,7 @@ func (kj *KiwiJar) GetCookie(u *url.URL, name string) *http.Cookie {
kj.mutex.Lock()
defer kj.mutex.Unlock()
res <- kj.cookieMap[u.Host][name]
res <- kj.cookieMap[u.Hostname()][name]
}()
return <-res
@@ -114,7 +117,7 @@ func (kj *KiwiJar) SetCookie(u *url.URL, cookie *http.Cookie) {
kj.mutex.Lock()
defer kj.mutex.Unlock()
kj.cookieMap[u.Host][cookie.Name] = cookie
kj.cookieMap[u.Hostname()][cookie.Name] = cookie
done <- true
}()
@@ -132,7 +135,7 @@ func (kj *KiwiJar) newDomain(domain *url.URL) {
kj.mutex.Lock()
defer kj.mutex.Unlock()
kj.cookieMap[domain.Host] = make(map[string]*http.Cookie, 16)
kj.cookieMap[domain.Hostname()] = make(map[string]*http.Cookie, 16)
done <- true
}()

View File

@@ -1,6 +1,7 @@
package libkiwi
import (
"context"
"errors"
"net/http"
"net/url"
@@ -39,17 +40,16 @@ func NewKF(hc http.Client, host string, cookies string) (kf *KF, err error) {
return
}
func (kf *KF) GetPage(u *url.URL) (resp *http.Response, err error) {
func (kf *KF) GetPage(ctx context.Context, u *url.URL) (resp *http.Response, err error) {
if u == nil {
err = errors.New("Received nil URL.")
return
}
req, err := http.NewRequest("GET", u.String(), nil)
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
return
}
// req.Header.Set("Cookie", kf.cookies)
resp, err = kf.Client.Do(req)
if err != nil {
@@ -58,25 +58,25 @@ func (kf *KF) GetPage(u *url.URL) (resp *http.Response, err error) {
// KiwiFlare redirect is signaled by 203 status.
if resp.StatusCode == 203 {
err = kf.solveKiwiFlare()
err = kf.solveKiwiFlare(ctx)
if err != nil {
return
}
// Try fetching the page again now that we're authed.
return kf.GetPage(u)
return kf.GetPage(ctx, u)
}
return
}
func (kf *KF) RefreshSession() (tk string, err error) {
func (kf *KF) RefreshSession(ctx context.Context) (tk string, err error) {
// Clear any existing session token to request a new one.
kf.Client.Jar.(*KiwiJar).SetCookie(kf.domain, &http.Cookie{
Name: "xf_session",
Value: "",
})
resp, err := kf.GetPage(kf.domain)
resp, err := kf.GetPage(ctx, kf.domain)
if err != nil {
return
}
@@ -86,12 +86,12 @@ func (kf *KF) RefreshSession() (tk string, err error) {
return
}
func (kf *KF) solveKiwiFlare() error {
func (kf *KF) solveKiwiFlare(ctx context.Context) error {
c, err := firebird.NewChallenge(kf.Client, kf.domain.String())
if err != nil {
return err
}
s, err := firebird.Solve(c)
s, err := firebird.Solve(ctx, c)
if err != nil {
return err
}

View File

@@ -1,6 +1,7 @@
package libkiwi
import (
"context"
"log"
"net/http"
"net/url"
@@ -16,8 +17,12 @@ func TestGetPage(t *testing.T) {
if err != nil {
t.Error(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.Println("Getting homepage")
resp, err := kf.GetPage(kf.domain)
resp, err := kf.GetPage(ctx, kf.domain)
if err != nil {
t.Error(err)
}
@@ -37,11 +42,16 @@ func TestRefreshSession(t *testing.T) {
if err != nil {
t.Error(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.Println("Refreshing xf_session")
tk, err := kf.RefreshSession()
tk, err := kf.RefreshSession(ctx)
if err != nil {
t.Error(err)
}
log.Println("New xf_session token: " + tk)
}
@@ -51,9 +61,11 @@ func TestCookieString(t *testing.T) {
if err != nil {
t.Error(err)
}
u, err := url.Parse("https://" + TEST_HOST)
if err != nil {
t.Error(err)
}
log.Println("Cookies from jar: " + kf.Client.Jar.(*KiwiJar).CookieString(u))
}