f123a413fd73593d94bbbce8705586395cac3c90
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
) => {
29 request
.on('error', function(err
) {
30 console
.error(`error is ${err}`);
31 }).on('data', function(chunk
) {
32 console
.log(`chunk is ${chunk}`)
34 }).on('end', function() {
35 console
.log(`body is ${body}`)
37 console
.log('server handling request')
38 const serveFile
= (fPath
) => {
39 fs
.readFile(fPath
, { encoding
: 'utf8' }, (err
, data
) => {
40 if (err
|| data
== undefined) {
41 response
.writeHead(404)
45 response
.writeHead(200, { 'Content-Type': mime
.lookup(fPath
) })
51 const htArgv
= request
.url
.slice(1).split("?")
52 let routePath
= htArgv
[0].split('/')
53 let routeName
= routePath
[0]
54 if (routeName
=== '' || routeName
=== 'index.html')
55 serveFile(opts
['index'])
56 else if (routeName
in opts
['bindings']) {
57 let localPath
= path
.normalize(opts
['bindings'][routeName
].concat(path
.sep
+ routePath
.slice(1).join(path
.sep
)))
58 if (localPath
.includes(opts
['bindings'][routeName
])) {
59 fs
.readdir(localPath
, (err
, files
) => {
63 serveFile(`${localPath}/index.html`)
67 console
.log(`SEC: ${localPath} references files not in route`)
70 /* TODO: Handle reconnecting host */
71 else if (routeName
in router
.routes
) {
73 const route
= router
.routes
[routeName
]
75 /* Client is INIT GET */
76 if (request
.headers
['x-strapp-type'] == undefined) {
77 console
.log('client init GET')
78 response
.writeHead(200, { 'Content-Type': 'text/html' })
79 response
.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`)
81 //TODO: if route.socket == undefined: have server delay this send until host connects
82 // (this happens when a client connects to an active route with no currently-online host)
84 else { /* Client sent offer, waiting for answer */
85 console
.log(JSON
.parse(request
.headers
['x-strapp-type']))
86 route
.socket
.on('message', (hostResponse
) => {
87 console
.log(hostResponse
)
93 router
.routes
[routeName
] = true
95 newRoute
.host
= request
.headers
['x-forwarded-for'] || request
.connection
.remoteAddress
96 getport().then( (port
) => {
99 newRoute
.httpd
= http
.createServer()
101 newRoute
.httpd
= https
.createServer(router
.httpsOpts
)
102 newRoute
.httpd
.listen(newRoute
.port
)
103 newRoute
.wsd
= new ws
.Server( { server
: newRoute
.httpd
} )
104 newRoute
.wsd
.on('connection', (sock
) => {
105 newRoute
.socket
= sock
106 sock
.on('message', (msg
) => { console
.log(`[${newRoute.host}] ${msg}`) })
108 console
.log(`Listening for websocket ${newRoute.host} on port ${newRoute.port}`)
109 router
.routes
[routeName
] = newRoute
111 response
.writeHead(200, { 'Content-Type': 'text/html' })
112 response
.write(`${router.skelPage[0]}` +
113 `\tconst _strapp_port = ${newRoute.port}\n` +
114 `\tconst _strapp_protocol = '${router.wsProtocol}'\n` +
115 `${router.hostJS}\n${router.skelPage[1]}`)
124 * @summary Boot up the router. With TLS, we must wait for file reads to sync.
126 if (!opts
['no-tls']) {
131 const syncRead
= () => {
132 if (++filesRead
== 2) {
133 if (key
== undefined)
134 console
.log(`ERR: Key ${opts['ca-key']} inaccessible, tls will fail`)
135 if(cert
== undefined)
136 console
.log(`ERR: Cert ${opts['ca-cert']} inaccessible, tls will fail`)
137 else if (key
!= undefined) {
138 router
.httpsOpts
= { cert
: cert
, key
: key
}
139 router
.httpd
= https
.createServer(router
.httpsOpts
, router
.respond
)
140 .listen(opts
['port'])
144 fs
.readFile(opts
['ca-key'], { encoding
: 'utf8' }, (err
, data
) => {
148 fs
.readFile(opts
['ca-cert'], { encoding
: 'utf8' }, (err
, data
) => {
149 if (!err
) cert
= data
154 router
.httpd
= http
.createServer(router
.respond
).listen(opts
['port'])
156 //TODO: if ("electron" in process.versions) open a local renderwindow, and route to it