1 19 20 package com.sslexplorer.tunnels.wizards.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 35 import com.sslexplorer.boot.Util; 36 import com.sslexplorer.core.BundleActionMessage; 37 import com.sslexplorer.core.forms.AbstractResourceDetailsWizardForm; 38 import com.sslexplorer.input.validators.HostnameOrIPAddressWithReplacementsValidator; 39 import com.sslexplorer.input.validators.IPAddressValidator; 40 import com.sslexplorer.tunnels.TransportType; 41 import com.sslexplorer.tunnels.TunnelPlugin; 42 import com.sslexplorer.wizard.AbstractWizardSequence; 43 44 public class TunnelDetailsForm extends AbstractResourceDetailsWizardForm { 45 46 public final static String ATTR_SOURCE_PORT = "sourcePort"; 47 public final static String ATTR_DESTINATION_HOST = "destinationHost"; 48 public final static String ATTR_DESTINATION_PORT = "destinationPort"; 49 public final static String ATTR_TYPE = "tunnelType"; 50 public final static String ATTR_TRANSPORT = "transport"; 51 public final static String ATTR_AUTO_START = "autoStart"; 52 public final static String ATTR_SOURCE_INTERFACE = "sourceInterface"; 53 54 private String sourcePort; 55 private String destinationHost; 56 private String destinationPort; 57 private int tunnelType; 58 private String transport; 59 private boolean autoStart; 60 private String sourceInterface; 61 62 final static Log log = LogFactory.getLog(TunnelDetailsForm.class); 63 64 public TunnelDetailsForm() { 65 super(true, true, "/WEB-INF/jsp/content/tunnels/tunnelWizard/tunnelDetails.jspf", "resourceName", true, false, 66 "tunnelDetails", "tunnels", "tunnelWizard.tunnelDetails", 2, TunnelPlugin.SSL_TUNNEL_RESOURCE_TYPE); 67 } 68 69 public void init(AbstractWizardSequence sequence, HttpServletRequest request) throws Exception { 70 super.init(sequence, request); 71 sourcePort = ((Integer ) sequence.getAttribute(ATTR_SOURCE_PORT, new Integer (0))).toString(); 72 destinationHost = (String ) sequence.getAttribute(ATTR_DESTINATION_HOST, ""); 73 destinationPort = ((Integer ) sequence.getAttribute(ATTR_DESTINATION_PORT, new Integer (0))).toString(); 74 tunnelType = ((Integer ) sequence.getAttribute(ATTR_TYPE, new Integer (TransportType.LOCAL_TUNNEL_ID))).intValue(); 75 transport = (String ) sequence.getAttribute(ATTR_TRANSPORT, String.valueOf(TransportType.TCP_TUNNEL)); 76 autoStart = ((Boolean ) sequence.getAttribute(ATTR_AUTO_START, Boolean.FALSE)).booleanValue(); 77 sourceInterface = ((String ) sequence.getAttribute(ATTR_SOURCE_INTERFACE, "127.0.0.1")); 78 } 79 80 public void apply(AbstractWizardSequence sequence) throws Exception { 81 super.apply(sequence); 82 sequence.putAttribute(ATTR_SOURCE_PORT, new Integer (sourcePort)); 83 sequence.putAttribute(ATTR_DESTINATION_HOST, destinationHost); 84 sequence.putAttribute(ATTR_DESTINATION_PORT, new Integer (destinationPort)); 85 sequence.putAttribute(ATTR_TYPE, new Integer (tunnelType)); 86 sequence.putAttribute(ATTR_TRANSPORT, transport); 87 sequence.putAttribute(ATTR_AUTO_START, new Boolean (autoStart)); 88 sequence.putAttribute(ATTR_SOURCE_INTERFACE, sourceInterface); 89 } 90 91 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 92 if (getResourceName() != null && isCommiting()) { 93 ActionErrors errs = super.validate(mapping, request); 94 AbstractWizardSequence seq = getWizardSequence(request); 95 96 97 if (!Util.isNullOrTrimmedBlank(sourceInterface)) { 98 102 if(getTunnelType() == TransportType.REMOTE_TUNNEL_ID) { 103 if(!sourceInterface.trim().equals("0.0.0.0") && 104 !sourceInterface.trim().equals("127.0.0.2")) 105 try { 106 InetAddress addr = InetAddress.getByName(sourceInterface); 107 NetworkInterface nif = NetworkInterface.getByInetAddress(addr); 108 if(nif == null) { 109 throw new Exception (); 110 } 111 } 112 catch(Exception e) { 113 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidRemoteSourceInterface")); 114 } 115 } 116 else { 117 122 if(!IPAddressValidator.isIpAddressExpressionValid(sourceInterface)) { 123 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidLocaleSourceInterface")); 124 } 125 } 126 } 127 128 try { 129 int port = Integer.valueOf(sourcePort).intValue(); 130 if(port < 1 || port > 65535) { 131 throw new IllegalArgumentException (); 132 } 133 } catch (Exception e) { 134 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq 135 .getCurrentPageForm().getResourcePrefix() 136 + ".error.sourcePortNotInteger")); 137 } 138 139 try { 140 int port = Integer.valueOf(destinationPort).intValue(); 141 if(port < 1 || port > 65535) { 142 throw new IllegalArgumentException (); 143 } 144 } catch (Exception e) { 145 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq 146 .getCurrentPageForm().getResourcePrefix() 147 + ".error.destinationPortNotInteger")); 148 } 149 150 if (destinationHost == null || destinationHost.equals("")) { 151 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq 152 .getCurrentPageForm().getResourcePrefix() 153 + ".error.noDestinationHost")); 154 } 155 else{ 156 if(!HostnameOrIPAddressWithReplacementsValidator.isValidAsHostOrIp(destinationHost)) { 157 errs.add(Globals.ERROR_KEY, new ActionMessage("tunnelWizard.tunnelDetails.error.invalidHost")); 158 } 159 } 160 161 if (transport.equals(TransportType.UDP_TUNNEL) && tunnelType == TransportType.REMOTE_TUNNEL_ID) { 162 errs.add(Globals.ERROR_KEY, new BundleActionMessage(seq.getCurrentPageForm().getResourceBundle(), seq 163 .getCurrentPageForm().getResourcePrefix() 164 + ".error.remote.udp")); 165 } 166 167 return errs; 168 } 169 return null; 170 } 171 172 public String getSourcePort() { 173 return sourcePort; 174 } 175 176 public void setSourcePort(String sourcePort) { 177 this.sourcePort = sourcePort; 178 } 179 180 public void setSourceInterface(String sourceInterface) { 181 this.sourceInterface = sourceInterface; 182 } 183 184 public String getSourceInterface() { 185 return sourceInterface; 186 } 187 188 public boolean isAutoStart() { 189 return autoStart; 190 } 191 192 public void setAutoStart(boolean autoStart) { 193 this.autoStart = autoStart; 194 } 195 196 public String getDestinationHost() { 197 return destinationHost; 198 } 199 200 public void setDestinationHost(String destinationHost) { 201 this.destinationHost = destinationHost; 202 } 203 204 public String getDestinationPort() { 205 return destinationPort; 206 } 207 208 public void setDestinationPort(String destinationPort) { 209 this.destinationPort = destinationPort; 210 } 211 212 public int getTunnelType() { 213 return tunnelType; 214 } 215 216 public void setTunnelType(int tunnelType) { 217 this.tunnelType = tunnelType; 218 } 219 220 public void setTransport(String transport) { 221 this.transport = transport; 222 } 223 224 public String getTransport() { 225 return transport; 226 } 227 228 public List getTunnelTypeList() { 229 return TransportType.TYPES; 230 } 231 232 public List getTransportList() { 233 return TransportType.TRANSPORTS; 234 } 235 } | Popular Tags |