2019-06-01 14:02:49 +00:00
'use strict'
import consul from 'consul'
import { exec } from './src/io/run.mjs'
import { readFile } from './src/io/files.mjs'
2019-12-04 17:04:30 +00:00
import ctlg _control _loop from './src/catalog/control_loop.mjs'
2019-06-01 14:02:49 +00:00
import ctlg _consul from './src/catalog/consul.mjs'
import inj _iptables from './src/injector/iptables.mjs'
const get _args = ( ) => process
. argv
. slice ( 2 )
. map ( a => a . split ( '=' ) )
. reduce ( ( dict , tuple ) => {
dict [ tuple [ 0 ] ] = tuple . length > 1 ? tuple [ 1 ] : null
return dict
} , { } )
/ * *
* If we have multiple catalogs
* we cache the results of the other ones
* /
function * notifications _aggregator ( injectors ) {
const states = [ ]
for ( let idx = 0 ; true ; idx ++ ) {
yield async ( tag _list ) => {
states [ idx ] = tag _list
2019-12-04 17:04:30 +00:00
const merged = states . reduce ( ( acc , tag ) => [ ... acc , ... tag ] , [ ] )
await Promise . all ( injectors . map ( notify => notify ( merged ) ) )
2019-06-01 14:02:49 +00:00
}
}
}
const main = async ( ) => {
try {
const args = get _args ( )
// Initialize all injectors
const injectors = [
2019-12-04 17:04:30 +00:00
await inj _iptables ( args . ipt _base , readFile , exec , console . log ) ,
// await inj_upnp
2019-06-01 14:02:49 +00:00
]
// Initialize all catalogs and map them to the injectors
const aggr = notifications _aggregator ( injectors )
const catalogs = [
2019-12-04 17:04:30 +00:00
// this catalog is used to defeat deriving config due to single resource updated async. by multiple prog or by external program not tracked by catalogs
await ctlg _control _loop ( setInterval , 60000 , aggr . next ( ) . value ) ,
2019-06-01 14:02:49 +00:00
await ctlg _consul ( args . node , consul ( ) , console . log , aggr . next ( ) . value )
]
console . log ( "[main] initialized" )
} catch ( e ) {
console . error ( "initialization failed" , e )
process . exit ( 1 )
}
}
main ( )