--- /dev/null
+/*
+ * 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);
+ }
+ }
+ }
+
+}
--- /dev/null
+/*
+ * 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)" : ""
+ );
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+
+}
--- /dev/null
+/*
+ * 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)
+ );
+ }
+
+}
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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)
+ );
+ }
+
+}