1 | package com.bowman.cardserv;
|
---|
2 |
|
---|
3 | import com.bowman.cardserv.cws.*;
|
---|
4 | import com.bowman.cardserv.interfaces.*;
|
---|
5 | import com.bowman.cardserv.rmi.RemoteHandler;
|
---|
6 | import com.bowman.cardserv.tv.TvService;
|
---|
7 | import com.bowman.cardserv.util.*;
|
---|
8 | import com.bowman.cardserv.crypto.DESUtil;
|
---|
9 | import com.bowman.util.*;
|
---|
10 | import com.bowman.xml.*;
|
---|
11 |
|
---|
12 | import java.io.*;
|
---|
13 | import java.text.*;
|
---|
14 | import java.util.*;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Created by IntelliJ IDEA.
|
---|
18 | * ser: bowman
|
---|
19 | * Date: Oct 8, 2005
|
---|
20 | * Time: 4:04:22 PM
|
---|
21 | */
|
---|
22 | public class ProxyConfig implements FileChangeListener {
|
---|
23 |
|
---|
24 | public static final String DEFAULT_CONFIG = "config/proxy.xml";
|
---|
25 | public static final long DEFAULT_INTERVAL = 3000;
|
---|
26 |
|
---|
27 | private static ProxyConfig instance = null;
|
---|
28 |
|
---|
29 | public static ProxyConfig getInstance() {
|
---|
30 | if(instance == null) instance = new ProxyConfig();
|
---|
31 | return instance;
|
---|
32 | }
|
---|
33 |
|
---|
34 | private XmlConfigurable rootConfigurable;
|
---|
35 | private File cfgFile;
|
---|
36 | private FileWatchdog fw;
|
---|
37 |
|
---|
38 | private int proxyOriginId = (int)System.currentTimeMillis(); // use lower 4 bytes of sys clock as id for this run
|
---|
39 | private int logRotateCount, logRotateLimit;
|
---|
40 | private String logFile, logLevel, wtBadFlags, msgtoemu, clientMsg;
|
---|
41 | private int wtMaxDelay, etMinCount, maxThreads, sessionTimeout, newcamdMaxMsgSize, maxPending, maxConnectionsIP;
|
---|
42 | private boolean silent, debug, userAllowOnFailure, logFailures, logEcm, logEmm, logZap, hideIPs, blockCaidMismatch, osdMsg;
|
---|
43 | private boolean wtIncludeFile, userAllowDifferentIp;
|
---|
44 |
|
---|
45 | private boolean firstRead = true, started = false;
|
---|
46 | private byte[] defaultProfileDesKey, defaultConnectorDesKey, defaultClientId;
|
---|
47 | private int defaultConnectorKeepAlive, defaultConnectorMinDelay, defaultConnectorMaxQueue, sessionKeepAlive;
|
---|
48 | private Set sessionKeepAliveClients = Collections.EMPTY_SET, defaultDenyList = new HashSet();
|
---|
49 |
|
---|
50 | private CacheHandler cacheHandler;
|
---|
51 | private UserManager userManager;
|
---|
52 | private CwsConnectorManager connManager;
|
---|
53 | private RemoteHandler remoteHandler;
|
---|
54 | private ProxyLogger logger;
|
---|
55 | private CamdMessageListener defaultListener;
|
---|
56 |
|
---|
57 | private Map proxyPlugins = new HashMap();
|
---|
58 | private Map pluginLoadData = new HashMap();
|
---|
59 | private Map profiles = new HashMap();
|
---|
60 | private Map profilesById = new HashMap();
|
---|
61 | private Set disabledProfiles = new HashSet();
|
---|
62 |
|
---|
63 | private boolean sidLinkerEnabled;
|
---|
64 | private SidCacheLinker sidLinker;
|
---|
65 |
|
---|
66 | private ProxyConfig() {}
|
---|
67 |
|
---|
68 | public Map getProfiles() {
|
---|
69 | return profiles;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public Set getRealProfiles() {
|
---|
73 | Set real = new HashSet(profiles.values());
|
---|
74 | real.remove(CaProfile.MULTIPLE);
|
---|
75 | return real;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public CaProfile getProfile(String name) {
|
---|
79 | if(name == null) return null;
|
---|
80 | else return (CaProfile)profiles.get(name);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public CaProfile getProfile() {
|
---|
84 | return (CaProfile)profiles.values().iterator().next();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public CaProfile getProfileById(int networkId, int caId) {
|
---|
88 | if(networkId == 0) return null;
|
---|
89 | return (CaProfile)profilesById.get(CaProfile.getKeyStr(networkId, caId));
|
---|
90 | }
|
---|
91 |
|
---|
92 | public String getProfileNameById(int networkId, int caId) {
|
---|
93 | CaProfile profile = getProfileById(networkId, caId);
|
---|
94 | if(profile == null) return null;
|
---|
95 | else return profile.getName();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public boolean isLogEcm() {
|
---|
99 | return logEcm;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public boolean isLogEmm() {
|
---|
103 | return logEmm;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public boolean isLogZap() {
|
---|
107 | return logZap;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public String getLogFile() {
|
---|
111 | return logFile;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public int getProxyOriginId() {
|
---|
115 | return proxyOriginId;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public int getLogRotateCount() {
|
---|
119 | return logRotateCount;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public int getLogRotateLimit() {
|
---|
123 | return logRotateLimit;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public String getLogLevel() {
|
---|
127 | return logLevel;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public boolean isSilent() {
|
---|
131 | return silent;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public boolean isDebug() {
|
---|
135 | return debug;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public boolean isIncludeFileEvents() {
|
---|
139 | return wtIncludeFile;
|
---|
140 | }
|
---|
141 |
|
---|
142 | public boolean isHideIPs() {
|
---|
143 | return hideIPs;
|
---|
144 | }
|
---|
145 |
|
---|
146 | public boolean isUserAllowOnFailure() {
|
---|
147 | return userAllowOnFailure;
|
---|
148 | }
|
---|
149 |
|
---|
150 | public boolean isUserAllowDifferentIp() {
|
---|
151 | return userAllowDifferentIp;
|
---|
152 | }
|
---|
153 |
|
---|
154 | public boolean isLogFailures() {
|
---|
155 | return logFailures;
|
---|
156 | }
|
---|
157 |
|
---|
158 | public boolean isStarted() {
|
---|
159 | return started;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public boolean isBlockCaidMismatch() {
|
---|
163 | return blockCaidMismatch;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public byte[] getDefaultProfileDesKey() {
|
---|
167 | return defaultProfileDesKey;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public Set getDefaultDenyList() {
|
---|
171 | return defaultDenyList;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public byte[] getDefaultConnectorDesKey() {
|
---|
175 | return defaultConnectorDesKey;
|
---|
176 | }
|
---|
177 |
|
---|
178 | public byte[] getDefaultClientId() {
|
---|
179 | return defaultClientId;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public int getDefaultConnectorKeepAlive() {
|
---|
183 | return defaultConnectorKeepAlive;
|
---|
184 | }
|
---|
185 |
|
---|
186 | public int getSessionKeepAlive() {
|
---|
187 | return sessionKeepAlive;
|
---|
188 | }
|
---|
189 |
|
---|
190 | public Set getKeepAliveExcludedClients() {
|
---|
191 | return sessionKeepAliveClients;
|
---|
192 | }
|
---|
193 |
|
---|
194 | public int getDefaultConnectorMinDelay() {
|
---|
195 | return defaultConnectorMinDelay;
|
---|
196 | }
|
---|
197 |
|
---|
198 | public int getDefaultConnectorMaxQueue() {
|
---|
199 | return defaultConnectorMaxQueue;
|
---|
200 | }
|
---|
201 |
|
---|
202 | public int getSessionTimeout() {
|
---|
203 | return sessionTimeout;
|
---|
204 | }
|
---|
205 |
|
---|
206 | public int getMaxThreads() {
|
---|
207 | return maxThreads;
|
---|
208 | }
|
---|
209 |
|
---|
210 | public int getNewcamdMaxMsgSize() {
|
---|
211 | return newcamdMaxMsgSize;
|
---|
212 | }
|
---|
213 |
|
---|
214 | public CwsConnectorManager getConnManager() {
|
---|
215 | return connManager;
|
---|
216 | }
|
---|
217 |
|
---|
218 | public CacheHandler getCacheHandler() {
|
---|
219 | return cacheHandler;
|
---|
220 | }
|
---|
221 |
|
---|
222 | public SidCacheLinker getSidCacheLinker() {
|
---|
223 | if(!sidLinkerEnabled) return null;
|
---|
224 | else return sidLinker;
|
---|
225 | }
|
---|
226 |
|
---|
227 | public UserManager getUserManager() {
|
---|
228 | return userManager;
|
---|
229 | }
|
---|
230 |
|
---|
231 | public RemoteHandler getRemoteHandler() {
|
---|
232 | return remoteHandler;
|
---|
233 | }
|
---|
234 |
|
---|
235 | public void setRemoteHandler(RemoteHandler remoteHandler) {
|
---|
236 | this.remoteHandler = remoteHandler;
|
---|
237 | }
|
---|
238 |
|
---|
239 | public Map getProxyPlugins() {
|
---|
240 | return proxyPlugins;
|
---|
241 | }
|
---|
242 |
|
---|
243 | public int getServiceCount() {
|
---|
244 | int count = 0;
|
---|
245 | Map services;
|
---|
246 | for(Iterator iter = profiles.values().iterator(); iter.hasNext(); ) {
|
---|
247 | services = ((CaProfile)iter.next()).getServices();
|
---|
248 | if(services != null) count += services.size();
|
---|
249 | }
|
---|
250 | return count;
|
---|
251 | }
|
---|
252 |
|
---|
253 | public TvService getService(CamdNetMessage msg) {
|
---|
254 | CaProfile profile;
|
---|
255 | profile = getProfile(msg.getProfileName());
|
---|
256 | if(profile == null) profile = getProfileById(msg.getNetworkId(), msg.getCaId());
|
---|
257 | String profileName = profile==null?"*":profile.getName();
|
---|
258 | return getService(profileName, new ServiceMapping(msg));
|
---|
259 | }
|
---|
260 |
|
---|
261 | public TvService getService(String profileName, ServiceMapping id) {
|
---|
262 | TvService service = getService(profileName, id.serviceId);
|
---|
263 | return new TvService(service, id.getCustomData());
|
---|
264 | }
|
---|
265 |
|
---|
266 | public TvService getService(String profileName, int serviceId) {
|
---|
267 | if(serviceId == 0 || connManager.isServiceUnknown(profileName, serviceId))
|
---|
268 | return TvService.getUnknownService(profileName, serviceId);
|
---|
269 |
|
---|
270 | Integer id = new Integer(serviceId);
|
---|
271 | CaProfile profile = (CaProfile)profiles.get(profileName);
|
---|
272 | TvService service = (TvService)profile.getServices().get(id);
|
---|
273 | if(service == null) return TvService.getUnknownService(profileName, serviceId);
|
---|
274 | else return service;
|
---|
275 | }
|
---|
276 |
|
---|
277 | public String getServiceName(String profileName, ServiceMapping id) {
|
---|
278 | TvService service = getService(profileName, id.serviceId);
|
---|
279 | if(service == null) return id.toString();
|
---|
280 | else return id + "/" + service.getName();
|
---|
281 | }
|
---|
282 |
|
---|
283 | public String getServiceName(CamdNetMessage msg) {
|
---|
284 | TvService service = getService(msg);
|
---|
285 | if(service == null) return new ServiceMapping(msg).toString();
|
---|
286 | else return new ServiceMapping(service) + "/" + service.getName();
|
---|
287 | }
|
---|
288 |
|
---|
289 | public String getServiceName(String profileName, int serviceId) {
|
---|
290 | TvService service = getService(profileName, serviceId);
|
---|
291 | if(service == null) return Integer.toHexString(serviceId);
|
---|
292 | else return Integer.toHexString(serviceId) + "/" + service.getName();
|
---|
293 | }
|
---|
294 |
|
---|
295 | public int getServiceType(String profileName, int serviceId) {
|
---|
296 | TvService service = getService(profileName, serviceId);
|
---|
297 | if(service == null) return -1;
|
---|
298 | else return service.getType();
|
---|
299 | }
|
---|
300 |
|
---|
301 | public boolean isTransactionWarning(String flags, int duration) {
|
---|
302 | boolean warning = false;
|
---|
303 | if(duration > wtMaxDelay) {
|
---|
304 | // dont consider long timeouts towards disconnected clients to be a duration warning
|
---|
305 | if(flags.indexOf('D') > -1) {
|
---|
306 | if(duration < connManager.getMaxCwWait(null)) warning = true;
|
---|
307 | } else warning = true;
|
---|
308 | } else {
|
---|
309 | for(int i = 0; i < flags.length(); i++)
|
---|
310 | if(wtBadFlags.indexOf(flags.charAt(i)) > -1) {
|
---|
311 | warning = true;
|
---|
312 | break;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | return warning;
|
---|
316 | }
|
---|
317 |
|
---|
318 | public synchronized void parseConfig(ProxyXmlConfig currentConfig) throws ConfigException {
|
---|
319 |
|
---|
320 | String version = currentConfig.getStringValue("ver");
|
---|
321 | if(!version.equals(CardServProxy.APP_VERSION))
|
---|
322 | throw new ConfigException("Config file version '" + version + " does not match application version '" +
|
---|
323 | CardServProxy.APP_VERSION + "'.");
|
---|
324 |
|
---|
325 | ProxyXmlConfig logConfig = currentConfig.getSubConfig("logging");
|
---|
326 |
|
---|
327 | logEcm = "true".equalsIgnoreCase(logConfig.getStringValue("log-ecm", "true"));
|
---|
328 | logEmm = "true".equalsIgnoreCase(logConfig.getStringValue("log-emm", "true"));
|
---|
329 | logZap = "true".equalsIgnoreCase(logConfig.getStringValue("log-zapping", "true"));
|
---|
330 |
|
---|
331 | logRotateLimit = 0; logRotateCount = 0;
|
---|
332 | try {
|
---|
333 | ProxyXmlConfig logFileConfig = logConfig.getSubConfig("log-file");
|
---|
334 | logRotateCount = logFileConfig.getIntValue("rotate-count");
|
---|
335 | if(logRotateCount < 1) logRotateCount = 0;
|
---|
336 | logRotateLimit = logFileConfig.getIntValue("rotate-max-size");
|
---|
337 | if(logRotateLimit < 1) logRotateLimit = 0;
|
---|
338 | } catch (ConfigException e) {}
|
---|
339 |
|
---|
340 | logFile = logConfig.getFileValue("log-file", true);
|
---|
341 | if(logRotateCount > 0 && logRotateLimit > 0)
|
---|
342 | if(!new File(logFile).delete()) { /* ignore */ }
|
---|
343 |
|
---|
344 | logLevel = logConfig.getStringValue("log-level");
|
---|
345 | silent = "true".equalsIgnoreCase(logConfig.getStringValue("silent", "false"));
|
---|
346 | debug = "true".equalsIgnoreCase(logConfig.getStringValue("debug", "false"));
|
---|
347 | hideIPs = "true".equalsIgnoreCase(logConfig.getStringValue("hide-ip-addresses", "false"));
|
---|
348 | try {
|
---|
349 | CustomFormatter.setDateFormat(new SimpleDateFormat(logConfig.getStringValue("log-dateformat", "yyMMdd HH:mm:ss.SSS")));
|
---|
350 | } catch (IllegalArgumentException e) {
|
---|
351 | throw new ConfigException(logConfig.getFullName(), "Illegal date format: " + e.getMessage());
|
---|
352 | }
|
---|
353 |
|
---|
354 | try {
|
---|
355 | ProxyXmlConfig wtConfig = logConfig.getSubConfig("warning-threshold");
|
---|
356 | wtBadFlags = wtConfig.getStringValue("bad-flags");
|
---|
357 | wtMaxDelay = wtConfig.getTimeValue("max-delay", "ms");
|
---|
358 | wtIncludeFile = "true".equalsIgnoreCase(wtConfig.getStringValue("include-file-events", "true"));
|
---|
359 | } catch (ConfigException e) {
|
---|
360 | wtBadFlags = "YMNTSAOQGXWDHU-";
|
---|
361 | wtMaxDelay = 5000;
|
---|
362 | wtIncludeFile = true;
|
---|
363 | }
|
---|
364 |
|
---|
365 | try {
|
---|
366 | ProxyXmlConfig etConfig = logConfig.getSubConfig("event-threshold");
|
---|
367 | etMinCount = etConfig.getIntValue("min-count", 1);
|
---|
368 | } catch (ConfigException e) {
|
---|
369 | etMinCount = 1;
|
---|
370 | }
|
---|
371 |
|
---|
372 | ProxyXmlConfig umConfig = currentConfig.getSubConfig("user-manager");
|
---|
373 | userAllowOnFailure = "true".equalsIgnoreCase(umConfig.getStringValue("allow-on-failure", "false"));
|
---|
374 | userAllowDifferentIp = "true".equalsIgnoreCase(umConfig.getStringValue("allow-different-ip", "false"));
|
---|
375 | logFailures = "true".equalsIgnoreCase(umConfig.getStringValue("log-failures", "true"));
|
---|
376 |
|
---|
377 | ProxyXmlConfig profileConf = currentConfig.getSubConfig("ca-profiles");
|
---|
378 | try {
|
---|
379 | defaultProfileDesKey = profileConf.getBytesValue("default-des-key");
|
---|
380 | } catch (ConfigException e) {}
|
---|
381 | defaultDenyList.clear();
|
---|
382 | try {
|
---|
383 | String denyList = profileConf.getStringValue("default-deny-list");
|
---|
384 | defaultDenyList.addAll(Arrays.asList(denyList.split(" ")));
|
---|
385 | } catch (ConfigException e) {}
|
---|
386 |
|
---|
387 | maxThreads = profileConf.getIntValue("max-threads", 1000);
|
---|
388 | maxPending = profileConf.getIntValue("max-pending", 3);
|
---|
389 | sessionTimeout = profileConf.getTimeValue("session-timeout", 240, "m");
|
---|
390 | newcamdMaxMsgSize = profileConf.getIntValue("newcamd-maxmsgsize", 400);
|
---|
391 | blockCaidMismatch = "true".equalsIgnoreCase(profileConf.getStringValue("block-caid-mismatch", "true"));
|
---|
392 | maxConnectionsIP = profileConf.getIntValue("max-connections-ip", 0);
|
---|
393 | osdMsg = "true".equalsIgnoreCase(profileConf.getStringValue("msg-enabled", "false"));
|
---|
394 | clientMsg = profileConf.getStringValue("msg-words", " Your Account will Be Expire In ");
|
---|
395 | msgtoemu = profileConf.getStringValue("msg-emu", "");
|
---|
396 |
|
---|
397 | ProxyXmlConfig keepAliveConf = null;
|
---|
398 | try {
|
---|
399 | keepAliveConf = profileConf.getSubConfig("session-keepalive");
|
---|
400 | } catch (ConfigException e) {}
|
---|
401 | if(keepAliveConf != null) {
|
---|
402 | sessionKeepAlive = profileConf.getTimeValue("session-keepalive", 0, "m");
|
---|
403 | String skacStr = keepAliveConf.getStringValue("exclude-clients", "").toLowerCase();
|
---|
404 | if(skacStr.length() > 0) sessionKeepAliveClients = new HashSet(Arrays.asList(skacStr.split(" ")));
|
---|
405 | else sessionKeepAliveClients = Collections.EMPTY_SET;
|
---|
406 | } else {
|
---|
407 | sessionKeepAliveClients = Collections.EMPTY_SET;
|
---|
408 | sessionKeepAlive = 0;
|
---|
409 | }
|
---|
410 |
|
---|
411 | ProxyXmlConfig connManConf = currentConfig.getSubConfig("connection-manager");
|
---|
412 | try {
|
---|
413 | defaultConnectorDesKey = connManConf.getBytesValue("default-des-key");
|
---|
414 | } catch (ConfigException e) {}
|
---|
415 |
|
---|
416 | try {
|
---|
417 | defaultClientId = connManConf.getBytesValue("default-client-id");
|
---|
418 | } catch (ConfigException e) {
|
---|
419 | defaultClientId = new byte[] {0x00, 0x00};
|
---|
420 | }
|
---|
421 |
|
---|
422 | defaultConnectorKeepAlive = connManConf.getTimeValue("default-keepalive-interval", 0, "s");
|
---|
423 | defaultConnectorMinDelay = connManConf.getTimeValue("default-min-delay", 20, "ms");
|
---|
424 | defaultConnectorMaxQueue = connManConf.getIntValue("default-max-queue", 50);
|
---|
425 |
|
---|
426 | ProxyXmlConfig cacheConf = currentConfig.getSubConfig("cache-handler");
|
---|
427 | sidLinkerEnabled = "true".equalsIgnoreCase(cacheConf.getStringValue("enable-service-linking", "false"));
|
---|
428 |
|
---|
429 | if(logger == null) logger = ProxyLogger.getLabeledLogger(getClass().getName());
|
---|
430 | loadProfiles(currentConfig);
|
---|
431 | loadPlugins(currentConfig);
|
---|
432 |
|
---|
433 | if(rootConfigurable != null) rootConfigurable.configUpdated(currentConfig);
|
---|
434 |
|
---|
435 | if(!firstRead) {
|
---|
436 | updateModules(currentConfig);
|
---|
437 | loadCacheLinker();
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | public synchronized void readConfig(XmlConfigurable root, File cfgFile) throws FileNotFoundException, ConfigException {
|
---|
442 | if(cfgFile == null) {
|
---|
443 | this.cfgFile = new File(DEFAULT_CONFIG);
|
---|
444 |
|
---|
445 | if(!this.cfgFile.exists()) {
|
---|
446 | System.err.println("Config file '" + this.cfgFile.getPath() + "' not found, generating template...");
|
---|
447 | try {
|
---|
448 | BufferedReader br = new BufferedReader(new InputStreamReader(CardServProxy.class.getResourceAsStream("proxy.xml"), "UTF-8"));
|
---|
449 | String line; StringBuffer sb = new StringBuffer();
|
---|
450 | while((line = br.readLine()) != null) sb.append(line).append('\n');
|
---|
451 | String xml = sb.toString();
|
---|
452 | xml = MessageFormat.format(xml, new Object[] {CardServProxy.APP_VERSION});
|
---|
453 | saveCfgFile(xml.getBytes("UTF-8"));
|
---|
454 | System.err.println("Default su credentials: admin/secret (statusweb @ port 8082)");
|
---|
455 | } catch (IOException e) {
|
---|
456 | throw new ConfigException("Failed to generate default config: " + e, e);
|
---|
457 | }
|
---|
458 | }
|
---|
459 | } else this.cfgFile = cfgFile;
|
---|
460 |
|
---|
461 | this.rootConfigurable = root;
|
---|
462 |
|
---|
463 | if(fw == null) {
|
---|
464 | fw = new FileWatchdog(this.cfgFile.getPath(), DEFAULT_INTERVAL);
|
---|
465 | fw.addFileChangeListener(this);
|
---|
466 | fw.start();
|
---|
467 | }
|
---|
468 |
|
---|
469 | ProxyXmlConfig currentConfig;
|
---|
470 | try {
|
---|
471 | currentConfig = new ProxyXmlConfig(new XMLConfig(new FileInputStream(this.cfgFile), false, "UTF-8"));
|
---|
472 | } catch(XMLConfigException e) {
|
---|
473 | throw new ConfigException("Unable to parse " + this.cfgFile + ": " + e.getMessage(), e);
|
---|
474 | }
|
---|
475 |
|
---|
476 | parseConfig(currentConfig);
|
---|
477 |
|
---|
478 | if(firstRead) {
|
---|
479 | firstRead = false;
|
---|
480 | loadModules(currentConfig);
|
---|
481 | loadCacheLinker();
|
---|
482 | }
|
---|
483 | }
|
---|
484 |
|
---|
485 | private void loadCacheLinker() {
|
---|
486 | if(sidLinker == null) sidLinker = new SidCacheLinker(); // always load
|
---|
487 | if(sidLinkerEnabled) {
|
---|
488 | sidLinker.init();
|
---|
489 | cacheHandler.setListener(sidLinker);
|
---|
490 | } else cacheHandler.setListener(null);
|
---|
491 | }
|
---|
492 |
|
---|
493 | private void loadProfiles(ProxyXmlConfig currentConfig) throws ConfigException {
|
---|
494 | disabledProfiles.clear();
|
---|
495 | ProxyXmlConfig profileConf = currentConfig.getSubConfig("ca-profiles");
|
---|
496 | Iterator iter = profileConf.getMultipleSubConfigs("profile");
|
---|
497 | Set newProfiles = new HashSet();
|
---|
498 | CaProfile profile;
|
---|
499 | ProxyXmlConfig profileConfig;
|
---|
500 | while(iter.hasNext()) {
|
---|
501 | profileConfig = (ProxyXmlConfig)iter.next();
|
---|
502 | profile = (CaProfile)profiles.get(profileConfig.getStringValue("name"));
|
---|
503 | if(profile == null) {
|
---|
504 | profile = new CaProfile();
|
---|
505 | profile.configUpdated(profileConfig);
|
---|
506 | addProfile(profile);
|
---|
507 | profileChanged(profile, true);
|
---|
508 | } else {
|
---|
509 | profilesById.remove(profile.getKeyStr());
|
---|
510 | profile.configUpdated(profileConfig);
|
---|
511 | if(!profile.isEnabled()) {
|
---|
512 | profiles.remove(profile.getName());
|
---|
513 | disabledProfiles.add(profile.getName());
|
---|
514 | } else profilesById.put(profile.getKeyStr(), profile);
|
---|
515 | profileChanged(profile, false);
|
---|
516 | }
|
---|
517 | newProfiles.add(profile.getName());
|
---|
518 | }
|
---|
519 | String name;
|
---|
520 | for(iter = new ArrayList(profiles.keySet()).iterator(); iter.hasNext(); ) {
|
---|
521 | name = (String)iter.next();
|
---|
522 | if(CaProfile.MULTIPLE.getName().equals(name)) continue;
|
---|
523 | if(!newProfiles.contains(name)) {
|
---|
524 | profile = (CaProfile)profiles.remove(name);
|
---|
525 | profilesById.remove(profile.getKeyStr());
|
---|
526 | profile.setEnabled(false);
|
---|
527 | disabledProfiles.remove(profile.getName());
|
---|
528 |
|
---|
529 | profileChanged(profile, false);
|
---|
530 | }
|
---|
531 | }
|
---|
532 | if(!profiles.containsKey(CaProfile.MULTIPLE.getName())) addProfile(CaProfile.MULTIPLE);
|
---|
533 | loadExtendedPort(profileConf);
|
---|
534 | }
|
---|
535 |
|
---|
536 | public void addProfile(CaProfile profile) throws ConfigException {
|
---|
537 | if(profile.isEnabled()) {
|
---|
538 | if(profiles.containsKey(profile.getName()))
|
---|
539 | throw new ConfigException("ca-profiles", "Duplicate profile definition: " + profile.getName());
|
---|
540 | if(getProfileById(profile.getNetworkId(), profile.getCaId()) != null)
|
---|
541 | throw new ConfigException("ca-profiles", "Duplicate profile definition for network-id '" +
|
---|
542 | profile.getNetworkIdStr() + "' and ca-id '" + profile.getCaIdStr() + "'.");
|
---|
543 | profiles.put(profile.getName(), profile);
|
---|
544 | profilesById.put(profile.getKeyStr(), profile);
|
---|
545 | // if the proxy is already running, start any new added profiles immediately
|
---|
546 | if(defaultListener != null) profile.startListening(defaultListener);
|
---|
547 | } else {
|
---|
548 | disabledProfiles.add(profile.getName());
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | private Properties previousPortCfg;
|
---|
553 | private ListenPort extendedPort;
|
---|
554 |
|
---|
555 | private void loadExtendedPort(ProxyXmlConfig profileConfig) throws ConfigException {
|
---|
556 | ProxyXmlConfig port = null;
|
---|
557 | try {
|
---|
558 | port = profileConfig.getSubConfig("extended-newcamd");
|
---|
559 | } catch (ConfigException e) {}
|
---|
560 | if(port != null && "true".equalsIgnoreCase(port.getStringValue("enabled", "true"))) {
|
---|
561 | Properties newPortCfg = port.toProperties();
|
---|
562 |
|
---|
563 | try {
|
---|
564 | boolean debug = "true".equalsIgnoreCase(port.getStringValue("debug"));
|
---|
565 | CaProfile.MULTIPLE.setDebug(debug);
|
---|
566 | } catch (ConfigException e) {}
|
---|
567 |
|
---|
568 | if(newPortCfg.equals(previousPortCfg)) return; // no changes, avoid closing and reopening port
|
---|
569 | else previousPortCfg = newPortCfg;
|
---|
570 |
|
---|
571 | if(extendedPort != null) {
|
---|
572 | extendedPort.configUpdated(port); // update the instance used by existing sessions
|
---|
573 | extendedPort.destroy(); // but destroy/reopen in case portnr changed
|
---|
574 | CaProfile.MULTIPLE.removeListenPort(extendedPort);
|
---|
575 | try {
|
---|
576 | Thread.sleep(500); // give port a chance to close
|
---|
577 | } catch(InterruptedException e) {
|
---|
578 | e.printStackTrace();
|
---|
579 | }
|
---|
580 | }
|
---|
581 | extendedPort = new ListenPort("ExtNewcamd");
|
---|
582 | extendedPort.configUpdated(port);
|
---|
583 | if(defaultListener != null) extendedPort.start(defaultListener, CaProfile.MULTIPLE);
|
---|
584 | CaProfile.MULTIPLE.addListenPort(extendedPort);
|
---|
585 | profileChanged(CaProfile.MULTIPLE, false);
|
---|
586 | } else {
|
---|
587 | if(extendedPort != null) {
|
---|
588 | CaProfile.MULTIPLE.removeListenPort(extendedPort);
|
---|
589 | extendedPort.destroy();
|
---|
590 | extendedPort = null;
|
---|
591 | }
|
---|
592 | previousPortCfg = null;
|
---|
593 | profileChanged(CaProfile.MULTIPLE, false);
|
---|
594 | }
|
---|
595 | }
|
---|
596 |
|
---|
597 | private void profileChanged(CaProfile profile, boolean added) {
|
---|
598 | if(connManager != null) connManager.cwsProfileChanged(profile, added);
|
---|
599 | }
|
---|
600 |
|
---|
601 | private void loadModules(ProxyXmlConfig currentConfig) throws ConfigException {
|
---|
602 | cacheHandler = (CacheHandler)loadInstance(currentConfig.getSubConfig("cache-handler"),
|
---|
603 | "cache-config", CacheHandler.class);
|
---|
604 | userManager = (UserManager)loadInstance(currentConfig.getSubConfig("user-manager"),
|
---|
605 | "auth-config", UserManager.class);
|
---|
606 | connManager = new CwsConnectorManager();
|
---|
607 | connManager.configUpdated(currentConfig.getSubConfig("connection-manager"));
|
---|
608 | }
|
---|
609 |
|
---|
610 | private void loadPlugins(ProxyXmlConfig currentConfig) throws ConfigException {
|
---|
611 | stopPlugins();
|
---|
612 | ProxyXmlConfig pluginConf;
|
---|
613 | try {
|
---|
614 | pluginConf = currentConfig.getSubConfig("proxy-plugins");
|
---|
615 | } catch (ConfigException e) {
|
---|
616 | return;
|
---|
617 | }
|
---|
618 | Iterator iter = pluginConf.getMultipleSubConfigs("plugin");
|
---|
619 | if(iter == null) return;
|
---|
620 | ProxyXmlConfig tmp;
|
---|
621 | while(iter.hasNext()) {
|
---|
622 | tmp = (ProxyXmlConfig)iter.next();
|
---|
623 | if("true".equalsIgnoreCase(tmp.getStringValue("enabled", "true"))) {
|
---|
624 | loadPlugin(tmp);
|
---|
625 | }
|
---|
626 | }
|
---|
627 | }
|
---|
628 |
|
---|
629 | private ProxyPlugin loadPlugin(ProxyXmlConfig tmp) throws ConfigException {
|
---|
630 | ProxyPlugin plugin;
|
---|
631 | plugin = (ProxyPlugin)loadInstance(tmp, "plugin-config", ProxyPlugin.class);
|
---|
632 | proxyPlugins.put(plugin.getName().toLowerCase(), plugin);
|
---|
633 | if(fw != null) {
|
---|
634 | String jarName = tmp.getStringValue("jar-file", "");
|
---|
635 | if(jarName.length() > 0) {
|
---|
636 | String jarFile = new File("plugins", jarName).getPath();
|
---|
637 | pluginLoadData.put(plugin.getName().toLowerCase(), new PluginMetaData(tmp, jarFile));
|
---|
638 | fw.addFile(jarFile);
|
---|
639 | }
|
---|
640 | }
|
---|
641 | return plugin;
|
---|
642 | }
|
---|
643 |
|
---|
644 | public void stopPlugins() {
|
---|
645 | if(!proxyPlugins.isEmpty() && logger != null) logger.info("Stopping " + proxyPlugins.size() + " loaded plugins...");
|
---|
646 | ProxyPlugin plugin;
|
---|
647 | for(Iterator iter = proxyPlugins.values().iterator(); iter.hasNext(); ) {
|
---|
648 | plugin = (ProxyPlugin)iter.next();
|
---|
649 | stopPlugin(plugin);
|
---|
650 | iter.remove();
|
---|
651 | }
|
---|
652 | proxyPlugins.clear();
|
---|
653 | pluginLoadData.clear();
|
---|
654 | System.gc();
|
---|
655 | System.runFinalization();
|
---|
656 | }
|
---|
657 |
|
---|
658 | private void stopPlugin(ProxyPlugin plugin) {
|
---|
659 | try {
|
---|
660 | plugin.stop();
|
---|
661 | } catch (Throwable t) {
|
---|
662 | if(logger != null) logger.warning("Uncaught exception in plugin stop() for '" + plugin.getName() + "': " + t);
|
---|
663 | if(logger != null) logger.throwing(t);
|
---|
664 | }
|
---|
665 | ClassLoader cl = plugin.getClass().getClassLoader();
|
---|
666 | if(cl instanceof PluginClassLoader) ((PluginClassLoader)cl).flush();
|
---|
667 |
|
---|
668 | if(fw != null && pluginLoadData.containsKey(plugin.getName())) {
|
---|
669 | String jarFile = ((PluginMetaData)pluginLoadData.get(plugin.getName().toLowerCase())).jarFile;
|
---|
670 | if(jarFile != null) fw.removeFile(jarFile);
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | private void updateModules(ProxyXmlConfig currentConfig) throws ConfigException {
|
---|
675 | cacheHandler.configUpdated(currentConfig.getSubConfig("cache-handler").getSubConfig("cache-config"));
|
---|
676 | userManager.configUpdated(currentConfig.getSubConfig("user-manager").getSubConfig("auth-config"));
|
---|
677 | connManager.configUpdated(currentConfig.getSubConfig("connection-manager"));
|
---|
678 | }
|
---|
679 |
|
---|
680 | public static Object loadInstance(ProxyXmlConfig subConf, String confName, Class intf) throws ConfigException {
|
---|
681 | return loadInstance(subConf, confName, intf, null);
|
---|
682 | }
|
---|
683 |
|
---|
684 | private static Object loadInstance(ProxyXmlConfig subConf, String confName, Class intf, ClassLoader cl)
|
---|
685 | throws ConfigException
|
---|
686 | {
|
---|
687 | String className = subConf.getStringValue("class");
|
---|
688 |
|
---|
689 | if(cl == null) {
|
---|
690 | String jarName = subConf.getStringValue("jar-file", "");
|
---|
691 | if(!"".equals(jarName)) { // jar-file specified, load using disposable classloader
|
---|
692 | File jarFile = null;
|
---|
693 | try {
|
---|
694 | jarFile = new File("plugins", jarName);
|
---|
695 | if(!jarFile.exists()) throw new ConfigException(subConf.getFullName(), "File not found: " + jarFile.getAbsolutePath());
|
---|
696 | cl = new PluginClassLoader(jarFile, ProxyConfig.class.getClassLoader());
|
---|
697 | } catch(IOException e) {
|
---|
698 | throw new ConfigException(subConf.getFullName(), "Bad jar-file: " + jarFile.getAbsolutePath(), e);
|
---|
699 | }
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | Class c;
|
---|
704 | try {
|
---|
705 | if(cl == null) c = Class.forName(className);
|
---|
706 | else c = Class.forName(className, true, cl);
|
---|
707 | if(!intf.isAssignableFrom(c))
|
---|
708 | throw new ConfigException(subConf.getFullName(), "Class '" + className + "' does not implement interface: " +
|
---|
709 | intf.getName());
|
---|
710 | } catch(ClassNotFoundException e) {
|
---|
711 | throw new ConfigException(subConf.getFullName(), "Class not found: " + className);
|
---|
712 | } catch(NoClassDefFoundError e) {
|
---|
713 | throw new ConfigException(subConf.getFullName(), "Class defs not found: " + e.getMessage());
|
---|
714 | }
|
---|
715 |
|
---|
716 | XmlConfigurable xc;
|
---|
717 | try {
|
---|
718 | xc = (XmlConfigurable)c.newInstance();
|
---|
719 | } catch(Exception e) {
|
---|
720 | throw new ConfigException(subConf.getFullName(), "Unable to instantiate class '" + className + "': " + e, e);
|
---|
721 | }
|
---|
722 | if(confName != null) {
|
---|
723 | ProxyXmlConfig pluginConfig = null;
|
---|
724 | try {
|
---|
725 | pluginConfig = subConf.getSubConfig(confName);
|
---|
726 | } catch(ConfigException e) {}
|
---|
727 |
|
---|
728 | if(pluginConfig == null) xc.configUpdated(new ProxyXmlConfig("plugin-config", "/cardserv-proxy/proxy-plugins/plugin")); // empty dummy
|
---|
729 | else xc.configUpdated(pluginConfig);
|
---|
730 | }
|
---|
731 | return xc;
|
---|
732 | }
|
---|
733 |
|
---|
734 | public void fileChanged(String fileName) {
|
---|
735 | if(logger == null) logger = ProxyLogger.getLabeledLogger(getClass().getName());
|
---|
736 | logger.info("File changes detected: " + fileName);
|
---|
737 |
|
---|
738 | if(cfgFile.getPath().equals(fileName)) {
|
---|
739 | try {
|
---|
740 | readConfig(rootConfigurable, new File(fileName));
|
---|
741 | } catch(FileNotFoundException e) {
|
---|
742 | logger.severe("Configuration file not found: " + fileName);
|
---|
743 | } catch(ConfigException e) {
|
---|
744 | logConfigException(logger, e);
|
---|
745 | } catch(Exception e) {
|
---|
746 | logger.severe("Internal error updating configuration: " + e, e);
|
---|
747 | }
|
---|
748 | } else if(fileName.endsWith(".jar")) {
|
---|
749 |
|
---|
750 | for(Iterator iter = new ArrayList(proxyPlugins.keySet()).iterator(); iter.hasNext(); ) {
|
---|
751 | String name = (String)iter.next();
|
---|
752 | if(name != null) {
|
---|
753 | PluginMetaData pm = (PluginMetaData)pluginLoadData.get(name);
|
---|
754 | if(pm != null && fileName.equals(pm.jarFile)) {
|
---|
755 |
|
---|
756 | logger.info("Stopping plugin '" + proxyPlugins.get(name).getClass() + "'...");
|
---|
757 |
|
---|
758 | stopPlugin((ProxyPlugin)proxyPlugins.get(name));
|
---|
759 | proxyPlugins.remove(name);
|
---|
760 | pluginLoadData.remove(name);
|
---|
761 |
|
---|
762 | try {
|
---|
763 | Thread.sleep(100);
|
---|
764 | } catch(InterruptedException e) {
|
---|
765 | e.printStackTrace();
|
---|
766 | }
|
---|
767 |
|
---|
768 | System.gc();
|
---|
769 | System.runFinalization();
|
---|
770 |
|
---|
771 | ProxyPlugin plugin = null;
|
---|
772 | try {
|
---|
773 | plugin = loadPlugin(pm.config);
|
---|
774 |
|
---|
775 | logger.info("Starting plugin '" + proxyPlugins.get(name).getClass() + "'...");
|
---|
776 |
|
---|
777 | plugin.start((CardServProxy)rootConfigurable);
|
---|
778 | } catch(ConfigException e) {
|
---|
779 | logConfigException(logger, e);
|
---|
780 | if(plugin != null) proxyPlugins.remove(plugin.getName().toLowerCase());
|
---|
781 | }
|
---|
782 |
|
---|
783 | }
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | }
|
---|
788 |
|
---|
789 | }
|
---|
790 |
|
---|
791 | public void setDefaultMsgListener(CamdMessageListener listener) {
|
---|
792 | defaultListener = listener;
|
---|
793 | started = true;
|
---|
794 | if(extendedPort != null) extendedPort.start(listener, CaProfile.MULTIPLE);
|
---|
795 | }
|
---|
796 |
|
---|
797 | public CamdMessageListener getDefaultMsgListener() {
|
---|
798 | return defaultListener;
|
---|
799 | }
|
---|
800 |
|
---|
801 | public File getCfgFile() {
|
---|
802 | return cfgFile;
|
---|
803 | }
|
---|
804 |
|
---|
805 | public void saveCfgFile(byte[] newFile) throws IOException {
|
---|
806 | if(fw != null) fw.removeFile(cfgFile.getPath()); // disable watchdog before rewriting file
|
---|
807 | if(cfgFile.exists())
|
---|
808 | if(!cfgFile.renameTo(new File(cfgFile.getPath() + ".bak"))) { /* ignore */ }
|
---|
809 | BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(cfgFile));
|
---|
810 | bos.write(newFile);
|
---|
811 | bos.close();
|
---|
812 | if(fw != null) fw.addFile(cfgFile.getPath());
|
---|
813 | }
|
---|
814 |
|
---|
815 | public int getEtMinCount() {
|
---|
816 | return etMinCount;
|
---|
817 | }
|
---|
818 |
|
---|
819 | public int getMaxPending() {
|
---|
820 | return maxPending;
|
---|
821 | }
|
---|
822 |
|
---|
823 | public int getMaxConnectionsIP() {
|
---|
824 | return maxConnectionsIP;
|
---|
825 | }
|
---|
826 |
|
---|
827 | public boolean isProfileDisabled(String profileName) {
|
---|
828 | return disabledProfiles.contains(profileName);
|
---|
829 | }
|
---|
830 |
|
---|
831 | public static void logConfigException(ProxyLogger logger, ConfigException e) {
|
---|
832 | logger.severe("Configuration error:");
|
---|
833 | if(e.getLabel() != null) {
|
---|
834 | if(e.getLabel().indexOf(' ') == -1) logger.severe("- Element: <" + e.getLabel() + ">");
|
---|
835 | else {
|
---|
836 | logger.severe("- Element: <" + e.getLabel());
|
---|
837 | if(e.getSubLabel() != null) logger.severe("- Attribute: " + e.getSubLabel());
|
---|
838 | }
|
---|
839 | }
|
---|
840 | logger.severe("- Message: " + e.getMessage());
|
---|
841 | }
|
---|
842 |
|
---|
843 | public static String providerIdentsToString(Set providers) {
|
---|
844 | Set set = new TreeSet();
|
---|
845 | for(Iterator iter = providers.iterator(); iter.hasNext(); ) {
|
---|
846 | set.add(DESUtil.intToByteString(((Integer)iter.next()).intValue(), 3));
|
---|
847 | }
|
---|
848 | return set.toString();
|
---|
849 | }
|
---|
850 |
|
---|
851 | public static String providerIdentsToString(Integer[] providers) {
|
---|
852 | Set set = new TreeSet();
|
---|
853 | for(int i = 0; i < providers.length; i++) {
|
---|
854 | set.add(DESUtil.intToByteString(providers[i].intValue(), 3));
|
---|
855 | }
|
---|
856 | return set.toString();
|
---|
857 | }
|
---|
858 |
|
---|
859 | public static Set getServiceTokens(String param, String list, boolean excludeCdata) throws ConfigException {
|
---|
860 | Set result = new HashSet();
|
---|
861 | String[] tokens = list.split(" ");
|
---|
862 | String[] service; ServiceMapping sm;
|
---|
863 | for(int i = 0; i < tokens.length; i++) {
|
---|
864 | service = tokens[i].split(":");
|
---|
865 | try {
|
---|
866 |
|
---|
867 | sm = new ServiceMapping(Integer.parseInt(service[0], 16), 0);
|
---|
868 | boolean identSet = false;
|
---|
869 | if(!excludeCdata && service.length >= 2) {
|
---|
870 | if(service[1].length() > 4) {
|
---|
871 | sm.setProviderIdent(Integer.parseInt(service[1], 16));
|
---|
872 | identSet = true;
|
---|
873 | } else sm.setCustomId(Integer.parseInt(service[1], 16));
|
---|
874 | if(service.length == 3) {
|
---|
875 | if(service[2].length() > 4) {
|
---|
876 | sm.setProviderIdent(Integer.parseInt(service[1], 16));
|
---|
877 | identSet = true;
|
---|
878 | } else sm.setCustomId(Integer.parseInt(service[2], 16));
|
---|
879 | }
|
---|
880 | }
|
---|
881 | if(!identSet) sm.setProviderIdent(ServiceMapping.NO_PROVIDER);
|
---|
882 |
|
---|
883 | result.add(sm);
|
---|
884 | } catch (NumberFormatException e) {
|
---|
885 | throw new ConfigException(param, "Bad hex integer value: " + e.getMessage());
|
---|
886 | }
|
---|
887 | }
|
---|
888 | return result;
|
---|
889 | }
|
---|
890 |
|
---|
891 | private static class PluginMetaData {
|
---|
892 |
|
---|
893 | ProxyXmlConfig config;
|
---|
894 | String jarFile;
|
---|
895 |
|
---|
896 | private PluginMetaData(ProxyXmlConfig config, String jarFile) {
|
---|
897 | this.config = config;
|
---|
898 | this.jarFile = jarFile;
|
---|
899 | }
|
---|
900 | }
|
---|
901 | public String getclientMsg() {
|
---|
902 | return clientMsg;
|
---|
903 | }
|
---|
904 | public String getmsgtoemu() {
|
---|
905 | return msgtoemu;
|
---|
906 | }
|
---|
907 | public boolean getosdMsg(){
|
---|
908 | return osdMsg;
|
---|
909 | }
|
---|
910 | }
|
---|