-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
131 lines (126 loc) · 4.34 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const Bunyan = require('bunyan');
const bunyanFormat = require('bunyan-format');
const fs = require('fs');
const ini = require('ini');
const URL = require('url').URL;
const LogNothing = Bunyan.createLogger({
name: 'BBS',
level: Bunyan.FATAL + 100,
});
function groomTNC(config, section, defaultPort) {
const options = config[section];
if (options) {
if (!options.logger) {
if (config.log) {
var newLog = Object.assign({}, config.log);
newLog.name = section;
delete newLog.outputMode;
options.logger = Bunyan.createLogger(newLog);
} else {
options.logger = LogNothing;
}
}
options.port = parseInt(options.port || (defaultPort + ''));
if (options.localAddress) {
options.localAddress = options.localAddress.trim().split(/[\s,]+/);
} else {
delete options.localAddress;
}
if (options.localPort) {
options.localPort = options.localPort.trim().split(/[\s,]+/);
} else {
delete options.localPort;
}
}
}
function groomVARA(config, section, defaultPort) {
groomTNC(config, section, defaultPort);
const options = config[section];
if (options && options.dataPort) {
options.dataPort = parseInt(`${options.dataPort}`);
}
}
function groom(config) {
if (!config.logger) {
if (!config.log) {
config.logger = LogNothing;
} else {
if (!config.log.stream && config.log.outputMode) {
config.log.stream = bunyanFormat({
outputMode: config.log.outputMode,
color: false,
});
}
if (!config.log.name) {
config.log.name = 'BBS';
}
if (config.log.level) {
config.log.level = Bunyan[config.log.level];
}
config.logger = Bunyan.createLogger(Object.assign(
{}, config.log, {outputMode: undefined}));
}
}
groomTNC(config, 'AGWPE', 8000);
groomVARA(config, 'VARA HF', 8300);
groomVARA(config, 'VARA FM', 8300);
if (config.LDAP) {
if (!config.LDAP.userIdAttribute) {
config.LDAP.userIdAttribute = 'uid';
}
if (!config.LDAP.passwordAttribute) {
config.LDAP.passwordAttribute = 'userPassword';
}
if (config.LDAP.URL) {
config.LDAP.URL = config.LDAP.URL.split(/\s+/);
var url = new URL(config.LDAP.URL[0]);
if (!config.LDAP.host && url.hostname) {
config.LDAP.host = url.hostname;
}
if (config.LDAP.port) {
config.LDAP.port = parseInt(config.LDAP.port);
} else if (url.port) {
config.LDAP.port = ((typeof url.port) == 'string') ? parseInt(url.port) : url.port;
} else if (url.protocol && url.protocol.toLowerCase() == 'ldaps') {
config.LDAP.port = 636;
} else {
config.LDAP.port = 389;
}
if (!config.LDAP.bindDN && url.username != null) {
config.LDAP.bindDN = url.username;
}
if (!config.LDAP.password && url.password != null) {
config.LDAP.password = url.password;
}
if (!config.LDAP.baseDN && url.pathname && url.pathname.length > 1) {
config.LDAP.baseDN = url.pathname.substring(1);
} else {
config.LDAP.baseDN = ''; // search from the root
}
} else {
var url = 'ldap://';
if (config.LDAP.host) {
url += config.LDAP.host;
}
if (config.LDAP.port) {
config.LDAP.port = parseInt(config.LDAP.port);
url += `:${config.LDAP.port}`;
}
if (config.LDAP.baseDN) {
url += `/${config.LDAP.baseDN}`;
}
config.LDAP.URL = url;
}
}
if (config.POP) {
config.POP.port = parseInt(config.POP.port || '110');
}
if (config.SMTP) {
config.SMTP.port = parseInt(config.SMTP.port || '25');
}
}
exports.readFile = function(fileName) {
var config = ini.parse(fs.readFileSync(fileName, 'utf-8'));
groom(config);
return config;
}