-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxes-ext-native-shell.go
104 lines (96 loc) · 2.26 KB
/
boxes-ext-native-shell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/rpc"
"os"
"path"
"strconv"
"strings"
"unsafe"
"github.com/firefox-boxes/boxes-shell/logging"
)
type Msg struct {
Msg string `json:"msg"`
}
func encodeMessage(msg string) ([]byte, []byte) {
b, err := json.Marshal(Msg{Msg:msg})
if err != nil {
panic(err)
}
bl := uint32(len(b))
return (*[4]byte)(unsafe.Pointer(&bl))[:], b
}
func sendMessage(msg string) {
logging.Info("Sending `%v`", msg)
bl, b := encodeMessage(msg)
fmt.Print(string(bl))
fmt.Print(string(b))
}
func decode(msg []byte) string {
var command string
json.Unmarshal(msg, &command)
logging.Info("Arrival:3 m=%v decoded=%v", msg, command)
return command
}
func readInput() []byte {
reader := bufio.NewReader(os.Stdin)
length := [4]byte{}
n, err := io.ReadFull(reader, length[:])
if err != nil {
logging.Info("%v bytes read: %v", err, n)
panic(err)
}
l := *(*uint32)(unsafe.Pointer(&length))
logging.Info("Arrival:1 lb=%v l=%v", length, l)
input := make([]byte, l, l)
n, err = io.ReadFull(reader, input)
if err != nil {
logging.Info("%v bytes read: %v", err, n)
panic(err)
}
logging.Info("Arrival:2 content=%v", logging.ProcessStr(string(input), 40))
return input
}
func main() {
client, err := rpc.Dial("tcp", "127.0.0.1:6688")
if err != nil {
logging.Info(err.Error())
panic(err)
}
logging.Info("Connected to 127.0.0.1:6688")
defer client.Close()
sendMessage(query(client, "whoami " + strconv.Itoa(os.Getppid())))
for {
cmdString := decode(readInput())
cmdString = strings.TrimSuffix(cmdString, "\n")
result := query(client, cmdString)
if strings.HasPrefix(cmdString, "box:new") {
home, err := os.UserHomeDir()
if err != nil {
logging.Info(err.Error())
panic(err)
}
to, err := os.OpenFile(path.Join(home, ".FirefoxBoxes", "Boxes", result, "extensions", "boxes@whatsyouridea.com.xpi"), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
logging.Info(err.Error())
panic(err)
}
from, err := os.Open(path.Join(home, ".FirefoxBoxes", "boxes-ext-latest.xpi"))
if err != nil {
logging.Info(err.Error())
panic(err)
}
_, err = io.Copy(to, from)
if err != nil {
logging.Info(err.Error())
panic(err)
}
to.Close()
from.Close()
}
sendMessage(result)
}
}