Skip to content

Commit

Permalink
Fix turning ipv4 into ipv6 with a hostlookup
Browse files Browse the repository at this point in the history
also use the new socks interface
  • Loading branch information
firefart committed Dec 12, 2023
1 parent 909a564 commit 5b6cabe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/firefart/stunner

go 1.19
go 1.21

require (
github.com/firefart/gosocks v0.2.0
github.com/firefart/gosocks v0.3.0
github.com/pion/dtls/v2 v2.2.8
github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli/v2 v2.26.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/firefart/gosocks v0.2.0 h1:zidnZ38a04fJCC5Gga9gpofu3kNpd+x8NNjhoYsSFvQ=
github.com/firefart/gosocks v0.2.0/go.mod h1:nJNHO+0hy3xLB7JbTiIu6X6wvN0gKK6hV/YWIppFzp4=
github.com/firefart/gosocks v0.3.0 h1:k8cVQmHQipzdOgpiwHAxKUxXE+cwk5W1i6rteoQMzWM=
github.com/firefart/gosocks v0.3.0/go.mod h1:Kboswm/Albj/QPeuVNAfyp3j7zxfrBMr8B1IhWd5EK0=
github.com/pion/dtls/v2 v2.2.8 h1:BUroldfiIbV9jSnC6cKOMnyiORRWrWWpV11JUyEu5OA=
github.com/pion/dtls/v2 v2.2.8/go.mod h1:8WiMkebSHFD0T+dIU+UeBaoV7kDhOW5oDCzZ7WZ/F9s=
github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY=
Expand Down
36 changes: 21 additions & 15 deletions internal/socksimplementations/socksturntcphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type SocksTurnTCPHandler struct {
}

// PreHandler connects to the STUN server, sets the connection up and returns the data connections
func (s *SocksTurnTCPHandler) PreHandler(request socks.Request) (io.ReadWriteCloser, *socks.Error) {
func (s *SocksTurnTCPHandler) Init(request socks.Request) (io.ReadWriteCloser, *socks.Error) {
var target netip.Addr
var err error
switch request.AddressType {
Expand All @@ -40,14 +40,20 @@ func (s *SocksTurnTCPHandler) PreHandler(request socks.Request) (io.ReadWriteClo
}
target = tmp
case socks.RequestAddressTypeDomainname:
names, err := helper.ResolveName(s.Ctx, string(request.DestinationAddress))
if err != nil {
return nil, &socks.Error{Reason: socks.RequestReplyHostUnreachable, Err: err}
}
if len(names) == 0 {
return nil, &socks.Error{Reason: socks.RequestReplyHostUnreachable, Err: fmt.Errorf("%s could not be resolved", string(request.DestinationAddress))}
// check if the input is an ip adress
if ip, err := netip.ParseAddr(string(request.DestinationAddress)); err == nil {
target = ip
} else {
// input is a hostname
names, err := helper.ResolveName(s.Ctx, string(request.DestinationAddress))
if err != nil {
return nil, &socks.Error{Reason: socks.RequestReplyHostUnreachable, Err: err}
}
if len(names) == 0 {
return nil, &socks.Error{Reason: socks.RequestReplyHostUnreachable, Err: fmt.Errorf("%s could not be resolved", string(request.DestinationAddress))}
}
target = names[0]
}
target = names[0]
default:
return nil, &socks.Error{Reason: socks.RequestReplyAddressTypeNotSupported, Err: fmt.Errorf("AddressType %#x not implemented", request.AddressType)}
}
Expand Down Expand Up @@ -101,19 +107,19 @@ func (s *SocksTurnTCPHandler) Refresh(ctx context.Context) {
}
}

// CopyFromRemoteToClient is used to copy data
func (s *SocksTurnTCPHandler) CopyFromRemoteToClient(ctx context.Context, remote io.ReadCloser, client io.WriteCloser) error {
i, err := io.Copy(client, remote)
// ReadFromClient is used to copy data
func (s *SocksTurnTCPHandler) ReadFromClient(ctx context.Context, client io.ReadCloser, remote io.WriteCloser) error {
i, err := io.Copy(remote, client)
if err != nil {
return fmt.Errorf("CopyFromRemoteToClient: %w", err)
}
s.Log.Debugf("[socks] wrote %d bytes to client", i)
return nil
}

// CopyFromClientToRemote is used to copy data
func (s *SocksTurnTCPHandler) CopyFromClientToRemote(ctx context.Context, client io.ReadCloser, remote io.WriteCloser) error {
i, err := io.Copy(remote, client)
// ReadFromRemote is used to copy data
func (s *SocksTurnTCPHandler) ReadFromRemote(ctx context.Context, remote io.ReadCloser, client io.WriteCloser) error {
i, err := io.Copy(client, remote)
if err != nil {
return fmt.Errorf("CopyFromClientToRemote: %w", err)
}
Expand All @@ -122,7 +128,7 @@ func (s *SocksTurnTCPHandler) CopyFromClientToRemote(ctx context.Context, client
}

// Cleanup closes the stored control connection
func (s *SocksTurnTCPHandler) Cleanup() error {
func (s *SocksTurnTCPHandler) Close() error {
if s.ControlConnection != nil {
return s.ControlConnection.Close()
}
Expand Down

0 comments on commit 5b6cabe

Please sign in to comment.