2019-07-02 12:40:25 +00:00
|
|
|
package lp2p
|
2019-07-01 09:09:48 +00:00
|
|
|
|
2019-07-01 10:53:58 +00:00
|
|
|
import (
|
2019-07-01 09:09:48 +00:00
|
|
|
"fmt"
|
2019-07-01 10:53:58 +00:00
|
|
|
"strings"
|
2019-07-01 09:09:48 +00:00
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
pnet "github.com/libp2p/go-libp2p-pnet"
|
|
|
|
)
|
|
|
|
|
2019-07-01 10:53:58 +00:00
|
|
|
var lotusKey = "/key/swarm/psk/1.0.0/\n/base16/\n20c72398e6299c7bbc1b501fdcc8abe4f89f798e9b93b2d2bc02e3c29b6a088e"
|
2019-07-01 09:09:48 +00:00
|
|
|
|
2019-07-01 10:53:58 +00:00
|
|
|
type PNetFingerprint []byte
|
2019-07-01 09:09:48 +00:00
|
|
|
|
2019-07-01 10:53:58 +00:00
|
|
|
func PNet() (opts Libp2pOpts, fp PNetFingerprint, err error) {
|
|
|
|
protec, err := pnet.NewProtector(strings.NewReader(lotusKey))
|
2019-07-01 09:09:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return opts, nil, fmt.Errorf("failed to configure private network: %s", err)
|
|
|
|
}
|
|
|
|
fp = protec.Fingerprint()
|
|
|
|
|
|
|
|
opts.Opts = append(opts.Opts, libp2p.PrivateNetwork(protec))
|
|
|
|
return opts, fp, nil
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:53:58 +00:00
|
|
|
/*
|
2019-07-01 09:09:48 +00:00
|
|
|
func PNetChecker(repo repo.Repo, ph host.Host, lc fx.Lifecycle) error {
|
|
|
|
// TODO: better check?
|
|
|
|
swarmkey, err := repo.SwarmKey()
|
|
|
|
if err != nil || swarmkey == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(_ context.Context) error {
|
|
|
|
go func() {
|
|
|
|
t := time.NewTicker(30 * time.Second)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
<-t.C // swallow one tick
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
if len(ph.Network().Peers()) == 0 {
|
2019-10-09 20:23:04 +00:00
|
|
|
log.Warn("We are in private network and have no peers.")
|
|
|
|
log.Warn("This might be configuration mistake.")
|
2019-07-01 09:09:48 +00:00
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
OnStop: func(_ context.Context) error {
|
|
|
|
close(done)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-01 20:00:22 +00:00
|
|
|
*/
|