New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding UserAgent to Nat for description of port mapping #2310
base: master
Are you sure you want to change the base?
Changes from 2 commits
9deed9d
996c3eb
721799b
41d0843
f84217d
6c1e4e3
087c446
89290fe
be95951
75f4c11
9ce892b
f4c6c04
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 |
---|---|---|
|
@@ -34,7 +34,7 @@ type entry struct { | |
var discoverGateway = nat.DiscoverGateway | ||
|
||
// DiscoverNAT looks for a NAT device in the network and returns an object that can manage port mappings. | ||
func DiscoverNAT(ctx context.Context) (*NAT, error) { | ||
func DiscoverNAT(ctx context.Context, userAgent string) (*NAT, error) { | ||
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. Do we want to change the interface of this public method? 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. What are the alternatives? 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. Introducing a new func. 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. If we create a new function, we need to designate a default userAgent somewhere in this file. The default value is set in another file. 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 default should be in this file. That was the old behavior. If users want to change it they can call the new function (maybe call it This change as is is a breaking API change. I like to avoid them unless necessary. I don't think it's necessary here. 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. @MarcoPolo Okay. Then what to do with this one? What to pass in this line? What will be the default value here? 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. And then what will be in this place? "if" and two constructors? 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. @MarcoPolo Okay. Why shouldn't there be an incoming userAgent parameter in this function? After all, we cannot communicate with NAT without userAgent. 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.
Because it's a breaking API change. Users of this function, when they update go-libp2p will need to update their usage of this function and pass in a userAgent. It'd be nicer if we add another function 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. @sukunrt Okay. I fixed it. |
||
natInstance, err := discoverGateway(ctx) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -60,6 +60,7 @@ func DiscoverNAT(ctx context.Context) (*NAT, error) { | |
mappings: make(map[entry]int), | ||
ctx: ctx, | ||
ctxCancel: cancel, | ||
userAgent: userAgent, | ||
} | ||
nat.refCount.Add(1) | ||
go func() { | ||
|
@@ -77,7 +78,8 @@ type NAT struct { | |
natmu sync.Mutex | ||
nat nat.NAT | ||
// External IP of the NAT. Will be renewed periodically (every CacheTime). | ||
extAddr netip.Addr | ||
extAddr netip.Addr | ||
userAgent string | ||
|
||
refCount sync.WaitGroup | ||
ctx context.Context | ||
|
@@ -220,14 +222,13 @@ func (nat *NAT) background() { | |
|
||
func (nat *NAT) establishMapping(protocol string, internalPort int) (externalPort int) { | ||
log.Debugf("Attempting port map: %s/%d", protocol, internalPort) | ||
const comment = "libp2p" | ||
|
||
nat.natmu.Lock() | ||
var err error | ||
externalPort, err = nat.nat.AddPortMapping(protocol, internalPort, comment, MappingDuration) | ||
externalPort, err = nat.nat.AddPortMapping(protocol, internalPort, nat.userAgent, MappingDuration) | ||
if err != nil { | ||
// Some hardware does not support mappings with timeout, so try that | ||
externalPort, err = nat.nat.AddPortMapping(protocol, internalPort, comment, 0) | ||
externalPort, err = nat.nat.AddPortMapping(protocol, internalPort, nat.userAgent, 0) | ||
} | ||
nat.natmu.Unlock() | ||
|
||
|
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.
Nicer: use the option pattern.
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.
@marten-seemann What does that mean? In what place? Where is it better to take the variable from?
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.
@marten-seemann Ok. Maybe this is the right way?
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.
Marten likely meant defining a new options type for the
NATManager
instead of extending Swarm's options set and Network interface with UserAgant getter.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.
@Wondertan Why not?
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.
I was talking about the option pattern, like here: https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/circuitv2/relay/options.go
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.
@marten-seemann And you think putting the UserAgent in the Swarm - Network is not right?
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.
Yeah, it doesn't really belong in the swarm. What made you put it there?
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.
@marten-seemann I don't know. I just read the comments about what the swarm was doing and looked at which direction the data was going. If the Network is establishing connections, it needs a UserAgent.
Okay, if this idea of mine is wrong, I'll fix the code. I'll try to make an options.
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.
@marten-seemann The new version with options is ready. But it seems to me it is too much for one parameter.
I like the second version. Through Network.