Skip to content

Commit d414a53

Browse files
committed
add public funcs to work with auth: ReadMethods ServeConnNoAuth
1 parent bea9abf commit d414a53

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (a UserPassAuthenticator) Authenticate(reader io.Reader, writer io.Writer)
166166
// authenticate is used to handle connection authentication
167167
func (s *Server) authenticate(conn io.Writer, bufConn io.Reader) (*AuthContext, error) {
168168
// Get the methods
169-
methods, err := readMethods(bufConn)
169+
methods, err := ReadMethods(bufConn)
170170
if err != nil {
171171
return nil, fmt.Errorf("failed to get auth methods: %v", err)
172172
}
@@ -190,9 +190,9 @@ func noAcceptableAuth(conn io.Writer) error {
190190
return ErrNoSupportedAuth
191191
}
192192

193-
// readMethods is used to read the number of methods
193+
// ReadMethods is used to read the number of methods
194194
// and proceeding auth methods
195-
func readMethods(r io.Reader) ([]byte, error) {
195+
func ReadMethods(r io.Reader) ([]byte, error) {
196196
header := []byte{0}
197197
if _, err := r.Read(header); err != nil {
198198
return nil, err

socks5.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,32 @@ func (s *Server) ServeConn(conn net.Conn) error {
188188

189189
return nil
190190
}
191+
192+
// ServeConnNoAuth is used to serve a single connection where the auth
193+
// negotiation has already been handled by the caller (e.g. locally on the client side).
194+
// It skips the version byte read and authentication, going directly to request handling.
195+
func (s *Server) ServeConnNoAuth(conn net.Conn) error {
196+
defer conn.Close()
197+
198+
request, err := NewRequest(conn)
199+
if err != nil {
200+
if err == errUnrecognizedAddrType {
201+
if err := sendReply(conn, ReplyAddrTypeNotSupported, nil); err != nil {
202+
return fmt.Errorf("failed to send reply: %v", err)
203+
}
204+
}
205+
return fmt.Errorf("failed to read destination address: %v", err)
206+
}
207+
request.AuthContext = &AuthContext{Method: AuthMethodNoAuth}
208+
if client, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
209+
request.RemoteAddr = &AddrSpec{IP: client.IP, Port: client.Port}
210+
}
211+
212+
if err := s.handleRequest(request, conn); err != nil {
213+
err = fmt.Errorf("failed to handle request: %v", err)
214+
s.config.Logger.Printf("socks: %v", err)
215+
return err
216+
}
217+
218+
return nil
219+
}

0 commit comments

Comments
 (0)