39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package proto
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
protocol "github.com/libp2p/go-libp2p/core/protocol"
|
|
"github.com/portal-co/portald/provision"
|
|
)
|
|
|
|
var BashId = "/p2p/portal-co-bash/0.0.1"
|
|
|
|
func AddBash(h host.Host) {
|
|
h.SetStreamHandler(protocol.ID(BashId), func(s network.Stream) {
|
|
o, _ := os.MkdirTemp("/tmp", "portal-*")
|
|
defer os.RemoveAll(o)
|
|
e := exec.Command("bwrap", "--unshare-all", "--share-net", "--ro-bind", "/", "/", "--ro-bind", "/ipfs", "/ipfs", "--dev /dev", "--proc", "/proc", "--tmp", "/tmp", "--bind", o, o, "/bin/bash")
|
|
e.Stdin = s
|
|
e.Stdout = s
|
|
e.Dir = o
|
|
e.Run()
|
|
})
|
|
}
|
|
func GetBash(ctx context.Context, host host.Host, peer peer.ID) (network.Stream, error) {
|
|
return host.NewStream(ctx, peer, protocol.ID(BashId))
|
|
}
|
|
func RunIn(ctx context.Context, host host.Host, peer peer.ID, x provision.ProcessInputs) (provision.ProcessOutputs, error) {
|
|
s, err := GetBash(ctx, host, peer)
|
|
if err != nil {
|
|
return provision.ProcessOutputs{}, err
|
|
}
|
|
defer s.Close()
|
|
return provision.Run(s, x)
|
|
}
|