mirror of
https://github.com/glanceapp/glance.git
synced 2025-06-21 18:31:24 +02:00
Allow using a standard HTTP proxy in reddit widget
This commit is contained in:
parent
abbb4950a5
commit
108c83588c
@ -746,6 +746,7 @@ Example:
|
|||||||
| collapse-after | integer | no | 5 |
|
| collapse-after | integer | no | 5 |
|
||||||
| comments-url-template | string | no | https://www.reddit.com/{POST-PATH} |
|
| comments-url-template | string | no | https://www.reddit.com/{POST-PATH} |
|
||||||
| request-url-template | string | no | |
|
| request-url-template | string | no | |
|
||||||
|
| proxy | string or multiple parameters | no | |
|
||||||
| sort-by | string | no | hot |
|
| sort-by | string | no | hot |
|
||||||
| top-period | string | no | day |
|
| top-period | string | no | day |
|
||||||
| search | string | no | |
|
| search | string | no | |
|
||||||
@ -807,7 +808,7 @@ r/selfhosted/comments/bsp01i/welcome_to_rselfhosted_please_read_this_first/
|
|||||||
`{SUBREDDIT}` - the subreddit name
|
`{SUBREDDIT}` - the subreddit name
|
||||||
|
|
||||||
##### `request-url-template`
|
##### `request-url-template`
|
||||||
A custom request url that will be used to fetch the data instead. This is useful when you're hosting Glance on a VPS and Reddit is blocking the requests, and you want to route it through an HTTP proxy.
|
A custom request URL that will be used to fetch the data. This is useful when you're hosting Glance on a VPS where Reddit is blocking the requests and you want to route them through a proxy that accepts the URL as either a part of the path or a query parameter.
|
||||||
|
|
||||||
Placeholders:
|
Placeholders:
|
||||||
|
|
||||||
@ -818,6 +819,29 @@ https://proxy/{REQUEST-URL}
|
|||||||
https://your.proxy/?url={REQUEST-URL}
|
https://your.proxy/?url={REQUEST-URL}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##### `proxy`
|
||||||
|
A custom HTTP/HTTPS proxy URL that will be used to fetch the data. This is useful when you're hosting Glance on a VPS where Reddit is blocking the requests and you want to bypass the restriction by routing the requests through a proxy. Example:
|
||||||
|
|
||||||
|
```
|
||||||
|
proxy: http://user:pass@proxy.com:8080
|
||||||
|
proxy: https://user:pass@proxy.com:443
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, you can specify the proxy URL as well as additional options by using multiple parameters:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
proxy:
|
||||||
|
url: http://proxy.com:8080
|
||||||
|
allow-insecure: true
|
||||||
|
timeout: 10s
|
||||||
|
```
|
||||||
|
|
||||||
|
###### `allow-insecure`
|
||||||
|
When set to `true`, allows the use of insecure connections such as when the proxy has a self-signed certificate.
|
||||||
|
|
||||||
|
###### `timeout`
|
||||||
|
The maximum time to wait for a response from the proxy. The value is a string and must be a number followed by one of s, m, h, d. Example: `10s` for 10 seconds, `1m` for 1 minute, etc
|
||||||
|
|
||||||
##### `sort-by`
|
##### `sort-by`
|
||||||
Can be used to specify the order in which the posts should get returned. Possible values are `hot`, `new`, `top` and `rising`.
|
Can be used to specify the order in which the posts should get returned. Possible values are `hot`, `new`, `top` and `rising`.
|
||||||
|
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package glance
|
package glance
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -169,3 +172,50 @@ func (i *customIconField) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
*i = newCustomIconField(value)
|
*i = newCustomIconField(value)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type proxyOptionsField struct {
|
||||||
|
URL string `yaml:"url"`
|
||||||
|
AllowInsecure bool `yaml:"allow-insecure"`
|
||||||
|
Timeout durationField `yaml:"timeout"`
|
||||||
|
client *http.Client `yaml:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *proxyOptionsField) UnmarshalYAML(node *yaml.Node) error {
|
||||||
|
type proxyOptionsFieldAlias proxyOptionsField
|
||||||
|
alias := (*proxyOptionsFieldAlias)(p)
|
||||||
|
var proxyURL string
|
||||||
|
|
||||||
|
if err := node.Decode(&proxyURL); err != nil {
|
||||||
|
if err := node.Decode(alias); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if proxyURL == "" && p.URL == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.URL != "" {
|
||||||
|
proxyURL = p.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedUrl, err := url.Parse(proxyURL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parsing proxy URL: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var timeout = defaultClientTimeout
|
||||||
|
if p.Timeout > 0 {
|
||||||
|
timeout = time.Duration(p.Timeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
p.client = &http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
Proxy: http.ProxyURL(parsedUrl),
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: p.AllowInsecure},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -19,19 +19,20 @@ var (
|
|||||||
|
|
||||||
type redditWidget struct {
|
type redditWidget struct {
|
||||||
widgetBase `yaml:",inline"`
|
widgetBase `yaml:",inline"`
|
||||||
Posts forumPostList `yaml:"-"`
|
Posts forumPostList `yaml:"-"`
|
||||||
Subreddit string `yaml:"subreddit"`
|
Subreddit string `yaml:"subreddit"`
|
||||||
Style string `yaml:"style"`
|
Proxy proxyOptionsField `yaml:"proxy"`
|
||||||
ShowThumbnails bool `yaml:"show-thumbnails"`
|
Style string `yaml:"style"`
|
||||||
ShowFlairs bool `yaml:"show-flairs"`
|
ShowThumbnails bool `yaml:"show-thumbnails"`
|
||||||
SortBy string `yaml:"sort-by"`
|
ShowFlairs bool `yaml:"show-flairs"`
|
||||||
TopPeriod string `yaml:"top-period"`
|
SortBy string `yaml:"sort-by"`
|
||||||
Search string `yaml:"search"`
|
TopPeriod string `yaml:"top-period"`
|
||||||
ExtraSortBy string `yaml:"extra-sort-by"`
|
Search string `yaml:"search"`
|
||||||
CommentsUrlTemplate string `yaml:"comments-url-template"`
|
ExtraSortBy string `yaml:"extra-sort-by"`
|
||||||
Limit int `yaml:"limit"`
|
CommentsUrlTemplate string `yaml:"comments-url-template"`
|
||||||
CollapseAfter int `yaml:"collapse-after"`
|
Limit int `yaml:"limit"`
|
||||||
RequestUrlTemplate string `yaml:"request-url-template"`
|
CollapseAfter int `yaml:"collapse-after"`
|
||||||
|
RequestUrlTemplate string `yaml:"request-url-template"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *redditWidget) initialize() error {
|
func (widget *redditWidget) initialize() error {
|
||||||
@ -94,6 +95,7 @@ func (widget *redditWidget) update(ctx context.Context) {
|
|||||||
widget.Search,
|
widget.Search,
|
||||||
widget.CommentsUrlTemplate,
|
widget.CommentsUrlTemplate,
|
||||||
widget.RequestUrlTemplate,
|
widget.RequestUrlTemplate,
|
||||||
|
widget.Proxy.client,
|
||||||
widget.ShowFlairs,
|
widget.ShowFlairs,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -161,7 +163,16 @@ func templateRedditCommentsURL(template, subreddit, postId, postPath string) str
|
|||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate, requestUrlTemplate string, showFlairs bool) (forumPostList, error) {
|
func fetchSubredditPosts(
|
||||||
|
subreddit,
|
||||||
|
sort,
|
||||||
|
topPeriod,
|
||||||
|
search,
|
||||||
|
commentsUrlTemplate,
|
||||||
|
requestUrlTemplate string,
|
||||||
|
proxyClient *http.Client,
|
||||||
|
showFlairs bool,
|
||||||
|
) (forumPostList, error) {
|
||||||
query := url.Values{}
|
query := url.Values{}
|
||||||
var requestUrl string
|
var requestUrl string
|
||||||
|
|
||||||
@ -180,8 +191,12 @@ func fetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate
|
|||||||
requestUrl = fmt.Sprintf("https://www.reddit.com/r/%s/%s.json?%s", subreddit, sort, query.Encode())
|
requestUrl = fmt.Sprintf("https://www.reddit.com/r/%s/%s.json?%s", subreddit, sort, query.Encode())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var client requestDoer = defaultHTTPClient
|
||||||
|
|
||||||
if requestUrlTemplate != "" {
|
if requestUrlTemplate != "" {
|
||||||
requestUrl = strings.ReplaceAll(requestUrlTemplate, "{REQUEST-URL}", requestUrl)
|
requestUrl = strings.ReplaceAll(requestUrlTemplate, "{REQUEST-URL}", requestUrl)
|
||||||
|
} else if proxyClient != nil {
|
||||||
|
client = proxyClient
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := http.NewRequest("GET", requestUrl, nil)
|
request, err := http.NewRequest("GET", requestUrl, nil)
|
||||||
@ -191,7 +206,7 @@ func fetchSubredditPosts(subreddit, sort, topPeriod, search, commentsUrlTemplate
|
|||||||
|
|
||||||
// Required to increase rate limit, otherwise Reddit randomly returns 429 even after just 2 requests
|
// Required to increase rate limit, otherwise Reddit randomly returns 429 even after just 2 requests
|
||||||
setBrowserUserAgentHeader(request)
|
setBrowserUserAgentHeader(request)
|
||||||
responseJson, err := decodeJsonFromRequest[subredditResponseJson](defaultHTTPClient, request)
|
responseJson, err := decodeJsonFromRequest[subredditResponseJson](client, request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user