diff --git a/Readme.md b/Readme.md index 7ddd2ba..70c998a 100644 --- a/Readme.md +++ b/Readme.md @@ -42,120 +42,6 @@ Close is called after the request finishes or errors out. It is used to clean up Refresh is called in a seperate goroutine and should loop forever to do refreshes of the connection if needed. The passed in context is cancelled after the request so be sure to check on the Done event. -## Usage +## Examples -### Default Usage - -```golang -package main - -import ( - "time", - - socks "github.com/firefart/gosocks" - "github.com/sirupsen/logrus" -) - -func main() { - handler := socks.DefaultHandler{ - Timeout: 1*time.Second, - } - listen := "127.0.0.1:1080" - p := socks.Proxy{ - ServerAddr: listen, - Proxyhandler: handler, - Timeout: 1*time.Second, - Log: logrus.New(), - } - p.Log.Infof("starting SOCKS server on %s", listen) - if err := p.Start(); err != nil { - panic(err) - } - <-p.Done -} -``` - -### Usage with custom handlers - -```golang -package main - -import ( - "time" - "io" - "fmt" - "net" - "context" - - socks "github.com/firefart/gosocks" - "github.com/sirupsen/logrus" -) - -func main() { - log := logrus.New() - handler := MyCustomHandler{ - Timeout: 1*time.Second, - PropA: "A", - PropB: "B", - Log: log, - } - p := socks.Proxy{ - ServerAddr: "127.0.0.1:1080", - Proxyhandler: handler, - Timeout: 1*time.Second, - Log: log, - } - log.Infof("starting SOCKS server on %s", listen) - if err := p.Start(); err != nil { - panic(err) - } - <-p.Done -} - -type MyCustomHandler struct { - Timeout time.Duration, - PropA string, - PropB string, - Log Logger, -} - -func (s *MyCustomHandler) Init(request socks.Request) (io.ReadWriteCloser, *socks.Error) { - conn, err := net.DialTimeout("tcp", s.Server, s.Timeout) - if err != nil { - return nil, &socks.SocksError{Reason: socks.RequestReplyHostUnreachable, Err: fmt.Errorf("error on connecting to server: %w", err)} - } - return conn, nil -} - -func (s *MyCustomHandler) Refresh(ctx context.Context) { - tick := time.NewTicker(10 * time.Second) - select { - case <-ctx.Done(): - return - case <-tick.C: - s.Log.Debug("refreshing connection") - } -} - -func (s *MyCustomHandler) ReadFromRemote(ctx context.Context, remote io.ReadCloser, client io.WriteCloser) error { - i, err := io.Copy(client, remote) - if err != nil { - return err - } - s.Log.Debugf("wrote %d bytes to client", i) - return nil -} - -func (s *MyCustomHandler) ReadFromClient(ctx context.Context, client io.ReadCloser, remote io.WriteCloser) error { - i, err := io.Copy(remote, client) - if err != nil { - return err - } - s.Log.Debugf("wrote %d bytes to remote", i) - return nil -} - -func (s *MyCustomHandler) Close() error { - return nil -} -``` +For examples please have a look at the examples folder diff --git a/examples/custom/main.go b/examples/custom/main.go new file mode 100644 index 0000000..edaaf11 --- /dev/null +++ b/examples/custom/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "context" + "fmt" + "io" + "net" + "time" + + socks "github.com/firefart/gosocks" +) + +func main() { + log := &socks.NilLogger{} + handler := MyCustomHandler{ + Timeout: 1 * time.Second, + PropA: "A", + PropB: "B", + Log: log, + } + p := socks.Proxy{ + ServerAddr: "127.0.0.1:1080", + Proxyhandler: &handler, + Timeout: 1 * time.Second, + Log: log, + } + log.Infof("starting SOCKS server on %s", p.ServerAddr) + if err := p.Start(); err != nil { + panic(err) + } + <-p.Done +} + +type MyCustomHandler struct { + Timeout time.Duration + PropA string + PropB string + Log socks.Logger +} + +func (s *MyCustomHandler) Init(request socks.Request) (io.ReadWriteCloser, *socks.Error) { + target := fmt.Sprintf("%s:%d", request.DestinationAddress, request.DestinationPort) + s.Log.Infof("Connecting to target %s", target) + remote, err := net.DialTimeout("tcp", target, s.Timeout) + if err != nil { + return nil, socks.NewError(socks.RequestReplyNetworkUnreachable, err) + } + return remote, nil +} + +func (s *MyCustomHandler) Refresh(ctx context.Context) { + tick := time.NewTicker(10 * time.Second) + select { + case <-ctx.Done(): + return + case <-tick.C: + s.Log.Debug("refreshing connection") + } +} + +func (s *MyCustomHandler) ReadFromRemote(ctx context.Context, remote io.ReadCloser, client io.WriteCloser) error { + i, err := io.Copy(client, remote) + if err != nil { + return err + } + s.Log.Debugf("wrote %d bytes to client", i) + return nil +} + +func (s *MyCustomHandler) ReadFromClient(ctx context.Context, client io.ReadCloser, remote io.WriteCloser) error { + i, err := io.Copy(remote, client) + if err != nil { + return err + } + s.Log.Debugf("wrote %d bytes to remote", i) + return nil +} + +func (s MyCustomHandler) Close() error { + return nil +} diff --git a/examples/default/main.go b/examples/default/main.go new file mode 100644 index 0000000..f0ea45c --- /dev/null +++ b/examples/default/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "time" + + socks "github.com/firefart/gosocks" +) + +func main() { + handler := socks.DefaultHandler{ + Timeout: 1 * time.Second, + } + listen := "127.0.0.1:1080" + p := socks.Proxy{ + ServerAddr: listen, + Proxyhandler: handler, + Timeout: 1 * time.Second, + Log: &socks.NilLogger{}, + } + p.Log.Infof("starting SOCKS server on %s", listen) + if err := p.Start(); err != nil { + panic(err) + } + <-p.Done +}