2019-01-05 15:16:13 -08:00
|
|
|
import { Socket as uSocket, sSocket} from '../src'
|
|
|
|
import { fs } from 'mz'
|
2018-01-08 13:06:01 -08:00
|
|
|
|
2019-01-05 15:16:13 -08:00
|
|
|
// made key cert into module that also uses environment variables
|
|
|
|
const TLS = process.env.TLS || false
|
|
|
|
const TLS_DIR = process.env.TLS_DIR || '/opt/certs'
|
|
|
|
const TLS_NAME = process.env.TLD_NAME || 'wc.kebler.net'
|
|
|
|
const TLS_KEY_PATH = process.env.TLS_KEY_PATH || `${TLS_DIR}/${TLS_NAME}.key`
|
|
|
|
const TLS_CRT_PATH = process.env.TLS_CRT_PATH || `${TLS_DIR}/${TLS_NAME}.crt`
|
|
|
|
|
|
|
|
let Socket = uSocket
|
|
|
|
|
|
|
|
;
|
2018-01-08 13:06:01 -08:00
|
|
|
(async () => {
|
2019-01-05 15:16:13 -08:00
|
|
|
// TODO dynamic import
|
|
|
|
if(TLS_KEY_PATH && TLS_CRT_PATH && TLS) {
|
|
|
|
Socket = sSocket
|
|
|
|
console.log('using TLS')
|
|
|
|
}
|
2018-01-08 13:06:01 -08:00
|
|
|
|
2018-01-20 14:30:21 -08:00
|
|
|
class Test extends Socket {
|
2018-02-03 13:33:25 -08:00
|
|
|
constructor(opts) {
|
|
|
|
super(opts)
|
2018-01-08 13:06:01 -08:00
|
|
|
}
|
|
|
|
|
2018-02-03 13:33:25 -08:00
|
|
|
async _packetProcess(packet) {
|
2018-02-13 13:51:58 -08:00
|
|
|
console.log('packet being processed at socket')
|
2018-01-13 20:46:14 -08:00
|
|
|
if (packet.cmd) return await this[packet.cmd](packet.data,packet.name)
|
|
|
|
return {error: 'no command in packet', packet: packet }
|
2018-01-08 13:06:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async doit(data,name) {
|
2018-02-12 14:41:06 -08:00
|
|
|
return new Promise(resolve => {
|
|
|
|
let res = {}
|
2018-02-13 13:51:58 -08:00
|
|
|
console.log('data sent to doit = ', data)
|
2018-02-12 14:41:06 -08:00
|
|
|
res.status ='success'
|
|
|
|
res.name = name
|
|
|
|
res.cmd = 'reply'
|
|
|
|
res.data = 'this would be response data from socket doit function'
|
|
|
|
resolve(res)
|
|
|
|
})
|
|
|
|
|
2018-01-08 13:06:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-05 15:16:13 -08:00
|
|
|
const options = {
|
|
|
|
tls: TLS,
|
|
|
|
key: await fs.readFile(TLS_KEY_PATH),
|
|
|
|
cert: await fs.readFile(TLS_CRT_PATH),
|
|
|
|
// This is necessary only if using client certificate authentication.
|
|
|
|
// requestCert: true,
|
|
|
|
// This is necessary only if the client uses a self-signed certificate.
|
|
|
|
// ca: [ fs.readFileSync('client-cert.pem') ]
|
|
|
|
}
|
|
|
|
|
|
|
|
options.path = true
|
|
|
|
|
|
|
|
|
2018-03-02 08:34:10 -08:00
|
|
|
// let test = new Test()
|
2019-01-05 15:16:13 -08:00
|
|
|
let test = new Test(options)
|
2018-01-20 14:30:21 -08:00
|
|
|
await test.create()
|
2018-01-08 13:06:01 -08:00
|
|
|
|
|
|
|
})().catch(err => {
|
|
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
|
|
|
})
|