Commit 6beb6d22 authored by Thiago Camargo's avatar Thiago Camargo Committed by thiago

[JM-1032] - Fixes on STUN Server Start and Stop

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8039 b35dd754-fafc-0310-a699-88a17e54d16e
parent efbf1c23
...@@ -34,15 +34,15 @@ import java.util.*; ...@@ -34,15 +34,15 @@ import java.util.*;
* @author Thiago Camargo * @author Thiago Camargo
*/ */
public class STUNService extends BasicModule { public class STUNService extends BasicModule {
private static final String ELEMENT_NAME = "stun"; private static final String ELEMENT_NAME = "stun";
private static final String NAMESPACE = "google:jingleinfo"; private static final String NAMESPACE = "google:jingleinfo";
private static final String DEFAULT_EXTERNAL_ADDRESSES = private static final String DEFAULT_EXTERNAL_ADDRESSES =
"stun.xten.net:3478;" + "stun.xten.net:3478;" +
"jivesoftware.com:3478;" + "jivesoftware.com:3478;" +
"igniterealtime.org:3478;" + "igniterealtime.org:3478;" +
"stun.fwdnet.net:3478"; "stun.fwdnet.net:3478";
private IQHandler stunIQHandler; private IQHandler stunIQHandler;
private StunServer stunServer = null; private StunServer stunServer = null;
private boolean enabled = false; private boolean enabled = false;
...@@ -105,12 +105,10 @@ public class STUNService extends BasicModule { ...@@ -105,12 +105,10 @@ public class STUNService extends BasicModule {
// //
if (enabled && !oldValue) { if (enabled && !oldValue) {
startSTUNService(); startSTUNService();
} } else if (!enabled && oldValue) {
else if (!enabled && oldValue) {
stop(); stop();
} }
} } else if (property.equals("stun.local.enabled")) {
else if (property.equals("stun.local.enabled")) {
localEnabled = JiveGlobals.getBooleanProperty("stun.local.enabled", false); localEnabled = JiveGlobals.getBooleanProperty("stun.local.enabled", false);
} }
} }
...@@ -118,8 +116,7 @@ public class STUNService extends BasicModule { ...@@ -118,8 +116,7 @@ public class STUNService extends BasicModule {
public void propertyDeleted(String property, Map<String, Object> params) { public void propertyDeleted(String property, Map<String, Object> params) {
if (property.equals("stun.enabled")) { if (property.equals("stun.enabled")) {
enabled = true; enabled = true;
} } else if (property.equals("stun.local.enabled")) {
else if (property.equals("stun.local.enabled")) {
localEnabled = false; localEnabled = false;
} }
} }
...@@ -151,8 +148,7 @@ public class STUNService extends BasicModule { ...@@ -151,8 +148,7 @@ public class STUNService extends BasicModule {
if (primary != null && secondary != null) { if (primary != null && secondary != null) {
stunServer = new StunServer(primaryPort, primary, secondaryPort, secondary); stunServer = new StunServer(primaryPort, primary, secondaryPort, secondary);
stunServer.start(); stunServer.start();
} } else {
else {
setLocalEnabled(false); setLocalEnabled(false);
} }
} }
...@@ -178,8 +174,10 @@ public class STUNService extends BasicModule { ...@@ -178,8 +174,10 @@ public class STUNService extends BasicModule {
private void stopSTUNService() { private void stopSTUNService() {
XMPPServer server = XMPPServer.getInstance(); XMPPServer server = XMPPServer.getInstance();
server.getIQDiscoInfoHandler().removeServerFeature(NAMESPACE); server.getIQDiscoInfoHandler().removeServerFeature(NAMESPACE);
server.getIQRouter().removeHandler(stunIQHandler); if (stunIQHandler != null) {
stunIQHandler = null; server.getIQRouter().removeHandler(stunIQHandler);
stunIQHandler = null;
}
} }
public void stop() { public void stop() {
...@@ -250,7 +248,7 @@ public class STUNService extends BasicModule { ...@@ -250,7 +248,7 @@ public class STUNService extends BasicModule {
/** /**
* Set the service enable status. * Set the service enable status.
* *
* @param enabled boolean to enable or disable * @param enabled boolean to enable or disable
* @param localEnabled local Server enable or disable * @param localEnabled local Server enable or disable
*/ */
public void setEnabled(boolean enabled, boolean localEnabled) { public void setEnabled(boolean enabled, boolean localEnabled) {
...@@ -259,8 +257,7 @@ public class STUNService extends BasicModule { ...@@ -259,8 +257,7 @@ public class STUNService extends BasicModule {
if (isLocalEnabled()) { if (isLocalEnabled()) {
startLocalServer(); startLocalServer();
} }
} } else {
else {
stopSTUNService(); stopSTUNService();
} }
this.enabled = enabled; this.enabled = enabled;
...@@ -276,8 +273,7 @@ public class STUNService extends BasicModule { ...@@ -276,8 +273,7 @@ public class STUNService extends BasicModule {
this.localEnabled = enabled; this.localEnabled = enabled;
if (isLocalEnabled()) { if (isLocalEnabled()) {
startLocalServer(); startLocalServer();
} } else {
else {
stopLocal(); stopLocal();
} }
} }
...@@ -343,7 +339,7 @@ public class STUNService extends BasicModule { ...@@ -343,7 +339,7 @@ public class STUNService extends BasicModule {
* Abstraction method used to convert a String into a STUN Server Address List * Abstraction method used to convert a String into a STUN Server Address List
* *
* @param addresses the String representation of server addresses with their * @param addresses the String representation of server addresses with their
* respective ports (server1:port1;server2:port2). * respective ports (server1:port1;server2:port2).
* @return STUN server addresses list. * @return STUN server addresses list.
*/ */
private List<StunServerAddress> getStunServerAddresses(String addresses) { private List<StunServerAddress> getStunServerAddresses(String addresses) {
...@@ -376,59 +372,58 @@ public class STUNService extends BasicModule { ...@@ -376,59 +372,58 @@ public class STUNService extends BasicModule {
} }
public IQ handleIQ(IQ iq) throws UnauthorizedException { public IQ handleIQ(IQ iq) throws UnauthorizedException {
IQ reply = IQ.createResultIQ(iq); IQ reply = IQ.createResultIQ(iq);
Element childElement = iq.getChildElement(); Element childElement = iq.getChildElement();
String namespace = childElement.getNamespaceURI(); String namespace = childElement.getNamespaceURI();
Element childElementCopy = iq.getChildElement().createCopy(); Element childElementCopy = iq.getChildElement().createCopy();
reply.setChildElement(childElementCopy); reply.setChildElement(childElementCopy);
if (NAMESPACE.equals(namespace)) { if (NAMESPACE.equals(namespace)) {
if (isEnabled()) { if (isEnabled()) {
Element stun = childElementCopy.addElement("stun"); Element stun = childElementCopy.addElement("stun");
// If the local server is configured as a STUN server, send it as the first item. // If the local server is configured as a STUN server, send it as the first item.
if (isLocalEnabled()) { if (isLocalEnabled()) {
StunServerAddress local; StunServerAddress local;
local = new StunServerAddress(primaryAddress, String.valueOf(primaryPort)); local = new StunServerAddress(primaryAddress, String.valueOf(primaryPort));
if (!externalServers.contains(local)) { if (!externalServers.contains(local)) {
Element server = stun.addElement("server");
server.addAttribute("host", local.getServer());
server.addAttribute("udp", local.getPort());
}
}
// Add any external STUN servers that are specified.
for (StunServerAddress stunServerAddress : externalServers) {
Element server = stun.addElement("server"); Element server = stun.addElement("server");
server.addAttribute("host", stunServerAddress.getServer()); server.addAttribute("host", local.getServer());
server.addAttribute("udp", stunServerAddress.getPort()); server.addAttribute("udp", local.getPort());
}
try {
String ip = sessionManager.getSession(iq.getFrom()).getConnection().getInetAddress().getHostAddress();
if (ip != null) {
Element publicIp = childElementCopy.addElement("publicip");
publicIp.addAttribute("ip", ip);
}
} }
catch (UnknownHostException e) { }
e.printStackTrace(); // Add any external STUN servers that are specified.
for (StunServerAddress stunServerAddress : externalServers) {
Element server = stun.addElement("server");
server.addAttribute("host", stunServerAddress.getServer());
server.addAttribute("udp", stunServerAddress.getPort());
}
try {
String ip = sessionManager.getSession(iq.getFrom()).getConnection().getInetAddress().getHostAddress();
if (ip != null) {
Element publicIp = childElementCopy.addElement("publicip");
publicIp.addAttribute("ip", ip);
} }
} }
catch (UnknownHostException e) {
} e.printStackTrace();
else { }
// Answer an error since the server can't handle the requested namespace
reply.setError(PacketError.Condition.service_unavailable);
} }
try { } else {
Log.debug("RETURNED:" + reply.toXML()); // Answer an error since the server can't handle the requested namespace
} reply.setError(PacketError.Condition.service_unavailable);
catch (Exception e) { }
Log.error(e);
} try {
return reply; Log.debug("RETURNED:" + reply.toXML());
}
catch (Exception e) {
Log.error(e);
}
return reply;
} }
public IQHandlerInfo getInfo() { public IQHandlerInfo getInfo() {
return new IQHandlerInfo(ELEMENT_NAME, NAMESPACE); return new IQHandlerInfo(ELEMENT_NAME, NAMESPACE);
} }
} }
......
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
boolean add = request.getParameter("add") != null; boolean add = request.getParameter("add") != null;
int remove = ParamUtils.getIntParameter(request, "remove", -1); int remove = ParamUtils.getIntParameter(request, "remove", -1);
boolean success = false; boolean success = false;
boolean enabled = false; boolean enabled = true;
boolean localEnabled = false; boolean localEnabled = false;
String primaryAddress; String primaryAddress;
...@@ -73,8 +73,7 @@ ...@@ -73,8 +73,7 @@
secondaryAddress = ParamUtils.getParameter(request, "secondaryAddress", true); secondaryAddress = ParamUtils.getParameter(request, "secondaryAddress", true);
JiveGlobals.setProperty("stun.address.secondary", secondaryAddress); JiveGlobals.setProperty("stun.address.secondary", secondaryAddress);
enabled = ParamUtils.getBooleanParameter(request, "enabled", enabled); enabled = JiveGlobals.getBooleanProperty("stun.enabled", enabled);
JiveGlobals.setProperty("stun.enabled", String.valueOf(enabled));
localEnabled = ParamUtils.getBooleanParameter(request, "localEnabled", localEnabled); localEnabled = ParamUtils.getBooleanParameter(request, "localEnabled", localEnabled);
JiveGlobals.setProperty("stun.local.enabled", String.valueOf(localEnabled)); JiveGlobals.setProperty("stun.local.enabled", String.valueOf(localEnabled));
...@@ -86,12 +85,10 @@ ...@@ -86,12 +85,10 @@
success = stunService.isEnabled() == enabled && stunService.isLocalEnabled() == localEnabled; success = stunService.isEnabled() == enabled && stunService.isLocalEnabled() == localEnabled;
} } else if (remove > -1) {
else if (remove > -1) {
stunService.removeExternalServer(remove); stunService.removeExternalServer(remove);
success = true; success = true;
} } else if (add) {
else if (add) {
String server = ParamUtils.getParameter(request, "externalServer", true); String server = ParamUtils.getParameter(request, "externalServer", true);
String port = ParamUtils.getParameter(request, "externalPort", true); String port = ParamUtils.getParameter(request, "externalPort", true);
...@@ -176,88 +173,102 @@ ...@@ -176,88 +173,102 @@
<form action="" method="post" name="settings"> <form action="" method="post" name="settings">
<div class="jive-contentBoxHeader"> <div class="jive-contentBoxHeader">
<fmt:message key="stun.settings.title"/> <fmt:message key="stun.settings.title"/>
</div> </div>
<div class="jive-contentBox"> <div class="jive-contentBox">
<table cellpadding="3" cellspacing="5" border="0"> <table cellpadding="3" cellspacing="5" border="0">
<tbody> <tbody>
<tr> <tr>
<td align="left" colspan="2"> <td align="left" colspan="2">
<fmt:message key="stun.settings.localenabled"/> <fmt:message key="stun.settings.localenabled"/>
:&nbsp<input type="checkbox" :&nbsp<input type="checkbox"
name="localEnabled" name="localEnabled"
<%=stunService.isLocalEnabled()?"checked":""%> <%=stunService.isLocalEnabled()?"checked":""%>
align="left"> align="left">
</td> </td>
</tr> </tr>
<tr> <tr>
<td align="left"> <td align="left">
<fmt:message key="stun.settings.primaryaddress"/>: <fmt:message key="stun.settings.primaryaddress"/>
</td><td> :
<select size="1" name="primaryAddress"> </td>
<option value="CHOOSE">-- Select Address --</option> <td>
<% <select size="1" name="primaryAddress">
List<InetAddress> addresses = stunService.getAddresses(); <option value="CHOOSE">-- Select Address --</option>
for (InetAddress iaddress : addresses) { <%
String hostAddress = iaddress.getHostAddress();
boolean isPrimaryAddress = hostAddress.equals(stunService.getPrimaryAddress());
%> List<InetAddress> addresses = stunService.getAddresses();
<option value="<%= hostAddress %>" <% if(isPrimaryAddress) { %>selected <% } %> ><%= hostAddress %> for (InetAddress iaddress : addresses) {
</option> String hostAddress = iaddress.getHostAddress();
<% } %> boolean isPrimaryAddress = hostAddress.equals(stunService.getPrimaryAddress());
</td>
</tr>
<tr> %>
<td align="left"> <option value="<%= hostAddress %>" <% if (isPrimaryAddress) { %>
<fmt:message key="stun.settings.secondaryaddress"/>: selected <% } %> ><%= hostAddress %>
</td><td> </option>
<select size="1" name="secondaryAddress"> <% } %>
<option value="CHOOSE">-- Select Address --</option> </td>
<% </tr>
for (InetAddress iaddress : addresses) { <tr>
String hostAddress = iaddress.getHostAddress(); <td align="left">
boolean isSecondaryAddress = hostAddress.equals(stunService.getSecondaryAddress()); <fmt:message key="stun.settings.secondaryaddress"/>
%> :
<option value="<%= hostAddress %>" <% if(isSecondaryAddress) { %>selected <% } %> ><%= hostAddress %> </td>
</option> <td>
<% } %> <select size="1" name="secondaryAddress">
</select> <option value="CHOOSE">-- Select Address --</option>
</td> <%
</tr> for (InetAddress iaddress : addresses) {
<tr> String hostAddress = iaddress.getHostAddress();
<td align="left"> boolean isSecondaryAddress = hostAddress.equals(stunService.getSecondaryAddress());
<fmt:message key="stun.settings.primaryport"/>: %>
</td><td> <option value="<%= hostAddress %>" <% if (isSecondaryAddress) { %>
<input type="text" size="6" selected <% } %> ><%= hostAddress %>
maxlength="10" </option>
name="primaryPort" <% } %>
value="<%=stunService.getPrimaryPort()%>" </select>
align="left"> </td>
</td> </tr>
</tr> <tr>
<tr> <td align="left">
<td align="left"> <fmt:message key="stun.settings.primaryport"/>
<fmt:message key="stun.settings.secondaryport"/>: :
</td><td> </td>
<input type="text" size="6" <td>
maxlength="10" <input type="text" size="6"
name="secondaryPort" maxlength="10"
value="<%=stunService.getSecondaryPort()%>" name="primaryPort"
align="left"> value="<%=stunService.getPrimaryPort()%>"
</td> align="left">
</tr> </td>
<tr> </tr>
<td> <tr>
<input type="hidden" name="save"> <td align="left">
<input type="button" name="set" value="<fmt:message key="global.save_settings" />" <fmt:message key="stun.settings.secondaryport"/>
onclick="checkAndSubmit()"> :
</td> </td>
</tr> <td>
</tbody> <input type="text" size="6"
</table> maxlength="10"
</div> name="secondaryPort"
value="<%=stunService.getSecondaryPort()%>"
align="left">
</td>
</tr>
<tr>
<td>
<input type="hidden" name="save">
<input type="button" name="set" value="<fmt:message key="global.save_settings" />"
onclick="checkAndSubmit()">
</td>
</tr>
</tbody>
</table>
</div>
</form> </form>
<form action="" method="post" name="add"> <form action="" method="post" name="add">
<div class="jive-contentBoxHeader"> <div class="jive-contentBoxHeader">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment