forked from johnlauer/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 101
/
main.go
executable file
·408 lines (349 loc) · 12.8 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Version 1.95
// Supports Windows, Linux, Mac, and Raspberry Pi, Beagle Bone Black
package main
import (
"flag"
"go/build"
"log"
"net/http"
//"path/filepath"
"errors"
"fmt"
"net"
"os"
//"net/http/pprof"
//"runtime"
"io"
"runtime/debug"
"text/template"
"time"
)
var (
version = "1.96"
versionFloat = float32(1.96)
addr = flag.String("addr", ":8989", "http service address. example :8800 to run on port 8800, example 10.0.0.2:9000 to run on specific IP address and port, example 10.0.0.2 to run on specific IP address")
// addr = flag.String("addr", ":8980", "http service address. example :8800 to run on port 8800, example 10.0.0.2:9000 to run on specific IP address and port, example 10.0.0.2 to run on specific IP address")
saddr = flag.String("saddr", ":8990", "https service address. example :8801 to run https on port 8801")
scert = flag.String("scert", "cert.pem", "https certificate file")
skey = flag.String("skey", "key.pem", "https key file")
//assets = flag.String("assets", defaultAssetPath(), "path to assets")
// verbose = flag.Bool("v", true, "show debug logging")
verbose = flag.Bool("v", false, "show debug logging")
//homeTempl *template.Template
isLaunchSelf = flag.Bool("ls", false, "Launch self 5 seconds later. This flag is used when you ask for a restart from a websocket client.")
isAllowExec = flag.Bool("allowexec", false, "Allow terminal commands to be executed (default false)")
// regular expression to sort the serial port list
// typically this wouldn't be provided, but if the user wants to clean
// up their list with a regexp so it's cleaner inside their end-user interface
// such as ChiliPeppr, this can make the massive list that Linux gives back
// to you be a bit more manageable
regExpFilter = flag.String("regex", "", "Regular expression to filter serial port list, i.e. -regex usb|acm")
// allow garbageCollection()
//isGC = flag.Bool("gc", false, "Is garbage collection on? Off by default.")
//isGC = flag.Bool("gc", true, "Is garbage collection on? Off by default.")
gcType = flag.String("gc", "std", "Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)")
// whether to do buffer flow debugging
bufFlowDebugType = flag.String("bufflowdebug", "off", "off = (default) We do not send back any debug JSON, on = We will send back a JSON response with debug info based on the configuration of the buffer flow that the user picked")
// hostname. allow user to override, otherwise we look it up
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
// turn off cayenn
isDisableCayenn = flag.Bool("disablecayenn", false, "Disable loading of Cayenn TCP/UDP server on port 8988")
// isLoadCayenn = flag.Bool("allowcayenn", false, "Allow loading of Cayenn TCP/UDP server on port 8988")
createScript = flag.Bool("createstartupscript", false, "Create an /etc/init.d/serial-port-json-server startup script. Available only on Linux.")
// createScript = flag.Bool("createstartupscript", true, "Create an /etc/init.d/serial-port-json-server startup script. Available only on Linux.")
)
type NullWriter int
func (NullWriter) Write([]byte) (int, error) { return 0, nil }
func defaultAssetPath() string {
//p, err := build.Default.Import("gary.burd.info/go-websocket-chat", "", build.FindOnly)
p, err := build.Default.Import("github.com/johnlauer/serial-port-json-server", "", build.FindOnly)
if err != nil {
return "."
}
return p.Dir
}
func homeHandler(c http.ResponseWriter, req *http.Request) {
homeTemplate.Execute(c, req.Host)
}
func launchSelfLater() {
log.Println("Going to launch myself 5 seconds later.")
time.Sleep(2 * 1000 * time.Millisecond)
log.Println("Done waiting 5 secs. Now launching...")
}
func main() {
// Test USB list
// GetUsbList()
// parse all passed in command line arguments
flag.Parse()
// setup logging
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
// see if we are supposed to wait 5 seconds
if *isLaunchSelf {
launchSelfLater()
}
// see if they want to just create startup script
if *createScript {
createStartupScript()
return
}
//getList()
log.Println("Version:" + version)
// hostname
hn, _ := os.Hostname()
if *hostname == "unknown-hostname" {
*hostname = hn
}
log.Println("Hostname:", *hostname)
// turn off garbage collection
// this is dangerous, as u could overflow memory
//if *isGC {
if *gcType == "std" {
log.Println("Garbage collection is on using Standard mode, meaning we just let Golang determine when to garbage collect.")
} else if *gcType == "max" {
log.Println("Garbage collection is on for MAXIMUM real-time collecting on each send/recv from serial port. Higher CPU, but less stopping of the world to garbage collect since it is being done on a constant basis.")
} else {
log.Println("Garbage collection is off. Memory use will grow unbounded. You WILL RUN OUT OF RAM unless you send in the gc command to manually force garbage collection. Lower CPU, but progressive memory footprint.")
debug.SetGCPercent(-1)
}
if *isAllowExec {
log.Println("Enabling exec commands because you passed in -allowexec")
}
ip, err := externalIP()
if err != nil {
log.Println(err)
}
//homeTempl = template.Must(template.ParseFiles(filepath.Join(*assets, "home.html")))
// see if they provided a regex filter
if len(*regExpFilter) > 0 {
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
}
//GetDarwinMeta()
if !*verbose {
log.Println("You can enter verbose mode to see all logging by starting with the -v command line switch.")
// log.SetOutput(new(NullWriter)) //route all logging to nullwriter
}
// list serial ports
portList, _ := GetList()
metaports, _ := GetMetaList()
/*if errSys != nil {
log.Printf("Got system error trying to retrieve serial port list. Err:%v\n", errSys)
log.Fatal("Exiting")
}*/
// serial port list thread
go func() {
time.Sleep(1300 * time.Millisecond)
log.SetOutput(io.Writer(os.Stdout))
log.Println("Your serial ports:")
if len(portList) == 0 {
log.Println("\tThere are no serial ports to list.")
}
for _, element := range portList {
// if we have meta data for this port, use it
setMetaDataForOsSerialPort(&element, metaports)
log.Printf("\t%v\n", element)
}
if !*verbose {
//log.Println("You can enter verbose mode to see all logging by starting with the -v command line switch.")
log.SetOutput(new(NullWriter)) //route all logging to nullwriter
}
}()
// launch the hub routine which is the singleton for the websocket server
go h.run()
// launch our serial port routine
go sh.run()
// launch our dummy data routine
//go d.run()
// Run the UDP & TCP Server that are part of the Cayenn protocol
// This lets us listen for devices announcing they
// are alive on our local network, or are sending data from sensors,
// or acknowledgements to commands we send the device.
// This is used by Cayenn devices such as ESP8266 devices that
// can speak to SPJS and allow SPJS to pass through their data back to
// clients such as ChiliPeppr.
if *isDisableCayenn == false {
log.Println("Attempting to load Cayenn TCP/UDP server on port 8988...")
go udpServerRun()
go tcpServerRun()
} else {
log.Println("Disabled loading of Cayenn TCP/UDP server on port 8988")
}
// Setup GPIO server
// Ignore GPIO for now, but it would be nice to get GPIO going natively
//gpio.PreInit()
// when the app exits, clean up our gpio ports
//defer gpio.CleanupGpio()
http.HandleFunc("/", homeHandler)
http.HandleFunc("/ws", wsHandler)
go startHttp(ip)
go startHttps(ip)
log.Println("The Serial Port JSON Server is now running.")
log.Println("If you are using ChiliPeppr, you may go back to it and connect to this server.")
// turn off logging output unless user wanted verbose mode
// actually, this is now done after the serial port list thread completes
if !*verbose {
// log.SetOutput(new(NullWriter)) //route all logging to nullwriter
}
// wait
ch := make(chan bool)
<-ch
}
func startHttp(ip string) {
f := flag.Lookup("addr")
log.Println("Starting http server and websocket on " + ip + "" + f.Value.String())
if err := http.ListenAndServe(*addr, nil); err != nil {
fmt.Printf("Error trying to bind to http port: %v, so exiting...\n", err)
fmt.Printf("This can sometimes mean you are already running SPJS and accidentally trying to run a second time, thus why the port would be in use. Also, check your permissions/credentials to make sure you can bind to IP address ports.")
log.Fatal("Error ListenAndServe:", err)
}
}
func startHttps(ip string) {
// generate self-signed cert for testing or local trusted networks
// openssl req -x509 -nodes -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365
f := flag.Lookup("saddr")
cert, certErr := os.Open(*scert)
key, keyErr := os.Open(*skey)
cert.Close()
key.Close()
if certErr != nil || keyErr != nil {
log.Println("Missing tls cert and/or key. Will not start HTTPS server.")
//fmt.Println("Missing tls cert and/or key. Will not start HTTPS server.")
return
}
log.Println("Starting https server and websocket on " + ip + "" + f.Value.String())
if err := http.ListenAndServeTLS(*saddr, *scert, *skey, nil); err != nil {
fmt.Printf("Error trying to bind to https port: %v, so exiting...\n", err)
log.Fatal("Error ListenAndServeTLS:", err)
}
}
func externalIP() (string, error) {
//log.Println("Getting external IP")
ifaces, err := net.Interfaces()
if err != nil {
log.Println("Got err getting external IP addr")
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
//log.Println("Iface down")
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
//log.Println("Loopback")
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
log.Println("Got err on iface.Addrs()")
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
//log.Println("Ip was nil or loopback")
continue
}
ip = ip.To4()
if ip == nil {
//log.Println("Was not ipv4 addr")
continue // not an ipv4 address
}
//log.Println("IP is ", ip.String())
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}
var homeTemplate = template.Must(template.New("home").Parse(homeTemplateHtml))
// If you navigate to this server's homepage, you'll get this HTML
// so you can directly interact with the serial port server
const homeTemplateHtml = `<!DOCTYPE html>
<html>
<head>
<title>Serial Port Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var conn;
var msg = $("#msg");
var log = $("#log");
function appendLog(msg) {
var d = log[0]
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
msg.appendTo(log)
if (doScroll) {
d.scrollTop = d.scrollHeight - d.clientHeight;
}
}
$("#form").submit(function() {
if (!conn) {
return false;
}
if (!msg.val()) {
return false;
}
conn.send(msg.val() + "\n");
msg.val("");
return false
});
if (window["WebSocket"]) {
conn = new WebSocket("ws://{{$}}/ws");
conn.onclose = function(evt) {
appendLog($("<div><b>Connection closed.</b></div>"))
}
conn.onmessage = function(evt) {
appendLog($("<div/>").text(evt.data))
}
} else {
appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
}
});
</script>
<style type="text/css">
html {
overflow: hidden;
}
body {
overflow: hidden;
padding: 0;
margin: 0;
width: 100%;
height: 100%;
background: gray;
}
#log {
background: white;
margin: 0;
padding: 0.5em 0.5em 0.5em 0.5em;
position: absolute;
top: 0.5em;
left: 0.5em;
right: 0.5em;
bottom: 3em;
overflow: auto;
}
#form {
padding: 0 0.5em 0 0.5em;
margin: 0;
position: absolute;
bottom: 1em;
left: 0px;
width: 100%;
overflow: hidden;
}
</style>
</head>
<body>
<div id="log"></div>
<form id="form">
<input type="submit" value="Send" />
<input type="text" id="msg" size="64"/>
</form>
</body>
</html>
`