From e3eba9b6c64b1e8d884a51638deca0078f2f4760 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Tue, 11 May 2021 20:10:57 -0700 Subject: added proxy for tor --- main.go | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 43ea539..cedc09c 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import "fmt" import "strings" import "strconv" import "net/http" +import "net/url" import "database/sql" import _ "github.com/lib/pq" import "math/rand" @@ -33,6 +34,8 @@ var SiteEmailPassword = GetConfigValue("emailpass") var SiteEmailServer = GetConfigValue("emailserver") //mail.fchan.xyz var SiteEmailPort = GetConfigValue("emailport") //587 +var TorProxy = "127.0.0.1:9050" + var ldjson = "application/ld+json" var activitystreams = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" @@ -55,7 +58,7 @@ func main() { FollowingBoards = GetActorFollowingDB(db, Domain) Boards = GetBoardCollection(db) - + // root actor is used to follow remote feeds that are not local //name, prefname, summary, auth requirements, restricted if GetConfigValue("instancename") != "" { @@ -1982,19 +1985,48 @@ func localShort(url string) string { } func remoteShort(url string) string { - re := regexp.MustCompile(`\w+$`) + re := regexp.MustCompile(`\w+$`) + + id := re.FindString(StripTransferProtocol(url)); - id := re.FindString(StripTransferProtocol(url)); + re = regexp.MustCompile(`.+/.+/`) - re = regexp.MustCompile(`.+/.+/`) + actorurl := re.FindString(StripTransferProtocol(url)) - actorurl := re.FindString(StripTransferProtocol(url)) + re = regexp.MustCompile(`/.+/`) + + actorname := re.FindString(actorurl) + + actorname = strings.Replace(actorname, "/", "", -1) + + return "f" + actorname + "-" + id +} - re = regexp.MustCompile(`/.+/`) +func RouteProxy(req *http.Request) (*http.Response, error) { - actorname := re.FindString(actorurl) + var proxyType = GetPathProxyType(req.URL.Host) - actorname = strings.Replace(actorname, "/", "", -1) + if proxyType == "tor" { + proxyUrl, err := url.Parse("socks5://" + TorProxy) + + CheckError(err, "error parsing tor proxy url") + + proxyTransport := &http.Transport{Proxy: http.ProxyURL(proxyUrl)} + client := &http.Client{ Transport: proxyTransport, Timeout: time.Second * 10 } + return client.Do(req) + } + + return http.DefaultClient.Do(req) +} + +func GetPathProxyType(path string) string { + if TorProxy != "" { + re := regexp.MustCompile(`(http://|http://)?(www.)?\w+\.onion`) + onion := re.MatchString(path) + if onion { + return "tor" + } + } - return "f" + actorname + "-" + id + return "clearnet" } -- cgit v1.2.3