1 19 20 package com.sslexplorer.tunnels.forms; 21 22 import java.net.InetAddress ; 23 import java.net.NetworkInterface ; 24 import java.util.List ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.Globals; 31 import org.apache.struts.action.ActionErrors; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionMessage; 34 import org.apache.struts.util.LabelValueBean; 35 36 import com.sslexplorer.boot.HostService; 37 import com.sslexplorer.boot.PropertyList; 38 import com.sslexplorer.boot.Util; 39 import com.sslexplorer.input.MultiSelectSelectionModel; 40 import com.sslexplorer.input.validators.HostnameOrIPAddressWithReplacementsValidator; 41 import com.sslexplorer.input.validators.IPAddressValidator; 42 import com.sslexplorer.policyframework.Resource; 43 import com.sslexplorer.policyframework.forms.AbstractFavoriteResourceForm; 44 import com.sslexplorer.security.User; 45 import com.sslexplorer.tunnels.TransportType; 46 import com.sslexplorer.tunnels.Tunnel; 47 48 55 public class TunnelForm extends AbstractFavoriteResourceForm { 56 static Log log = LogFactory.getLog(TunnelForm.class); 57 58 60 private String selectedTab = "details"; 61 62 private String sourceInterface; 63 private String sourcePort; 64 private String destinationHost; 65 private String destinationPort; 66 private int tunnelType; 67 private String transport; 68 private boolean autoStart; 69 70 76 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 77 ActionErrors errs = super.validate(mapping, request); 78 if (isCommiting()) { 79 80 81 if (!Util.isNullOrTrimmedBlank(sourceInterface)) { 82 86 if(getTunnelType() == TransportType.REMOTE_TUNNEL_ID) { 87 if(!sourceInterface.trim().equals("0.0.0.0") && 88 !sourceInterface.trim().equals("127.0.0.2")) 89 try { 90 InetAddress addr = InetAddress.getByName(sourceInterface); 91 NetworkInterface nif = NetworkInterface.getByInetAddress(addr); 92 if(nif == null) { 93 throw new Exception (); 94 } 95 } 96 catch(Exception e) { 97 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidRemoteSourceInterface")); 98 } 99 } 100 else { 101 106 if(!IPAddressValidator.isIpAddressExpressionValid(sourceInterface)) { 107 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidLocaleSourceInterface")); 108 } 109 } 110 } 111 112 try { 113 int port = Integer.valueOf(sourcePort).intValue(); 114 if(port < 1 || port > 65535) { 115 throw new IllegalArgumentException (); 116 } 117 } catch (Exception e) { 118 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.sourcePortNotInteger")); 119 } 120 121 try { 122 int port = Integer.valueOf(destinationPort).intValue(); 123 if(port < 1 || port > 65535) { 124 throw new IllegalArgumentException (); 125 } 126 Integer.valueOf(destinationPort).intValue(); 127 } catch (Exception e) { 128 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.destinationPortNotInteger")); 129 } 130 131 if (Util.isNullOrTrimmedBlank(destinationHost)) { 132 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.noDestinationHost")); 133 } 134 else{ 135 if(!HostnameOrIPAddressWithReplacementsValidator.isValidAsHostOrIp(destinationHost)) { 136 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidHost")); 137 } 138 } 139 140 if (getResourceName().equalsIgnoreCase("default") 141 && (!getEditing() || (getEditing() && !getResource().getResourceName().equalsIgnoreCase("default")))) { 142 errs.add(Globals.ERROR_KEY, new ActionMessage("error.createNetworkPlace.cantUseNameDefault")); 143 setResourceName(""); 144 } 145 } 146 return errs; 147 } 148 149 154 public int getTabCount() { 155 return 3; 156 } 157 158 163 public String getTabName(int idx) { 164 switch (idx) { 165 case 0: 166 return "details"; 167 case 1: 168 return "other"; 169 default: 170 return "policies"; 171 } 172 } 173 174 179 public String getTabTitle(int idx) { 180 181 return null; 183 } 184 185 194 public void initialise(User user, Resource resource, boolean editing, MultiSelectSelectionModel policyModel, 195 PropertyList selectedPolicies, User owner, boolean assignOnly) throws Exception { 196 super.initialise(user, resource, editing, policyModel, selectedPolicies, owner, assignOnly); 197 Tunnel tunnel = (Tunnel) resource; 198 sourcePort = String.valueOf(tunnel.getSourcePort()); 199 destinationHost = tunnel.getDestination().getHost(); 200 destinationPort = String.valueOf(tunnel.getDestination().getPort()); 201 tunnelType = tunnel.getType(); 202 transport = tunnel.getTransport(); 203 autoStart = tunnel.isAutoStart(); 204 sourceInterface = tunnel.getSourceInterface(); 205 } 206 207 212 public String getSelectedTab() { 213 return selectedTab; 214 } 215 216 221 public void setSelectedTab(String selectedTab) { 222 this.selectedTab = selectedTab; 223 } 224 225 230 public void applyToResource() throws Exception { 231 Tunnel tunnel = (Tunnel) getResource(); 232 tunnel.setType(getTunnelType()); 233 tunnel.setAutoStart(isAutoStart()); 234 tunnel.setTransport(getTransport()); 235 tunnel.setSourcePort(Integer.parseInt(getSourcePort())); 236 tunnel.setDestination(new HostService(getDestinationHost(), Integer.parseInt(getDestinationPort()))); 237 tunnel.setSourceInterface(getSourceInterface()); 238 } 239 240 245 public String getSourcePort() { 246 return sourcePort; 247 } 248 249 254 public void setSourcePort(String sourcePort) { 255 this.sourcePort = sourcePort; 256 } 257 258 263 public String getSourceInterface() { 264 return sourceInterface; 265 } 266 267 272 public void setSourceInterface(String sourceInterface) { 273 this.sourceInterface = sourceInterface; 274 } 275 276 281 public boolean isAutoStart() { 282 return autoStart; 283 } 284 285 290 public void setAutoStart(boolean autoStart) { 291 this.autoStart = autoStart; 292 } 293 294 299 public String getDestinationHost() { 300 return destinationHost; 301 } 302 303 308 public void setDestinationHost(String destinationHost) { 309 this.destinationHost = destinationHost; 310 } 311 312 317 public String getDestinationPort() { 318 return destinationPort; 319 } 320 321 326 public void setDestinationPort(String destinationPort) { 327 this.destinationPort = destinationPort; 328 } 329 330 337 public int getTunnelType() { 338 return tunnelType; 339 } 340 341 348 public void setTunnelType(int tunnelType) { 349 this.tunnelType = tunnelType; 350 } 351 352 358 public void setTransport(String transport) { 359 this.transport = transport; 360 } 361 362 368 public String getTransport() { 369 return transport; 370 } 371 372 377 public List getTunnelTypeList() { 378 return TransportType.TYPES; 379 } 380 381 387 public List getTransportList() { 388 return TransportType.TRANSPORTS; 389 } 390 391 396 public String getTunnelTypeString() { 397 return ((LabelValueBean) getTunnelTypeList().get(getTunnelType())).getLabel(); 398 } 399 400 406 public void reset(ActionMapping mapping, HttpServletRequest request) { 407 super.reset(mapping, request); 408 this.sourceInterface = "127.0.0.1"; 409 this.autoStart = false; 410 } 411 412 417 public String getTabBundle(int idx) { 418 return null; 419 } 420 } | Popular Tags |