feeds always fetches via wghttp proxy
This commit is contained in:
@@ -3,6 +3,9 @@ package feeds
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
@@ -14,6 +17,16 @@ import (
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
ProxyU = func() *url.URL {
|
||||
u, err := url.Parse("socks5://wghttp.inhome.blapointe.com:63114")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return u
|
||||
}()
|
||||
)
|
||||
|
||||
func (feed Feed) ShouldExecute() (bool, error) {
|
||||
if feed.Entry.Deleted.IsZero() {
|
||||
return false, nil
|
||||
@@ -36,7 +49,12 @@ func (feed Feed) ShouldExecute() (bool, error) {
|
||||
}
|
||||
|
||||
func (feed Feed) Fetch(ctx context.Context) (Items, error) {
|
||||
gfeed, err := gofeed.NewParser().ParseURLWithContext(feed.Version.URL, ctx)
|
||||
resp, err := proxyFetch(ctx, feed.Version.URL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
gfeed, err := gofeed.NewParser().ParseString(resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -84,3 +102,30 @@ func (feed Feed) Fetch(ctx context.Context) (Items, error) {
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func proxyFetch(ctx context.Context, u string) (string, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
c := http.Client{Timeout: time.Minute}
|
||||
if ProxyU != nil {
|
||||
c.Transport = &http.Transport{Proxy: http.ProxyURL(ProxyU)}
|
||||
}
|
||||
|
||||
resp, err := c.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer io.Copy(io.Discard, resp.Body)
|
||||
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("failed fetch: (%d) %s", resp.StatusCode, b)
|
||||
}
|
||||
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user