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 console
.log('server handling request')
29 const serveFile
= (fPath
) => {
30 fs
.readFile(fPath
, { encoding
: 'utf8' }, (err
, data
) => {
31 if (err
|| data
== undefined) {
32 response
.writeHead(404)
36 response
.writeHead(200, { 'Content-Type': mime
.lookup(fPath
) })
42 const htArgv
= request
.url
.slice(1).split("?")
43 let routePath
= htArgv
[0].split('/')
44 let routeName
= routePath
[0]
45 if (routeName
=== '' || routeName
=== 'index.html')
46 serveFile(opts
['index'])
47 else if (routeName
in opts
['bindings']) {
48 let localPath
= path
.normalize(opts
['bindings'][routeName
].concat(path
.sep
+ routePath
.slice(1).join(path
.sep
)))
49 if (localPath
.includes(opts
['bindings'][routeName
])) {
50 fs
.readdir(localPath
, (err
, files
) => {
54 serveFile(`${localPath}/index.html`)
58 console
.log(`SEC: ${localPath} references files not in route`)
61 /* TODO: Handle reconnecting host */
62 else if (routeName
in router
.routes
) {
64 const route
= router
.routes
[routeName
]
66 /* Client is INIT GET */
67 if (request
.headers
['x-strapp-type'] == undefined) {
68 console
.log('client init GET')
69 response
.writeHead(200, { 'Content-Type': 'text/html' })
70 response
.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`)
72 //TODO: if route.socket == undefined: have server delay this send until host connects
73 // (this happens when a client connects to an active route with no currently-online host)
75 else { /* Client sent offer, waiting for answer */
76 console
.log('Server: Sending client offer to host')
77 route
.socket
.send(request
.headers
['x-strapp-type'])
78 route
.socket
.on('message', (hostResponse
) => {
79 console
.log('Server: Sending host answer to client')
80 console
.log(hostResponse
)
81 response
.writeHead(200, { 'Content-Type': 'application/json' })
82 response
.write(hostResponse
)
90 router
.routes
[routeName
] = true
92 newRoute
.host
= request
.headers
['x-forwarded-for'] || request
.connection
.remoteAddress
93 getport().then( (port
) => {
96 newRoute
.httpd
= http
.createServer()
98 newRoute
.httpd
= https
.createServer(router
.httpsOpts
)
99 newRoute
.httpd
.listen(newRoute
.port
)
100 newRoute
.wsd
= new ws
.Server( { server
: newRoute
.httpd
} )
101 newRoute
.wsd
.on('connection', (sock
) => {
102 newRoute
.socket
= sock
103 sock
.on('message', (msg
) => { console
.log(`[${newRoute.host}] ${msg}`) })
105 console
.log(`Listening for websocket ${newRoute.host} on port ${newRoute.port}`)
106 router
.routes
[routeName
] = newRoute
108 response
.writeHead(200, { 'Content-Type': 'text/html' })
109 response
.write(`${router.skelPage[0]}` +
110 `\tconst _strapp_port = ${newRoute.port}\n` +
111 `\tconst _strapp_protocol = '${router.wsProtocol}'\n` +
112 `${router.hostJS}\n${router.skelPage[1]}`)
121 * @summary Boot up the router. With TLS, we must wait for file reads to sync.
123 if (!opts
['no-tls']) {
128 const syncRead
= () => {
129 if (++filesRead
== 2) {
130 if (key
== undefined)
131 console
.log(`ERR: Key ${opts['ca-key']} inaccessible, tls will fail`)
132 if(cert
== undefined)
133 console
.log(`ERR: Cert ${opts['ca-cert']} inaccessible, tls will fail`)
134 else if (key
!= undefined) {
135 router
.httpsOpts
= { cert
: cert
, key
: key
}
136 router
.httpd
= https
.createServer(router
.httpsOpts
, router
.respond
)
137 .listen(opts
['port'])
141 fs
.readFile(opts
['ca-key'], { encoding
: 'utf8' }, (err
, data
) => {
145 fs
.readFile(opts
['ca-cert'], { encoding
: 'utf8' }, (err
, data
) => {
146 if (!err
) cert
= data
151 router
.httpd
= http
.createServer(router
.respond
).listen(opts
['port'])
153 //TODO: if ("electron" in process.versions) open a local renderwindow, and route to it