-
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
better user-agent handling #702
Changes from 4 commits
d73a517
e337633
3bfc2cd
99c5d3c
9cf6af4
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ os: | |
language: go | ||
|
||
go: | ||
- 1.11.x | ||
- 1.12.x | ||
|
||
env: | ||
global: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package identify | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"runtime/debug" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -31,9 +33,27 @@ const ID = "/ipfs/id/1.0.0" | |
|
||
// LibP2PVersion holds the current protocol version for a client running this code | ||
// TODO(jbenet): fix the versioning mess. | ||
// XXX: Don't change this till 2020. You'll break all go-ipfs versions prior to | ||
// 0.4.17 which asserted an exact version match. | ||
const LibP2PVersion = "ipfs/0.1.0" | ||
|
||
var ClientVersion = "go-libp2p/3.3.4" | ||
// ClientVersion is the default user agent. | ||
// | ||
// Deprecated: Set this with the UserAgent option. | ||
var ClientVersion = "github.com/libp2p/go-libp2p" | ||
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. let's just call this 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. Sure but users using go mod will still use their full paths by default. E.g., 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. oh ok, nevermind then. 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 will increase the size of the identify messages by 15 bytes. It's ok but worth noting. |
||
|
||
func init() { | ||
bi, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return | ||
} | ||
version := bi.Main.Version | ||
if version == "(devel)" { | ||
ClientVersion = bi.Main.Path | ||
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. let's keep the 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. Anything built manually (i.e., built from a manually checked-out source tree) will have a To get a proper version, one would have to build with Users that care can override this. 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. ok. 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. Can we suffix with @unknown? |
||
} else { | ||
ClientVersion = fmt.Sprintf("%s@%s", bi.Main.Path, bi.Main.Version) | ||
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 is the path/version of the main module (application), not libp2p itself. |
||
} | ||
} | ||
|
||
// transientTTL is a short ttl for invalidated previously connected addrs | ||
const transientTTL = 10 * time.Second | ||
|
@@ -47,7 +67,8 @@ const transientTTL = 10 * time.Second | |
// * Our IPFS Agent Version | ||
// * Our public Listen Addresses | ||
type IDService struct { | ||
Host host.Host | ||
Host host.Host | ||
UserAgent string | ||
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. Keeping this exported could lead to races if the user modifies the value at runtime. Not a biggie, but just pointing it out. |
||
|
||
ctx context.Context | ||
|
||
|
@@ -70,9 +91,21 @@ type IDService struct { | |
|
||
// NewIDService constructs a new *IDService and activates it by | ||
// attaching its stream handler to the given host.Host. | ||
func NewIDService(ctx context.Context, h host.Host) *IDService { | ||
func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService { | ||
var cfg config | ||
for _, opt := range opts { | ||
opt(&cfg) | ||
} | ||
|
||
userAgent := ClientVersion | ||
if cfg.userAgent != "" { | ||
userAgent = cfg.userAgent | ||
} | ||
|
||
s := &IDService{ | ||
Host: h, | ||
Host: h, | ||
UserAgent: userAgent, | ||
|
||
ctx: ctx, | ||
currid: make(map[network.Conn]chan struct{}), | ||
observedAddrs: NewObservedAddrSet(ctx), | ||
|
@@ -306,7 +339,7 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) { | |
|
||
// set protocol versions | ||
pv := LibP2PVersion | ||
av := ClientVersion | ||
av := ids.UserAgent | ||
mes.ProtocolVersion = &pv | ||
mes.AgentVersion = &av | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package identify | ||
|
||
type config struct { | ||
userAgent string | ||
} | ||
|
||
type Option func(*config) | ||
|
||
func UserAgent(ua string) Option { | ||
return func(cfg *config) { | ||
cfg.userAgent = ua | ||
} | ||
} |
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 should make this configurable too. Many projects using libp2p don't care about compatibility with IPFS and their networks are deliberately segregated.
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.
See #714