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.*;
* @author Thiago Camargo
*/
public class STUNService extends BasicModule {
private static final String ELEMENT_NAME = "stun";
private static final String NAMESPACE = "google:jingleinfo";
private static final String DEFAULT_EXTERNAL_ADDRESSES =
"stun.xten.net:3478;" +
"jivesoftware.com:3478;" +
"igniterealtime.org:3478;" +
"stun.fwdnet.net:3478";
"stun.xten.net:3478;" +
"jivesoftware.com:3478;" +
"igniterealtime.org:3478;" +
"stun.fwdnet.net:3478";
private IQHandler stunIQHandler;
private StunServer stunServer = null;
private boolean enabled = false;
......@@ -105,12 +105,10 @@ public class STUNService extends BasicModule {
//
if (enabled && !oldValue) {
startSTUNService();
}
else if (!enabled && oldValue) {
} else if (!enabled && oldValue) {
stop();
}
}
else if (property.equals("stun.local.enabled")) {
} else if (property.equals("stun.local.enabled")) {
localEnabled = JiveGlobals.getBooleanProperty("stun.local.enabled", false);
}
}
......@@ -118,8 +116,7 @@ public class STUNService extends BasicModule {
public void propertyDeleted(String property, Map<String, Object> params) {
if (property.equals("stun.enabled")) {
enabled = true;
}
else if (property.equals("stun.local.enabled")) {
} else if (property.equals("stun.local.enabled")) {
localEnabled = false;
}
}
......@@ -151,8 +148,7 @@ public class STUNService extends BasicModule {
if (primary != null && secondary != null) {
stunServer = new StunServer(primaryPort, primary, secondaryPort, secondary);
stunServer.start();
}
else {
} else {
setLocalEnabled(false);
}
}
......@@ -178,8 +174,10 @@ public class STUNService extends BasicModule {
private void stopSTUNService() {
XMPPServer server = XMPPServer.getInstance();
server.getIQDiscoInfoHandler().removeServerFeature(NAMESPACE);
server.getIQRouter().removeHandler(stunIQHandler);
stunIQHandler = null;
if (stunIQHandler != null) {
server.getIQRouter().removeHandler(stunIQHandler);
stunIQHandler = null;
}
}
public void stop() {
......@@ -250,7 +248,7 @@ public class STUNService extends BasicModule {
/**
* 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
*/
public void setEnabled(boolean enabled, boolean localEnabled) {
......@@ -259,8 +257,7 @@ public class STUNService extends BasicModule {
if (isLocalEnabled()) {
startLocalServer();
}
}
else {
} else {
stopSTUNService();
}
this.enabled = enabled;
......@@ -276,8 +273,7 @@ public class STUNService extends BasicModule {
this.localEnabled = enabled;
if (isLocalEnabled()) {
startLocalServer();
}
else {
} else {
stopLocal();
}
}
......@@ -343,7 +339,7 @@ public class STUNService extends BasicModule {
* Abstraction method used to convert a String into a STUN Server Address List
*
* @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.
*/
private List<StunServerAddress> getStunServerAddresses(String addresses) {
......@@ -376,59 +372,58 @@ public class STUNService extends BasicModule {
}
public IQ handleIQ(IQ iq) throws UnauthorizedException {
IQ reply = IQ.createResultIQ(iq);
Element childElement = iq.getChildElement();
String namespace = childElement.getNamespaceURI();
Element childElementCopy = iq.getChildElement().createCopy();
reply.setChildElement(childElementCopy);
if (NAMESPACE.equals(namespace)) {
if (isEnabled()) {
Element stun = childElementCopy.addElement("stun");
// If the local server is configured as a STUN server, send it as the first item.
if (isLocalEnabled()) {
StunServerAddress local;
local = new StunServerAddress(primaryAddress, String.valueOf(primaryPort));
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) {
IQ reply = IQ.createResultIQ(iq);
Element childElement = iq.getChildElement();
String namespace = childElement.getNamespaceURI();
Element childElementCopy = iq.getChildElement().createCopy();
reply.setChildElement(childElementCopy);
if (NAMESPACE.equals(namespace)) {
if (isEnabled()) {
Element stun = childElementCopy.addElement("stun");
// If the local server is configured as a STUN server, send it as the first item.
if (isLocalEnabled()) {
StunServerAddress local;
local = new StunServerAddress(primaryAddress, String.valueOf(primaryPort));
if (!externalServers.contains(local)) {
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);
}
server.addAttribute("host", local.getServer());
server.addAttribute("udp", local.getPort());
}
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);
}
}
}
else {
// Answer an error since the server can't handle the requested namespace
reply.setError(PacketError.Condition.service_unavailable);
catch (UnknownHostException e) {
e.printStackTrace();
}
}
try {
Log.debug("RETURNED:" + reply.toXML());
}
catch (Exception e) {
Log.error(e);
}
return reply;
} else {
// Answer an error since the server can't handle the requested namespace
reply.setError(PacketError.Condition.service_unavailable);
}
try {
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);
}
}
......
......@@ -52,7 +52,7 @@
boolean add = request.getParameter("add") != null;
int remove = ParamUtils.getIntParameter(request, "remove", -1);
boolean success = false;
boolean enabled = false;
boolean enabled = true;
boolean localEnabled = false;
String primaryAddress;
......@@ -73,8 +73,7 @@
secondaryAddress = ParamUtils.getParameter(request, "secondaryAddress", true);
JiveGlobals.setProperty("stun.address.secondary", secondaryAddress);
enabled = ParamUtils.getBooleanParameter(request, "enabled", enabled);
JiveGlobals.setProperty("stun.enabled", String.valueOf(enabled));
enabled = JiveGlobals.getBooleanProperty("stun.enabled", enabled);
localEnabled = ParamUtils.getBooleanParameter(request, "localEnabled", localEnabled);
JiveGlobals.setProperty("stun.local.enabled", String.valueOf(localEnabled));
......@@ -86,12 +85,10 @@
success = stunService.isEnabled() == enabled && stunService.isLocalEnabled() == localEnabled;
}
else if (remove > -1) {
} else if (remove > -1) {
stunService.removeExternalServer(remove);
success = true;
}
else if (add) {
} else if (add) {
String server = ParamUtils.getParameter(request, "externalServer", true);
String port = ParamUtils.getParameter(request, "externalPort", true);
......@@ -176,88 +173,102 @@
<form action="" method="post" name="settings">
<div class="jive-contentBoxHeader">
<fmt:message key="stun.settings.title"/>
</div>
<div class="jive-contentBox">
<table cellpadding="3" cellspacing="5" border="0">
<tbody>
<tr>
<td align="left" colspan="2">
<fmt:message key="stun.settings.localenabled"/>
:&nbsp<input type="checkbox"
name="localEnabled"
<%=stunService.isLocalEnabled()?"checked":""%>
align="left">
</td>
</tr>
<tr>
<td align="left">
<fmt:message key="stun.settings.primaryaddress"/>:
</td><td>
<select size="1" name="primaryAddress">
<option value="CHOOSE">-- Select Address --</option>
<%
List<InetAddress> addresses = stunService.getAddresses();
for (InetAddress iaddress : addresses) {
String hostAddress = iaddress.getHostAddress();
boolean isPrimaryAddress = hostAddress.equals(stunService.getPrimaryAddress());
%>
<option value="<%= hostAddress %>" <% if(isPrimaryAddress) { %>selected <% } %> ><%= hostAddress %>
</option>
<% } %>
</td>
</tr>
<tr>
<td align="left">
<fmt:message key="stun.settings.secondaryaddress"/>:
</td><td>
<select size="1" name="secondaryAddress">
<option value="CHOOSE">-- Select Address --</option>
<%
for (InetAddress iaddress : addresses) {
String hostAddress = iaddress.getHostAddress();
boolean isSecondaryAddress = hostAddress.equals(stunService.getSecondaryAddress());
%>
<option value="<%= hostAddress %>" <% if(isSecondaryAddress) { %>selected <% } %> ><%= hostAddress %>
</option>
<% } %>
</select>
</td>
</tr>
<tr>
<td align="left">
<fmt:message key="stun.settings.primaryport"/>:
</td><td>
<input type="text" size="6"
maxlength="10"
name="primaryPort"
value="<%=stunService.getPrimaryPort()%>"
align="left">
</td>
</tr>
<tr>
<td align="left">
<fmt:message key="stun.settings.secondaryport"/>:
</td><td>
<input type="text" size="6"
maxlength="10"
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>
<div class="jive-contentBoxHeader">
<fmt:message key="stun.settings.title"/>
</div>
<div class="jive-contentBox">
<table cellpadding="3" cellspacing="5" border="0">
<tbody>
<tr>
<td align="left" colspan="2">
<fmt:message key="stun.settings.localenabled"/>
:&nbsp<input type="checkbox"
name="localEnabled"
<%=stunService.isLocalEnabled()?"checked":""%>
align="left">
</td>
</tr>
<tr>
<td align="left">
<fmt:message key="stun.settings.primaryaddress"/>
:
</td>
<td>
<select size="1" name="primaryAddress">
<option value="CHOOSE">-- Select Address --</option>
<%
List<InetAddress> addresses = stunService.getAddresses();
for (InetAddress iaddress : addresses) {
String hostAddress = iaddress.getHostAddress();
boolean isPrimaryAddress = hostAddress.equals(stunService.getPrimaryAddress());
%>
<option value="<%= hostAddress %>" <% if (isPrimaryAddress) { %>
selected <% } %> ><%= hostAddress %>
</option>
<% } %>
</td>
</tr>
<tr>
<td align="left">
<fmt:message key="stun.settings.secondaryaddress"/>
:
</td>
<td>
<select size="1" name="secondaryAddress">
<option value="CHOOSE">-- Select Address --</option>
<%
for (InetAddress iaddress : addresses) {
String hostAddress = iaddress.getHostAddress();
boolean isSecondaryAddress = hostAddress.equals(stunService.getSecondaryAddress());
%>
<option value="<%= hostAddress %>" <% if (isSecondaryAddress) { %>
selected <% } %> ><%= hostAddress %>
</option>
<% } %>
</select>
</td>
</tr>
<tr>
<td align="left">
<fmt:message key="stun.settings.primaryport"/>
:
</td>
<td>
<input type="text" size="6"
maxlength="10"
name="primaryPort"
value="<%=stunService.getPrimaryPort()%>"
align="left">
</td>
</tr>
<tr>
<td align="left">
<fmt:message key="stun.settings.secondaryport"/>
:
</td>
<td>
<input type="text" size="6"
maxlength="10"
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 action="" method="post" name="add">
<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