refactor for socks

This commit is contained in:
sirjonasxx 2020-06-01 11:24:05 +02:00
parent 58e455142a
commit 2bbdcfaf28
10 changed files with 71 additions and 78 deletions

View File

@ -26,11 +26,13 @@ public class NormalProxyProvider extends ProxyProvider {
private volatile List<HProxy> potentialProxies = new ArrayList<>();
private volatile HProxy proxy = null;
private boolean useSocks;
public NormalProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, List<String> potentialHosts) {
public NormalProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, List<String> potentialHosts, boolean useSocks) {
super(proxySetter, stateSetter, hConnection);
this.potentialHosts = potentialHosts;
this.useSocks = useSocks;
}
@ -107,9 +109,29 @@ public class NormalProxyProvider extends ProxyProvider {
new Thread(() -> {
try {
Socket server = new Socket(proxy.getActual_domain(), proxy.getActual_port());
Socket server;
if (!useSocks) {
server = new Socket(proxy.getActual_domain(), proxy.getActual_port());
}
else {
SocksConfiguration configuration = ProxyProviderFactory.getSocksConfig();
if (configuration == null) {
showInvalidConnectionError();
abort();
return;
}
server = configuration.createSocket();
server.connect(new InetSocketAddress(proxy.getActual_domain(), proxy.getActual_port()), 1200);
}
startProxyThread(client, server, proxy);
} catch (InterruptedException | IOException e) {
} catch (SocketException | SocketTimeoutException e) {
// should only happen when SOCKS configured badly
showInvalidConnectionError();
abort();
e.printStackTrace();
}
catch (InterruptedException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

View File

@ -9,6 +9,10 @@ import gearth.protocol.memory.Rc4Obtainer;
import gearth.protocol.packethandler.IncomingPacketHandler;
import gearth.protocol.packethandler.OutgoingPacketHandler;
import gearth.protocol.packethandler.PacketHandler;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.Region;
import java.io.IOException;
import java.net.Socket;
@ -113,5 +117,13 @@ public abstract class ProxyProvider {
stateSetter.setState(HState.NOT_CONNECTED);
}
protected void showInvalidConnectionError() {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR, "You entered invalid connection information, G-Earth could not connect", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.setResizable(false);
alert.show();
});
}
}

View File

@ -6,9 +6,7 @@ import gearth.protocol.HConnection;
import gearth.protocol.connection.HProxySetter;
import gearth.protocol.connection.HStateSetter;
import gearth.protocol.connection.proxy.unix.LinuxRawIpProxyProvider;
import gearth.protocol.connection.proxy.unix.LinuxRawIpSocksProxyProvider;
import gearth.protocol.connection.proxy.windows.WindowsRawIpProxyProvider;
import gearth.protocol.connection.proxy.windows.WindowsRawIpSocksProxyProvider;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
@ -95,11 +93,11 @@ public class ProxyProviderFactory {
if (hostIsIpAddress(domain)) {
if (OSValidator.isWindows()) {
if (WindowsRawIpProxyProvider.isNoneConnected(domain) &&
(!socksConfig.useSocks() || socksConfig.dontUseFirstTime()) ) {
return new WindowsRawIpProxyProvider(proxySetter, stateSetter, hConnection, domain, port);
(!socksConfig.useSocks() || socksConfig.onlyUseIfNeeded()) ) {
return new WindowsRawIpProxyProvider(proxySetter, stateSetter, hConnection, domain, port, false);
}
else if (socksConfig.useSocks()) {
return new WindowsRawIpSocksProxyProvider(proxySetter, stateSetter, hConnection, domain, port);
return new WindowsRawIpProxyProvider(proxySetter, stateSetter, hConnection, domain, port, true);
}
Platform.runLater(() -> {
@ -114,12 +112,7 @@ public class ProxyProviderFactory {
return null;
}
else if (OSValidator.isUnix()) {
if (!socksConfig.useSocks() || socksConfig.dontUseFirstTime()) {
return new LinuxRawIpProxyProvider(proxySetter, stateSetter, hConnection, domain, port);
}
else {
return new LinuxRawIpSocksProxyProvider(proxySetter, stateSetter, hConnection, domain, port);
}
return new LinuxRawIpProxyProvider(proxySetter, stateSetter, hConnection, domain, port, socksConfig.useSocks() && !socksConfig.onlyUseIfNeeded());
}
return null;
@ -132,7 +125,7 @@ public class ProxyProviderFactory {
}
private ProxyProvider provide(List<String> potentialHosts) {
return new NormalProxyProvider(proxySetter, stateSetter, hConnection, potentialHosts);
return new NormalProxyProvider(proxySetter, stateSetter, hConnection, potentialHosts, socksConfig.useSocks() && !socksConfig.onlyUseIfNeeded());
}
public static void setSocksConfig(SocksConfiguration configuration) {

View File

@ -1,9 +1,23 @@
package gearth.protocol.connection.proxy;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketException;
public interface SocksConfiguration {
boolean useSocks();
int getSocksPort();
String getSocksHost();
boolean dontUseFirstTime();
boolean onlyUseIfNeeded();
default Socket createSocket() throws SocketException {
Proxy socks = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(getSocksHost(), getSocksPort()));
Socket server = new Socket(socks);
server.setSoTimeout(1200);
return server;
}
}

View File

@ -28,12 +28,13 @@ public class LinuxRawIpProxyProvider extends ProxyProvider {
protected IpMapper ipMapper = IpMapperFactory.get();
protected HProxy proxy = null;
protected volatile boolean useSocks = false;
private boolean useSocks;
public LinuxRawIpProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port) {
public LinuxRawIpProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port, boolean useSocks) {
super(proxySetter, stateSetter, hConnection);
this.input_host = input_host;
this.input_port = input_port;
this.useSocks = useSocks;
}
@Override
@ -164,7 +165,7 @@ public class LinuxRawIpProxyProvider extends ProxyProvider {
}
}
private void createSocksProxyThread(Socket client) throws SocketException, InterruptedException {
private void createSocksProxyThread(Socket client) throws SocketException {
SocksConfiguration configuration = ProxyProviderFactory.getSocksConfig();
if (configuration == null) {
@ -174,33 +175,19 @@ public class LinuxRawIpProxyProvider extends ProxyProvider {
return;
}
Proxy socks = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(configuration.getSocksHost(), configuration.getSocksPort()));
Socket server = new Socket(socks);
server.setSoTimeout(1200);
Socket server = configuration.createSocket();
try {
server.connect(new InetSocketAddress(proxy.getActual_domain(), proxy.getActual_port()), 1200);
startProxyThread(client, server, proxy);
}
catch (SocketTimeoutException e) {
catch (Exception e) {
maybeRemoveMapping();
stateSetter.setState(HState.NOT_CONNECTED);
showInvalidConnectionError();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void showInvalidConnectionError() {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR, "You entered invalid connection information, G-Earth could not connect", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.setResizable(false);
alert.show();
});
}
protected void maybeAddMapping() {
ipMapper.enable();
ipMapper.addMapping(proxy.getActual_domain());

View File

@ -1,12 +0,0 @@
package gearth.protocol.connection.proxy.unix;
import gearth.protocol.HConnection;
import gearth.protocol.connection.HProxySetter;
import gearth.protocol.connection.HStateSetter;
public class LinuxRawIpSocksProxyProvider extends LinuxRawIpProxyProvider {
public LinuxRawIpSocksProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port) {
super(proxySetter, stateSetter, hConnection, input_host, input_port);
useSocks = true;
}
}

View File

@ -29,8 +29,8 @@ public class WindowsRawIpProxyProvider extends LinuxRawIpProxyProvider {
private boolean hasMapped = false;
public WindowsRawIpProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port) {
super(proxySetter, stateSetter, hConnection, input_host, input_port);
public WindowsRawIpProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port, boolean useSocks) {
super(proxySetter, stateSetter, hConnection, input_host, input_port, useSocks);
}

View File

@ -1,22 +0,0 @@
package gearth.protocol.connection.proxy.windows;
import gearth.protocol.HConnection;
import gearth.protocol.connection.HProxySetter;
import gearth.protocol.connection.HState;
import gearth.protocol.connection.HStateSetter;
import gearth.protocol.connection.proxy.ProxyProviderFactory;
import gearth.protocol.connection.proxy.SocksConfiguration;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class WindowsRawIpSocksProxyProvider extends WindowsRawIpProxyProvider {
public WindowsRawIpSocksProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port) {
super(proxySetter, stateSetter, hConnection, input_host, input_port);
useSocks = true;
}
}

View File

@ -5,7 +5,6 @@ import gearth.protocol.HConnection;
import gearth.protocol.connection.HState;
import gearth.protocol.connection.proxy.ProxyProviderFactory;
import gearth.protocol.connection.proxy.SocksConfiguration;
import gearth.protocol.connection.proxy.windows.WindowsRawIpSocksProxyProvider;
import gearth.ui.SubForm;
import gearth.ui.info.InfoController;
import javafx.scene.control.*;
@ -41,7 +40,7 @@ public class ExtraController extends SubForm implements SocksConfiguration {
public GridPane grd_socksInfo;
public TextField txt_socksPort;
public TextField txt_socksIp;
public CheckBox cbx_ignoreSocksOnce;
public CheckBox cbx_socksUseIfNeeded;
public void initialize() {
@ -57,7 +56,7 @@ public class ExtraController extends SubForm implements SocksConfiguration {
JSONObject socksInitValue = Cacher.getCacheContents().getJSONObject(SOCKS_CACHE_KEY);
txt_socksIp.setText(socksInitValue.getString(SOCKS_IP));
txt_socksPort.setText(socksInitValue.getString(SOCKS_PORT));
cbx_ignoreSocksOnce.setSelected(socksInitValue.getBoolean(IGNORE_ONCE));
cbx_socksUseIfNeeded.setSelected(socksInitValue.getBoolean(IGNORE_ONCE));
}
cbx_debug.selectedProperty().addListener(observable -> HConnection.DEBUG = cbx_debug.isSelected());
@ -93,7 +92,7 @@ public class ExtraController extends SubForm implements SocksConfiguration {
JSONObject jsonObject = new JSONObject();
jsonObject.put(SOCKS_IP, txt_socksIp.getText());
jsonObject.put(SOCKS_PORT, txt_socksPort.getText());
jsonObject.put(IGNORE_ONCE, cbx_ignoreSocksOnce.isSelected());
jsonObject.put(IGNORE_ONCE, cbx_socksUseIfNeeded.isSelected());
Cacher.put(SOCKS_CACHE_KEY, jsonObject);
}
@ -127,7 +126,7 @@ public class ExtraController extends SubForm implements SocksConfiguration {
}
@Override
public boolean dontUseFirstTime() {
return cbx_ignoreSocksOnce.isSelected();
public boolean onlyUseIfNeeded() {
return cbx_socksUseIfNeeded.isSelected();
}
}

View File

@ -66,7 +66,7 @@
<Insets left="5.0" />
</padding>
<children>
<CheckBox fx:id="cbx_ignoreSocksOnce" mnemonicParsing="false" selected="true" text="Ignore for first connection" GridPane.rowIndex="1" />
<CheckBox fx:id="cbx_socksUseIfNeeded" mnemonicParsing="false" selected="true" text="Only use if needed" GridPane.rowIndex="1" />
<GridPane prefHeight="119.0" prefWidth="259.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="159.0" minWidth="10.0" prefWidth="68.0" />