UPnPService.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver;
  20. import java.io.IOException;
  21. import java.net.InetAddress;
  22. import java.util.Map;
  23. import org.bitlet.weupnp.GatewayDevice;
  24. import org.bitlet.weupnp.GatewayDiscover;
  25. import org.bitlet.weupnp.PortMappingEntry;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.xml.sax.SAXException;
  29. /**
  30. * @author UnAfraid
  31. */
  32. public class UPnPService
  33. {
  34. private static final Logger _log = LoggerFactory.getLogger(UPnPService.class);
  35. private static final String PROTOCOL = "TCP";
  36. private final GatewayDiscover _gatewayDiscover = new GatewayDiscover();
  37. private GatewayDevice _activeGW;
  38. protected UPnPService()
  39. {
  40. try
  41. {
  42. load();
  43. }
  44. catch (Exception e)
  45. {
  46. _log.warn("{}: There was a problem while initializing the UPnP Service!", getClass().getSimpleName(), e);
  47. }
  48. }
  49. private void load() throws Exception
  50. {
  51. if (!Config.ENABLE_UPNP)
  52. {
  53. _log.info("UPnP Service is disabled.");
  54. return;
  55. }
  56. _log.info("Looking for UPnP Gateway Devices...");
  57. final Map<InetAddress, GatewayDevice> gateways = _gatewayDiscover.discover();
  58. if (gateways.isEmpty())
  59. {
  60. _log.info("No UPnP gateways found.");
  61. return;
  62. }
  63. // choose the first active gateway for the tests
  64. _activeGW = _gatewayDiscover.getValidGateway();
  65. if (_activeGW != null)
  66. {
  67. _log.info("Using UPnP gateway: {}", _activeGW.getFriendlyName());
  68. }
  69. else
  70. {
  71. _log.info("No active UPnP gateway found.");
  72. return;
  73. }
  74. _log.info("Using local address: {} External address: {}", _activeGW.getLocalAddress().getHostAddress(), _activeGW.getExternalIPAddress());
  75. if (Server.serverMode == Server.MODE_GAMESERVER)
  76. {
  77. addPortMapping(Config.PORT_GAME, "L2j Game Server");
  78. }
  79. else if (Server.serverMode == Server.MODE_LOGINSERVER)
  80. {
  81. addPortMapping(Config.PORT_LOGIN, "L2j Login Server");
  82. }
  83. }
  84. public void removeAllPorts() throws Exception
  85. {
  86. if (_activeGW != null)
  87. {
  88. if (Server.serverMode == Server.MODE_GAMESERVER)
  89. {
  90. deletePortMapping(Config.PORT_GAME);
  91. }
  92. else if (Server.serverMode == Server.MODE_LOGINSERVER)
  93. {
  94. deletePortMapping(Config.PORT_LOGIN);
  95. }
  96. }
  97. }
  98. private void addPortMapping(int port, String description) throws IOException, SAXException
  99. {
  100. final PortMappingEntry portMapping = new PortMappingEntry();
  101. final InetAddress localAddress = _activeGW.getLocalAddress();
  102. // Attempt to re-map
  103. if (_activeGW.getSpecificPortMappingEntry(port, PROTOCOL, portMapping))
  104. {
  105. _activeGW.deletePortMapping(port, PROTOCOL);
  106. }
  107. if (_activeGW.addPortMapping(port, port, localAddress.getHostAddress(), PROTOCOL, description))
  108. {
  109. _log.info("Mapping successfull on ", localAddress.getHostAddress(), port);
  110. }
  111. else
  112. {
  113. _log.info("Mapping failed on [{}:{}] - Already mapped?", localAddress.getHostAddress(), port);
  114. }
  115. }
  116. private void deletePortMapping(int port) throws IOException, SAXException
  117. {
  118. if (_activeGW.deletePortMapping(port, PROTOCOL))
  119. {
  120. _log.info("Mapping was deleted from [{}:{}]", _activeGW.getLocalAddress().getHostAddress(), port);
  121. }
  122. }
  123. public static UPnPService getInstance()
  124. {
  125. return SingletonHolder._instance;
  126. }
  127. private static class SingletonHolder
  128. {
  129. protected static final UPnPService _instance = new UPnPService();
  130. }
  131. }