1 19 20 package com.sslexplorer.replacementproxy; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import com.sslexplorer.boot.Replacer; 31 import com.sslexplorer.boot.Util; 32 import com.sslexplorer.policyframework.LaunchSession; 33 import com.sslexplorer.util.Utils; 34 35 38 public class ProxyReplacer implements Replacer { 39 40 final static Log log = LogFactory.getLog(ProxyReplacer.class); 41 42 private RequestProcessor requestProcessor; 43 private BaseSearch baseSearch; 44 45 48 public ProxyReplacer(RequestProcessor requestProcessor, BaseSearch baseSearch) { 49 super(); 50 this.requestProcessor = requestProcessor; 51 this.baseSearch = baseSearch; 52 } 53 54 public String getProxiedPath(URL context, String path, String base) { 55 if (path.startsWith("#")) { 57 return path; 58 } 59 60 String lc = path.toLowerCase(); 62 if (lc.startsWith("mailto:") || lc.startsWith("javascript:") || lc.startsWith("ftp:") || lc.startsWith("news")) { 63 return path; 64 } 65 66 URL pathURL = null; 71 String ref = ""; 72 try { 73 pathURL = new URL (path); 74 if (pathURL.getRef() != null) { 75 ref = "#" + pathURL.getRef(); 76 int idx = path.lastIndexOf('#'); 77 path = path.substring(0, idx); 78 pathURL = new URL (path); 79 } 80 } catch (MalformedURLException murle) { 81 82 int idx = path.lastIndexOf('#'); 83 if (idx != -1) { 84 ref = "#" + path.substring(idx + 1); 85 path = path.substring(0, idx); 86 } 87 } 88 89 102 106 String newPath = null; 107 try { 108 if (base != null) { 109 if (pathURL != null) { 110 newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/" + Util.urlEncode(Utils.htmlunescape(pathURL.toExternalForm())) + ref; 112 } else { 113 if (path.startsWith("./")) { 115 path = path.substring(2); 116 } 117 try { 118 URL baseURL = new URL (base); 119 URL actual = new URL (baseURL, path); 120 newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/" + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref; 122 } catch (MalformedURLException murle) { 123 log.error("Invalidate base URL.", murle); 124 } 125 } 126 } else { 127 URL actual = new URL (context, path); 128 newPath = "/replacementProxyEngine/" + requestProcessor.getLaunchId() + "/" + Util.urlEncode(Utils.htmlunescape(actual.toExternalForm())) + ref; 130 } 131 } catch (MalformedURLException ex) { 132 log.error("Could not convert path from '" + path + "' using " + context.toExternalForm(), ex); 133 newPath = path; 134 } 135 if (newPath == null) { 136 log.warn("Failed to proxy path " + path + ", using original."); 137 newPath = path; 138 } 139 if (log.isDebugEnabled()) 140 log.debug("Created proxy path " + newPath); 141 142 int idx = newPath.lastIndexOf("%2F"); 144 newPath = newPath.substring(0, idx) + "/" + newPath.substring(idx + 3); 145 146 return newPath; 147 } 148 149 public String getReplacement(Pattern pattern, Matcher matcher, String replacementPattern) { 150 StringBuffer buf = new StringBuffer (); 151 char ch; 152 boolean esc = false; 153 try { 154 for (int i = 0; i < replacementPattern.length(); i++) { 155 ch = replacementPattern.charAt(i); 156 if (esc) { 157 buf.append(ch); 158 esc = false; 159 } else { 160 if (ch == '\\') { 161 esc = true; 162 } else if (ch == '%') { 163 i++; 164 ch = replacementPattern.charAt(i); 165 int group = Integer.parseInt(String.valueOf(ch)); 166 String groupText = matcher.group(group); 167 if (groupText != null && !groupText.equals("")) { 168 buf.append(getProxiedPath(requestProcessor.getRequestParameters().getProxiedURLBase(), groupText, baseSearch.getBase())); 169 } 170 } else if (ch == '$') { 171 i++; 172 ch = replacementPattern.charAt(i); 173 int group = Integer.parseInt(String.valueOf(ch)); 174 String groupText = matcher.group(group); 175 if (groupText != null && !groupText.equals("")) { 176 buf.append(groupText); 177 } 178 } else if (ch == '^') { 179 i++; 180 ch = replacementPattern.charAt(i); 181 if(ch == 'T') { 182 buf.append(requestProcessor.getLaunchId()); 183 } 184 else { 185 int group = Integer.parseInt(String.valueOf(ch)); 186 String groupText = matcher.group(group); 187 if (groupText != null && !groupText.equals("")) { 188 buf.append(new URL (requestProcessor.getRequestParameters().getProxiedURLBase(), groupText).toExternalForm()); 189 } 190 } 191 } else if(ch == '~') { 192 i++; 193 ch = replacementPattern.charAt(i); 194 int group = Integer.parseInt(String.valueOf(ch)); 195 String groupText = matcher.group(group); 196 if (groupText != null && !groupText.equals("")) { 197 groupText = groupText.replaceAll("\\\\", ""); 198 buf.append(getProxiedPath(requestProcessor.getRequestParameters().getProxiedURLBase(), groupText, baseSearch.getBase())); 199 } 200 }else { 201 buf.append(ch); 202 } 203 } 204 } 205 return buf.toString(); 206 } catch (Throwable t) { 207 log.warn("Invalid replacement pattern " + replacementPattern, t); 208 } 209 return replacementPattern; 210 } 211 212 } 213 | Popular Tags |