Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -41,6 +44,31 @@
@Path("/servers")
public class NutRestProvider {

private static final Set<String> ALLOWED_UPSD_HOSTS = new HashSet<String>(Arrays.asList(
"127.0.0.1",
"localhost"
));

public static boolean isAllowedServerHost(String host) {
return host != null && ALLOWED_UPSD_HOSTS.contains(host);
}

public static Set<String> getAllowedServerHosts() {
Set<String> retval = new HashSet<String>();
retval.addAll(ALLOWED_UPSD_HOSTS);
return retval;
}

public static void addAllowedServerHost(String host) {
if (host != null)
ALLOWED_UPSD_HOSTS.add(host);
}

public static void removeAllowedServerHost(String host) {
if (host != null)
ALLOWED_UPSD_HOSTS.remove(host);
}

@GET
public String get() {
return "UPSD connections";
Expand All @@ -64,6 +92,9 @@ public Server(String server) throws IOException, UnknownHostException, NutExcept
if(idx!=-1)
{
String host = server.substring(0, idx);
if (!isAllowedServerHost(host)) {
throw new IllegalArgumentException("Server host is not allowed");
}
String portPart = server.substring(idx+1);
try {
int port = Integer.parseInt(portPart);
Expand All @@ -76,6 +107,9 @@ public Server(String server) throws IOException, UnknownHostException, NutExcept
}
else
{
if (!isAllowedServerHost(server)) {
throw new IllegalArgumentException("Server host is not allowed");
}
client.connect(server, 3493);
}
}
Expand Down