multi: apply roasbeef diff to support incoming socks.ProxiedAddr

This commit is contained in:
MeshCollider authored and Olaoluwa Osuntokun committed 2018-02-05 17:37:46 -08:00
1 parent 36219427ad
commit 4b1cc98808
7 files changed
+86 -43

No files matched your search

+58 -24
View File
@@ -3,6 +3,8 @@ package channeldb
import (
"io"
"net"
"github.com/btcsuite/go-socks/socks"
)
// addressType specifies the network protocol and version that should be used
@@ -23,6 +25,40 @@ const (
v3OnionAddr addressType = 3
)
func encodeTCPAddr(w io.Writer, addr *net.TCPAddr) error {
var scratch [16]byte
if addr.IP.To4() != nil {
scratch[0] = uint8(tcp4Addr)
if _, err := w.Write(scratch[:1]); err != nil {
return err
}
copy(scratch[:4], addr.IP.To4())
if _, err := w.Write(scratch[:4]); err != nil {
return err
}
} else {
scratch[0] = uint8(tcp6Addr)
if _, err := w.Write(scratch[:1]); err != nil {
return err
}
copy(scratch[:], addr.IP.To16())
if _, err := w.Write(scratch[:]); err != nil {
return err
}
}
byteOrder.PutUint16(scratch[:2], uint16(addr.Port))
if _, err := w.Write(scratch[:2]); err != nil {
return err
}
return nil
}
// deserializeAddr reads the serialized raw representation of an address and
// deserializes it into the actual address, to avoid performing address
// resolution in the database module
@@ -70,33 +106,31 @@ func deserializeAddr(r io.Reader) (net.Addr, error) {
// serializeAddr serializes an address into a raw byte representation so it
// can be deserialized without requiring address resolution
func serializeAddr(w io.Writer, address net.Addr) error {
var scratch [16]byte
if address.Network() == "tcp" {
if address.(*net.TCPAddr).IP.To4() != nil {
scratch[0] = uint8(tcp4Addr)
if _, err := w.Write(scratch[:1]); err != nil {
return err
}
copy(scratch[:4], address.(*net.TCPAddr).IP.To4())
if _, err := w.Write(scratch[:4]); err != nil {
return err
}
} else {
scratch[0] = uint8(tcp6Addr)
if _, err := w.Write(scratch[:1]); err != nil {
return err
}
copy(scratch[:], address.(*net.TCPAddr).IP.To16())
if _, err := w.Write(scratch[:]); err != nil {
return err
}
switch addr := address.(type) {
case *net.TCPAddr:
return encodeTCPAddr(w, addr)
// If this is a proxied address (due to the connection being
// established over a SOCKs proxy, then we'll convert it into its
// corresponding TCP address.
case *socks.ProxiedAddr:
// If we can't parse the host as an IP (though we should be
// able to at this point), then we'll skip this address all
// together.
//
// TODO(roasbeef): would be nice to be able to store hosts
// though...
ip := net.ParseIP(addr.Host)
if ip == nil {
return nil
}
byteOrder.PutUint16(scratch[:2],
uint16(address.(*net.TCPAddr).Port))
if _, err := w.Write(scratch[:2]); err != nil {
return err
tcpAddr := &net.TCPAddr{
IP: ip,
Port: addr.Port,
}
return encodeTCPAddr(w, tcpAddr)
}
return nil