-
Notifications
You must be signed in to change notification settings - Fork 1
/
alfredclient.go
166 lines (152 loc) · 3.88 KB
/
alfredclient.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bytes"
"compress/gzip"
"compress/zlib"
"flag"
"fmt"
"github.com/hwhw/mesh/alfred"
"io"
"io/ioutil"
"os"
"strconv"
)
var network = flag.String("p", "unix", "network (unix, tcp) to use for connecting server")
var address = flag.String("a", "/var/run/alfred.sock", "address to connect to")
var zlibpipe = flag.Bool("z", false, "zlib compress/uncompress data")
var gzippipe = flag.Bool("g", false, "gzip/gunzip data")
func failure(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(-1)
}
func usage() {
failure(`
Usage:
alfredclient [options] <command> <command options>
available options:
-p<network> network to use (unix, tcp)
-a<address> address to use for connecting server
available commands and additional options if any:
set <type> will set local data for type <type> (1-255)
data will be read from stdin.
-g gzip compress data before storing
-z zlib compress data before storing
get <type> will fetch and output data
-g gzip uncompress data before outputting
-z zlib uncompress data before outputting
mode <modeid> will request server to switch to operation mode <modeid>
0: slave mode
1: master mode
2: stealth master mode (only go-based alfred implementation)
interfaces <iface list>
request server to listen on given interfaces.
`)
}
func compress(data []byte) []byte {
if *zlibpipe || *gzippipe {
zipped := new(bytes.Buffer)
var zwriter io.WriteCloser
if *zlibpipe {
zwriter = zlib.NewWriter(zipped)
} else {
zwriter = gzip.NewWriter(zipped)
}
_, err := zwriter.Write(data)
if err == nil {
err = zwriter.Close()
}
if err != nil {
failure("error gzipping data: %v\n", err)
}
return zipped.Bytes()
} else {
return data
}
}
func uncompress(data []byte) []byte {
if *zlibpipe || *gzippipe {
var err error
var n int
unzipped := make([]byte, 0, 1024)
out := make([]byte, 1024)
zreader := bytes.NewReader(data)
var uzreader io.Reader
if *zlibpipe {
uzreader, err = zlib.NewReader(zreader)
} else {
uzreader, err = gzip.NewReader(zreader)
}
pos := 0
for err == nil {
n, err = uzreader.Read(out[0:1024])
pos += n
unzipped = append(unzipped, out[0:n]...)
}
return unzipped
} else {
return data
}
}
func main() {
flag.Parse()
client := alfred.NewClient(*network, *address, nil)
var reterr error
switch flag.Arg(0) {
case "set":
id, err := strconv.Atoi(flag.Arg(1))
if err != nil || id < 1 || id > 255 {
failure("error: invalid type %v\n", flag.Arg(1))
goto failure
}
buf, err := ioutil.ReadAll(os.Stdin)
if err != nil {
failure("error: reading data from stdin, %v\n", err)
}
reterr = client.PushData(uint8(id), compress(buf))
case "get":
id, err := strconv.Atoi(flag.Arg(1))
if err != nil || id < 1 || id > 255 {
failure("error: invalid type %v\n", flag.Arg(1))
goto failure
}
reterr = client.Request(uint8(id), func(d alfred.Data) error {
fmt.Printf("{%s, \"", d.Source)
buf := uncompress(d.Data)
for _, c := range buf {
switch {
case c == 0x5C: // backslash
os.Stdout.Write([]byte{c, c})
case c == 0x22: // parenthesis
os.Stdout.Write([]byte{0x5C, c})
case 0x20 <= c && c <= 0x7E:
os.Stdout.Write([]byte{c})
default:
fmt.Printf("\\x%02x", c)
}
}
fmt.Print("\"},\n")
return nil
})
case "mode":
mode, err := strconv.Atoi(flag.Arg(1))
if err != nil || mode < 0 || mode > 2 {
failure("error: invalid mode %v\n", flag.Arg(1))
goto failure
}
reterr = client.ModeSwitch(uint8(mode))
case "interfaces":
ifaces := []byte(flag.Arg(1))
if len(ifaces) < 1 {
failure("you need to specify interface(s)")
}
reterr = client.ChangeInterface(ifaces)
default:
usage()
}
if reterr != nil {
failure("error: %v\n", reterr)
}
return
failure:
os.Exit(-1)
}