Skip to content

Commit

Permalink
new: allow overriding the user-agent with an option
Browse files Browse the repository at this point in the history
Instead of using a global variable.

This also:

* Adds an option to the identify service to set the user agent.
* Removes the ability to pass an identify service to NewHost as any reasonable
  Identify service already needs to be constructed with an instance of the host.
  • Loading branch information
Stebalien committed Aug 13, 2019
1 parent 071f7de commit f4e778d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type RoutingC func(host.Host) (routing.PeerRouting, error)
// This is *not* a stable interface. Use the options defined in the root
// package.
type Config struct {
UserAgent string

PeerKey crypto.PrivKey

Transports []TptC
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,11 @@ var NoTransports = func(cfg *Config) error {
cfg.Transports = []config.TptC{}
return nil
}

// UserAgent sets the libp2p user-agent sent along with the identify protocol
func UserAgent(userAgent string) Option {
return func(cfg *Config) error {
cfg.UserAgent = userAgent
return nil
}
}
19 changes: 9 additions & 10 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ type HostOpts struct {
// If below 0, timeouts on streams will be deactivated.
NegotiationTimeout time.Duration

// IdentifyService holds an implementation of the /ipfs/id/ protocol.
// If omitted, a new *identify.IDService will be used.
IdentifyService *identify.IDService

// AddrsFactory holds a function which can be used to override or filter the result of Addrs.
// If omitted, there's no override or filtering, and the results of Addrs and AllAddrs are the same.
AddrsFactory AddrsFactory
Expand All @@ -121,6 +117,9 @@ type HostOpts struct {

// EnablePing indicates whether to instantiate the ping service
EnablePing bool

// UserAgent sets the user-agent for the host. Defaults to ClientVersion.
UserAgent string
}

// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
Expand Down Expand Up @@ -154,12 +153,12 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo
h.mux = opts.MultistreamMuxer
}

if opts.IdentifyService != nil {
h.ids = opts.IdentifyService
} else {
// we can't set this as a default above because it depends on the *BasicHost.
h.ids = identify.NewIDService(goprocessctx.WithProcessClosing(ctx, h.proc), h)
}
// we can't set this as a default above because it depends on the *BasicHost.
h.ids = identify.NewIDService(
goprocessctx.WithProcessClosing(ctx, h.proc),
h,
identify.UserAgent(opts.UserAgent),
)

if uint64(opts.NegotiationTimeout) != 0 {
h.negtimeout = opts.NegotiationTimeout
Expand Down
21 changes: 17 additions & 4 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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

ctx context.Context

Expand All @@ -70,9 +71,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),
Expand Down Expand Up @@ -306,7 +319,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
}
Expand Down
13 changes: 13 additions & 0 deletions p2p/protocol/identify/opts.go
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
}
}

0 comments on commit f4e778d

Please sign in to comment.