-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Network CLI #9
Merged
Merged
Network CLI #9
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cdca2ff
Some net commands / apis
magik6k f9b5343
daemon: allow setting listen addr
magik6k 9a244eb
Addrs listen api
magik6k 797ef4e
Actually implement NetConnect
magik6k 8289016
Fix gomod dep
magik6k d852b3f
API struct instead of DI magic
magik6k 40d9fbd
Introduce reqContext for cli
Kubuxu 66a67b0
Wire in request context
Kubuxu 980bed1
Fix remaining
Kubuxu f9a34b3
Cleanup lint
Kubuxu 093fabc
Build tag to disable daemon build
magik6k 1bead4b
jsonrpc: client debug logging
magik6k 7069d57
Fix multiaddr json unmarshalling in NetAddrsListen
magik6k 63b316b
Improve output from net commands
magik6k 5b04fbb
config: default to random ports for now
magik6k 271c268
disable natPortMap for now
magik6k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// TODO: check if this exists anywhere else | ||
type MultiaddrSlice []ma.Multiaddr | ||
|
||
func (m *MultiaddrSlice) UnmarshalJSON(raw []byte) (err error) { | ||
var temp []string | ||
if err := json.Unmarshal(raw, &temp); err != nil { | ||
return err | ||
} | ||
|
||
res := make([]ma.Multiaddr, len(temp)) | ||
for i, str := range temp { | ||
res[i], err = ma.NewMultiaddr(str) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
*m = res | ||
return nil | ||
} | ||
|
||
var _ json.Unmarshaler = new(MultiaddrSlice) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,48 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/filecoin-project/go-lotus/api" | ||
"gopkg.in/urfave/cli.v2" | ||
) | ||
|
||
// Commands is the root group of CLI commands | ||
const ( | ||
metadataContext = "context" | ||
metadataAPI = "api" | ||
) | ||
|
||
// ApiConnector returns API instance | ||
type ApiConnector func() api.API | ||
|
||
func getApi(ctx *cli.Context) api.API { | ||
return ctx.App.Metadata[metadataAPI].(ApiConnector)() | ||
} | ||
|
||
// reqContext returns context for cli execution. Calling it for the first time | ||
// installs SIGTERM handler that will close returned context. | ||
// Not safe for concurrent execution. | ||
func reqContext(cctx *cli.Context) context.Context { | ||
if uctx, ok := cctx.App.Metadata[metadataContext]; ok { | ||
// unchecked cast as if something else is in there | ||
// it is crash worthy either way | ||
return uctx.(context.Context) | ||
} | ||
ctx, done := context.WithCancel(context.Background()) | ||
sigChan := make(chan os.Signal, 2) | ||
go func() { | ||
<-sigChan | ||
done() | ||
}() | ||
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
return ctx | ||
} | ||
|
||
var Commands = []*cli.Command{ | ||
netCmd, | ||
versionCmd, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p-core/peer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
madns "github.com/multiformats/go-multiaddr-dns" | ||
"gopkg.in/urfave/cli.v2" | ||
) | ||
|
||
var netCmd = &cli.Command{ | ||
Name: "net", | ||
Usage: "Manage P2P Network", | ||
Subcommands: []*cli.Command{ | ||
netPeers, | ||
netConnect, | ||
netListen, | ||
}, | ||
} | ||
|
||
var netPeers = &cli.Command{ | ||
Name: "peers", | ||
Usage: "Print peers", | ||
Action: func(cctx *cli.Context) error { | ||
api := getApi(cctx) | ||
ctx := reqContext(cctx) | ||
peers, err := api.NetPeers(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, peer := range peers { | ||
fmt.Println(peer) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var netListen = &cli.Command{ | ||
Name: "listen", | ||
Usage: "List listen addresses", | ||
Action: func(cctx *cli.Context) error { | ||
api := getApi(cctx) | ||
ctx := reqContext(cctx) | ||
|
||
addrs, err := api.NetAddrsListen(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, peer := range addrs { | ||
fmt.Println(peer) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
var netConnect = &cli.Command{ | ||
Name: "connect", | ||
Usage: "Connect to a peer", | ||
Action: func(cctx *cli.Context) error { | ||
api := getApi(cctx) | ||
ctx := reqContext(cctx) | ||
|
||
pis, err := parseAddresses(ctx, cctx.Args().Slice()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, pi := range pis { | ||
fmt.Printf("connect %s: ", pi.ID.Pretty()) | ||
err := api.NetConnect(ctx, pi) | ||
if err != nil { | ||
fmt.Println("failure") | ||
return err | ||
} | ||
fmt.Println("success") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
// parseAddresses is a function that takes in a slice of string peer addresses | ||
// (multiaddr + peerid) and returns a slice of properly constructed peers | ||
func parseAddresses(ctx context.Context, addrs []string) ([]peer.AddrInfo, error) { | ||
// resolve addresses | ||
maddrs, err := resolveAddresses(ctx, addrs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return peer.AddrInfosFromP2pAddrs(maddrs...) | ||
} | ||
|
||
const ( | ||
dnsResolveTimeout = 10 * time.Second | ||
) | ||
|
||
// resolveAddresses resolves addresses parallelly | ||
func resolveAddresses(ctx context.Context, addrs []string) ([]ma.Multiaddr, error) { | ||
ctx, cancel := context.WithTimeout(ctx, dnsResolveTimeout) | ||
defer cancel() | ||
|
||
var maddrs []ma.Multiaddr | ||
var wg sync.WaitGroup | ||
resolveErrC := make(chan error, len(addrs)) | ||
|
||
maddrC := make(chan ma.Multiaddr) | ||
|
||
for _, addr := range addrs { | ||
maddr, err := ma.NewMultiaddr(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// check whether address ends in `ipfs/Qm...` | ||
if _, last := ma.SplitLast(maddr); last.Protocol().Code == ma.P_IPFS { | ||
maddrs = append(maddrs, maddr) | ||
continue | ||
} | ||
wg.Add(1) | ||
go func(maddr ma.Multiaddr) { | ||
defer wg.Done() | ||
raddrs, err := madns.Resolve(ctx, maddr) | ||
if err != nil { | ||
resolveErrC <- err | ||
return | ||
} | ||
// filter out addresses that still doesn't end in `ipfs/Qm...` | ||
found := 0 | ||
for _, raddr := range raddrs { | ||
if _, last := ma.SplitLast(raddr); last != nil && last.Protocol().Code == ma.P_IPFS { | ||
maddrC <- raddr | ||
found++ | ||
} | ||
} | ||
if found == 0 { | ||
resolveErrC <- fmt.Errorf("found no ipfs peers at %s", maddr) | ||
} | ||
}(maddr) | ||
} | ||
go func() { | ||
wg.Wait() | ||
close(maddrC) | ||
}() | ||
|
||
for maddr := range maddrC { | ||
maddrs = append(maddrs, maddr) | ||
} | ||
|
||
select { | ||
case err := <-resolveErrC: | ||
return nil, err | ||
default: | ||
} | ||
|
||
return maddrs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build nodaemon | ||
|
||
package daemon | ||
|
||
import ( | ||
"errors" | ||
|
||
"gopkg.in/urfave/cli.v2" | ||
) | ||
|
||
// Cmd is the `go-lotus daemon` command | ||
var Cmd = &cli.Command{ | ||
Name: "daemon", | ||
Usage: "Start a lotus daemon process", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "api", | ||
Value: ":1234", | ||
}, | ||
}, | ||
Action: func(cctx *cli.Context) error { | ||
return errors.New("daemon support not included in this binary") | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a slightly weird way to wire this through
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed (this is just the first thing I tried)