Skip to content

Commit

Permalink
v0.5.0 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
TuanKiri authored Apr 17, 2024
1 parent dc0e2fa commit 4a75b38
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 13 deletions.
2 changes: 2 additions & 0 deletions authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func (s *Server) usernamePasswordAuthenticate(ctx context.Context, writer io.Wri
return
}

ctx = contextWithUsername(ctx, string(username))

passwordLen, err := reader.ReadByte()
if err != nil {
s.logger.Error(ctx, "failed to read password length: "+err.Error())
Expand Down
14 changes: 13 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import "context"

type ctxKey int

const remoteAddressKey ctxKey = iota
const (
remoteAddressKey ctxKey = iota
usernameKey
)

func contextWithRemoteAddress(ctx context.Context, address string) context.Context {
return context.WithValue(ctx, remoteAddressKey, address)
Expand All @@ -14,3 +17,12 @@ func RemoteAddressFromContext(ctx context.Context) (string, bool) {
value, ok := ctx.Value(remoteAddressKey).(string)
return value, ok
}

func contextWithUsername(ctx context.Context, username string) context.Context {
return context.WithValue(ctx, usernameKey, username)
}

func UsernameFromContext(ctx context.Context, username string) (string, bool) {
value, ok := ctx.Value(usernameKey).(string)
return value, ok
}
4 changes: 2 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func connectionRefusedError(err error) bool {
return strings.Contains(err.Error(), "refused")
}

func closedListenerError(e error) bool {
return errors.Is(e, net.ErrClosed)
func closedListenerError(err error) bool {
return errors.Is(err, net.ErrClosed)
}
13 changes: 13 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package socks5

import "context"

type Metrics interface {
UploadBytes(ctx context.Context, n int64)
DownloadBytes(ctx context.Context, n int64)
}

type nopMetrics struct{}

func (m *nopMetrics) UploadBytes(_ context.Context, _ int64) {}
func (m *nopMetrics) DownloadBytes(_ context.Context, _ int64) {}
5 changes: 5 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Options struct {
Logger Logger // default: stdoutLogger
Store Store // default: defaultStore
Driver Driver // default: defaultDriver
Metrics Metrics // default: nopMetrics
}

func (o Options) authMethods() map[byte]struct{} {
Expand Down Expand Up @@ -66,5 +67,9 @@ func optsWithDefaults(opts *Options) *Options {
}
}

if opts.Metrics == nil {
opts.Metrics = &nopMetrics{}
}

return opts
}
6 changes: 3 additions & 3 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ type closeWriter interface {
CloseWrite() error
}

func relay(dst io.Writer, src io.Reader) error {
_, err := io.Copy(dst, src)
func relay(dst io.Writer, src io.Reader) (int64, error) {
n, err := io.Copy(dst, src)

if writer, ok := dst.(closeWriter); ok {
// send EOF for next io.Copy
writer.CloseWrite()
}

return err
return n, err
}
12 changes: 7 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Server struct {
logger Logger
store Store
driver Driver
metrics Metrics
active chan struct{}
done chan struct{}
closeListener func() error
Expand All @@ -34,11 +35,12 @@ func New(opts *Options) *Server {
getPasswordTimeout: opts.GetPasswordTimeout,
authMethods: opts.authMethods(),
},
logger: opts.Logger,
store: opts.Store,
driver: opts.Driver,
active: make(chan struct{}),
done: make(chan struct{}),
logger: opts.Logger,
store: opts.Store,
driver: opts.Driver,
metrics: opts.Metrics,
active: make(chan struct{}),
done: make(chan struct{}),
}
}

Expand Down
8 changes: 6 additions & 2 deletions socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ func (s *Server) connect(ctx context.Context, writer io.Writer, reader *bufio.Re
var g errgroup.Group

g.Go(func() error {
return relay(target, reader)
n, err := relay(target, reader)
s.metrics.UploadBytes(ctx, n)
return err
})

g.Go(func() error {
return relay(writer, target)
n, err := relay(writer, target)
s.metrics.DownloadBytes(ctx, n)
return err
})

if err = g.Wait(); err != nil {
Expand Down

0 comments on commit 4a75b38

Please sign in to comment.