mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
Some checks are pending
CodeQL / codeql (push) Waiting to run
Docker Build / docker-build (push) Waiting to run
Gateway Conformance / gateway-conformance (push) Waiting to run
Gateway Conformance / gateway-conformance-libp2p-experiment (push) Waiting to run
Go Build / go-build (push) Waiting to run
Go Check / go-check (push) Waiting to run
Go Lint / go-lint (push) Waiting to run
Go Test / go-test (push) Waiting to run
Interop / interop-prep (push) Waiting to run
Interop / helia-interop (push) Blocked by required conditions
Interop / ipfs-webui (push) Blocked by required conditions
Sharness / sharness-test (push) Waiting to run
Spell Check / spellcheck (push) Waiting to run
* chore: update to go-log/v2 go-log v2 has been out for quite a while now and it is time to deprecate v1. Replace all use of go-log with go-log/v2 Makes /api/v0/log/tail useful over HTTP Updates dependencies that have moved to go-lov/v2 Removes support for ContextWithLoggable as this is not needed for tracing-like functionality - Replaces: PR #8765 - Closes issue #8753 - Closes issue #9245 - Closes issue #10809 Other fixes: * update go-ipfs-cmds * update http logs test * fix test * Read/send one line of log data at a time * Update -log-level docs
104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
package libp2p
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"time"
|
|
|
|
version "github.com/ipfs/kubo"
|
|
config "github.com/ipfs/kubo/config"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
"github.com/libp2p/go-libp2p"
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
|
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
var log = logging.Logger("p2pnode")
|
|
|
|
type Libp2pOpts struct {
|
|
fx.Out
|
|
|
|
Opts []libp2p.Option `group:"libp2p"`
|
|
}
|
|
|
|
func ConnectionManager(low, high int, grace time.Duration) func() (opts Libp2pOpts, err error) {
|
|
return func() (opts Libp2pOpts, err error) {
|
|
cm, err := connmgr.NewConnManager(low, high, connmgr.WithGracePeriod(grace))
|
|
if err != nil {
|
|
return opts, err
|
|
}
|
|
opts.Opts = append(opts.Opts, libp2p.ConnectionManager(cm))
|
|
return
|
|
}
|
|
}
|
|
|
|
func PstoreAddSelfKeys(id peer.ID, sk crypto.PrivKey, ps peerstore.Peerstore) error {
|
|
if err := ps.AddPubKey(id, sk.GetPublic()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return ps.AddPrivKey(id, sk)
|
|
}
|
|
|
|
func UserAgent() func() (opts Libp2pOpts, err error) {
|
|
return simpleOpt(libp2p.UserAgent(version.GetUserAgentVersion()))
|
|
}
|
|
|
|
func simpleOpt(opt libp2p.Option) func() (opts Libp2pOpts, err error) {
|
|
return func() (opts Libp2pOpts, err error) {
|
|
opts.Opts = append(opts.Opts, opt)
|
|
return
|
|
}
|
|
}
|
|
|
|
type priorityOption struct {
|
|
priority, defaultPriority config.Priority
|
|
opt libp2p.Option
|
|
}
|
|
|
|
func prioritizeOptions(opts []priorityOption) libp2p.Option {
|
|
type popt struct {
|
|
priority int64 // lower priority values mean higher priority
|
|
opt libp2p.Option
|
|
}
|
|
enabledOptions := make([]popt, 0, len(opts))
|
|
for _, o := range opts {
|
|
if prio, ok := o.priority.WithDefault(o.defaultPriority); ok {
|
|
enabledOptions = append(enabledOptions, popt{
|
|
priority: prio,
|
|
opt: o.opt,
|
|
})
|
|
}
|
|
}
|
|
sort.Slice(enabledOptions, func(i, j int) bool {
|
|
return enabledOptions[i].priority < enabledOptions[j].priority
|
|
})
|
|
p2pOpts := make([]libp2p.Option, len(enabledOptions))
|
|
for i, opt := range enabledOptions {
|
|
p2pOpts[i] = opt.opt
|
|
}
|
|
return libp2p.ChainOptions(p2pOpts...)
|
|
}
|
|
|
|
func ForceReachability(val *config.OptionalString) func() (opts Libp2pOpts, err error) {
|
|
return func() (opts Libp2pOpts, err error) {
|
|
if val.IsDefault() {
|
|
return
|
|
}
|
|
v := val.WithDefault("unrecognized")
|
|
switch v {
|
|
case "public":
|
|
opts.Opts = append(opts.Opts, libp2p.ForceReachabilityPublic())
|
|
case "private":
|
|
opts.Opts = append(opts.Opts, libp2p.ForceReachabilityPrivate())
|
|
default:
|
|
return opts, fmt.Errorf("unrecognized reachability option: %s", v)
|
|
}
|
|
return
|
|
}
|
|
}
|