diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-11-02 14:55:59 -0300 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 36a41e03c59624a2b4a7eb174e9a003e288a1d7d (patch) | |
tree | 149c72f53b4fe41308ebc1624ed46037760254e0 /util/proxy.go | |
parent | fbf9732a7a7a599fdc35b7e9e2072d32d2ea9d33 (diff) |
restructuring, part 4 of many
Diffstat (limited to 'util/proxy.go')
-rw-r--r-- | util/proxy.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/util/proxy.go b/util/proxy.go new file mode 100644 index 0000000..1fc9b03 --- /dev/null +++ b/util/proxy.go @@ -0,0 +1,39 @@ +package util + +import ( + "net/http" + "net/url" + "regexp" + "time" + + "github.com/FChannel0/FChannel-Server/config" +) + +func RouteProxy(req *http.Request) (*http.Response, error) { + var proxyType = GetPathProxyType(req.URL.Host) + + if proxyType == "tor" { + proxyUrl, err := url.Parse("socks5://" + config.TorProxy) + if err != nil { + return nil, err + } + + proxyTransport := &http.Transport{Proxy: http.ProxyURL(proxyUrl)} + client := &http.Client{Transport: proxyTransport, Timeout: time.Second * 15} + return client.Do(req) + } + + return http.DefaultClient.Do(req) +} + +func GetPathProxyType(path string) string { + if config.TorProxy != "" { + re := regexp.MustCompile(`(http://|http://)?(www.)?\w+\.onion`) + onion := re.MatchString(path) + if onion { + return "tor" + } + } + + return "clearnet" +} |