Fix panic when target.LocalAddr() is not *net.TCPAddr
This happens when you use custom dialer that returns custom net.Conn that returns a non-*net.TCPAddr net.Addr for LocalAddr() calls. For example, if you dial through an SSH connection, it might be difficult to figure out remote local address, so you return a stub instead.
This commit is contained in:
parent
d86385b825
commit
f87199c59e
11
request.go
11
request.go
@ -190,9 +190,7 @@ func (s *Server) handleConnect(ctx context.Context, conn conn, req *Request) err
|
|||||||
defer target.Close()
|
defer target.Close()
|
||||||
|
|
||||||
// Send success
|
// Send success
|
||||||
local := target.LocalAddr().(*net.TCPAddr)
|
if err := sendReply(conn, successReply, addrSpecFromNetAddr(target.LocalAddr())); err != nil {
|
||||||
bind := AddrSpec{IP: local.IP, Port: local.Port}
|
|
||||||
if err := sendReply(conn, successReply, &bind); err != nil {
|
|
||||||
return fmt.Errorf("Failed to send reply: %v", err)
|
return fmt.Errorf("Failed to send reply: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,6 +300,13 @@ func readAddrSpec(r io.Reader) (*AddrSpec, error) {
|
|||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addrSpecFromNetAddr(addr net.Addr) *AddrSpec {
|
||||||
|
if tcpAddr, ok := addr.(*net.TCPAddr); ok {
|
||||||
|
return &AddrSpec{IP: tcpAddr.IP, Port: tcpAddr.Port}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// sendReply is used to send a reply message
|
// sendReply is used to send a reply message
|
||||||
func sendReply(w io.Writer, resp uint8, addr *AddrSpec) error {
|
func sendReply(w io.Writer, resp uint8, addr *AddrSpec) error {
|
||||||
// Format the address
|
// Format the address
|
||||||
|
Loading…
Reference in New Issue
Block a user