Skip insecure certificates check when scraping (#1120)

* Ignore insecure certificates when scraping
* add ScraperCertCheck to scraper config options
This commit is contained in:
bnkai 2021-03-01 02:47:39 +02:00 committed by GitHub
parent a9ac176e91
commit 144cd6e4f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 47 additions and 0 deletions

View file

@ -31,6 +31,7 @@ fragment ConfigGeneralData on ConfigGeneralResult {
excludes excludes
imageExcludes imageExcludes
scraperUserAgent scraperUserAgent
scraperCertCheck
scraperCDPPath scraperCDPPath
stashBoxes { stashBoxes {
name name

View file

@ -81,6 +81,8 @@ input ConfigGeneralInput {
scraperUserAgent: String scraperUserAgent: String
"""Scraper CDP path. Path to chrome executable or remote address""" """Scraper CDP path. Path to chrome executable or remote address"""
scraperCDPPath: String scraperCDPPath: String
"""Whether the scraper should check for invalid certificates"""
scraperCertCheck: Boolean!
"""Stash-box instances used for tagging""" """Stash-box instances used for tagging"""
stashBoxes: [StashBoxInput!]! stashBoxes: [StashBoxInput!]!
} }
@ -144,6 +146,8 @@ type ConfigGeneralResult {
scraperUserAgent: String scraperUserAgent: String
"""Scraper CDP path. Path to chrome executable or remote address""" """Scraper CDP path. Path to chrome executable or remote address"""
scraperCDPPath: String scraperCDPPath: String
"""Whether the scraper should check for invalid certificates"""
scraperCertCheck: Boolean!
"""Stash-box instances used for tagging""" """Stash-box instances used for tagging"""
stashBoxes: [StashBox!]! stashBoxes: [StashBox!]!
} }

View file

@ -151,6 +151,8 @@ func (r *mutationResolver) ConfigureGeneral(ctx context.Context, input models.Co
refreshScraperCache = true refreshScraperCache = true
} }
config.Set(config.ScraperCertCheck, input.ScraperCertCheck)
if input.StashBoxes != nil { if input.StashBoxes != nil {
if err := config.ValidateStashBoxes(input.StashBoxes); err != nil { if err := config.ValidateStashBoxes(input.StashBoxes); err != nil {
return nil, err return nil, err

View file

@ -71,6 +71,7 @@ func makeConfigGeneralResult() *models.ConfigGeneralResult {
Excludes: config.GetExcludes(), Excludes: config.GetExcludes(),
ImageExcludes: config.GetImageExcludes(), ImageExcludes: config.GetImageExcludes(),
ScraperUserAgent: &scraperUserAgent, ScraperUserAgent: &scraperUserAgent,
ScraperCertCheck: config.GetScraperCertCheck(),
ScraperCDPPath: &scraperCDPPath, ScraperCDPPath: &scraperCDPPath,
StashBoxes: config.GetStashBoxes(), StashBoxes: config.GetStashBoxes(),
} }

View file

@ -86,6 +86,7 @@ const SessionStoreKey = "session_store_key"
// scraping options // scraping options
const ScrapersPath = "scrapers_path" const ScrapersPath = "scrapers_path"
const ScraperUserAgent = "scraper_user_agent" const ScraperUserAgent = "scraper_user_agent"
const ScraperCertCheck = "scraper_cert_check"
const ScraperCDPPath = "scraper_cdp_path" const ScraperCDPPath = "scraper_cdp_path"
// stash-box options // stash-box options
@ -274,6 +275,17 @@ func GetScraperCDPPath() string {
return viper.GetString(ScraperCDPPath) return viper.GetString(ScraperCDPPath)
} }
// GetScraperCertCheck returns true if the scraper should check for insecure
// certificates when fetching an image or a page.
func GetScraperCertCheck() bool {
ret := true
if viper.IsSet(ScraperCertCheck) {
ret = viper.GetBool(ScraperCertCheck)
}
return ret
}
func GetStashBoxes() []*models.StashBox { func GetStashBoxes() []*models.StashBox {
var boxes []*models.StashBox var boxes []*models.StashBox
viper.UnmarshalKey(StashBoxes, &boxes) viper.UnmarshalKey(StashBoxes, &boxes)

View file

@ -1,11 +1,13 @@
package scraper package scraper
import ( import (
"crypto/tls"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"time" "time"
stashConfig "github.com/stashapp/stash/pkg/manager/config"
"github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils" "github.com/stashapp/stash/pkg/utils"
) )
@ -83,6 +85,8 @@ func setMovieBackImage(m *models.ScrapedMovie, globalConfig GlobalConfig) error
func getImage(url string, globalConfig GlobalConfig) (*string, error) { func getImage(url string, globalConfig GlobalConfig) (*string, error) {
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{ // ignore insecure certificates
TLSClientConfig: &tls.Config{InsecureSkipVerify: !stashConfig.GetScraperCertCheck()}},
Timeout: imageGetTimeout, Timeout: imageGetTimeout,
} }

View file

@ -3,6 +3,7 @@ package scraper
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/tls"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -22,6 +23,7 @@ import (
"golang.org/x/net/publicsuffix" "golang.org/x/net/publicsuffix"
"github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/logger"
stashConfig "github.com/stashapp/stash/pkg/manager/config"
) )
// Timeout for the scrape http request. Includes transfer time. May want to make this // Timeout for the scrape http request. Includes transfer time. May want to make this
@ -49,6 +51,9 @@ func loadURL(url string, scraperConfig config, globalConfig GlobalConfig) (io.Re
printCookies(jar, scraperConfig, "Jar cookies set from scraper") printCookies(jar, scraperConfig, "Jar cookies set from scraper")
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{ // ignore insecure certificates
TLSClientConfig: &tls.Config{InsecureSkipVerify: !stashConfig.GetScraperCertCheck()},
},
Timeout: scrapeGetTimeout, Timeout: scrapeGetTimeout,
// defaultCheckRedirect code with max changed from 10 to 20 // defaultCheckRedirect code with max changed from 10 to 20
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {

View file

@ -1,4 +1,5 @@
### 🎨 Improvements ### 🎨 Improvements
* Add option to skip checking of insecure SSL certificates when scraping.
* Auto-play video previews on mobile devices. * Auto-play video previews on mobile devices.
* Replace hover menu with dropdown menu for O-Counter. * Replace hover menu with dropdown menu for O-Counter.
* Support random strings for scraper cookie values. * Support random strings for scraper cookie values.

View file

@ -125,6 +125,7 @@ export const SettingsConfigurationPanel: React.FC = () => {
const [scraperCDPPath, setScraperCDPPath] = useState<string | undefined>( const [scraperCDPPath, setScraperCDPPath] = useState<string | undefined>(
undefined undefined
); );
const [scraperCertCheck, setScraperCertCheck] = useState<boolean>(true);
const [stashBoxes, setStashBoxes] = useState<IStashBoxInstance[]>([]); const [stashBoxes, setStashBoxes] = useState<IStashBoxInstance[]>([]);
const { data, error, loading } = useConfiguration(); const { data, error, loading } = useConfiguration();
@ -164,6 +165,7 @@ export const SettingsConfigurationPanel: React.FC = () => {
imageExcludes, imageExcludes,
scraperUserAgent, scraperUserAgent,
scraperCDPPath, scraperCDPPath,
scraperCertCheck,
stashBoxes: stashBoxes.map( stashBoxes: stashBoxes.map(
(b) => (b) =>
({ ({
@ -212,6 +214,7 @@ export const SettingsConfigurationPanel: React.FC = () => {
setImageExcludes(conf.general.imageExcludes); setImageExcludes(conf.general.imageExcludes);
setScraperUserAgent(conf.general.scraperUserAgent ?? undefined); setScraperUserAgent(conf.general.scraperUserAgent ?? undefined);
setScraperCDPPath(conf.general.scraperCDPPath ?? undefined); setScraperCDPPath(conf.general.scraperCDPPath ?? undefined);
setScraperCertCheck(conf.general.scraperCertCheck);
setStashBoxes( setStashBoxes(
conf.general.stashBoxes.map((box, i) => ({ conf.general.stashBoxes.map((box, i) => ({
name: box?.name ?? undefined, name: box?.name ?? undefined,
@ -717,6 +720,20 @@ export const SettingsConfigurationPanel: React.FC = () => {
http://localhost:9222/json/version) to a Chrome instance. http://localhost:9222/json/version) to a Chrome instance.
</Form.Text> </Form.Text>
</Form.Group> </Form.Group>
<Form.Group>
<Form.Check
id="scaper-cert-check"
checked={scraperCertCheck}
label="Check for insecure certificates"
onChange={() => setScraperCertCheck(!scraperCertCheck)}
/>
<Form.Text className="text-muted">
Some sites use insecure ssl certificates. When unticked the scraper
skips the insecure certificates check and allows scraping of those
sites. If you get a certificate error when scraping untick this.
</Form.Text>
</Form.Group>
</Form.Group> </Form.Group>
<hr /> <hr />