From: gotty Date: Sun, 12 Jan 2020 00:25:47 +0000 (+0300) Subject: [LIB-13] Add classes to work with proxy servers X-Git-Url: https://git.hedgecode.org/?p=chesshog-scanner.git;a=commitdiff_plain;h=573d7b45d42661d2798193bc58b588a99ab94451 [LIB-13] Add classes to work with proxy servers --- diff --git a/src/main/java/org/hedgecode/chess/scanner/proxy/Proxy.java b/src/main/java/org/hedgecode/chess/scanner/proxy/Proxy.java new file mode 100644 index 0000000..7d75a72 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/proxy/Proxy.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.proxy; + +import org.hedgecode.chess.scanner.ChessHogScannerConstants; +import org.hedgecode.chess.scanner.proxy.type.HTTPSetter; +import org.hedgecode.chess.scanner.proxy.type.SOCKSSetter; + +/** + * Setter for proxy settings. + * + * @author Dmitry Samoshin aka gotty + */ +public enum Proxy { + + UNDEFINED ( ChessHogScannerConstants.PROXY_UNDEFINED, null ), + HTTP ( ChessHogScannerConstants.PROXY_HTTP, new HTTPSetter() ), + SOCKS ( ChessHogScannerConstants.PROXY_SOCKS, new SOCKSSetter() ); + + private String name; + private ProxySetter setter; + + Proxy(String name, ProxySetter setter) { + this.name = name; + this.setter = setter; + } + + private ProxySetter getSetter() { + return setter; + } + + public static Proxy byName(String name) { + for (Proxy proxy : Proxy.values()) { + if (proxy.name.equals(name)) + return proxy; + } + return UNDEFINED; + } + + public static void setProxy(ProxyParams proxyParams) { + setProxy( + proxyParams.getType(), + proxyParams.getHost(), proxyParams.getPort(), + proxyParams.getUser(), proxyParams.getPassword() + ); + } + + public static void setProxy(Proxy type, String host, String port) { + setProxy(type, host, port, null, null); + } + + public static void setProxy(Proxy type, String host, String port, String user, String password) { + ProxySetter proxySetter = type.getSetter(); + if (proxySetter != null) { + proxySetter.setProxyHost(host); + proxySetter.setProxyPort(port); + if (user != null) { + proxySetter.setProxyAuth(user, password); + } + } + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/proxy/ProxyParams.java b/src/main/java/org/hedgecode/chess/scanner/proxy/ProxyParams.java new file mode 100644 index 0000000..6dba49b --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/proxy/ProxyParams.java @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.proxy; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Aggregate the proxy parameters for {@link Proxy}. + * + * @author Dmitry Samoshin aka gotty + */ +public class ProxyParams { + + private static final Pattern PROXY_SERVER_PATTERN = Pattern.compile( + "^([^:]+):([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}):([0-9]{1,5})$" + ); + + private static final Pattern PROXY_AUTH_PATTERN = Pattern.compile( + "^([^:]+):(.+)$" + ); + + private Proxy type; + + private String host; + private String port; + + private String user = null; + private String password = null; + + public ProxyParams(String proxyServer, String proxyAuth) { + + Matcher matcher = PROXY_SERVER_PATTERN.matcher(proxyServer); + if (matcher.find()) { + type = Proxy.byName(matcher.group(1)); + host = matcher.group(2); + port = matcher.group(3); + } + + if (proxyAuth != null && !proxyAuth.isEmpty()) { + matcher = PROXY_AUTH_PATTERN.matcher(proxyAuth); + if (matcher.find()) { + user = matcher.group(1); + password = matcher.group(2); + } + } + } + + public ProxyParams(Proxy type, String host, String port) { + this.type = type; + this.host = host; + this.port = port; + } + + public ProxyParams(Proxy type, String host, String port, String user, String password) { + this.type = type; + this.host = host; + this.port = port; + this.user = user; + this.password = password; + } + + public Proxy getType() { + return type; + } + + public String getHost() { + return host; + } + + public String getPort() { + return port; + } + + public String getUser() { + return user; + } + + public String getPassword() { + return password; + } + + @Override + public String toString() { + return String.format( + "[%s] %s:%s%s", + type, + host, + port, + user != null ? " (with auth)" : "" + ); + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/proxy/ProxySetter.java b/src/main/java/org/hedgecode/chess/scanner/proxy/ProxySetter.java new file mode 100644 index 0000000..f809c90 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/proxy/ProxySetter.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.proxy; + +/** + * Proxy server setter interface. + * + * @author Dmitry Samoshin aka gotty + */ +public interface ProxySetter { + + void setProxyHost(String proxyHost); + + void setProxyPort(String proxyPort); + + void setProxyAuth(String user, String password); + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/proxy/type/HTTPSetter.java b/src/main/java/org/hedgecode/chess/scanner/proxy/type/HTTPSetter.java new file mode 100644 index 0000000..188aa7c --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/proxy/type/HTTPSetter.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.proxy.type; + +import java.net.Authenticator; + +import org.hedgecode.chess.scanner.proxy.ProxySetter; + +/** + * HTTP/HTTPS proxy server setter. + * + * @author Dmitry Samoshin aka gotty + */ +public class HTTPSetter implements ProxySetter { + + @Override + public void setProxyHost(String proxyHost) { + System.setProperty("http.proxyHost", proxyHost); + System.setProperty("https.proxyHost", proxyHost); + } + + @Override + public void setProxyPort(String proxyPort) { + System.setProperty("http.proxyPort", proxyPort); + System.setProperty("https.proxyPort", proxyPort); + } + + @Override + public void setProxyAuth(String user, String password) { + Authenticator.setDefault( + new ProxyAuthenticator(user, password) + ); + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/proxy/type/ProxyAuthenticator.java b/src/main/java/org/hedgecode/chess/scanner/proxy/type/ProxyAuthenticator.java new file mode 100644 index 0000000..04744ea --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/proxy/type/ProxyAuthenticator.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.proxy.type; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; + +/** + * Proxy server authenticator by user/password. + * + * @author Dmitry Samoshin aka gotty + */ +public class ProxyAuthenticator extends Authenticator { + + private PasswordAuthentication passwordAuthentication; + + ProxyAuthenticator(String user, String password) { + passwordAuthentication = new PasswordAuthentication( + user, password == null ? new char[]{} : password.toCharArray() + ); + } + + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return passwordAuthentication; + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/proxy/type/SOCKSSetter.java b/src/main/java/org/hedgecode/chess/scanner/proxy/type/SOCKSSetter.java new file mode 100644 index 0000000..323065a --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/proxy/type/SOCKSSetter.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.proxy.type; + +import java.net.Authenticator; + +import org.hedgecode.chess.scanner.proxy.ProxySetter; + +/** + * SOCKS proxy server setter. + * + * @author Dmitry Samoshin aka gotty + */ +public class SOCKSSetter implements ProxySetter { + + @Override + public void setProxyHost(String proxyHost) { + System.setProperty("socksProxyHost", proxyHost); + } + + @Override + public void setProxyPort(String proxyPort) { + System.setProperty("socksProxyPort", proxyPort); + } + + @Override + public void setProxyAuth(String user, String password) { + System.setProperty("java.net.socks.username", user); + System.setProperty("java.net.socks.password", password); + Authenticator.setDefault( + new ProxyAuthenticator(user, password) + ); + } + +}