-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
128 lines (111 loc) · 2.59 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
)
const (
version = "1.0"
)
type message struct {
Event string `json:"event"`
Address string `json:"address,omitempty"`
Payload string `json:"payload,omitempty"`
Error string `json:"error,omitempty"`
Debug string `json:"debug,omitempty"`
}
func write(msg message) {
msgBytes, error := json.Marshal(msg)
if error != nil {
return
}
lenBytes := make([]byte, 4)
nativeEndian.PutUint32(lenBytes, uint32(len(msgBytes)))
os.Stdout.Write(lenBytes)
os.Stdout.Write(msgBytes)
}
func read() message {
buffer := make([]byte, 4)
length, error := os.Stdin.Read(buffer)
if error != nil || length != 4 {
return message{
Event: "error",
Error: "cannot read input size",
Debug: error.Error() + ">" + string(buffer),
}
}
size := nativeEndian.Uint32(buffer)
buffer = make([]byte, size)
length, error = os.Stdin.Read(buffer)
if error != nil && length != int(size) {
return message{
Event: "error",
Error: "cannot read input message",
Debug: error.Error() + ">" + string(buffer),
}
}
msg := message{}
error = json.Unmarshal(buffer, &msg)
if error != nil {
return message{
Event: "error",
Error: "cannot deserialize input message",
Debug: error.Error() + ">" + string(buffer),
}
}
return msg
}
func main() {
versionPtr := flag.Bool("version", false, "print version")
installPtr := flag.Bool("install", false, "install host app for browsers")
chromeExtIDPtr := flag.String("chromeExtID", "aaaaaaaaaaabbbbbbbbbbccccccccccc", "chrome extension id to allow")
firefoxExtIDPtr := flag.String("firefoxExtID", "[email protected]", "firefox extension id to allow")
uninstallPtr := flag.Bool("uninstall", false, "uninstall host app from browsers")
flag.Parse()
if *versionPtr {
os.Stdout.Write([]byte(fmt.Sprintf("version: %s\n", version)))
os.Exit(0)
}
if *installPtr {
if install(*chromeExtIDPtr, *firefoxExtIDPtr) {
os.Stdout.Write([]byte("install: succeeded\n"))
os.Exit(0)
} else {
os.Stdout.Write([]byte("install: failed\n"))
os.Exit(1)
}
}
if *uninstallPtr {
uninstall()
os.Stdout.Write([]byte("uninstall: completed\n"))
os.Exit(0)
}
init := read()
if init.Event == "error" {
write(message{
Event: "close",
Error: init.Error,
Debug: init.Debug,
})
os.Exit(1)
}
switch init.Event {
case "open-udpPeer":
udpPeer(init.Address)
break
case "open-tcpServer":
tcpServer(init.Address)
break
case "open-tcpClient":
tcpClient(init.Address)
break
default:
write(message{
Event: "close",
Error: "unknown socket type",
})
os.Exit(1)
break
}
}