2018-09-13 13:47:08 +00:00
|
|
|
package p2pd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-12-03 13:03:59 +00:00
|
|
|
"fmt"
|
2018-09-13 13:47:08 +00:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
libp2p "github.com/libp2p/go-libp2p"
|
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
2018-09-30 08:01:00 +00:00
|
|
|
dht "github.com/libp2p/go-libp2p-kad-dht"
|
2018-09-30 12:13:42 +00:00
|
|
|
dhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
|
2018-09-15 07:33:00 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2018-09-13 13:47:08 +00:00
|
|
|
proto "github.com/libp2p/go-libp2p-protocol"
|
2018-12-03 09:25:18 +00:00
|
|
|
ps "github.com/libp2p/go-libp2p-pubsub"
|
2018-09-30 12:13:42 +00:00
|
|
|
rhost "github.com/libp2p/go-libp2p/p2p/host/routed"
|
2018-09-15 07:33:00 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2018-09-13 13:47:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("p2pd")
|
|
|
|
|
|
|
|
type Daemon struct {
|
|
|
|
ctx context.Context
|
|
|
|
host host.Host
|
|
|
|
listener net.Listener
|
|
|
|
|
2018-12-03 09:25:18 +00:00
|
|
|
dht *dht.IpfsDHT
|
|
|
|
pubsub *ps.PubSub
|
2018-09-30 08:01:00 +00:00
|
|
|
|
2018-09-13 13:47:08 +00:00
|
|
|
mx sync.Mutex
|
|
|
|
// stream handlers: map of protocol.ID to unix socket path
|
|
|
|
handlers map[proto.ID]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDaemon(ctx context.Context, path string, opts ...libp2p.Option) (*Daemon, error) {
|
|
|
|
h, err := libp2p.New(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.Listen("unix", path)
|
|
|
|
if err != nil {
|
|
|
|
h.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &Daemon{
|
|
|
|
ctx: ctx,
|
|
|
|
host: h,
|
|
|
|
listener: l,
|
|
|
|
handlers: make(map[proto.ID]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
go d.listen()
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:13:42 +00:00
|
|
|
func (d *Daemon) EnableDHT(client bool) error {
|
|
|
|
var opts []dhtopts.Option
|
|
|
|
|
|
|
|
if client {
|
|
|
|
opts = append(opts, dhtopts.Client(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
dht, err := dht.New(d.ctx, d.host, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.dht = dht
|
|
|
|
d.host = rhost.Wrap(d.host, d.dht)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-03 13:03:59 +00:00
|
|
|
func (d *Daemon) EnablePubsub(router string, sign, strict bool) error {
|
|
|
|
var opts []ps.Option
|
|
|
|
|
|
|
|
if sign {
|
|
|
|
opts = append(opts, ps.WithMessageSigning(sign))
|
|
|
|
|
2018-12-03 13:05:19 +00:00
|
|
|
if strict {
|
|
|
|
opts = append(opts, ps.WithStrictSignatureVerification(strict))
|
|
|
|
}
|
2018-12-03 13:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch router {
|
|
|
|
case "floodsub":
|
|
|
|
pubsub, err := ps.NewFloodSub(d.ctx, d.host, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.pubsub = pubsub
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case "gossipsub":
|
|
|
|
pubsub, err := ps.NewGossipSub(d.ctx, d.host, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.pubsub = pubsub
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown pubsub router: %s", router)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-15 07:33:00 +00:00
|
|
|
func (d *Daemon) ID() peer.ID {
|
|
|
|
return d.host.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) Addrs() []ma.Multiaddr {
|
|
|
|
return d.host.Addrs()
|
|
|
|
}
|
|
|
|
|
2018-09-13 13:47:08 +00:00
|
|
|
func (d *Daemon) listen() {
|
|
|
|
for {
|
|
|
|
c, err := d.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error accepting connection: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("incoming connection")
|
|
|
|
go d.handleConn(c)
|
|
|
|
}
|
|
|
|
}
|