Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: things-go/go-socks5
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f3fb86528eb0b358070135deb1c09ae29ba301ce
Choose a base ref
..
head repository: things-go/go-socks5
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 40c50a97a0ac8caa649c9fd3e99eb641d68f3bdf
Choose a head ref
Showing with 175 additions and 674 deletions.
  1. +1 −1 go.mod
  2. +2 −2 go.sum
  3. +6 −17 handle.go
  4. +0 −43 option.go
  5. +0 −14 server.go
  6. +166 −597 server_test.go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ go 1.18

require (
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.29.0
golang.org/x/net v0.25.0
)

require (
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
23 changes: 6 additions & 17 deletions handle.go
Original file line number Diff line number Diff line change
@@ -80,40 +80,29 @@ func (sf *Server) handleRequest(write io.Writer, req *Request) error {
return fmt.Errorf("bind to %v blocked by rules", req.RawDestAddr)
}

var last Handler
// Switch on the command
switch req.Command {
case statute.CommandConnect:
last = sf.handleConnect
if sf.userConnectHandle != nil {
last = sf.userConnectHandle
}
if len(sf.userConnectMiddlewares) != 0 {
return sf.userConnectMiddlewares.Execute(ctx, write, req, last)
return sf.userConnectHandle(ctx, write, req)
}
return sf.handleConnect(ctx, write, req)
case statute.CommandBind:
last = sf.handleBind
if sf.userBindHandle != nil {
last = sf.userBindHandle
}
if len(sf.userBindMiddlewares) != 0 {
return sf.userBindMiddlewares.Execute(ctx, write, req, last)
return sf.userBindHandle(ctx, write, req)
}
return sf.handleBind(ctx, write, req)
case statute.CommandAssociate:
last = sf.handleAssociate
if sf.userAssociateHandle != nil {
last = sf.userAssociateHandle
}
if len(sf.userAssociateMiddlewares) != 0 {
return sf.userAssociateMiddlewares.Execute(ctx, write, req, last)
return sf.userAssociateHandle(ctx, write, req)
}
return sf.handleAssociate(ctx, write, req)
default:
if err := SendReply(write, statute.RepCommandNotSupported, nil); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
return fmt.Errorf("unsupported command[%v]", req.Command)
}
return last(ctx, write, req)
}

// handleConnect is used to handle a connect command
43 changes: 0 additions & 43 deletions option.go
Original file line number Diff line number Diff line change
@@ -124,46 +124,3 @@ func WithAssociateHandle(h func(ctx context.Context, writer io.Writer, request *
s.userAssociateHandle = h
}
}

// Handler is used to handle a user's commands
type Handler func(ctx context.Context, writer io.Writer, request *Request) error

// WithMiddleware is used to add interceptors in chain
type Middleware func(ctx context.Context, writer io.Writer, request *Request) error

// MiddlewareChain is used to add interceptors in chain
type MiddlewareChain []Middleware

// Execute is used to add interceptors in chain
func (m MiddlewareChain) Execute(ctx context.Context, writer io.Writer, request *Request, last Handler) error {
if len(m) == 0 {
return nil
}
for i := 0; i < len(m); i++ {
if err := m[i](ctx, writer, request); err != nil {
return err
}
}
return last(ctx, writer, request)
}

// WithConnectMiddleware is used to add interceptors in chain
func WithConnectMiddleware(m Middleware) Option {
return func(s *Server) {
s.userConnectMiddlewares = append(s.userConnectMiddlewares, m)
}
}

// WithBindMiddleware is used to add interceptors in chain
func WithBindMiddleware(m Middleware) Option {
return func(s *Server) {
s.userBindMiddlewares = append(s.userBindMiddlewares, m)
}
}

// WithAssociateMiddleware is used to add interceptors in chain
func WithAssociateMiddleware(m Middleware) Option {
return func(s *Server) {
s.userAssociateMiddlewares = append(s.userAssociateMiddlewares, m)
}
}
14 changes: 0 additions & 14 deletions server.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ package socks5
import (
"bufio"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
@@ -58,10 +57,6 @@ type Server struct {
userConnectHandle func(ctx context.Context, writer io.Writer, request *Request) error
userBindHandle func(ctx context.Context, writer io.Writer, request *Request) error
userAssociateHandle func(ctx context.Context, writer io.Writer, request *Request) error
// user's middleware
userConnectMiddlewares MiddlewareChain
userBindMiddlewares MiddlewareChain
userAssociateMiddlewares MiddlewareChain
}

// NewServer creates a new Server
@@ -98,15 +93,6 @@ func (sf *Server) ListenAndServe(network, addr string) error {
return sf.Serve(l)
}

// ListenAndServeTLS is used to create a TLS listener and serve on it
func (sf *Server) ListenAndServeTLS(network, addr string, c *tls.Config) error {
l, err := tls.Listen(network, addr, c)
if err != nil {
return err
}
return sf.Serve(l)
}

// Serve is used to serve connections from a listener
func (sf *Server) Serve(l net.Listener) error {
defer l.Close()
Loading