2 * @file Node entry and main driver
3 * @author Jordan Lavatai, Ken Grimes
6 * @copyright loljk 2017
7 * @summary HTTP(S) Router that uses the first directory in the requested URL
10 const fs
= require('fs')
11 const ws
= require('ws')
12 const path
= require('path')
13 const http
= require('http')
14 const https
= require('https')
15 const getport
= require('get-port')
16 const mime
= require('mime')
17 const opts
= require('./opts.js')
20 skelPage
: fs
.readFileSync('./skel.html', { encoding
: 'utf8' }).split('<!--STRAPP_SRC-->'),
21 clientJS
: fs
.readFileSync(opts
['client-js']),
22 hostJS
: fs
.readFileSync(opts
['host-js']),
26 wsProtocol
: opts
['no-tls'] ? 'ws' : 'wss',
27 respond
: (request
,response
) => {
28 const serveFile
= (fPath
) => {
29 fs
.readFile(fPath
, { encoding
: 'utf8' }, (err
, data
) => {
30 if (err
|| data
== undefined) {
31 response
.writeHead(404)
35 response
.writeHead(200, { 'Content-Type': mime
.lookup(fPath
) })
41 const htArgv
= request
.url
.slice(1).split("?")
42 let routePath
= htArgv
[0].split('/')
43 let routeName
= routePath
[0]
44 if (routeName
=== '' || routeName
=== 'index.html')
45 serveFile(opts
['index'])
46 else if (routeName
in opts
['bindings']) {
47 let localPath
= path
.normalize(opts
['bindings'][routeName
].concat(path
.sep
+ routePath
.slice(1).join(path
.sep
)))
48 if (localPath
.includes(opts
['bindings'][routeName
])) {
49 fs
.readdir(localPath
, (err
, files
) => {
53 serveFile(`${localPath}/index.html`)
57 console
.log(`SEC: ${localPath} references files not in route`)
60 else if (routeName
in router
.routes
) {
61 const route
= router
.routes
[routeName
]
62 response
.writeHead(200, { 'Content-Type': 'text/html' })
63 response
.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`)
65 //TODO: if route.socket == undefined: have server delay this send until host connects
66 // (this happens when a client connects to an active route with no currently-online host)
67 route
.socket
.send(request
.headers
['x-forwarded-for'] || request
.connection
.remoteAddress
)
70 router
.routes
[routeName
] = true
72 newRoute
.host
= request
.headers
['x-forwarded-for'] || request
.connection
.remoteAddress
73 getport().then( (port
) => {
76 newRoute
.httpd
= http
.createServer()
78 newRoute
.httpd
= https
.createServer(router
.httpsOpts
)
79 newRoute
.httpd
.listen(newRoute
.port
)
80 newRoute
.wsd
= new ws
.Server( { server
: newRoute
.httpd
} )
81 newRoute
.wsd
.on('connection', (sock
) => {
82 newRoute
.socket
= sock
83 sock
.on('message', (msg
) => { console
.log(`[${newRoute.host}] ${msg}`) })
85 console
.log(`Listening for websocket ${newRoute.host} on port ${newRoute.port}`)
86 router
.routes
[routeName
] = newRoute
88 response
.writeHead(200, { 'Content-Type': 'text/html' })
89 response
.write(`${router.skelPage[0]}` +
90 `\tconst _strapp_port = ${newRoute.port}\n` +
91 `\tconst _strapp_protocol = '${router.wsProtocol}'\n` +
92 `${router.hostJS}\n${router.skelPage[1]}`)
101 * @summary Boot up the router. With TLS, we must wait for file reads to sync.
103 if (!opts
['no-tls']) {
107 const syncRead
= () => {
108 if (++filesRead
== 2) {
109 if (key
== undefined)
110 console
.log(`ERR: Key ${opts['ca-key']} inaccessible, tls will fail`)
111 if(cert
== undefined)
112 console
.log(`ERR: Cert ${opts['ca-cert']} inaccessible, tls will fail`)
113 else if (key
!= undefined) {
114 router
.httpsOpts
= { cert
: cert
, key
: key
}
115 router
.httpd
= https
.createServer(this.httpsOpts
, router
.respond
)
116 .listen(opts
['port'])
120 fs
.readFile(opts
['ca-key'], { encoding
: 'utf8' }, (err
, data
) => {
124 fs
.readFile(opts
['ca-cert'], { encoding
: 'utf8' }, (err
, data
) => {
125 if (!err
) cert
= data
130 router
.httpd
= http
.createServer(router
.respond
).listen(opts
['port'])
132 //TODO: if ("electron" in process.versions) open a local renderwindow, and route to it