-
Notifications
You must be signed in to change notification settings - Fork 12
replace the port number for double NAT mapping #101
Changes from 1 commit
f5a90b9
3960002
741ca1a
9d5510a
4c5f23f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,10 @@ package autonat | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -115,6 +117,7 @@ func (as *autoNATService) handleDial(p peer.ID, obsaddr ma.Multiaddr, mpi *pb.Me | |
obsHost, _ = manet.ToIP(obsaddr) | ||
} | ||
|
||
obscodename, obsport, _ := as.extractPort(obsaddr) | ||
for _, maddr := range mpi.GetAddrs() { | ||
addr, err := ma.NewMultiaddrBytes(maddr) | ||
if err != nil { | ||
|
@@ -123,7 +126,18 @@ func (as *autoNATService) handleDial(p peer.ID, obsaddr ma.Multiaddr, mpi *pb.Me | |
} | ||
|
||
if as.config.dialPolicy.skipDial(addr) { | ||
continue | ||
if manet.IsPrivateAddr(addr) { | ||
addrcodename, addrport, err := as.extractPort(addr) | ||
if err == nil && addrcodename == obscodename && addrport != obsport { //make a new address | ||
obsportstr := fmt.Sprintf("/%s/%s", obscodename, obsport) | ||
addrportstr := fmt.Sprintf("/%s/%s", addrcodename, addrport) | ||
addr, err = ma.NewMultiaddr(strings.Replace(obsaddr.String(), obsportstr, addrportstr, -1)) //replace the private addr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could have issues with circuit addresses or other weird edges of multiaddr. rather than string replace prefer finding the component with the expected code, and changing it in that way using the multiaddr library for parsing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The nat The |
||
} else { | ||
huo-ju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
} | ||
} else { | ||
continue | ||
} | ||
} | ||
|
||
if ip, err := manet.ToIP(addr); err != nil || !obsHost.Equal(ip) { | ||
|
@@ -228,3 +242,16 @@ func (as *autoNATService) background(ctx context.Context) { | |
} | ||
} | ||
} | ||
|
||
func (as *autoNATService) extractPort(m ma.Multiaddr) (name string, port string, err error) { | ||
for _, p := range m.Protocols() { | ||
if p.Code == ma.P_TCP || p.Code == ma.P_UDP { | ||
v, err := m.ValueForProtocol(p.Code) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rather than iterating through all protocols in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The multiformats encoding format is |
||
if err != nil { | ||
return "", "", err | ||
} | ||
return p.Name, v, nil | ||
} | ||
} | ||
return "", "", errors.New("can't extract port") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may skip an address because it's a relay address (e.g.). This logic will continue to dial said addresses.