use of org.eclipse.kura.net.firewall.FirewallPortForwardConfigIP4 in project kura by eclipse.
the class GwtNetworkServiceImpl method updateDeviceFirewallPortForwards.
@Override
public void updateDeviceFirewallPortForwards(GwtXSRFToken xsrfToken, List<GwtFirewallPortForwardEntry> entries) throws GwtKuraException {
s_logger.debug("updateDeviceFirewallPortForwards() :: updating port forward entries");
checkXSRFToken(xsrfToken);
NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
List<FirewallPortForwardConfigIP<? extends IPAddress>> firewallPortForwardConfigIPs = new ArrayList<FirewallPortForwardConfigIP<? extends IPAddress>>();
try {
for (GwtFirewallPortForwardEntry entry : entries) {
String network = null;
String prefix = null;
if (entry.getPermittedNetwork() != null) {
String[] parts = entry.getPermittedNetwork().split("/");
network = parts[0];
prefix = parts[1];
}
FirewallPortForwardConfigIP<IP4Address> firewallPortForwardConfigIP = new FirewallPortForwardConfigIP4();
firewallPortForwardConfigIP.setInboundInterface(GwtSafeHtmlUtils.htmlEscape(entry.getInboundInterface()));
firewallPortForwardConfigIP.setOutboundInterface(GwtSafeHtmlUtils.htmlEscape(entry.getOutboundInterface()));
firewallPortForwardConfigIP.setAddress((IP4Address) IPAddress.parseHostAddress(GwtSafeHtmlUtils.htmlEscape(entry.getAddress())));
firewallPortForwardConfigIP.setProtocol(NetProtocol.valueOf(GwtSafeHtmlUtils.htmlEscape(entry.getProtocol())));
firewallPortForwardConfigIP.setInPort(entry.getInPort());
firewallPortForwardConfigIP.setOutPort(entry.getOutPort());
boolean masquerade = entry.getMasquerade().equals("yes") ? true : false;
firewallPortForwardConfigIP.setMasquerade(masquerade);
if (network != null && prefix != null) {
firewallPortForwardConfigIP.setPermittedNetwork(new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(network), Short.parseShort(prefix)));
}
firewallPortForwardConfigIP.setPermittedMac(GwtSafeHtmlUtils.htmlEscape(entry.getPermittedMAC()));
firewallPortForwardConfigIP.setSourcePortRange(GwtSafeHtmlUtils.htmlEscape(entry.getSourcePortRange()));
s_logger.debug("adding port forward entry for inbound iface {} - port {}", GwtSafeHtmlUtils.htmlEscape(entry.getInboundInterface()), entry.getInPort());
firewallPortForwardConfigIPs.add(firewallPortForwardConfigIP);
}
nas.setFirewallPortForwardingConfiguration(firewallPortForwardConfigIPs);
} catch (KuraException e) {
s_logger.warn("Exception while updating firewall port forwards", e);
throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
} catch (NumberFormatException e) {
s_logger.warn("Exception while updating firewall port forwards", e);
throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
} catch (UnknownHostException e) {
s_logger.warn("Exception while updating firewall port forwards", e);
throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
}
}
use of org.eclipse.kura.net.firewall.FirewallPortForwardConfigIP4 in project kura by eclipse.
the class GwtNetworkServiceImpl method findDeviceFirewallPortForwards.
@Override
public ArrayList<GwtFirewallPortForwardEntry> findDeviceFirewallPortForwards(GwtXSRFToken xsrfToken) throws GwtKuraException {
checkXSRFToken(xsrfToken);
NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
List<GwtFirewallPortForwardEntry> gwtPortForwardEntries = new ArrayList<GwtFirewallPortForwardEntry>();
try {
List<NetConfig> firewallConfigs = nas.getFirewallConfiguration();
if (firewallConfigs != null && !firewallConfigs.isEmpty()) {
for (NetConfig netConfig : firewallConfigs) {
if (netConfig instanceof FirewallPortForwardConfigIP4) {
s_logger.debug("findDeviceFirewallPortForwards() :: adding new Port Forward Entry");
GwtFirewallPortForwardEntry entry = new GwtFirewallPortForwardEntry();
entry.setInboundInterface(((FirewallPortForwardConfigIP4) netConfig).getInboundInterface());
entry.setOutboundInterface(((FirewallPortForwardConfigIP4) netConfig).getOutboundInterface());
entry.setAddress(((FirewallPortForwardConfigIP4) netConfig).getAddress().getHostAddress());
entry.setProtocol(((FirewallPortForwardConfigIP4) netConfig).getProtocol().toString());
entry.setInPort(((FirewallPortForwardConfigIP4) netConfig).getInPort());
entry.setOutPort(((FirewallPortForwardConfigIP4) netConfig).getOutPort());
String masquerade = ((FirewallPortForwardConfigIP4) netConfig).isMasquerade() ? "yes" : "no";
entry.setMasquerade(masquerade);
entry.setPermittedNetwork(((FirewallPortForwardConfigIP4) netConfig).getPermittedNetwork().toString());
entry.setPermittedMAC(((FirewallPortForwardConfigIP4) netConfig).getPermittedMac());
entry.setSourcePortRange(((FirewallPortForwardConfigIP4) netConfig).getSourcePortRange());
gwtPortForwardEntries.add(entry);
}
}
}
return new ArrayList<GwtFirewallPortForwardEntry>(gwtPortForwardEntries);
} catch (KuraException e) {
s_logger.warn("Failed", e);
throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
}
}
use of org.eclipse.kura.net.firewall.FirewallPortForwardConfigIP4 in project kura by eclipse.
the class FirewallConfigurationServiceImpl method getFirewallConfiguration.
@Override
public FirewallConfiguration getFirewallConfiguration() throws KuraException {
s_logger.debug("getting the firewall configuration");
FirewallConfiguration firewallConfiguration = new FirewallConfiguration();
LinuxFirewall firewall = LinuxFirewall.getInstance();
Iterator<LocalRule> localRules = firewall.getLocalRules().iterator();
while (localRules.hasNext()) {
LocalRule localRule = localRules.next();
if (localRule.getPortRange() != null) {
s_logger.debug("getFirewallConfiguration() :: Adding local rule for {}", localRule.getPortRange());
firewallConfiguration.addConfig(new FirewallOpenPortConfigIP4(localRule.getPortRange(), NetProtocol.valueOf(localRule.getProtocol()), localRule.getPermittedNetwork(), localRule.getPermittedInterfaceName(), localRule.getUnpermittedInterfaceName(), localRule.getPermittedMAC(), localRule.getSourcePortRange()));
} else {
s_logger.debug("getFirewallConfiguration() :: Adding local rule for {}", localRule.getPort());
firewallConfiguration.addConfig(new FirewallOpenPortConfigIP4(localRule.getPort(), NetProtocol.valueOf(localRule.getProtocol()), localRule.getPermittedNetwork(), localRule.getPermittedInterfaceName(), localRule.getUnpermittedInterfaceName(), localRule.getPermittedMAC(), localRule.getSourcePortRange()));
}
}
Iterator<PortForwardRule> portForwardRules = firewall.getPortForwardRules().iterator();
while (portForwardRules.hasNext()) {
PortForwardRule portForwardRule = portForwardRules.next();
try {
s_logger.debug("getFirewallConfiguration() :: Adding port forwarding - inbound iface is {}", portForwardRule.getInboundIface());
firewallConfiguration.addConfig(new FirewallPortForwardConfigIP4(portForwardRule.getInboundIface(), portForwardRule.getOutboundIface(), (IP4Address) IPAddress.parseHostAddress(portForwardRule.getAddress()), NetProtocol.valueOf(portForwardRule.getProtocol()), portForwardRule.getInPort(), portForwardRule.getOutPort(), portForwardRule.isMasquerade(), new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(portForwardRule.getPermittedNetwork()), (short) portForwardRule.getPermittedNetworkMask()), portForwardRule.getPermittedMAC(), portForwardRule.getSourcePortRange()));
} catch (UnknownHostException e) {
e.printStackTrace();
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
}
Iterator<NATRule> autoNatRules = firewall.getAutoNatRules().iterator();
while (autoNatRules.hasNext()) {
NATRule autoNatRule = autoNatRules.next();
s_logger.debug("getFirewallConfiguration() :: Adding auto NAT rules {}", autoNatRule.getSourceInterface());
firewallConfiguration.addConfig(new FirewallAutoNatConfig(autoNatRule.getSourceInterface(), autoNatRule.getDestinationInterface(), autoNatRule.isMasquerade()));
}
Iterator<NATRule> natRules = firewall.getNatRules().iterator();
while (natRules.hasNext()) {
NATRule natRule = natRules.next();
s_logger.debug("getFirewallConfiguration() :: Adding NAT rules {}", natRule.getSourceInterface());
firewallConfiguration.addConfig(new FirewallNatConfig(natRule.getSourceInterface(), natRule.getDestinationInterface(), natRule.getProtocol(), natRule.getSource(), natRule.getDestination(), natRule.isMasquerade()));
}
return firewallConfiguration;
}
Aggregations