PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hyk-proxy-gae/src/com/hyk/proxy/client/application/gae/config/Config.java

http://hyk-proxy.googlecode.com/
Java | 652 lines | 537 code | 84 blank | 31 comment | 79 complexity | aed3249c66ade4ea1c3d56ea3467f56e MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause, LGPL-2.1, GPL-3.0
  1. /**
  2. * This file is part of the hyk-proxy project.
  3. * Copyright (c) 2010 Yin QiWen <yinqiwen@gmail.com>
  4. *
  5. * Description: Config.java
  6. *
  7. * @author yinqiwen [ 2010-5-14 | 08:49:33 PM]
  8. *
  9. */
  10. package com.hyk.proxy.client.application.gae.config;
  11. import java.io.FileOutputStream;
  12. import java.net.URL;
  13. import java.net.URLDecoder;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.HashSet;
  17. import java.util.LinkedList;
  18. import java.util.List;
  19. import java.util.Set;
  20. import javax.net.ssl.SSLSocketFactory;
  21. import javax.xml.bind.JAXBContext;
  22. import javax.xml.bind.Marshaller;
  23. import javax.xml.bind.Unmarshaller;
  24. import javax.xml.bind.annotation.XmlAttribute;
  25. import javax.xml.bind.annotation.XmlElement;
  26. import javax.xml.bind.annotation.XmlElementWrapper;
  27. import javax.xml.bind.annotation.XmlElements;
  28. import javax.xml.bind.annotation.XmlRootElement;
  29. import javax.xml.bind.annotation.XmlTransient;
  30. import javax.xml.bind.annotation.XmlValue;
  31. import org.jivesoftware.smack.ConnectionConfiguration;
  32. import org.jivesoftware.smack.util.StringUtils;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. import com.hyk.proxy.client.util.GoogleAvailableService;
  36. import com.hyk.proxy.common.Constants;
  37. import com.hyk.proxy.common.Version;
  38. import com.hyk.proxy.common.secure.NoneSecurityService;
  39. /**
  40. *
  41. */
  42. @XmlRootElement(name = "Configure")
  43. public class Config
  44. {
  45. protected static Logger logger = LoggerFactory.getLogger(Config.class);
  46. private static Config instance = null;
  47. static
  48. {
  49. try
  50. {
  51. JAXBContext context = JAXBContext.newInstance(Config.class);
  52. Unmarshaller unmarshaller = context.createUnmarshaller();
  53. instance = (Config) unmarshaller.unmarshal(Config.class
  54. .getResource("/" + Constants.CLIENT_CONF_NAME));
  55. instance.init();
  56. }
  57. catch (Exception e)
  58. {
  59. logger.error("Failed to load default config file!", e);
  60. }
  61. }
  62. public static enum ConnectionMode
  63. {
  64. HTTP2GAE(1), XMPP2GAE(2), HTTPS2GAE(3);
  65. int value;
  66. ConnectionMode(int v)
  67. {
  68. value = v;
  69. }
  70. public static ConnectionMode fromInt(int v)
  71. {
  72. return values()[v - 1];
  73. }
  74. }
  75. public static class SimpleSocketAddress
  76. {
  77. @XmlAttribute
  78. public String host;
  79. @XmlAttribute
  80. public int port;
  81. }
  82. public static class HykProxyServerAuth
  83. {
  84. @XmlAttribute
  85. public String appid;
  86. @XmlAttribute
  87. public String user;
  88. @XmlAttribute
  89. public String passwd;
  90. }
  91. public static enum ProxyType
  92. {
  93. HTTP("http"), HTTPS("https");
  94. String value;
  95. ProxyType(String v)
  96. {
  97. value = v;
  98. }
  99. public static ProxyType fromStr(String str)
  100. {
  101. if (str.equalsIgnoreCase("http"))
  102. {
  103. return HTTP;
  104. }
  105. if (str.equalsIgnoreCase("https"))
  106. {
  107. return HTTPS;
  108. }
  109. return HTTP;
  110. }
  111. }
  112. public static class ProxyInfo
  113. {
  114. @XmlElement
  115. public String host;
  116. @XmlElement
  117. public int port = 80;
  118. @XmlElement
  119. public String user;
  120. @XmlElement
  121. public String passwd;
  122. @XmlElement
  123. public ProxyType type = ProxyType.HTTP;
  124. @XmlElement
  125. public String nextHopGoogleServer;
  126. }
  127. public static class XmppAccount
  128. {
  129. private static final String GTALK_SERVER = "talk.google.com";
  130. private static final String GTALK_SERVER_NAME = "gmail.com";
  131. private static final int GTALK_SERVER_PORT = 5222;
  132. private static final String OVI_SERVER = "chat.ovi.com";
  133. private static final String OVI_SERVER_NAME = "ovi.com";
  134. private static final int OVI_SERVER_PORT = 5223;
  135. protected static final int DEFAULT_PORT = 5222;
  136. public XmppAccount init()
  137. {
  138. String server = StringUtils.parseServer(jid).trim();
  139. // String name = null;
  140. if (server.equals(GTALK_SERVER_NAME))
  141. {
  142. if (null == this.serverHost || this.serverHost.isEmpty())
  143. {
  144. this.serverHost = GTALK_SERVER;
  145. }
  146. if (0 == this.serverPort)
  147. {
  148. this.serverPort = GTALK_SERVER_PORT;
  149. }
  150. this.name = jid;
  151. }
  152. else if (server.equals(OVI_SERVER_NAME))
  153. {
  154. if (null == this.serverHost || this.serverHost.isEmpty())
  155. {
  156. this.serverHost = OVI_SERVER;
  157. }
  158. if (0 == this.serverPort)
  159. {
  160. this.serverPort = OVI_SERVER_PORT;
  161. }
  162. this.name = StringUtils.parseName(jid);
  163. this.isOldSSLEnable = true;
  164. }
  165. else
  166. {
  167. if (null == this.serverHost || this.serverHost.isEmpty())
  168. {
  169. this.serverHost = server;
  170. }
  171. if (0 == this.serverPort)
  172. {
  173. this.serverPort = DEFAULT_PORT;
  174. }
  175. this.name = StringUtils.parseName(jid);
  176. }
  177. String serviceName = server;
  178. connectionConfig = new ConnectionConfiguration(this.serverHost,
  179. serverPort, serviceName);
  180. if (isOldSSLEnable)
  181. {
  182. connectionConfig
  183. .setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
  184. connectionConfig
  185. .setSocketFactory(SSLSocketFactory.getDefault());
  186. }
  187. return this;
  188. }
  189. @XmlAttribute(name = "user")
  190. public String jid;
  191. @XmlAttribute
  192. public String passwd;
  193. @XmlAttribute
  194. public int serverPort;
  195. @XmlAttribute
  196. public String serverHost;
  197. @XmlAttribute(name = "oldSSLEnable")
  198. public boolean isOldSSLEnable;
  199. @XmlTransient
  200. public ConnectionConfiguration connectionConfig;
  201. @XmlTransient
  202. public String name;
  203. }
  204. private List<HykProxyServerAuth> hykProxyServerAuths = new LinkedList<HykProxyServerAuth>();
  205. @XmlElements(@XmlElement(name = "hyk-proxy-server"))
  206. public void setHykProxyServerAuths(
  207. List<HykProxyServerAuth> hykProxyServerAuths)
  208. {
  209. this.hykProxyServerAuths = hykProxyServerAuths;
  210. }
  211. private List<XmppAccount> xmppAccounts;
  212. @XmlElements(@XmlElement(name = "XMPPAccount"))
  213. public void setXmppAccounts(List<XmppAccount> xmppAccounts)
  214. {
  215. this.xmppAccounts = xmppAccounts;
  216. }
  217. private int httpConnectionPoolSize;
  218. @XmlElement
  219. public void setHttpConnectionPoolSize(int httpConnectionPoolSize)
  220. {
  221. this.httpConnectionPoolSize = httpConnectionPoolSize;
  222. }
  223. private List<String> injectRangeHeaderSiteSet = new ArrayList<String>();
  224. private String injectRangeHeaderSites;
  225. @XmlElement
  226. void setInjectRangeHeaderSites(String injectRangeHeaderSites)
  227. {
  228. this.injectRangeHeaderSites = injectRangeHeaderSites;
  229. String[] sites = injectRangeHeaderSites.split(";");
  230. for(String s:sites)
  231. {
  232. injectRangeHeaderSiteSet.add(s.trim());
  233. }
  234. //System.out.println("#####" + injectRangeHeaderSiteSet);
  235. //System.exit(1);
  236. }
  237. String getInjectRangeHeaderSites()
  238. {
  239. return injectRangeHeaderSites;
  240. }
  241. public boolean isInjectRangeHeaderSitesMatchHost(String host)
  242. {
  243. for(String site:injectRangeHeaderSiteSet)
  244. {
  245. if(!site.isEmpty() && host.indexOf(site) != -1)
  246. {
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. private int rpcTimeOut;
  253. @XmlElement(name = "RPCTimeOut")
  254. public void setRpcTimeOut(int rpcTimeOut)
  255. {
  256. this.rpcTimeOut = rpcTimeOut;
  257. }
  258. private boolean simpleURLEnable;
  259. @XmlElement
  260. public void setSimpleURLEnable(boolean simpleURLEnable)
  261. {
  262. this.simpleURLEnable = simpleURLEnable;
  263. }
  264. private String compressor;
  265. @XmlElement
  266. public void setCompressor(String compressor)
  267. {
  268. this.compressor = compressor;
  269. }
  270. private int fetchLimitSize;
  271. @XmlElement
  272. public void setFetchLimitSize(int fetchLimitSize)
  273. {
  274. this.fetchLimitSize = fetchLimitSize;
  275. }
  276. private int maxFetcherNumber;
  277. @XmlElement
  278. public void setMaxFetcherNumber(int maxFetcherNumber)
  279. {
  280. this.maxFetcherNumber = maxFetcherNumber;
  281. }
  282. // @XmlElement
  283. private ProxyInfo localProxy;
  284. @XmlElement(name = "localProxy")
  285. public void setHykProxyClientLocalProxy(ProxyInfo localProxy)
  286. {
  287. this.localProxy = localProxy;
  288. }
  289. // @XmlElement(name = "localProxy")
  290. public ProxyInfo getHykProxyClientLocalProxy()
  291. {
  292. return localProxy;
  293. }
  294. // @XmlElement
  295. // private ProxyInfo defaultLocalProxy;
  296. private ConnectionMode client2ServerConnectionMode;
  297. @XmlTransient
  298. public ConnectionMode getClient2ServerConnectionMode()
  299. {
  300. return client2ServerConnectionMode;
  301. }
  302. public void setClient2ServerConnectionMode(
  303. ConnectionMode client2ServerConnectionMode)
  304. {
  305. this.client2ServerConnectionMode = client2ServerConnectionMode;
  306. }
  307. private String httpUpStreamEncrypter;
  308. public String getHttpUpStreamEncrypter()
  309. {
  310. return httpUpStreamEncrypter;
  311. }
  312. @XmlElement
  313. public void setHttpUpStreamEncrypter(String httpUpStreamEncrypter)
  314. {
  315. this.httpUpStreamEncrypter = httpUpStreamEncrypter;
  316. }
  317. public void init() throws Exception
  318. {
  319. if (localProxy != null)
  320. {
  321. if (null != localProxy.host)
  322. {
  323. localProxy.host = localProxy.host.trim();
  324. }
  325. if (null != localProxy.nextHopGoogleServer)
  326. {
  327. localProxy.nextHopGoogleServer = localProxy.nextHopGoogleServer
  328. .trim();
  329. if(localProxy.nextHopGoogleServer.isEmpty())
  330. {
  331. localProxy.nextHopGoogleServer = null;
  332. }
  333. }
  334. if (null == localProxy.host || localProxy.host.isEmpty())
  335. {
  336. localProxy = null;
  337. }
  338. }
  339. // if (defaultLocalProxy != null
  340. // && (null == defaultLocalProxy.host || defaultLocalProxy.host
  341. // .isEmpty()))
  342. // {
  343. // defaultLocalProxy = null;
  344. // }
  345. if (null != hykProxyServerAuths)
  346. {
  347. for (int i = 0; i < hykProxyServerAuths.size(); i++)
  348. {
  349. HykProxyServerAuth auth = hykProxyServerAuths.get(i);
  350. if (auth.appid == null || auth.appid.trim().isEmpty())
  351. {
  352. hykProxyServerAuths.remove(i);
  353. i--;
  354. continue;
  355. }
  356. if (auth.user == null || auth.user.equals(""))
  357. {
  358. auth.user = Constants.ANONYMOUSE_NAME;
  359. }
  360. if (auth.passwd == null || auth.passwd.equals(""))
  361. {
  362. auth.passwd = Constants.ANONYMOUSE_NAME;
  363. }
  364. auth.appid = auth.appid.trim();
  365. auth.user = auth.user.trim();
  366. auth.passwd = auth.passwd.trim();
  367. // if(null == localProxy &&
  368. // !ClientUtils.isHTTPServerReachable(auth.appid))
  369. // {
  370. // activateDefaultProxy();
  371. // }
  372. }
  373. }
  374. if (client2ServerConnectionMode.equals(ConnectionMode.XMPP2GAE))
  375. {
  376. for (int i = 0; i < xmppAccounts.size(); i++)
  377. {
  378. XmppAccount account = xmppAccounts.get(i);
  379. if (account.jid == null || account.jid.isEmpty())
  380. {
  381. xmppAccounts.remove(i);
  382. i--;
  383. }
  384. else
  385. {
  386. account.init();
  387. }
  388. }
  389. }
  390. if (client2ServerConnectionMode.equals(ConnectionMode.XMPP2GAE)
  391. && (null == xmppAccounts || xmppAccounts.isEmpty()))
  392. {
  393. throw new Exception("Since the connection mode is "
  394. + ConnectionMode.XMPP2GAE
  395. + ", at least one XMPP account needed.");
  396. }
  397. if (null == httpUpStreamEncrypter)
  398. {
  399. httpUpStreamEncrypter = NoneSecurityService.NAME;
  400. }
  401. if (localProxy == null || localProxy.host.contains("google"))
  402. {
  403. simpleURLEnable = true;
  404. }
  405. }
  406. @XmlElement
  407. void setConnectionMode(int mode)
  408. {
  409. client2ServerConnectionMode = ConnectionMode.fromInt(mode);
  410. }
  411. int getConnectionMode()
  412. {
  413. return client2ServerConnectionMode.value;
  414. }
  415. public List<HykProxyServerAuth> getHykProxyServerAuths()
  416. {
  417. return hykProxyServerAuths;
  418. }
  419. public int getHttpConnectionPoolSize()
  420. {
  421. return httpConnectionPoolSize;
  422. }
  423. public int getFetchLimitSize()
  424. {
  425. return fetchLimitSize;
  426. }
  427. public int getRpcTimeOut()
  428. {
  429. return rpcTimeOut;
  430. }
  431. public boolean isSimpleURLEnable()
  432. {
  433. return simpleURLEnable;
  434. }
  435. public String getCompressor()
  436. {
  437. return compressor;
  438. }
  439. public int getMaxFetcherNumber()
  440. {
  441. return maxFetcherNumber;
  442. }
  443. public List<XmppAccount> getXmppAccounts()
  444. {
  445. return xmppAccounts;
  446. }
  447. public void clearProxy()
  448. {
  449. localProxy = null;
  450. }
  451. public boolean selectDefaultHttpProxy()
  452. {
  453. if (null == localProxy)
  454. {
  455. ProxyInfo info = new ProxyInfo();
  456. info.host = GoogleAvailableService.getInstance()
  457. .getAvailableHttpService();
  458. if (null != info.host)
  459. {
  460. localProxy = info;
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. public boolean selectDefaultHttpsProxy()
  467. {
  468. if (null == localProxy)
  469. {
  470. ProxyInfo info = new ProxyInfo();
  471. info.host = GoogleAvailableService.getInstance()
  472. .getAvailableHttpsService();
  473. info.port = 443;
  474. info.type = ProxyType.HTTPS;
  475. if (null != info.host)
  476. {
  477. localProxy = info;
  478. return true;
  479. }
  480. }
  481. return false;
  482. }
  483. @XmlElementWrapper(name = "AppIdBindings")
  484. @XmlElements(@XmlElement(name = "Binding"))
  485. private List<AppIdBinding> appIdBindings;
  486. @XmlElement(name = "HttpProxyUserAgent")
  487. private HttpProxyUserAgent httpProxyUserAgent;
  488. static class AppIdBinding
  489. {
  490. @XmlAttribute
  491. String appid;
  492. @XmlElements(@XmlElement(name = "site"))
  493. List<String> sites;
  494. }
  495. static class HttpProxyUserAgent
  496. {
  497. @XmlAttribute
  498. String choice;
  499. @XmlElements(@XmlElement(name = "UserAgent"))
  500. List<UserAgent> agents;
  501. }
  502. static class UserAgent
  503. {
  504. @XmlAttribute
  505. String name;
  506. @XmlValue
  507. String value;
  508. }
  509. public String getBindingAppId(String host)
  510. {
  511. if (null != appIdBindings)
  512. {
  513. for (AppIdBinding binding : appIdBindings)
  514. {
  515. for (String site : binding.sites)
  516. {
  517. if (host.contains(site))
  518. {
  519. return binding.appid.trim();
  520. }
  521. }
  522. }
  523. }
  524. return null;
  525. }
  526. public String getSimulateUserAgent()
  527. {
  528. String defaultUserAgent = Constants.PROJECT_NAME + " V" + Version.value;
  529. if (null != httpProxyUserAgent)
  530. {
  531. String choice = httpProxyUserAgent.choice;
  532. List<UserAgent> list = httpProxyUserAgent.agents;
  533. for (UserAgent ua : list)
  534. {
  535. if (ua.name.equals(choice))
  536. {
  537. return ua.value.trim();
  538. }
  539. }
  540. }
  541. return defaultUserAgent;
  542. }
  543. public static Config getInstance()
  544. {
  545. return instance;
  546. }
  547. public void saveConfig() throws Exception
  548. {
  549. try
  550. {
  551. init();
  552. JAXBContext context = JAXBContext.newInstance(Config.class);
  553. Marshaller marshaller = context.createMarshaller();
  554. marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
  555. URL url = Config.class
  556. .getResource("/" + Constants.CLIENT_CONF_NAME);
  557. String conf = URLDecoder.decode(url.getFile(), "UTF-8");
  558. FileOutputStream fos = new FileOutputStream(conf);
  559. // fos.write("<!-- This is generated by hyk-proxy-client GUI, it's not the orignal conf file -->\r\n".getBytes());
  560. marshaller.marshal(this, fos);
  561. fos.close();
  562. }
  563. catch (Exception e)
  564. {
  565. throw e;
  566. }
  567. }
  568. }