-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhost.go
290 lines (258 loc) · 8.45 KB
/
host.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// host.go - Native messaging host config, message handler, and else.
// Copyright (c) 2018 - 2020 Richard Huang <rickypc@users.noreply.github.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Package host provides native-messaging host configurations, send and receive
// message handler, manifest install and uninstall, as well as auto update daily
// check.
//
// * Sending Message
//
// messaging := (&host.Host{}).Init()
//
// // host.H is a shortcut to map[string]interface{}
// response := &host.H{"key":"value"}
//
// // Write message from response to os.Stdout.
// if err := messaging.PostMessage(os.Stdout, response); err != nil {
// log.Fatalf("messaging.PostMessage error: %v", err)
// }
//
// // Log response.
// log.Printf("response: %+v", response)
//
// * Receiving Message
//
// // Ensure func main returned after calling runtime.Goexit
// // See https://golang.org/pkg/runtime/#Goexit.
// defer os.Exit(0)
//
// messaging := (&host.Host{}).Init()
//
// // host.H is a shortcut to map[string]interface{}
// request := &host.H{}
//
// // Read message from os.Stdin to request.
// if err := messaging.OnMessage(os.Stdin, request); err != nil {
// log.Fatalf("messaging.OnMessage error: %v", err)
// }
//
// // Log request.
// log.Printf("request: %+v", request)
//
// * Install and Uninstall Hooks
//
// // AllowedExts is a list of extensions that should have access to the native messaging host.
// // See [native messaging manifest][7]
// messaging := (&host.Host{
// AppName: "tld.domain.sub.app.name",
// AllowedExts: []string{"chrome-extension://XXX/", "chrome-extension://YYY/"},
// }).Init()
//
// ...
//
// // When you need to install.
// if err := messaging.Install(); err != nil {
// log.Printf("install error: %v", err)
// }
//
// ...
//
// // When you need to uninstall.
// host.Uninstall()
//
// * Auto Update Configuration
//
// // updates.xml example for cross platform executable:
// <?xml version='1.0' encoding='UTF-8'?>
// <gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
// <app appid='tld.domain.sub.app.name'>
// <updatecheck codebase='https://sub.domain.tld/app.download.all' version='1.0.0' />
// </app>
// </gupdate>
//
// // updates.xml example for individual platform executable:
// <?xml version='1.0' encoding='UTF-8'?>
// <gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
// <app appid='tld.domain.sub.app.name'>
// <updatecheck codebase='https://sub.domain.tld/app.download.darwin' os='darwin' version='1.0.0' />
// <updatecheck codebase='https://sub.domain.tld/app.download.linux' os='linux' version='1.0.0' />
// <updatecheck codebase='https://sub.domain.tld/app.download.exe' os='windows' version='1.0.0' />
// </app>
// </gupdate>
//
// // It will do daily update check.
// messaging := (&host.Host{
// AppName: "tld.domain.sub.app.name",
// UpdateUrl: "https://sub.domain.tld/updates.xml", // It follows [update manifest][2]
// Version: "1.0.0", // Current version, it must follow [SemVer][6]
// }).Init()
package host
import (
"encoding/binary"
"encoding/json"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
// ioutilWriteFile is a shortcut to ioutil.WriteFile. It helps write testable code.
var ioutilWriteFile = ioutil.WriteFile
// osMkdirAll is a shortcut to os.MkdirAll. It helps write testable code.
var osMkdirAll = os.MkdirAll
// runtimeGoexit is a shortcut to runtime.Goexit. It helps write testable code.
var runtimeGoexit = runtime.Goexit
// H is a map[string]interface{} type shortcut and represents a dynamic
// key-value-pair data.
type H map[string]interface{}
// Host represents a single native messaging host, where all native messaging
// host operations can be done.
type Host struct {
AppName string `json:"name"`
AppDesc string `json:"description"`
ExecName string `json:"path"`
AppType string `json:"type"`
AllowedExts []string `json:"allowed_origins"`
AutoUpdate bool `json:"-"`
ByteOrder binary.ByteOrder `json:"-"`
UpdateUrl string `json:"-"`
Version string `json:"-"`
}
// Init sets default value to its fields and return the Host pointer back.
//
// * AppName is an application name in manifest file and will be defaulted to
// current executable file name without extension, if any.
//
// * AppDesc is an application description in manifest file and will be defaulted
// to current AppName.
//
// * AppType is an application communication type in manifest file and will be
// defaulted to "stdio".
//
// * AutoUpdate indicates whether update check will be perform for this
// application and will be defaulted to true only if UpdateUrl and application
// Version are present, otherwise it will be false.
//
// * ByteOrder specifies how to convert byte sequences into unsigned integers and
// will be defaulted to binary.LittleEndian.
//
// * ExecName is an executable path used across the module and will get assigned
// to current executable's absolute path after the evaluation of any symbolic
// links.
//
// messaging := (&host.Host{}).Init()
func (h *Host) Init() *Host {
exec, _ := os.Executable()
evaled, _ := filepath.EvalSymlinks(exec)
h.ExecName, _ = filepath.Abs(evaled)
if h.AppName == "" {
h.AppName = strings.TrimSuffix(filepath.Base(h.ExecName), path.Ext(h.ExecName))
}
if h.AppDesc == "" {
h.AppDesc = h.AppName
}
if h.AppType == "" {
h.AppType = "stdio"
}
if h.ByteOrder == nil {
h.ByteOrder = binary.LittleEndian
}
if h.UpdateUrl != "" && h.Version != "" {
h.AutoUpdate = true
}
return h
}
// OnMessage reads message header and message body from given reader and
// unmarshal to given struct. It will return error when it come across one.
//
// // Ensure func main returned after calling runtime.Goexit
// // See https://golang.org/pkg/runtime/#Goexit.
// defer os.Exit(0)
//
// messaging := (&host.Host{}).Init()
//
// // host.H is a shortcut to map[string]interface{}
// request := &host.H{}
//
// // Read message from os.Stdin to request.
// if err := messaging.OnMessage(os.Stdin, request); err != nil {
// log.Fatalf("messaging.OnMessage error: %v", err)
// }
//
// // Log request.
// log.Printf("request: %+v", request)
func (h *Host) OnMessage(reader io.Reader, v interface{}) error {
length, err := h.readHeader(reader)
if err != nil {
return err
}
// Nothing to read.
if length == 0 {
return nil
}
// Read message body.
if err := json.NewDecoder(io.LimitReader(reader, int64(length))).Decode(v); err != nil {
return err
}
return nil
}
// readHeader reads message header and will return the message length. It will
// return error when it come across one.
func (h *Host) readHeader(reader io.Reader) (uint32, error) {
// Read message length.
var length uint32
if err := binary.Read(reader, h.ByteOrder, &length); err != nil {
if err == io.EOF {
h.AutoUpdateCheck()
// Exit gracefully.
runtimeGoexit()
}
return length, err
}
return length, nil
}
// PostMessage marshals given struct and writes message header and message body
// to given writer. It will return error when it come across one.
//
// messaging := (&host.Host{}).Init()
//
// // host.H is a shortcut to map[string]interface{}
// response := &host.H{"key":"value"}
//
// // Write message from response to os.Stdout.
// if err := messaging.PostMessage(os.Stdout, response); err != nil {
// log.Fatalf("messaging.PostMessage error: %v", err)
// }
//
// // Log response.
// log.Printf("response: %+v", response)
func (h *Host) PostMessage(writer io.Writer, v interface{}) error {
message, err := json.Marshal(v)
if err != nil {
return err
}
length := len(message)
if err := h.writeHeader(writer, length); err != nil {
return err
}
// Write message body.
if n, err := writer.Write(message); err != nil || n != length {
return err
}
return nil
}
// writeHeader writes message length into message header. It will return error
// when it come across one.
func (h *Host) writeHeader(writer io.Writer, length int) error {
header := make([]byte, 4)
h.ByteOrder.PutUint32(header, (uint32)(length))
if n, err := writer.Write(header); err != nil || n != len(header) {
return err
}
return nil
}