dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit b9b281097e13a56523ed68c06b4ccc71fc593303
parent c9692df274284bcd5933c1180a6e2f511f3d1598
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 30 Jan 2023 16:03:06 -0800

add tor http client

Diffstat:
Mcmd/dkfupload/main.go | 56++++++++++++++++++++++++++++++++++++++++++++++++++------
Mgo.mod | 2+-
2 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/cmd/dkfupload/main.go b/cmd/dkfupload/main.go @@ -1,21 +1,35 @@ package main import ( + "context" "flag" + "fmt" "github.com/dustin/go-humanize" + "golang.org/x/net/proxy" "log" "math" + "net" + "net/http" + "net/http/cookiejar" "os" "strings" "sync" ) +const ( + userAgent = "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0" + torProxyAddr = "127.0.0.1:9050" + dkfBaseURL = "http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion" +) + func main() { var nbThreads int var apiKey string + var filedropUUID string var fileName string var dry bool flag.StringVar(&apiKey, "api-key", "", "dkf api key") + flag.StringVar(&filedropUUID, "filedrop-uuid", "", "dkf filedrop uuid") flag.StringVar(&fileName, "file", "", "file to upload") flag.StringVar(&fileName, "f", "", "file to upload") flag.IntVar(&nbThreads, "threads", 20, "nb threads") @@ -35,13 +49,14 @@ func main() { fileSize := fs.Size() nbChunks := int64(math.Ceil(float64(fileSize) / float64(maxChunkSize))) - log.Printf(" file: %s\n", fs.Name()) - log.Printf(" file size: %s (%s)\n", humanize.Bytes(uint64(fileSize)), humanize.Comma(fileSize)) - log.Printf("Chunks size: %s (%s)\n", humanize.Bytes(uint64(maxChunkSize)), humanize.Comma(maxChunkSize)) - log.Printf(" Nb chunks: %d\n", nbChunks) - log.Printf(" Nb threads: %d\n", nbThreads) + log.Printf("filedrop UUID: %s\n", filedropUUID) + log.Printf(" file: %s\n", fs.Name()) + log.Printf(" file size: %s (%s)\n", humanize.Bytes(uint64(fileSize)), humanize.Comma(fileSize)) + log.Printf(" chunks size: %s (%s)\n", humanize.Bytes(uint64(maxChunkSize)), humanize.Comma(maxChunkSize)) + log.Printf(" nb chunks: %d\n", nbChunks) + log.Printf(" nb threads: %d\n", nbThreads) if dry { - log.Printf(" dry run: %t\n", dry) + log.Printf(" dry run: %t\n", dry) } log.Println(strings.Repeat("-", 80)) @@ -57,6 +72,12 @@ func main() { wg.Add(nbThreads) for i := 0; i < nbThreads; i++ { go func(i int, wg *sync.WaitGroup, chunksCh chan int64) { + username := fmt.Sprintf("user_%d", i) + password := username + client, err := GetHttpClient(&proxy.Auth{User: username, Password: password}) + if err != nil { + log.Fatalln(err.Error()) + } buf := make([]byte, maxChunkSize) for chunkNum := range chunksCh { offset := int64(chunkNum) * maxChunkSize @@ -64,6 +85,11 @@ func main() { log.Printf("Thread #%03d | chunk #%03d | read %d | from %d to %d\n", i, chunkNum, n, offset, offset+int64(n)) if !dry { // TODO: http post the chunk to server + req, _ := http.NewRequest(http.MethodPost, dkfBaseURL+"/filedrop", nil) + req.Header.Set("User-Agent", userAgent) + _, err := client.Do(req) + if err != nil { + } } } wg.Done() @@ -73,3 +99,21 @@ func main() { wg.Wait() log.Printf("All done\n") } + +// GetHttpClient http client that uses tor proxy +func GetHttpClient(auth *proxy.Auth) (*http.Client, error) { + dialer, err := proxy.SOCKS5("tcp", torProxyAddr, auth, proxy.Direct) + if err != nil { + return nil, fmt.Errorf("failed to connect to tor proxy : %w", err) + } + transport := &http.Transport{ + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + return dialer.Dial(network, addr) + }, + } + jar, err := cookiejar.New(nil) + if err != nil { + return nil, fmt.Errorf("failed to create cookie jar : %w", err) + } + return &http.Client{Transport: transport, Jar: jar}, nil +} diff --git a/go.mod b/go.mod @@ -33,6 +33,7 @@ require ( github.com/urfave/cli/v2 v2.3.0 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 golang.org/x/image v0.0.0-20211028202545-6944b10bf410 + golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 golang.org/x/text v0.3.6 gopkg.in/telegram-bot-api.v4 v4.6.4 gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 @@ -59,7 +60,6 @@ require ( github.com/technoweenie/multipartstreamer v1.0.1 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.1.0 // indirect - golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect gopkg.in/gorp.v1 v1.7.2 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect