diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-27 00:02:17 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-27 00:02:17 +0200 |
commit | c2b5808ca6ba592507bf49512f8c90883f74ece0 (patch) | |
tree | d1726aade2e189a6207ea7f8496023ac40b0cdf2 /internal/feed | |
parent | a3d2087107312c1816adbc3d25882d36eb5b7d9d (diff) | |
download | feed2imap-go-c2b5808ca6ba592507bf49512f8c90883f74ece0.tar.gz feed2imap-go-c2b5808ca6ba592507bf49512f8c90883f74ece0.tar.bz2 feed2imap-go-c2b5808ca6ba592507bf49512f8c90883f74ece0.zip |
Add tls-no-verify option to disallow certificate checks.
Closes #3
Diffstat (limited to 'internal/feed')
-rw-r--r-- | internal/feed/feed.go | 1 | ||||
-rw-r--r-- | internal/feed/parse.go | 30 | ||||
-rw-r--r-- | internal/feed/state.go | 2 |
3 files changed, 29 insertions, 4 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go index 7b9f0de..f111cd6 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -15,6 +15,7 @@ type Feed struct { feed *gofeed.Feed items []feeditem cached CachedFeed + Global config.GlobalOptions } type feedDescriptor struct { diff --git a/internal/feed/parse.go b/internal/feed/parse.go index c027bd3..cc55581 100644 --- a/internal/feed/parse.go +++ b/internal/feed/parse.go @@ -2,7 +2,9 @@ package feed import ( ctxt "context" + "crypto/tls" "fmt" + "net/http" "time" "github.com/mmcdole/gofeed" @@ -10,14 +12,36 @@ import ( "github.com/Necoro/feed2imap-go/pkg/log" ) -func context() (ctxt.Context, ctxt.CancelFunc) { - return ctxt.WithTimeout(ctxt.Background(), 60*time.Second) +// share HTTP clients +var stdHTTPClient = &http.Client{Transport: http.DefaultTransport} +var unsafeHTTPClient *http.Client + +func init() { + tlsConfig := &tls.Config{InsecureSkipVerify: true} + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.TLSClientConfig = tlsConfig + unsafeHTTPClient = &http.Client{Transport: transport} +} + +func context(timeout int) (ctxt.Context, ctxt.CancelFunc) { + return ctxt.WithTimeout(ctxt.Background(), time.Duration(timeout)*time.Second) +} + +func setHTTPClient(parser *gofeed.Parser, disableTLS bool) { + if disableTLS { + parser.Client = unsafeHTTPClient + } else { + parser.Client = stdHTTPClient + } } func parseFeed(feed *Feed) error { - ctx, cancel := context() + ctx, cancel := context(feed.Global.Timeout) defer cancel() + fp := gofeed.NewParser() + setHTTPClient(fp, *feed.NoTLS) + parsedFeed, err := fp.ParseURLWithContext(feed.Url, ctx) if err != nil { return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err) diff --git a/internal/feed/state.go b/internal/feed/state.go index 455602c..154fafd 100644 --- a/internal/feed/state.go +++ b/internal/feed/state.go @@ -104,7 +104,7 @@ func NewState(cfg *config.Config) *State { } for name, parsedFeed := range cfg.Feeds { - state.feeds[name] = &Feed{Feed: parsedFeed} + state.feeds[name] = &Feed{Feed: parsedFeed, Global: cfg.GlobalOptions} } return &state |