mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-24 20:07:45 +08:00
p2p: split forward into 2 commands
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
parent
eb45436512
commit
f5ab137fc0
@ -163,6 +163,7 @@ func TestCommands(t *testing.T) {
|
||||
"/p2p",
|
||||
"/p2p/close",
|
||||
"/p2p/forward",
|
||||
"/p2p/listen",
|
||||
"/p2p/ls",
|
||||
"/p2p/stream",
|
||||
"/p2p/stream/close",
|
||||
|
||||
@ -62,6 +62,7 @@ are refined`,
|
||||
"stream": p2pStreamCmd,
|
||||
|
||||
"forward": p2pForwardCmd,
|
||||
"listen": p2pListenCmd,
|
||||
"close": p2pCloseCmd,
|
||||
"ls": p2pLsCmd,
|
||||
},
|
||||
@ -69,19 +70,14 @@ are refined`,
|
||||
|
||||
var p2pForwardCmd = &cmds.Command{
|
||||
Helptext: cmdkit.HelpText{
|
||||
Tagline: "Forward connections to or from libp2p services",
|
||||
Tagline: "Forward connections to libp2p service",
|
||||
ShortDescription: `
|
||||
Forward connections made to <listen-address> to <target-address>.
|
||||
|
||||
<protocol> specifies the libp2p protocol name to use for libp2p
|
||||
connections and/or handlers. It must be prefixed with '` + P2PProtoPrefix + `'.
|
||||
|
||||
To create a libp2p service listener, specify '/ipfs' as <listen-address>
|
||||
|
||||
Examples:
|
||||
ipfs p2p forward ` + P2PProtoPrefix + `myproto /ipfs /ip4/127.0.0.1/tcp/1234
|
||||
- Forward connections to 'myproto' libp2p service to 127.0.0.1:1234
|
||||
|
||||
Example:
|
||||
ipfs p2p forward ` + P2PProtoPrefix + `myproto /ip4/127.0.0.1/tcp/4567 /ipfs/QmPeer
|
||||
- Forward connections to 127.0.0.1:4567 to '` + P2PProtoPrefix + `myproto' service on /ipfs/QmPeer
|
||||
|
||||
@ -117,26 +113,65 @@ Examples:
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(listen, "/ipfs") {
|
||||
if listen != "/ipfs" {
|
||||
res.SetError(errors.New("only '/ipfs' is allowed as libp2p listen address"), cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
if err := forwardRemote(n.Context(), n.P2P, proto, target); err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, target); err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, target); err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
res.SetOutput(nil)
|
||||
},
|
||||
}
|
||||
|
||||
var p2pListenCmd = &cmds.Command{
|
||||
Helptext: cmdkit.HelpText{
|
||||
Tagline: "Create libp2p service",
|
||||
ShortDescription: `
|
||||
Create libp2p service and forward connections made to <target-address>.
|
||||
|
||||
<protocol> specifies the libp2p handler name. It must be prefixed with '` + P2PProtoPrefix + `'.
|
||||
|
||||
Example:
|
||||
ipfs p2p listen ` + P2PProtoPrefix + `myproto /ip4/127.0.0.1/tcp/1234
|
||||
- Forward connections to 'myproto' libp2p service to 127.0.0.1:1234
|
||||
|
||||
`,
|
||||
},
|
||||
Arguments: []cmdkit.Argument{
|
||||
cmdkit.StringArg("protocol", true, false, "Protocol name."),
|
||||
cmdkit.StringArg("target-address", true, false, "Target endpoint."),
|
||||
},
|
||||
Options: []cmdkit.Option{
|
||||
cmdkit.BoolOption("allow-custom-protocol", "Don't require /x/ prefix"),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
n, err := p2pGetNode(req)
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
proto := req.Arguments()[0]
|
||||
target := req.Arguments()[1]
|
||||
|
||||
allowCustom, _, err := req.Option("allow-custom-protocol").Bool()
|
||||
if err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
if !allowCustom && !strings.HasPrefix(proto, P2PProtoPrefix) {
|
||||
res.SetError(errors.New("protocol name must be within '"+P2PProtoPrefix+"' namespace"), cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
if err := forwardRemote(n.Context(), n.P2P, proto, target); err != nil {
|
||||
res.SetError(err, cmdkit.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
res.SetOutput(nil)
|
||||
},
|
||||
}
|
||||
|
||||
// forwardRemote forwards libp2p service connections to a manet address
|
||||
func forwardRemote(ctx context.Context, p *p2p.P2P, proto string, target string) error {
|
||||
if strings.HasPrefix(target, "/ipfs") {
|
||||
|
||||
@ -292,7 +292,7 @@ port `$APP_PORT`.
|
||||
Then, configure the p2p listener by running:
|
||||
|
||||
```sh
|
||||
> ipfs p2p forward /x/kickass/1.0 /ipfs /ip4/127.0.0.1/tcp/$APP_PORT
|
||||
> ipfs p2p listen /x/kickass/1.0 /ip4/127.0.0.1/tcp/$APP_PORT
|
||||
```
|
||||
|
||||
This will configure IPFS to forward all incoming `/x/kickass/1.0` streams to
|
||||
@ -341,7 +341,7 @@ _you can get `$SERVER_ID` by running `ipfs id -f "<id>\n"`_
|
||||
***First, on the "server" node:***
|
||||
|
||||
```sh
|
||||
ipfs p2p forward /x/ssh /ipfs /ip4/127.0.0.1/tcp/22
|
||||
ipfs p2p listen /x/ssh /ip4/127.0.0.1/tcp/22
|
||||
```
|
||||
|
||||
***Then, on "client" node:***
|
||||
|
||||
@ -38,7 +38,7 @@ test_expect_success "enable filestore config setting" '
|
||||
'
|
||||
|
||||
test_expect_success 'start p2p listener' '
|
||||
ipfsi 0 p2p forward /x/p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log
|
||||
ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log
|
||||
'
|
||||
|
||||
# Server to client communications
|
||||
@ -136,7 +136,7 @@ test_expect_success "'ipfs p2p ls' output looks good" '
|
||||
'
|
||||
|
||||
test_expect_success "Cannot re-register app handler" '
|
||||
test_must_fail ipfsi 0 p2p forward /x/p2p-test /ipfs /ip4/127.0.0.1/tcp/10101
|
||||
test_must_fail ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs p2p stream ls' output is empty" '
|
||||
@ -188,7 +188,7 @@ check_test_ports
|
||||
test_expect_success "Setup: Idle stream(2)" '
|
||||
ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 &
|
||||
|
||||
ipfsi 0 p2p forward /x/p2p-test2 /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
|
||||
ipfsi 0 p2p listen /x/p2p-test2 /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
|
||||
ipfsi 1 p2p forward /x/p2p-test2 /ip4/127.0.0.1/tcp/10102 /ipfs/$PEERID_0 2>&1 > dialer-stdouterr.log &&
|
||||
ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 &
|
||||
|
||||
@ -225,7 +225,7 @@ test_expect_success "'ipfs p2p stream close -a' closes streams" '
|
||||
check_test_ports
|
||||
|
||||
test_expect_success "'ipfs p2p close' closes app numeric handlers" '
|
||||
ipfsi 0 p2p forward /x/1234 /ipfs /ip4/127.0.0.1/tcp/10101 &&
|
||||
ipfsi 0 p2p listen /x/1234 /ip4/127.0.0.1/tcp/10101 &&
|
||||
ipfsi 0 p2p close -p /x/1234 &&
|
||||
ipfsi 0 p2p ls > actual &&
|
||||
test_must_be_empty actual
|
||||
@ -240,7 +240,7 @@ test_expect_success "non /x/ scoped protocols are not allowed" '
|
||||
check_test_ports
|
||||
|
||||
test_expect_success 'start p2p listener on custom proto' '
|
||||
ipfsi 0 p2p forward --allow-custom-protocol /p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
|
||||
ipfsi 0 p2p listen --allow-custom-protocol /p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
|
||||
test_must_be_empty listener-stdouterr.log
|
||||
'
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user