diff --git a/TODO.md b/TODO.md index ec3d918..a58bd33 100644 --- a/TODO.md +++ b/TODO.md @@ -8,6 +8,9 @@ * Posting to telegram * Storage csv +## MVP 2 +* Podcasts + ## 1.0 * concurrent processing * Telegram rate limit diff --git a/exp/proxy/main.go b/exp/proxy/main.go new file mode 100644 index 0000000..f9e8d97 --- /dev/null +++ b/exp/proxy/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "io" + "log" + + "go.balki.me/tss/proxy" +) + +func main() { + client, err := proxy.GetClient("socks5://unix/run/tor/socks") + res, err := client.Get("https://ip.balki.me") + if err != nil { + log.Panicln(err) + } + data, err := io.ReadAll(res.Body) + if err != nil { + log.Panicln(err) + } + fmt.Printf("status: %v, data: %s\n", res.Status, data) +} diff --git a/exp/tor/proxy.go b/exp/tor/proxy.go new file mode 100644 index 0000000..f2bb32e --- /dev/null +++ b/exp/tor/proxy.go @@ -0,0 +1,30 @@ +package main + +import ( + "io" + "log" + "net/http" + "net/url" +) + +func main() { + //proxyUrl, err := url.Parse("socks5://unix//run/tor/socks") + proxyUrl, err := url.Parse("socks5://127.0.0.1:9050/#Foo") + if err != nil { + log.Panicln(err) + } + log.Println(len(proxyUrl.Path)) + client := http.Client{ + Transport: &http.Transport{ + Proxy: func(*http.Request) (*url.URL, error) { return proxyUrl, nil }, + }, + } + // res, err := client.Get("http://hz3ppwwc55ccokb63r4ou7za45c5raodu23absuregtqo5ha3h37gfyd.onion") + res, err := client.Get("https://ip.balki.me") + if err != nil { + log.Panicln(err) + } + + data, err := io.ReadAll(res.Body) + log.Printf("data: %s, status: %s\n", data, res.Status) +} diff --git a/go.mod b/go.mod index 0412b52..14e436c 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/stretchr/testify v1.7.1 go.uber.org/zap v1.21.0 + golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index 644c352..ebee1af 100644 --- a/go.sum +++ b/go.sum @@ -35,6 +35,7 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/proxy/proxy.go b/proxy/proxy.go new file mode 100644 index 0000000..84ddff9 --- /dev/null +++ b/proxy/proxy.go @@ -0,0 +1,47 @@ +package proxy + +import ( + "fmt" + "net/http" + "net/url" + + "golang.org/x/net/proxy" +) + +func GetClient(proxy string) (*http.Client, error) { + proxyUrl, err := url.Parse(proxy) + if err != nil { + return nil, fmt.Errorf("failed to parse proxyUrl, url:%s, err: %w", proxy, err) + } + if proxyUrl.Host == "unix" && proxyUrl.Scheme == "socks5" && + len(proxyUrl.Path) > 1 /* Path cannot be empty or just / */ { + return unixSocks5Proxy(proxyUrl.Path) + } + return proxyHttp(proxyUrl) +} + +func unixSocks5Proxy(path string) (*http.Client, error) { + // TODO: Auth? + dialer, err := proxy.SOCKS5("unix", path, nil /*auth*/, nil) + if err != nil { + return nil, fmt.Errorf("failed to make socks proxy, path: %s, err: %w", path, err) + } + ctxDialer, ok := dialer.(proxy.ContextDialer) + if !ok { + return nil, fmt.Errorf("proxy.SOCKS5 did not return a ContextDialer") // This should never happen + } + + return &http.Client{ + Transport: &http.Transport{ + DialContext: ctxDialer.DialContext, + }, + }, nil +} + +func proxyHttp(proxyUrl *url.URL) (*http.Client, error) { + return &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyURL(proxyUrl), + }, + }, nil +}