get proxy working

This commit is contained in:
Balakrishnan Balasubramanian 2022-04-29 13:38:49 -04:00
parent dfaaf301f2
commit af4b7297b0
6 changed files with 104 additions and 0 deletions

View File

@ -8,6 +8,9 @@
* Posting to telegram * Posting to telegram
* Storage csv * Storage csv
## MVP 2
* Podcasts
## 1.0 ## 1.0
* concurrent processing * concurrent processing
* Telegram rate limit * Telegram rate limit

22
exp/proxy/main.go Normal file
View File

@ -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)
}

30
exp/tor/proxy.go Normal file
View File

@ -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)
}

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/robfig/cron/v3 v3.0.1 github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.7.1 github.com/stretchr/testify v1.7.1
go.uber.org/zap v1.21.0 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 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
) )

1
go.sum
View File

@ -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-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-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-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/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-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

47
proxy/proxy.go Normal file
View File

@ -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
}