├── project
├── src
│ ├── test
│ │ ├── resources
│ │ │ ├── problem.txt
│ │ │ └── reverseProxy.test.properties
│ │ └── java
│ │ │ └── com
│ │ │ └── tek271
│ │ │ └── reverseProxy
│ │ │ ├── model
│ │ │ ├── ModelFactory.java
│ │ │ ├── MappingsTest.java
│ │ │ └── MappingTest.java
│ │ │ ├── utils
│ │ │ ├── PropertiesFileTest.java
│ │ │ ├── TextToolsTest.java
│ │ │ └── RegexToolsTest.java
│ │ │ ├── text
│ │ │ ├── UrlMapperTest.java
│ │ │ ├── TextTranslatorTest.java
│ │ │ └── TagTranslatorTest.java
│ │ │ └── config
│ │ │ └── PropertiesTest.java
│ └── main
│ │ ├── webapp
│ │ ├── index.jsp
│ │ └── WEB-INF
│ │ │ └── web.xml
│ │ ├── resources
│ │ ├── reverseProxy.properties
│ │ └── log4j.properties
│ │ └── java
│ │ └── com
│ │ └── tek271
│ │ └── reverseProxy
│ │ ├── config
│ │ ├── Config.java
│ │ └── Properties.java
│ │ ├── utils
│ │ ├── TextTools.java
│ │ ├── RegexFlagsEnum.java
│ │ ├── Tuple2.java
│ │ ├── RegexTools.java
│ │ ├── Tuple3.java
│ │ ├── FileTools.java
│ │ └── PropertiesFile.java
│ │ ├── model
│ │ ├── RegexPair.java
│ │ ├── Mappings.java
│ │ └── Mapping.java
│ │ ├── servlet
│ │ ├── ContentTranslator.java
│ │ ├── ContentUtils.java
│ │ ├── ContentType.java
│ │ └── ProxyFilter.java
│ │ └── text
│ │ ├── UrlMapper.java
│ │ ├── TextTranslator.java
│ │ └── TagTranslator.java
├── .settings
│ ├── org.maven.ide.eclipse.prefs
│ └── org.eclipse.jdt.core.prefs
├── .classpath
├── .project
└── pom.xml
├── .gitignore
├── README
├── COPYING.LESSER.txt
└── COPYING.txt
/project/src/test/resources/problem.txt:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | *.jar
3 | *.log
4 | .DS_Store?
5 |
6 | target/
7 |
--------------------------------------------------------------------------------
/project/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 |
2 |
3 | Reverse Proxy
4 |
5 |
6 |
--------------------------------------------------------------------------------
/project/.settings/org.maven.ide.eclipse.prefs:
--------------------------------------------------------------------------------
1 | #Sun Mar 13 20:01:54 EDT 2011
2 | activeProfiles=
3 | eclipse.preferences.version=1
4 | fullBuildGoals=process-test-resources
5 | includeModules=false
6 | resolveWorkspaceProjects=true
7 | resourceFilterGoals=process-resources resources\:testResources
8 | skipCompilerPlugin=true
9 | version=1
10 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/model/ModelFactory.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.model;
2 |
3 | public class ModelFactory {
4 | public static final Mapping MAPPING1= new Mapping("maps.google.com, localhost:8080, /rp/maps");
5 | public static final Mapping MAPPING2= new Mapping("www.facebook.com, localhost:8080, /rp/fb");
6 |
7 | public static final Mappings MAPPINGS = Mappings.create(MAPPING1, MAPPING2);
8 |
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/utils/PropertiesFileTest.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.utils;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import org.junit.Test;
6 |
7 | public class PropertiesFileTest {
8 | private static final String FILE= "reverseProxy.test.properties";
9 |
10 | @Test
11 | public void testSubset() {
12 | PropertiesFile propertiesFile= new PropertiesFile(FILE);
13 | PropertiesFile subset = propertiesFile.subset("mapping");
14 | assertEquals(5, subset.getProperties().size());
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/utils/TextToolsTest.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.utils;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import org.junit.Test;
6 |
7 | public class TextToolsTest {
8 |
9 | @Test public void testRemoveControlChars() {
10 | assertEquals("", TextTools.removeControlChars(null));
11 | assertEquals("", TextTools.removeControlChars(""));
12 | assertEquals(" ", TextTools.removeControlChars(" "));
13 | assertEquals(" ", TextTools.removeControlChars(" \t\n "));
14 | }
15 |
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/project/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 | Tek271 Reverse Proxy
7 |
8 |
9 | proxyFilter
10 | com.tek271.reverseProxy.servlet.ProxyFilter
11 |
12 |
13 |
14 | proxyFilter
15 | *
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/project/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/project/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | rp
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.maven.ide.eclipse.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.maven.ide.eclipse.maven2Nature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/project/src/main/resources/reverseProxy.properties:
--------------------------------------------------------------------------------
1 |
2 | # mapping info. The value consists of 3 parts:
3 | # the domain which we want to hide, usually it is blocked by the ISP of end users
4 | # the proxy domain
5 | # the proxy resource URI
6 | # for example, the following line will take requests to localhost/rp/maps
7 | # and re-route them to maps.google.com
8 | mapping.maps=maps.google.com, ${proxy.server}, /rp/maps
9 | mapping.mail=mail.google.com, ${proxy.server}, /rp/mail
10 | mapping.amnesty=www.amnesty.org, ${proxy.server}, /rp/amnesty
11 | mapping.twitter=twitter.com, ${proxy.server}, /rp/tw
12 | mapping.facebook=www.facebook.com, ${proxy.server}, /rp/fb
13 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/text/UrlMapperTest.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.text;
2 |
3 | import static org.junit.Assert.assertEquals;
4 |
5 | import org.junit.Test;
6 |
7 | import com.tek271.reverseProxy.model.ModelFactory;
8 |
9 | public class UrlMapperTest {
10 | private UrlMapper target= new UrlMapper(ModelFactory.MAPPING1);
11 |
12 |
13 | @Test
14 | public void testMapContentUrlUrl() {
15 | assertEquals("http://a.b", target.mapContentUrl("http://a.b"));
16 | String prefix = ModelFactory.MAPPING1.proxyResource; // /rp/maps
17 | assertEquals(prefix + "/home", target.mapContentUrl("/home"));
18 | }
19 |
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/config/PropertiesTest.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.config;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import org.junit.Test;
6 |
7 | import com.tek271.reverseProxy.model.Mappings;
8 | import com.tek271.reverseProxy.utils.PropertiesFile;
9 |
10 | public class PropertiesTest {
11 | private static final String FILE= "reverseProxy.test.properties";
12 |
13 | @Test
14 | public void testReadMappings() {
15 | PropertiesFile propertiesFile= new PropertiesFile(FILE, true);
16 | Mappings mappings = Properties.readMappings(propertiesFile);
17 | assertEquals(5, mappings.size());
18 | assertEquals("maps.google.com", mappings.get(0).hiddenDomain);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/project/src/test/resources/reverseProxy.test.properties:
--------------------------------------------------------------------------------
1 |
2 | test=whatever # inline comment
3 |
4 | # mapping info. The value consists of 3 parts:
5 | # the domain which we want to hide, usually it is blocked by the ISP of end users
6 | # the proxy domain
7 | # the proxy resource URI
8 | # for example, the following line, if the user goes to
9 | # http://localhost:8080/rp/maps
10 | # they will be re-routed to
11 | # http://maps.google.com
12 | mapping.maps=maps.google.com, localhost:8080, /rp/maps
13 | mapping.mail=mail.google.com, localhost:8080, /rp/mail
14 | mapping.facebook=www.facebook.com, localhost:8080, /rp/fb
15 | mapping.twitter=twitter.com, localhost:8080, /rp/tw
16 | mapping.amnesty=www.amnesty.org, localhost:8080, /rp/amnesty
17 |
--------------------------------------------------------------------------------
/project/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | #Mon Mar 14 01:30:20 EDT 2011
2 | eclipse.preferences.version=1
3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6 | org.eclipse.jdt.core.compiler.compliance=1.5
7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
13 | org.eclipse.jdt.core.compiler.source=1.5
14 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/model/MappingsTest.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.model;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import org.junit.Test;
6 |
7 | public class MappingsTest {
8 |
9 | @Test public void testfindByProxyUrl() {
10 | Mappings mappings= ModelFactory.MAPPINGS;
11 | assertEquals(mappings.get(0), mappings.findByProxyUrl("http://localhost:8080/rp/maps/1"));
12 | assertEquals(mappings.get(1), mappings.findByProxyUrl("http://localhost:8080/rp/fb/2"));
13 | }
14 |
15 | @Test public void testfindByHiddenUrl() {
16 | Mappings mappings= ModelFactory.MAPPINGS;
17 | assertEquals(mappings.get(0), mappings.findByHiddenUrl("http://maps.google.com/1"));
18 | assertEquals(mappings.get(1), mappings.findByHiddenUrl("http://www.facebook.com/2"));
19 | }
20 |
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/project/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 |
2 | log4j.rootLogger=DEBUG, console
3 |
4 | # Control logging generated by 3rd party libs
5 | log4j.category.org.apache=WARN, console, file
6 |
7 | # Console appender: sends output to console
8 | log4j.appender.console=org.apache.log4j.ConsoleAppender
9 | log4j.appender.console.layout=org.apache.log4j.PatternLayout
10 | log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p [%c:%L]: %m%n
11 |
12 | # File appender: writes output to a text file
13 | log4j.appender.file=org.apache.log4j.RollingFileAppender
14 | log4j.appender.file.File=logs/reverseProxy.log
15 | log4j.appender.file.MaxFileSize=5MB
16 | log4j.appender.file.MaxBackupIndex=99
17 | log4j.appender.file.layout=org.apache.log4j.PatternLayout
18 | log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %-5p - %m%n
19 | log4j.appender.file.append=true
20 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | This program uses GNU LGPL.
2 | Copyright © Abdul Habra 2011
3 | ahabra@yahoo.com
4 |
5 |
6 | INTRODUCTION
7 | ------------
8 | Definition of reverse proxy (from Wikipedia):
9 | "A reverse proxy is a type of proxy server that retrieves resources on behalf of a
10 | client from one or more servers. These resources are then returned to the client as
11 | though it originated from the reverse proxy itself."
12 |
13 | For example you can configure a reverse proxy such that when people go to
14 | www.my_personal_domain.com the proxy will show them results from google.com
15 |
16 | (assuming that you own my_personal_domain.com)
17 |
18 | The program is written in Java and is deployed as a standard WAR file to any
19 | servlet container, e.g. Tomcat.
20 |
21 |
22 | HOW TO BUILD
23 | ------------
24 | The code is in the "project" directory as a Maven project.
25 | To build the project, run this command:
26 |
27 | mvn clean package
28 |
29 | This will produce target/rp.war
30 | You can deploy rp.war to and Java web server
31 |
32 |
33 | CONFIGURATION
34 | -------------
35 | To configure the URLs for the reverse proxy, edit the file:
36 |
37 | project/src/main/resources/reverseProxy.properties
38 |
39 |
40 |
--------------------------------------------------------------------------------
/project/src/main/java/com/tek271/reverseProxy/config/Config.java:
--------------------------------------------------------------------------------
1 | /*
2 | This file is part of Tek271 Reverse Proxy Server.
3 |
4 | Tek271 Reverse Proxy Server is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU Lesser General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | Tek271 Reverse Proxy Server is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU Lesser General Public License for more details.
13 |
14 | You should have received a copy of the GNU Lesser General Public License
15 | along with Tek271 Reverse Proxy Server. If not, see http://www.gnu.org/licenses/
16 | */
17 | package com.tek271.reverseProxy.config;
18 |
19 | import com.tek271.reverseProxy.model.Mappings;
20 |
21 | public enum Config {
22 | singleton;
23 |
24 | public final Mappings mappings;
25 | public final Properties properties;
26 |
27 | private Config() {
28 | properties= new Properties();
29 | mappings= properties.mappings;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/utils/RegexToolsTest.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.utils;
2 |
3 | import static org.junit.Assert.assertEquals;
4 |
5 | import java.util.List;
6 |
7 | import org.junit.Test;
8 |
9 | import com.google.common.collect.ImmutableList;
10 |
11 | public class RegexToolsTest {
12 |
13 | @Test
14 | public void testFindAll() {
15 | String pattern= "A+";
16 | String text= "A-AA-AAA-";
17 | List> found = RegexTools.findAll(pattern, text, true);
18 | assertEquals(3, found.size());
19 | List> expected= ImmutableList.of(
20 | Tuple3.tuple3(0, 1, "A"),
21 | Tuple3.tuple3(2, 4, "AA"),
22 | Tuple3.tuple3(5, 8, "AAA")
23 | );
24 | assertEquals(expected, found);
25 | }
26 |
27 | @Test
28 | public void testFindAllWithGroups() {
29 | String pattern= "\\bhref\\b\\s*=\\s*(\\S*?)[\\s>]";
30 | String text= "t";
31 | List> found = RegexTools.findAll(pattern, text, true);
32 | assertEquals(1, found.size());
33 | List> expected= ImmutableList.of(
34 | Tuple3.tuple3(9, 12, "a.c")
35 | );
36 | assertEquals(expected, found);
37 | }
38 |
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/model/MappingTest.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.model;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import org.junit.Test;
6 |
7 | public class MappingTest {
8 |
9 | @Test
10 | public void constructorShouldParseInput() {
11 | Mapping mapping = new Mapping("maps.google.com, localhost:8080, /rp/maps");
12 | assertEquals("maps.google.com", mapping.hiddenDomain);
13 | assertEquals("localhost:8080", mapping.proxyDomain);
14 | assertEquals("/rp/maps", mapping.proxyResource);
15 | }
16 |
17 | @SuppressWarnings("unused")
18 | @Test(expected=IllegalArgumentException.class)
19 | public void constructorShouldFailIfNoInput() {
20 | new Mapping("");
21 | }
22 |
23 | @SuppressWarnings("unused")
24 | @Test(expected=IllegalArgumentException.class)
25 | public void constructorShouldFailIfInvalid() {
26 | new Mapping("maps.google.com, localhost:8080");
27 | }
28 |
29 | @Test
30 | public void testMapProxyToHidden() {
31 | String proxyUrl= "http://localhost:8080/rp/maps/home";
32 | String actual= ModelFactory.MAPPING1.mapProxyToHidden(proxyUrl);
33 | String expected= "http://maps.google.com/home";
34 | assertEquals(expected, actual);
35 | }
36 |
37 | @Test
38 | public void testMapHiddenToProxy() {
39 | String hiddenUrl= "http://maps.google.com/home";
40 | String actual= ModelFactory.MAPPING1.mapHiddenToProxy(hiddenUrl);
41 | String expected= "http://localhost:8080/rp/maps/home";
42 | assertEquals(expected, actual);
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/project/src/main/java/com/tek271/reverseProxy/utils/TextTools.java:
--------------------------------------------------------------------------------
1 | /*
2 | This file is part of Tek271 Reverse Proxy Server.
3 |
4 | Tek271 Reverse Proxy Server is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU Lesser General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | Tek271 Reverse Proxy Server is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU Lesser General Public License for more details.
13 |
14 | You should have received a copy of the GNU Lesser General Public License
15 | along with Tek271 Reverse Proxy Server. If not, see http://www.gnu.org/licenses/
16 | */
17 | package com.tek271.reverseProxy.utils;
18 |
19 | import org.apache.commons.lang.StringUtils;
20 |
21 | public class TextTools {
22 |
23 | public static String dquoteAround(String value) {
24 | return '"' + value + '"';
25 | }
26 |
27 | public static boolean isQuote(char quote) {
28 | return quote=='\'' || quote=='"';
29 | }
30 |
31 | /** remove chars less than 32 and replace with space */
32 | public static String removeControlChars(String text) {
33 | StringBuilder sb= new StringBuilder();
34 | for (int i=0, n=StringUtils.length(text) ; iclick", target.translate("click"));
31 | assertEquals("click", target.translate("click"));
32 | assertEquals("click", target.translate("click"));
33 | assertEquals("click", target.translate("click"));
34 | assertEquals("click", target.translate("click"));
35 | assertEquals("helloclick", target.translate("helloclick"));
36 | assertEquals("helloclick", target.translate("helloclick"));
37 | assertEquals("helloclick", target.translate("helloclick"));
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/project/src/main/java/com/tek271/reverseProxy/model/RegexPair.java:
--------------------------------------------------------------------------------
1 | /*
2 | This file is part of Tek271 Reverse Proxy Server.
3 |
4 | Tek271 Reverse Proxy Server is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU Lesser General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | Tek271 Reverse Proxy Server is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU Lesser General Public License for more details.
13 |
14 | You should have received a copy of the GNU Lesser General Public License
15 | along with Tek271 Reverse Proxy Server. If not, see http://www.gnu.org/licenses/
16 | */
17 | package com.tek271.reverseProxy.model;
18 |
19 | import java.util.regex.Pattern;
20 |
21 | import com.google.common.base.Objects;
22 |
23 | public class RegexPair {
24 |
25 | public final Pattern tagPattern;
26 | public final Pattern attributePattern;
27 |
28 | private final String tagRegex;
29 | private final String attributeRegex;
30 |
31 | private RegexPair(String tagRegex, String attributeRegex) {
32 | tagPattern= Pattern.compile(tagRegex, Pattern.CASE_INSENSITIVE);
33 | attributePattern= Pattern.compile(attributeRegex, Pattern.CASE_INSENSITIVE);
34 |
35 | this.tagRegex= tagRegex;
36 | this.attributeRegex= attributeRegex;
37 | }
38 |
39 | public static RegexPair pair(String tagRegex, String attributeRegex) {
40 | return new RegexPair(tagRegex, attributeRegex);
41 | }
42 |
43 | @Override
44 | public String toString() {
45 | return Objects.toStringHelper(this)
46 | .add("tagRegex", tagRegex)
47 | .add("attributeRegex", attributeRegex)
48 | .toString();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/project/src/main/java/com/tek271/reverseProxy/model/Mappings.java:
--------------------------------------------------------------------------------
1 | /*
2 | This file is part of Tek271 Reverse Proxy Server.
3 |
4 | Tek271 Reverse Proxy Server is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU Lesser General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | Tek271 Reverse Proxy Server is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU Lesser General Public License for more details.
13 |
14 | You should have received a copy of the GNU Lesser General Public License
15 | along with Tek271 Reverse Proxy Server. If not, see http://www.gnu.org/licenses/
16 | */
17 | package com.tek271.reverseProxy.model;
18 |
19 | import static org.apache.commons.lang.StringUtils.*;
20 |
21 | import java.util.ArrayList;
22 |
23 |
24 | public class Mappings extends ArrayList {
25 |
26 | private static final long serialVersionUID = 1L;
27 |
28 |
29 | public static Mappings create(Mapping...mappings) {
30 | Mappings r= new Mappings();
31 | for (Mapping mapping: mappings) {
32 | r.add(mapping);
33 | }
34 | return r;
35 | }
36 |
37 | private static final String SEP = "://";
38 |
39 |
40 | public Mapping findByProxyUrl(String proxyUrl) {
41 | String noProtocol= substringAfter(proxyUrl, SEP);
42 | for (Mapping mapping: this) {
43 | if (startsWithIgnoreCase(noProtocol, mapping.proxy)) return mapping;
44 | }
45 | return null;
46 | }
47 |
48 | public Mapping findByHiddenUrl(String hiddenUrl) {
49 | String noProtocol= substringAfter(hiddenUrl, SEP);
50 | for (Mapping mapping: this) {
51 | if (startsWithIgnoreCase(noProtocol, mapping.hiddenDomain)) return mapping;
52 | }
53 | return null;
54 | }
55 |
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/project/src/test/java/com/tek271/reverseProxy/text/TagTranslatorTest.java:
--------------------------------------------------------------------------------
1 | package com.tek271.reverseProxy.text;
2 |
3 | import static org.junit.Assert.assertEquals;
4 |
5 | import java.util.regex.Pattern;
6 |
7 | import org.junit.Test;
8 |
9 | import com.tek271.reverseProxy.model.ModelFactory;
10 |
11 | public class TagTranslatorTest {
12 | // Will find all tags
13 | private static final String ANCHORE_REGEX= "<[aA]\\b(\"[^\"]*\"|'[^']*'|[^'\">])*>"; // match tags
14 | private static final Pattern ANCHORE_PATTERN= Pattern.compile(ANCHORE_REGEX);
15 |
16 | private static final String HREF_REGEX= "\\bhref\\b\\s*=\\s*(\\S*?)[\\s>]"; // match href value
17 | private static final Pattern HREF_PATTERN= Pattern.compile(HREF_REGEX, Pattern.CASE_INSENSITIVE);
18 |
19 | TagTranslator target= new TagTranslator(ModelFactory.MAPPING1, ANCHORE_PATTERN, HREF_PATTERN);
20 |
21 | @Test
22 | public void testTranslateAnchor() {
23 | assertEquals("", target.translateTag(""));
24 | assertEquals("", target.translateTag(""));
25 | assertEquals("", target.translateTag(""));
26 | assertEquals("", target.translateTag(""));
27 | assertEquals("", target.translateTag(""));
28 | assertEquals("", target.translateTag(""));
29 | assertEquals("", target.translateTag(""));
30 | }
31 |
32 | @Test public void testAttributeValue() {
33 | assertEquals("abc", target.attributeValue("t").e3);
34 | assertEquals("abc", target.attributeValue("t").e3);
35 | assertEquals("abc", target.attributeValue("t").e3);
36 | assertEquals("abc", target.attributeValue("t").e3);
37 | assertEquals("abc", target.attributeValue("t").e3);
38 | assertEquals("abc", target.attributeValue("t").e3);
39 | assertEquals("abc", target.attributeValue(" mapped = mapFullUrlHiddenToProxy(url);
45 | if (mapped.isNull()) return url;
46 | return mapped.e2;
47 | }
48 |
49 | private static Tuple2 mapFullUrlHiddenToProxy(String fullHiddenUrl) {
50 | Mapping mapping= Config.singleton.mappings.findByHiddenUrl(fullHiddenUrl);
51 | if (mapping==null) return Tuple2.nil();
52 |
53 | String newUrl= mapping.mapHiddenToProxy(fullHiddenUrl);
54 | return Tuple2.tuple2(mapping, newUrl);
55 | }
56 |
57 |
58 | public static Tuple2 mapFullUrlProxyToHidden(String fullProxyUrl) {
59 | Mapping mapping= Config.singleton.mappings.findByProxyUrl(fullProxyUrl);
60 | if (mapping==null) return Tuple2.nil();
61 |
62 | String newUrl= mapping.mapProxyToHidden(fullProxyUrl);
63 | return Tuple2.tuple2(mapping, newUrl);
64 | }
65 |
66 | private static boolean isFullUrl(String url) {
67 | return startsWithIgnoreCase(url, "http://");
68 | }
69 |
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/project/src/main/java/com/tek271/reverseProxy/text/TextTranslator.java:
--------------------------------------------------------------------------------
1 | /*
2 | This file is part of Tek271 Reverse Proxy Server.
3 |
4 | Tek271 Reverse Proxy Server is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU Lesser General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | Tek271 Reverse Proxy Server is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU Lesser General Public License for more details.
13 |
14 | You should have received a copy of the GNU Lesser General Public License
15 | along with Tek271 Reverse Proxy Server. If not, see http://www.gnu.org/licenses/
16 | */
17 | package com.tek271.reverseProxy.text;
18 |
19 | import java.util.List;
20 |
21 | import com.google.common.collect.*;
22 | import com.tek271.reverseProxy.model.*;
23 | import static com.tek271.reverseProxy.model.RegexPair.*;
24 |
25 | public class TextTranslator {
26 |
27 | private static final RegexPair ANCHOR= pair("<[aA]\\b(\"[^\"]*\"|'[^']*'|[^'\">])*>", // match tags
28 | "\\bhref\\b\\s*=\\s*(\\S*?)[\\s>]"); // match href attribute value
29 |
30 | private static final RegexPair IMAGE= pair("
])*>", // match
tags
31 | "\\bsrc\\b\\s*=\\s*(\\S*?)[\\s>]"); // match src attribute value
32 |
33 | private static final RegexPair LINK= pair("])*>", // match tags
34 | "\\bhref\\b\\s*=\\s*(\\S*?)[\\s>]"); // match href attribute value
35 |
36 | private static final RegexPair SCRIPT= pair("