Skip to content

Commit

Permalink
Integrate PNet
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
  • Loading branch information
Kubuxu committed Feb 17, 2017
1 parent 24e3610 commit b217609
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 13 deletions.
6 changes: 6 additions & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/hex"
"errors"
_ "expvar"
"fmt"
Expand Down Expand Up @@ -347,6 +348,11 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
}
node.SetLocal(false)

if node.PNetFingerpint != nil {
fmt.Println("Swarm is limited to private network as of the swarm key")
fmt.Printf("Swarm key fingerprint: %s\n", hex.EncodeToString(node.PNetFingerpint))
}

printSwarmAddrs(node)

defer func() {
Expand Down
23 changes: 11 additions & 12 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interfaces and how core/... fits into the bigger IPFS picture, see:
package core

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -45,6 +46,7 @@ import (
addrutil "gx/ipfs/QmPB5aAzt2wo5Xk8SoZi6y2oFN7shQMvYWgduMATojkdpj/go-addr-util"
metrics "gx/ipfs/QmPj6rmE2sWJ65h6b8F4fcN5kySDhYqL2Ty8DWWF3WEUNS/go-libp2p-metrics"
pnet "gx/ipfs/QmQ2sk2irQYkeMMVKpLjq1bSiNPn9B5wZB53Nx8dd4dhnQ/go-libp2p-pnet"
floodsub "gx/ipfs/QmQDb7jmfC33qzGUGpWrfgRootQnmQvN7DMktfuekShsiF/floodsub"
pstore "gx/ipfs/QmQMQ2RUjnaEEX8ybmrhuFFGhAwPjyL1Eo6ZoJGD7aAccM/go-libp2p-peerstore"
mssmux "gx/ipfs/QmRVYfZ7tWNHPBzWiG6KWGzvT2hcGems8srihsQE29x1U5/go-smux-multistream"
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
Expand All @@ -67,7 +69,6 @@ import (
mplex "gx/ipfs/QmbPSoTDH3JFKkSxsCQDyVMFT76KcQz7ELWEetyyg3aeDA/go-smux-multiplex"
yamux "gx/ipfs/Qmbn7RYyWzBVXiUp9jZ1dA4VADHy9DtS7iZLwfhEUQvm3U/go-smux-yamux"
p2phost "gx/ipfs/QmbzbRyd22gcW92U1rA2yKagB3myMYhk45XBknJ49F9XWJ/go-libp2p-host"
floodsub "gx/ipfs/QmQDb7jmfC33qzGUGpWrfgRootQnmQvN7DMktfuekShsiF/floodsub"
ipnet "gx/ipfs/QmcT6bMjz32yoMdyvZsMvqFnbbsDxhTYw6FG1yMqKV8Rbh/go-libp2p-interface-pnet"
smux "gx/ipfs/QmeZBgYBHvxMukGK5ojg28BCNLB9SeXqT7XXg6o7r2GbJy/go-stream-muxer"
)
Expand Down Expand Up @@ -102,9 +103,10 @@ type IpfsNode struct {
Repo repo.Repo

// Local node
Pinning pin.Pinner // the pinning manager
Mounts Mounts // current mount state, if any.
PrivateKey ic.PrivKey // the local node's private Key
Pinning pin.Pinner // the pinning manager
Mounts Mounts // current mount state, if any.
PrivateKey ic.PrivKey // the local node's private Key
PNetFingerpint []byte // fingerprint of private network

// Services
Peerstore pstore.Peerstore // storage for other Peer instances
Expand Down Expand Up @@ -176,9 +178,6 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin

tpt := makeSmuxTransport(mplex)

<<<<<<< HEAD
peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter, addrfilter, tpt)
=======
swarmkey, err := n.Repo.SwarmKey()
if err != nil {
return err
Expand All @@ -193,8 +192,8 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
n.PNetFingerpint = protec.Fingerprint()
}

peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter, addrfilter, tpt, protec)
>>>>>>> e9c7c5abf... Update deps
peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, n.Reporter,
addrfilter, tpt, protec)
if err != nil {
return err
}
Expand Down Expand Up @@ -691,15 +690,15 @@ func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
return listen, nil
}

type HostOption func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport) (p2phost.Host, error)
type HostOption func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport, protc ipnet.Protector) (p2phost.Host, error)

var DefaultHostOption HostOption = constructPeerHost

// isolates the complex initialization steps
func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport) (p2phost.Host, error) {
func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, tpt smux.Transport, protec ipnet.Protector) (p2phost.Host, error) {

// no addresses to begin with. we'll start later.
swrm, err := swarm.NewSwarmWithProtector(ctx, nil, id, ps, nil, tpt, bwr)
swrm, err := swarm.NewSwarmWithProtector(ctx, nil, id, ps, protec, tpt, bwr)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion core/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
mocknet "gx/ipfs/QmU3g3psEDiC4tQh1Qu2NYg5aYVQqxC3m74ZavLwPfJEtu/go-libp2p/p2p/net/mock"
peer "gx/ipfs/QmZcUPvPhD1Xvk6mwijYF8AfR3mG31S1YsEfHG4khrFPRr/go-libp2p-peer"
host "gx/ipfs/QmbzbRyd22gcW92U1rA2yKagB3myMYhk45XBknJ49F9XWJ/go-libp2p-host"
ipnet "gx/ipfs/QmcT6bMjz32yoMdyvZsMvqFnbbsDxhTYw6FG1yMqKV8Rbh/go-libp2p-interface-pnet"
smux "gx/ipfs/QmeZBgYBHvxMukGK5ojg28BCNLB9SeXqT7XXg6o7r2GbJy/go-stream-muxer"
)

Expand All @@ -33,7 +34,7 @@ func NewMockNode() (*core.IpfsNode, error) {
}

func MockHostOption(mn mocknet.Mocknet) core.HostOption {
return func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, _ smux.Transport) (host.Host, error) {
return func(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr metrics.Reporter, fs []*net.IPNet, _ smux.Transport, _ ipnet.Protector) (host.Host, error) {
return mn.AddPeerWithPeerstore(id, ps)
}
}
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@
"hash": "QmU1N5xVAUXgo3XRTt6GhJ2SuJEbxj2zRgMS7FpjSR2U83",
"name": "semver",
"version": "3.3.0"
},
{
"author": "libp2p",
"hash": "QmQ2sk2irQYkeMMVKpLjq1bSiNPn9B5wZB53Nx8dd4dhnQ",
"name": "go-libp2p-pnet",
"version": "2.2.1"
}
],
"gxVersion": "0.10.0",
Expand Down
22 changes: 22 additions & 0 deletions repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -62,6 +63,7 @@ func (err NoRepoError) Error() string {
}

const apiFile = "api"
const swarmKeyFile = "swarm.key"

var (

Expand Down Expand Up @@ -592,6 +594,26 @@ func (r *FSRepo) GetStorageUsage() (uint64, error) {
return du, err
}

func (r *FSRepo) SwarmKey() ([]byte, error) {
repoPath := filepath.Clean(r.path)
spath := filepath.Join(repoPath, swarmKeyFile)

f, err := os.Open(spath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
} else {
return nil, err
}
}
defer f.Close()
if err != nil {
return nil, err
}

return ioutil.ReadAll(f)
}

var _ io.Closer = &FSRepo{}
var _ repo.Repo = &FSRepo{}

Expand Down
4 changes: 4 additions & 0 deletions repo/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ func (m *Mock) Close() error { return errTODO }
func (m *Mock) SetAPIAddr(addr ma.Multiaddr) error { return errTODO }

func (m *Mock) Keystore() keystore.Keystore { return nil }

func (m *Mock) SwarmKey() ([]byte, error) {
return nil, nil
}
2 changes: 2 additions & 0 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Repo interface {
// SetAPIAddr sets the API address in the repo.
SetAPIAddr(addr ma.Multiaddr) error

SwarmKey() ([]byte, error)

io.Closer
}

Expand Down

0 comments on commit b217609

Please sign in to comment.