Skip to content

Commit

Permalink
specify minimum tls version (#94)
Browse files Browse the repository at this point in the history
* specify minimum tls version

* feat: config supports tls

* add httpOpts to function sigs

* comment TLSVersion

---------

Co-authored-by: Guy Edwards <guyfedwards@gmail.com>
  • Loading branch information
Freyert and guyfedwards authored Aug 15, 2024
1 parent 8455e6d commit 6263b22
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (c Commands) fetchAllFeeds() ([]store.Item, []ErrorItem, error) {
for _, feed := range feeds {
wg.Add(1)

go fetchFeed(ch, &wg, feed, c.config.Version)
go fetchFeed(ch, &wg, feed, c.config.HTTPOptions, c.config.Version)
}

go func() {
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func defaultView(items []store.Item) (is []store.Item) {
return is
}

func fetchFeed(ch chan FetchResultError, wg *sync.WaitGroup, feed config.Feed, version string) {
func fetchFeed(ch chan FetchResultError, wg *sync.WaitGroup, feed config.Feed, httpOpts *config.HTTPOptions, version string) {
defer wg.Done()

r, err := rss.Fetch(feed, version)
r, err := rss.Fetch(feed, httpOpts, version)

if err != nil {
ch <- FetchResultError{res: rss.RSS{}, err: err, url: feed.URL}
Expand Down
24 changes: 18 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"crypto/tls"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -57,12 +58,13 @@ type Config struct {
Pager string `yaml:"pager,omitempty"`
Feeds []Feed `yaml:"feeds"`
// Preview feeds are distinguished from Feeds because we don't want to inadvertenly write those into the config file.
PreviewFeeds []Feed `yaml:"previewfeeds,omitempty"`
Backends *Backends `yaml:"backends,omitempty"`
ShowRead bool `yaml:"showread,omitempty"`
AutoRead bool `yaml:"autoread,omitempty"`
Openers []Opener `yaml:"openers,omitempty"`
Theme Theme `yaml:"theme,omitempty"`
PreviewFeeds []Feed `yaml:"previewfeeds,omitempty"`
Backends *Backends `yaml:"backends,omitempty"`
ShowRead bool `yaml:"showread,omitempty"`
AutoRead bool `yaml:"autoread,omitempty"`
Openers []Opener `yaml:"openers,omitempty"`
Theme Theme `yaml:"theme,omitempty"`
HTTPOptions *HTTPOptions `yaml:"http,omitempty"`
}

func (c *Config) ToggleShowRead() {
Expand Down Expand Up @@ -108,6 +110,9 @@ func New(configPath string, pager string, previewFeeds []string, version string)
TitleColor: "62",
FilterColor: "62",
},
HTTPOptions: &HTTPOptions{
MinTLSVersion: tls.VersionName(tls.VersionTLS12),
},
}, nil
}

Expand Down Expand Up @@ -138,6 +143,13 @@ func (c *Config) Load() error {
c.Feeds = fileConfig.Feeds
c.Openers = fileConfig.Openers

if fileConfig.HTTPOptions != nil {
if _, err := TLSVersion(fileConfig.HTTPOptions.MinTLSVersion); err != nil {
return err
}
c.HTTPOptions = fileConfig.HTTPOptions
}

if fileConfig.Theme.Glamour != "" {
c.Theme.Glamour = fileConfig.Theme.Glamour
}
Expand Down
29 changes: 29 additions & 0 deletions internal/config/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"crypto/tls"
"fmt"
)

// CloudFlare blocks requests unless a minimum TLSVersion is specified.
var tlsVersions map[string]uint16 = map[string]uint16{
"TLS 1.0": tls.VersionTLS10,
"TLS 1.1": tls.VersionTLS11,
"TLS 1.2": tls.VersionTLS12,
"TLS 1.3": tls.VersionTLS13,
}

type HTTPOptions struct {
//MinTLSVersion must be set to one of the strings returned by
//tls.VersionName. "TLS 1.2" by default.
MinTLSVersion string `yaml:"mintls,omitempty"`
}

// TLSVersion maps one of a few supported TLS version strings to the corresponding
// standard TLS library constant so that an HTTP client can be configured.
func TLSVersion(configStr string) (uint16, error) {
if version, ok := tlsVersions[configStr]; ok {
return version, nil
}
return 0, fmt.Errorf("unsupported tls version: %s", configStr)
}
17 changes: 16 additions & 1 deletion internal/rss/rss.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package rss

import (
"crypto/tls"
"fmt"
"net/http"
"time"

"github.com/mmcdole/gofeed"
Expand Down Expand Up @@ -31,8 +33,21 @@ type RSS struct {
Channel Channel `xml:"channel"`
}

func Fetch(f config.Feed, version string) (RSS, error) {
func Fetch(f config.Feed, httpOpts *config.HTTPOptions, version string) (RSS, error) {
fp := gofeed.NewParser()

fp.Client = &http.Client{}

if httpOpts != nil {
if version, err := config.TLSVersion(httpOpts.MinTLSVersion); err == nil {
fp.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: version,
},
}
}
}

fp.UserAgent = fmt.Sprintf("nom/%s", version)

feed, err := fp.ParseURL(f.URL)
Expand Down

0 comments on commit 6263b22

Please sign in to comment.