refactor stuff for linux raw ip and socks support

This commit is contained in:
sirjonasxx 2020-05-19 11:06:45 +02:00
parent 54b62636e7
commit a8ce413ef8
8 changed files with 281 additions and 194 deletions

View File

@ -5,10 +5,9 @@ import gearth.protocol.connection.HProxy;
import gearth.protocol.connection.HState;
import gearth.protocol.connection.proxy.ProxyProvider;
import gearth.protocol.connection.proxy.ProxyProviderFactory;
import gearth.protocol.connection.proxy.RawIpProxyProvider;
import gearth.protocol.connection.proxy.windows.WindowsRawIpProxyProvider;
import java.io.IOException;
import java.net.UnknownHostException;
public class HConnection {
@ -155,7 +154,7 @@ public class HConnection {
}
public boolean isRawIpMode() {
return proxyProvider != null && proxyProvider instanceof RawIpProxyProvider;
return proxyProvider != null && proxyProvider instanceof WindowsRawIpProxyProvider;
}
public ProxyProvider getProxyProvider() {

View File

@ -5,6 +5,10 @@ import gearth.misc.OSValidator;
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;
@ -17,6 +21,7 @@ import java.util.List;
public class ProxyProviderFactory {
public static final String HOTELS_CACHE_KEY = "hotelsConnectionInfo";
private static SocksConfiguration socksConfig = null;
public static List<String> autoDetectHosts;
static {
@ -88,18 +93,18 @@ public class ProxyProviderFactory {
}
if (hostIsIpAddress(domain)) {
SocksConfiguration config = SocksProxyProvider.getSocksConfig();
if (RawIpProxyProvider.isNoneConnected(domain) &&
(!config.useSocks() || config.dontUseFirstTime()) ) {
return new RawIpProxyProvider(proxySetter, stateSetter, hConnection, domain, port);
if (OSValidator.isWindows()) {
if (WindowsRawIpProxyProvider.isNoneConnected(domain) &&
(!socksConfig.useSocks() || socksConfig.dontUseFirstTime()) ) {
return new WindowsRawIpProxyProvider(proxySetter, stateSetter, hConnection, domain, port);
}
else if (config.useSocks()) {
return new SocksProxyProvider(proxySetter, stateSetter, hConnection, domain, port);
else if (socksConfig.useSocks()) {
return new WindowsRawIpSocksProxyProvider(proxySetter, stateSetter, hConnection, domain, port);
}
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR, "G-Earth is already connected to this hotel. " +
"Due to current limitations you can only connect one session per hotel to G-Earth in Raw IP mode.\n\n" +
"Due to current limitations you can only connect one session per hotel to G-Earth in Raw IP mode on Windows.\n\n" +
"You can bypass this by using a SOCKS proxy [Extra -> Advanced -> SOCKS]", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.setResizable(false);
@ -108,6 +113,17 @@ 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 null;
}
else {
List<String> potentialHost = new ArrayList<>();
potentialHost.add(domain+":"+port);
@ -118,4 +134,12 @@ public class ProxyProviderFactory {
private ProxyProvider provide(List<String> potentialHosts) {
return new NormalProxyProvider(proxySetter, stateSetter, hConnection, potentialHosts);
}
public static void setSocksConfig(SocksConfiguration configuration) {
socksConfig = configuration;
}
public static SocksConfiguration getSocksConfig() {
return socksConfig;
}
}

View File

@ -1,57 +0,0 @@
package gearth.protocol.connection.proxy;
import gearth.protocol.HConnection;
import gearth.protocol.connection.HProxySetter;
import gearth.protocol.connection.HState;
import gearth.protocol.connection.HStateSetter;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class SocksProxyProvider extends RawIpProxyProvider {
private static SocksConfiguration socksConfig = null;
public SocksProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port) {
super(proxySetter, stateSetter, hConnection, input_host, input_port);
}
@Override
protected boolean onBeforeIpMapping() {
return true;
}
@Override
protected void createProxyThread(Socket client) throws IOException, InterruptedException {
if (socksConfig == null) {
maybeRemoveMapping();
stateSetter.setState(HState.NOT_CONNECTED);
showInvalidConnectionError();
return;
}
Proxy socks = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(socksConfig.getSocksHost(), socksConfig.getSocksPort()));
Socket server = new Socket(socks);
server.setSoTimeout(1200);
try {
server.connect(new InetSocketAddress(proxy.getActual_domain(), proxy.getActual_port()), 1200);
startProxyThread(client, server, proxy);
}
catch (SocketTimeoutException e) {
maybeRemoveMapping();
stateSetter.setState(HState.NOT_CONNECTED);
showInvalidConnectionError();
}
}
public static void setSocksConfig(SocksConfiguration configuration) {
socksConfig = configuration;
}
public static SocksConfiguration getSocksConfig() {
return socksConfig;
}
}

View File

@ -1,37 +1,36 @@
package gearth.protocol.connection.proxy;
package gearth.protocol.connection.proxy.unix;
import gearth.misc.Cacher;
import gearth.protocol.HConnection;
import gearth.protocol.connection.HProxy;
import gearth.protocol.connection.HProxySetter;
import gearth.protocol.connection.HState;
import gearth.protocol.connection.HStateSetter;
import gearth.protocol.connection.proxy.ProxyProvider;
import gearth.protocol.connection.proxy.ProxyProviderFactory;
import gearth.protocol.connection.proxy.SocksConfiguration;
import gearth.protocol.hostreplacer.ipmapping.IpMapper;
import gearth.protocol.hostreplacer.ipmapping.IpMapperFactory;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.Region;
import org.json.JSONObject;
import java.io.IOException;
import java.math.BigInteger;
import java.net.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.UUID;
public class RawIpProxyProvider extends ProxyProvider {
public class LinuxRawIpProxyProvider extends ProxyProvider {
private volatile String input_host;
private volatile int input_port;
private IpMapper ipMapper = IpMapperFactory.get();
private boolean hasMapped = false;
protected IpMapper ipMapper = IpMapperFactory.get();
protected HProxy proxy = null;
public RawIpProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port) {
protected volatile boolean useSocks = false;
public LinuxRawIpProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection, String input_host, int input_port) {
super(proxySetter, stateSetter, hConnection);
this.input_host = input_host;
this.input_port = input_port;
@ -97,9 +96,7 @@ public class RawIpProxyProvider extends ProxyProvider {
@Override
public void abort() {
stateSetter.setState(HState.ABORTING);
if (hasMapped) {
maybeRemoveMapping();
}
tryCloseProxy();
super.abort();
}
@ -107,19 +104,18 @@ public class RawIpProxyProvider extends ProxyProvider {
@Override
protected void onConnect() {
super.onConnect();
maybeRemoveMapping();
tryCloseProxy();
}
@Override
protected void onConnectEnd() {
if (hasMapped) {
maybeRemoveMapping();
}
tryCloseProxy();
super.onConnectEnd();
}
private void tryCloseProxy() {
protected void tryCloseProxy() {
if (proxy.getProxy_server() != null && !proxy.getProxy_server().isClosed()) {
try {
proxy.getProxy_server().close();
@ -133,6 +129,10 @@ public class RawIpProxyProvider extends ProxyProvider {
// returns false if fail
protected boolean onBeforeIpMapping() throws IOException, InterruptedException {
if (useSocks) {
return true;
}
preConnectedServerConnections = new LinkedList<>();
for (int i = 0; i < 3; i++) {
Socket s1 = new Socket();
@ -153,7 +153,10 @@ public class RawIpProxyProvider extends ProxyProvider {
}
protected void createProxyThread(Socket client) throws IOException, InterruptedException {
if (preConnectedServerConnections.isEmpty()) {
if (useSocks) {
createSocksProxyThread(client);
}
else if (preConnectedServerConnections.isEmpty()) {
if (HConnection.DEBUG) System.out.println("pre-made server connections ran out of stock");
}
else {
@ -161,6 +164,33 @@ public class RawIpProxyProvider extends ProxyProvider {
}
}
private void createSocksProxyThread(Socket client) throws SocketException, InterruptedException {
SocksConfiguration configuration = ProxyProviderFactory.getSocksConfig();
if (configuration == null) {
maybeRemoveMapping();
stateSetter.setState(HState.NOT_CONNECTED);
showInvalidConnectionError();
return;
}
Proxy socks = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(configuration.getSocksHost(), configuration.getSocksPort()));
Socket server = new Socket(socks);
server.setSoTimeout(1200);
try {
server.connect(new InetSocketAddress(proxy.getActual_domain(), proxy.getActual_port()), 1200);
startProxyThread(client, server, proxy);
}
catch (SocketTimeoutException 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);
@ -171,107 +201,14 @@ public class RawIpProxyProvider extends ProxyProvider {
}
private void maybeAddMapping() {
if (!hasMapped) {
hasMapped = true;
if (isNoneConnected()) {
protected void maybeAddMapping() {
ipMapper.enable();
ipMapper.addMapping(proxy.getActual_domain());
}
addMappingCache();
}
}
protected void maybeRemoveMapping() {
if (hasMapped) {
hasMapped = false;
removeMappingCache();
if (isNoneConnected()) {
ipMapper.deleteMapping(proxy.getActual_domain());
}
}
}
private static final UUID INSTANCE_ID = UUID.randomUUID();
private static final String RAWIP_CONNECTIONS = "rawip_connections";
// let other G-Earth instances know you're connected
private void addMappingCache() {
new Thread(() -> {
while (hasMapped) {
updateMappingCache();
try {
Thread.sleep(55000);
} catch (InterruptedException ignored) {}
}
}).start();
}
// checks if no G-Earth instances are connected
// every G-Earth instance is supposed to update if it's still connected every 60 seconds
private boolean isNoneConnected() {
return isNoneConnected(proxy.getActual_domain());
}
private void updateMappingCache() {
JSONObject connections = getCurrentConnectionsCache();
JSONObject instance = new JSONObject();
instance.put("timestamp", BigInteger.valueOf(System.currentTimeMillis()));
connections.put(INSTANCE_ID.toString(), instance);
saveCurrentConnectionsCache(connections);
}
private void removeMappingCache() {
JSONObject connections = getCurrentConnectionsCache();
connections.remove(INSTANCE_ID.toString());
saveCurrentConnectionsCache(connections);
}
private JSONObject getCurrentConnectionsCache() {
return getCurrentConnectionsCache(proxy.getActual_domain());
}
private void saveCurrentConnectionsCache(JSONObject connections) {
if (!Cacher.getCacheContents().has(proxy.getActual_domain())) {
Cacher.put(RAWIP_CONNECTIONS, new JSONObject());
}
JSONObject gearthConnections = Cacher.getCacheContents().getJSONObject(RAWIP_CONNECTIONS);
gearthConnections.put(proxy.getActual_domain(), connections);
Cacher.put(RAWIP_CONNECTIONS, gearthConnections);
}
static private JSONObject getCurrentConnectionsCache(String actual_host) {
if (!Cacher.getCacheContents().has(RAWIP_CONNECTIONS)) {
Cacher.put(RAWIP_CONNECTIONS, new JSONObject());
}
JSONObject gearthConnections = Cacher.getCacheContents().getJSONObject(RAWIP_CONNECTIONS);
if (!gearthConnections.has(actual_host)) {
gearthConnections.put(actual_host, new JSONObject());
Cacher.put(RAWIP_CONNECTIONS, gearthConnections);
}
return gearthConnections.getJSONObject(actual_host);
}
static boolean isNoneConnected(String actual_host) {
JSONObject connections = getCurrentConnectionsCache(actual_host);
BigInteger timeoutTimestamp = BigInteger.valueOf(System.currentTimeMillis() - 60000);
for (String key : connections.keySet()) {
JSONObject connection = connections.getJSONObject(key);
if (!key.equals(INSTANCE_ID.toString())) {
if (connection.getBigInteger("timestamp").compareTo(timeoutTimestamp) > 0) {
return false;
}
}
}
return true;
}
}

View File

@ -0,0 +1,12 @@
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

@ -0,0 +1,149 @@
package gearth.protocol.connection.proxy.windows;
import gearth.misc.Cacher;
import gearth.protocol.HConnection;
import gearth.protocol.connection.HProxy;
import gearth.protocol.connection.HProxySetter;
import gearth.protocol.connection.HState;
import gearth.protocol.connection.HStateSetter;
import gearth.protocol.connection.proxy.ProxyProvider;
import gearth.protocol.connection.proxy.unix.LinuxRawIpProxyProvider;
import gearth.protocol.hostreplacer.ipmapping.IpMapper;
import gearth.protocol.hostreplacer.ipmapping.IpMapperFactory;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.Region;
import org.json.JSONObject;
import java.io.IOException;
import java.math.BigInteger;
import java.net.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.UUID;
// windows raw ip proxy provider extends the Linux one with the exception that it does not want to close
// the IP redirect on connect
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);
}
@Override
protected void onConnect() {
//NOTE: DOES NOT CALL SUPER METHOD
stateSetter.setState(HState.CONNECTED);
tryCloseProxy();
}
protected void maybeAddMapping() {
if (!hasMapped) {
hasMapped = true;
if (isNoneConnected()) {
ipMapper.enable();
ipMapper.addMapping(proxy.getActual_domain());
}
addMappingCache();
}
}
protected void maybeRemoveMapping() {
if (hasMapped) {
hasMapped = false;
removeMappingCache();
if (isNoneConnected()) {
ipMapper.deleteMapping(proxy.getActual_domain());
}
}
}
private static final UUID INSTANCE_ID = UUID.randomUUID();
private static final String RAWIP_CONNECTIONS = "rawip_connections";
// let other G-Earth instances know you're connected
private void addMappingCache() {
new Thread(() -> {
while (hasMapped) {
updateMappingCache();
try {
Thread.sleep(55000);
} catch (InterruptedException ignored) {}
}
}).start();
}
// checks if no G-Earth instances are connected
// every G-Earth instance is supposed to update if it's still connected every 60 seconds
private boolean isNoneConnected() {
return isNoneConnected(proxy.getActual_domain());
}
private void updateMappingCache() {
JSONObject connections = getCurrentConnectionsCache();
JSONObject instance = new JSONObject();
instance.put("timestamp", BigInteger.valueOf(System.currentTimeMillis()));
connections.put(INSTANCE_ID.toString(), instance);
saveCurrentConnectionsCache(connections);
}
private void removeMappingCache() {
JSONObject connections = getCurrentConnectionsCache();
connections.remove(INSTANCE_ID.toString());
saveCurrentConnectionsCache(connections);
}
private JSONObject getCurrentConnectionsCache() {
return getCurrentConnectionsCache(proxy.getActual_domain());
}
private void saveCurrentConnectionsCache(JSONObject connections) {
if (!Cacher.getCacheContents().has(proxy.getActual_domain())) {
Cacher.put(RAWIP_CONNECTIONS, new JSONObject());
}
JSONObject gearthConnections = Cacher.getCacheContents().getJSONObject(RAWIP_CONNECTIONS);
gearthConnections.put(proxy.getActual_domain(), connections);
Cacher.put(RAWIP_CONNECTIONS, gearthConnections);
}
private static JSONObject getCurrentConnectionsCache(String actual_host) {
if (!Cacher.getCacheContents().has(RAWIP_CONNECTIONS)) {
Cacher.put(RAWIP_CONNECTIONS, new JSONObject());
}
JSONObject gearthConnections = Cacher.getCacheContents().getJSONObject(RAWIP_CONNECTIONS);
if (!gearthConnections.has(actual_host)) {
gearthConnections.put(actual_host, new JSONObject());
Cacher.put(RAWIP_CONNECTIONS, gearthConnections);
}
return gearthConnections.getJSONObject(actual_host);
}
public static boolean isNoneConnected(String actual_host) {
JSONObject connections = getCurrentConnectionsCache(actual_host);
BigInteger timeoutTimestamp = BigInteger.valueOf(System.currentTimeMillis() - 60000);
for (String key : connections.keySet()) {
JSONObject connection = connections.getJSONObject(key);
if (!key.equals(INSTANCE_ID.toString())) {
if (connection.getBigInteger("timestamp").compareTo(timeoutTimestamp) > 0) {
return false;
}
}
}
return true;
}
}

View File

@ -0,0 +1,22 @@
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

@ -3,8 +3,9 @@ package gearth.ui.extra;
import gearth.misc.Cacher;
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.SocksProxyProvider;
import gearth.protocol.connection.proxy.windows.WindowsRawIpSocksProxyProvider;
import gearth.ui.SubForm;
import gearth.ui.info.InfoController;
import javafx.scene.control.*;
@ -64,7 +65,7 @@ public class ExtraController extends SubForm implements SocksConfiguration {
cbx_useSocks.selectedProperty().addListener(observable -> grd_socksInfo.setDisable(!cbx_useSocks.isSelected()));
SocksProxyProvider.setSocksConfig(this);
ProxyProviderFactory.setSocksConfig(this);
}
@Override