From 715e91be3cfedb87db1208f77e1742114f4e79f4 Mon Sep 17 00:00:00 2001 From: eyedeekay Date: Wed, 30 Apr 2025 22:40:57 -0400 Subject: [PATCH] add modules and examples --- example/httpproxy/main.go | 89 +++++++++++++++++++++++++++++++++++++++ go.mod | 15 +++++++ go.sum | 39 +++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 example/httpproxy/main.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/example/httpproxy/main.go b/example/httpproxy/main.go new file mode 100644 index 0000000..a03e5dc --- /dev/null +++ b/example/httpproxy/main.go @@ -0,0 +1,89 @@ +package main + +import ( + "bufio" + "bytes" + "io" + "log" + "net" + "net/http" + + metadialer "github.com/go-i2p/go-meta-dialer" +) + +// This example demonstrates how to use the MetaHTTPClient to handle HTTP requests +// over I2P and Tor networks. It sets up a simple HTTP proxy server that listens +// for incoming connections and forwards them to the appropriate dialer based on +// the URL scheme (I2P or Tor). The response is then sent back to the client. +// The MetaHTTPClient is a custom HTTP client that uses the MetaDialer to handle +// connections to I2P and Tor networks. + +func main() { + // Example usage of the MetaHTTPClient + client := metadialer.NewMetaHTTPClient(nil) + proxyListener, err := net.Listen("tcp", ":8080") + if err != nil { + panic(err) + } + defer proxyListener.Close() + // Start the proxy server + for { + conn, err := proxyListener.Accept() + if err != nil { + continue + } + go func(c net.Conn) { + defer c.Close() + // read the request from the connection + b, err := io.ReadAll(c) + if err != nil { + log.Println("Error reading request:", err) + return + } + // parse the request + oldreq, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(b))) + if err != nil { + log.Println("Error parsing request:", err) + return + } + // obtain the method and URL from the request + method := oldreq.Method + url := oldreq.URL.String() + // recreate the body + body := bytes.NewBuffer(b) + // print the method and URL + log.Println("Method:", method) + log.Println("URL:", url) + + // Handle the connection using the MetaHTTPClient + req, err := http.NewRequest(method, url, body) + if err != nil { + log.Println("Error creating request:", err) + return + } + resp, err := client.Do(req) + if err != nil { + log.Println("Error making request:", err) + return + } + defer resp.Body.Close() + // get the response body + respBody, err := io.ReadAll(resp.Body) + if err != nil { + log.Println("Error reading response body:", err) + return + } + // write the response back to the connection + _, err = c.Write(respBody) + if err != nil { + log.Println("Error writing response:", err) + return + } + // close the connection + c.Close() + // print the response status + log.Println("Response Status:", resp.Status) + }(conn) + } + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cdafb92 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module github.com/go-i2p/go-meta-dialer + +go 1.24.2 + +require github.com/go-i2p/onramp v0.33.92 + +require ( + github.com/cretz/bine v0.2.0 // indirect + github.com/go-i2p/i2pkeys v0.33.10-0.20241113193422-e10de5e60708 // indirect + github.com/go-i2p/sam3 v0.33.9 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect + golang.org/x/crypto v0.29.0 // indirect + golang.org/x/net v0.31.0 // indirect + golang.org/x/sys v0.27.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e8a294c --- /dev/null +++ b/go.sum @@ -0,0 +1,39 @@ +github.com/cretz/bine v0.2.0 h1:8GiDRGlTgz+o8H9DSnsl+5MeBK4HsExxgl6WgzOCuZo= +github.com/cretz/bine v0.2.0/go.mod h1:WU4o9QR9wWp8AVKtTM1XD5vUHkEqnf2vVSo6dBqbetI= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-i2p/i2pkeys v0.0.0-20241108200332-e4f5ccdff8c4/go.mod h1:m5TlHjPZrU5KbTd7Lr+I2rljyC6aJ88HdkeMQXV0U0E= +github.com/go-i2p/i2pkeys v0.33.10-0.20241113193422-e10de5e60708 h1:Tiy9IBwi21maNpK74yCdHursJJMkyH7w87tX1nXGWzg= +github.com/go-i2p/i2pkeys v0.33.10-0.20241113193422-e10de5e60708/go.mod h1:m5TlHjPZrU5KbTd7Lr+I2rljyC6aJ88HdkeMQXV0U0E= +github.com/go-i2p/onramp v0.33.92 h1:Dk3A0SGpdEw829rSjW2LqN8o16pUvuhiN0vn36z7Gpc= +github.com/go-i2p/onramp v0.33.92/go.mod h1:5sfB8H2xk05gAS2K7XAUZ7ekOfwGJu3tWF0fqdXzJG4= +github.com/go-i2p/sam3 v0.33.9 h1:3a+gunx75DFc6jxloUZTAVJbdP6736VU1dy2i7I9fKA= +github.com/go-i2p/sam3 v0.33.9/go.mod h1:oDuV145l5XWKKafeE4igJHTDpPwA0Yloz9nyKKh92eo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ= +golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo= +golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=