Files
goSam/README.md

184 lines
3.7 KiB
Markdown
Raw Permalink Normal View History

2014-02-09 05:40:28 -08:00
goSam
=====
A go library for using the [I2P](https://geti2p.net/en/) Simple Anonymous
Messaging ([SAM version 3.0](https://geti2p.net/en/docs/api/samv3)) bridge. It
has support for all streaming features SAM version 3.2.
2022-08-28 13:37:45 -04:00
STATUS: This project is maintained. I will respond to issues, pull requests, and feature requests within a few days. I am primarily maintaining functionality. This is widely used and easy to use, but thusfar, mostly by me. It sees a lot of testing and no breaking changes to the API are expected.
2024-11-14 10:38:29 -05:00
[![Go Report Card](https://goreportcard.com/badge/github.com/go-i2p/gosam)](https://goreportcard.com/report/github.com/go-i2p/gosam)
## Installation
```
2024-11-14 10:38:29 -05:00
go get github.com/go-i2p/gosam
```
## Using it for HTTP Transport
`Client.Dial` implements `net.Dial` so you can use go's library packages like http.
```go
package main
import (
"io"
"log"
"net/http"
"os"
2024-11-14 10:38:29 -05:00
"github.com/go-i2p/gosam"
)
func main() {
// create a default sam client
sam, err := goSam.NewDefaultClient()
checkErr(err)
log.Println("Client Created")
// create a transport that uses SAM to dial TCP Connections
tr := &http.Transport{
Dial: sam.Dial,
}
// create a client using this transport
client := &http.Client{Transport: tr}
// send a get request
resp, err := client.Get("http://stats.i2p/")
checkErr(err)
defer resp.Body.Close()
log.Printf("Get returned %+v\n", resp)
// create a file for the response
file, err := os.Create("stats.html")
checkErr(err)
defer file.Close()
// copy the response to the file
_, err = io.Copy(file, resp.Body)
checkErr(err)
log.Println("Done.")
}
2022-03-29 18:37:38 -04:00
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
```
### Using SAM by default, as a proxy for all HTTP Clients used by a Go application
This will make the SAM transport dialer the default for all HTTP clients.
```go
package main
import (
"io"
"log"
"net/http"
"os"
2024-11-14 10:38:29 -05:00
"github.com/go-i2p/gosam"
2022-03-29 18:37:38 -04:00
)
func main() {
sam, err := goSam.NewDefaultClient()
checkErr(err)
log.Println("Client Created")
// create a transport that uses SAM to dial TCP Connections
httpClient := &http.Client{
Transport: &http.Transport{
Dial: sam.Dial,
},
}
http.DefaultClient = httpClient
return nil
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
```
## Using it as a SOCKS proxy
`client` also implements a resolver compatible with
[`getlantern/go-socks5`](https://github.com/getlantern/go-socks5),
making it very easy to implement a SOCKS5 server.
```go
package main
import (
"flag"
2024-11-14 10:38:29 -05:00
"github.com/go-i2p/gosam"
"github.com/getlantern/go-socks5"
"log"
)
var (
samaddr = flag.String("sam", "127.0.0.1:7656", "SAM API address to use")
socksaddr = flag.String("socks", "127.0.0.1:7675", "SOCKS address to use")
)
func main() {
sam, err := goSam.NewClient(*samaddr)
if err != nil {
panic(err)
}
log.Println("Client Created")
// create a transport that uses SAM to dial TCP Connections
conf := &socks5.Config{
Dial: sam.DialContext,
Resolver: sam,
}
server, err := socks5.New(conf)
if err != nil {
panic(err)
}
// Create SOCKS5 proxy on localhost port 8000
if err := server.ListenAndServe("tcp", *socksaddr); err != nil {
panic(err)
}
}
```
2019-05-18 18:13:16 -04:00
### .deb package
A package for installing this on Debian is buildable, and a version for Ubuntu
is available as a PPA and mirrored via i2p. To build the deb package, from the
root of this repository with the build dependencies installed(git, i2p, go,
debuild) run the command
debuild -us -uc
to produce an unsigned deb for personal use only. For packagers,
debuild -S
will produce a viable source package for use with Launchpad PPA's and other
similar systems.
2020-11-12 22:46:43 -05:00
### TODO
* Improve recovery on failed sockets
* Implement `STREAM FORWARD`
* Implement datagrams (Repliable and Anon)
2023-01-10 16:15:25 +00:00