mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 16:34:02 +01:00
Random strings for cookie values (#1122)
This commit is contained in:
parent
14230d7b52
commit
acbdee76de
5 changed files with 60 additions and 6 deletions
|
|
@ -158,10 +158,11 @@ type scraperDebugOptions struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type scraperCookies struct {
|
type scraperCookies struct {
|
||||||
Name string `yaml:"Name"`
|
Name string `yaml:"Name"`
|
||||||
Value string `yaml:"Value"`
|
Value string `yaml:"Value"`
|
||||||
Domain string `yaml:"Domain"`
|
ValueRandom int `yaml:"ValueRandom"`
|
||||||
Path string `yaml:"Path"`
|
Domain string `yaml:"Domain"`
|
||||||
|
Path string `yaml:"Path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type cookieOptions struct {
|
type cookieOptions struct {
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/chromedp/chromedp"
|
"github.com/chromedp/chromedp"
|
||||||
|
|
||||||
"github.com/stashapp/stash/pkg/logger"
|
"github.com/stashapp/stash/pkg/logger"
|
||||||
|
"github.com/stashapp/stash/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// set cookies for the native http client
|
// set cookies for the native http client
|
||||||
|
|
@ -32,7 +33,7 @@ func setCookies(jar *cookiejar.Jar, scraperConfig config) {
|
||||||
for _, cookie := range ckURL.Cookies {
|
for _, cookie := range ckURL.Cookies {
|
||||||
httpCookie = &http.Cookie{
|
httpCookie = &http.Cookie{
|
||||||
Name: cookie.Name,
|
Name: cookie.Name,
|
||||||
Value: cookie.Value,
|
Value: getCookieValue(cookie),
|
||||||
Path: cookie.Path,
|
Path: cookie.Path,
|
||||||
Domain: cookie.Domain,
|
Domain: cookie.Domain,
|
||||||
}
|
}
|
||||||
|
|
@ -53,6 +54,13 @@ func setCookies(jar *cookiejar.Jar, scraperConfig config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCookieValue(cookie *scraperCookies) string {
|
||||||
|
if cookie.ValueRandom > 0 {
|
||||||
|
return utils.RandomSequence(cookie.ValueRandom)
|
||||||
|
}
|
||||||
|
return cookie.Value
|
||||||
|
}
|
||||||
|
|
||||||
// print all cookies from the jar of the native http client
|
// print all cookies from the jar of the native http client
|
||||||
func printCookies(jar *cookiejar.Jar, scraperConfig config, msg string) {
|
func printCookies(jar *cookiejar.Jar, scraperConfig config, msg string) {
|
||||||
driverOptions := scraperConfig.DriverOptions
|
driverOptions := scraperConfig.DriverOptions
|
||||||
|
|
@ -92,7 +100,7 @@ func setCDPCookies(driverOptions scraperDriverOptions) chromedp.Tasks {
|
||||||
|
|
||||||
for _, ckURL := range driverOptions.Cookies {
|
for _, ckURL := range driverOptions.Cookies {
|
||||||
for _, cookie := range ckURL.Cookies {
|
for _, cookie := range ckURL.Cookies {
|
||||||
success, err := network.SetCookie(cookie.Name, cookie.Value).
|
success, err := network.SetCookie(cookie.Name, getCookieValue(cookie)).
|
||||||
WithExpires(&expr).
|
WithExpires(&expr).
|
||||||
WithDomain(cookie.Domain).
|
WithDomain(cookie.Domain).
|
||||||
WithPath(cookie.Path).
|
WithPath(cookie.Path).
|
||||||
|
|
|
||||||
17
pkg/utils/strings.go
Normal file
17
pkg/utils/strings.go
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var characters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
|
||||||
|
|
||||||
|
func RandomSequence(n int) string {
|
||||||
|
b := make([]rune, n)
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
for i := range b {
|
||||||
|
b[i] = characters[rand.Intn(len(characters))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
### 🎨 Improvements
|
### 🎨 Improvements
|
||||||
|
* Support random strings for scraper cookie values.
|
||||||
* Added Rescan button to scene, image, gallery details overflow button.
|
* Added Rescan button to scene, image, gallery details overflow button.
|
||||||
|
|
|
||||||
|
|
@ -489,6 +489,33 @@ driver:
|
||||||
Domain: ".somewhere.com"
|
Domain: ".somewhere.com"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For some sites, the value of the cookie itself doesn't actually matter. In these cases, we can use the `ValueRandom`
|
||||||
|
property instead of `Value`. Unlike `Value`, `ValueRandom` requires an integer value greater than `0` where the value
|
||||||
|
indicates how long the cookie string should be.
|
||||||
|
|
||||||
|
In the following example, we will adapt the previous cookies to use `ValueRandom` instead. We set the `_test2` cookie
|
||||||
|
to randomly generate a value with a length of 6 characters and the `_warn` cookie to a length of 3.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
driver:
|
||||||
|
cookies:
|
||||||
|
- CookieURL: "https://www.example.com"
|
||||||
|
Cookies:
|
||||||
|
- Name: "_warning"
|
||||||
|
Domain: ".example.com"
|
||||||
|
Value: "true"
|
||||||
|
Path: "/"
|
||||||
|
- Name: "_test2"
|
||||||
|
ValueRandom: 6
|
||||||
|
Domain: ".example.com"
|
||||||
|
Path: "/"
|
||||||
|
- CookieURL: "https://api.somewhere.com"
|
||||||
|
Cookies:
|
||||||
|
- Name: "_warn"
|
||||||
|
ValueRandom: 3
|
||||||
|
Domain: ".somewhere.com"
|
||||||
|
```
|
||||||
|
|
||||||
When developing a scraper you can have a look at the cookies set by a site by adding
|
When developing a scraper you can have a look at the cookies set by a site by adding
|
||||||
|
|
||||||
* a `CookieURL` if you use the direct xpath scraper
|
* a `CookieURL` if you use the direct xpath scraper
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue