forked from j62/ctbrec
1
0
Fork 0

Add proxy support for the external browser

This commit is contained in:
0xboobface 2019-01-19 15:30:57 +01:00
parent ce839ee222
commit e2d3ef264e
4 changed files with 53 additions and 8 deletions

View File

@ -14,10 +14,11 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.OS; import ctbrec.OS;
import ctbrec.Settings.ProxyType;
import ctbrec.io.StreamRedirectThread; import ctbrec.io.StreamRedirectThread;
// TODO implement proxy support
public class ExternalBrowser implements AutoCloseable { public class ExternalBrowser implements AutoCloseable {
private static final transient Logger LOG = LoggerFactory.getLogger(ExternalBrowser.class); private static final transient Logger LOG = LoggerFactory.getLogger(ExternalBrowser.class);
private static final ExternalBrowser INSTANCE = new ExternalBrowser(); private static final ExternalBrowser INSTANCE = new ExternalBrowser();
@ -35,22 +36,27 @@ public class ExternalBrowser implements AutoCloseable {
return INSTANCE; return INSTANCE;
} }
public void run(String jsonConfig, Consumer<String> messageListener) throws InterruptedException, IOException { public void run(JSONObject jsonConfig, Consumer<String> messageListener) throws InterruptedException, IOException {
LOG.debug("Running browser with config {}", jsonConfig); LOG.debug("Running browser with config {}", jsonConfig);
lock.lock(); lock.lock();
try { try {
stopped = false; stopped = false;
this.messageListener = messageListener; this.messageListener = messageListener;
addProxyConfig(jsonConfig.getJSONObject("config"));
p = new ProcessBuilder(OS.getBrowserCommand()).start(); p = new ProcessBuilder(OS.getBrowserCommand()).start();
new StreamRedirectThread(p.getInputStream(), System.err); new StreamRedirectThread(p.getInputStream(), System.err);
new StreamRedirectThread(p.getErrorStream(), System.err); new StreamRedirectThread(p.getErrorStream(), System.err);
LOG.debug("Browser started"); LOG.debug("Browser started");
connectToRemoteControlSocket(); connectToRemoteControlSocket();
if(LOG.isTraceEnabled()) {
LOG.debug("Connected to remote control server. Sending config {}", jsonConfig); LOG.debug("Connected to remote control server. Sending config {}", jsonConfig);
} else {
out.write(jsonConfig.getBytes("utf-8")); LOG.debug("Connected to remote control server. Sending config");
}
out.write(jsonConfig.toString().getBytes("utf-8"));
out.write('\n'); out.write('\n');
out.flush(); out.flush();
@ -140,4 +146,43 @@ public class ExternalBrowser implements AutoCloseable {
} }
} }
} }
private void addProxyConfig(JSONObject jsonConfig) {
ProxyType proxyType = Config.getInstance().getSettings().proxyType;
switch (proxyType) {
case HTTP:
JSONObject proxy = new JSONObject();
proxy.put("address",
"http=" + Config.getInstance().getSettings().proxyHost + ':' + Config.getInstance().getSettings().proxyPort
+ ";https=" + Config.getInstance().getSettings().proxyHost + ':' + Config.getInstance().getSettings().proxyPort);
if(Config.getInstance().getSettings().proxyUser != null && !Config.getInstance().getSettings().proxyUser.isEmpty()) {
String username = Config.getInstance().getSettings().proxyUser;
String password = Config.getInstance().getSettings().proxyPassword;
proxy.put("user", username);
proxy.put("password", password);
}
jsonConfig.put("proxy", proxy);
break;
case SOCKS4:
proxy = new JSONObject();
proxy.put("address", "socks4://" + Config.getInstance().getSettings().proxyHost + ':' + Config.getInstance().getSettings().proxyPort);
jsonConfig.put("proxy", proxy);
break;
case SOCKS5:
proxy = new JSONObject();
proxy.put("address", "socks5://" + Config.getInstance().getSettings().proxyHost + ':' + Config.getInstance().getSettings().proxyPort);
if(Config.getInstance().getSettings().proxyUser != null && !Config.getInstance().getSettings().proxyUser.isEmpty()) {
String username = Config.getInstance().getSettings().proxyUser;
String password = Config.getInstance().getSettings().proxyPassword;
proxy.put("user", username);
proxy.put("password", password);
}
jsonConfig.put("proxy", proxy);
break;
case DIRECT:
default:
// nothing to do here
break;
}
}
} }

View File

@ -38,7 +38,7 @@ public class BongaCamsElectronLoginDialog {
config.put("h", 480); config.put("h", 480);
JSONObject msg = new JSONObject(); JSONObject msg = new JSONObject();
msg.put("config", config); msg.put("config", config);
browser.run(msg.toString(), msgHandler); browser.run(msg, msgHandler);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new IOException("Couldn't wait for login dialog", e); throw new IOException("Couldn't wait for login dialog", e);
} finally { } finally {

View File

@ -38,7 +38,7 @@ public class Cam4ElectronLoginDialog {
config.put("h", 640); config.put("h", 640);
JSONObject msg = new JSONObject(); JSONObject msg = new JSONObject();
msg.put("config", config); msg.put("config", config);
browser.run(msg.toString(), msgHandler); browser.run(msg, msgHandler);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new IOException("Couldn't wait for login dialog", e); throw new IOException("Couldn't wait for login dialog", e);
} finally { } finally {

View File

@ -34,7 +34,7 @@ public class LiveJasminElectronLoginDialog {
config.put("h", 720); config.put("h", 720);
JSONObject msg = new JSONObject(); JSONObject msg = new JSONObject();
msg.put("config", config); msg.put("config", config);
browser.run(msg.toString(), msgHandler); browser.run(msg, msgHandler);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new IOException("Couldn't wait for login dialog", e); throw new IOException("Couldn't wait for login dialog", e);
} catch (IOException e) { } catch (IOException e) {