Enforce Tor on Feeds

This commit is contained in:
Sarah Jamie Lewis 2019-06-25 16:12:59 -07:00
parent 0a36628ddf
commit 230cdac046
2 changed files with 19 additions and 12 deletions

View File

@ -49,4 +49,3 @@ You can also download images (or technically any other file) using a file called
You will need to be running a local `tor` proxy on port `9050`
There is very little in the way of graceful error handling, contributions appreciated, please also feel free to submit issues & feature requests.

30
main.go
View File

@ -19,9 +19,27 @@ import (
"time"
)
func makeTorifiedClient() *http.Client {
torDialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct)
if err != nil {
log.Fatalf("Could not connect to Tor Proxy: %v", err)
}
transportConfig := &http.Transport{
Dial: torDialer.Dial,
}
client := new(http.Client)
client.Transport = transportConfig
client.CheckRedirect = func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
}
return client
}
func fetch(url string, cachepath string) {
fmt.Printf("Fetching [%v]\n", url)
fp := gofeed.NewParser()
fp.Client = makeTorifiedClient()
feed, err := fp.ParseURL(url)
fmt.Printf("Feed %v %v\n", feed, err)
cache, _ := json.Marshal(feed)
@ -51,17 +69,7 @@ func report() map[string]gofeed.Feed {
func download(url string, cachepath string) {
fmt.Printf("Fetching [%v]\n", url)
torDialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct)
transportConfig := &http.Transport{
Dial: torDialer.Dial,
}
client := http.Client{
Transport: transportConfig,
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
client := makeTorifiedClient()
resp, err := client.Get(url)
if err == nil {