Skip to content

Commit

Permalink
add exampels
Browse files Browse the repository at this point in the history
  • Loading branch information
firefart committed Dec 12, 2023
1 parent d7958b1 commit 7971158
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 116 deletions.
118 changes: 2 additions & 116 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
81 changes: 81 additions & 0 deletions examples/custom/main.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions examples/default/main.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7971158

Please sign in to comment.