-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 5 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package basichost | ||
|
||
type Option func(*natManager) error | ||
|
||
// WithUserAgent is a natManager option that sets specific user agent for the NAT manager. | ||
func WithUserAgent(userAgent string) Option { | ||
return func(nmgr *natManager) error { | ||
nmgr.userAgent = userAgent | ||
return nil | ||
} | ||
} |
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.
The beauty of the option pattern is that the options are optional. You need to make them variadic though.
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 Ready. But I still think it's redundant to add an Option for a single parameter. Perhaps there will be more parameters in the future?
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 Maybe something else? Or is it done?
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.
Fixed the error. Now it works.