From 36a41e03c59624a2b4a7eb174e9a003e288a1d7d Mon Sep 17 00:00:00 2001 From: KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> Date: Tue, 2 Nov 2021 14:55:59 -0300 Subject: restructuring, part 4 of many --- util/proxy.go | 39 +++++++++++++++++++++++++++++++++++++++ util/util.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 util/proxy.go (limited to 'util') 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" +} diff --git a/util/util.go b/util/util.go index 7164937..4ac8267 100644 --- a/util/util.go +++ b/util/util.go @@ -12,3 +12,40 @@ func IsOnion(url string) bool { return false } + +func GetActorInstance(path string) (string, string) { + re := regexp.MustCompile(`([@]?([\w\d.-_]+)[@](.+))`) + atFormat := re.MatchString(path) + + if atFormat { + match := re.FindStringSubmatch(path) + if len(match) > 2 { + return match[2], match[3] + } + } + + re = regexp.MustCompile(`(https?://)(www)?([\w\d-_.:]+)(/|\s+|\r|\r\n)?$`) + mainActor := re.MatchString(path) + if mainActor { + match := re.FindStringSubmatch(path) + if len(match) > 2 { + return "main", match[3] + } + } + + re = regexp.MustCompile(`(https?://)?(www)?([\w\d-_.:]+)\/([\w\d-_.]+)(\/([\w\d-_.]+))?`) + httpFormat := re.MatchString(path) + + if httpFormat { + match := re.FindStringSubmatch(path) + if len(match) > 3 { + if match[4] == "users" { + return match[6], match[3] + } + + return match[4], match[3] + } + } + + return "", "" +} -- cgit v1.2.3