Add proxy support for the external browser
This commit is contained in:
parent
ce839ee222
commit
e2d3ef264e
|
@ -14,10 +14,11 @@ import org.json.JSONObject;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ctbrec.Config;
|
||||
import ctbrec.OS;
|
||||
import ctbrec.Settings.ProxyType;
|
||||
import ctbrec.io.StreamRedirectThread;
|
||||
|
||||
// TODO implement proxy support
|
||||
public class ExternalBrowser implements AutoCloseable {
|
||||
private static final transient Logger LOG = LoggerFactory.getLogger(ExternalBrowser.class);
|
||||
private static final ExternalBrowser INSTANCE = new ExternalBrowser();
|
||||
|
@ -35,22 +36,27 @@ public class ExternalBrowser implements AutoCloseable {
|
|||
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);
|
||||
lock.lock();
|
||||
try {
|
||||
stopped = false;
|
||||
this.messageListener = messageListener;
|
||||
|
||||
addProxyConfig(jsonConfig.getJSONObject("config"));
|
||||
|
||||
p = new ProcessBuilder(OS.getBrowserCommand()).start();
|
||||
new StreamRedirectThread(p.getInputStream(), System.err);
|
||||
new StreamRedirectThread(p.getErrorStream(), System.err);
|
||||
LOG.debug("Browser started");
|
||||
|
||||
connectToRemoteControlSocket();
|
||||
if(LOG.isTraceEnabled()) {
|
||||
LOG.debug("Connected to remote control server. Sending config {}", jsonConfig);
|
||||
|
||||
out.write(jsonConfig.getBytes("utf-8"));
|
||||
} else {
|
||||
LOG.debug("Connected to remote control server. Sending config");
|
||||
}
|
||||
out.write(jsonConfig.toString().getBytes("utf-8"));
|
||||
out.write('\n');
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ public class BongaCamsElectronLoginDialog {
|
|||
config.put("h", 480);
|
||||
JSONObject msg = new JSONObject();
|
||||
msg.put("config", config);
|
||||
browser.run(msg.toString(), msgHandler);
|
||||
browser.run(msg, msgHandler);
|
||||
} catch (InterruptedException e) {
|
||||
throw new IOException("Couldn't wait for login dialog", e);
|
||||
} finally {
|
||||
|
|
|
@ -38,7 +38,7 @@ public class Cam4ElectronLoginDialog {
|
|||
config.put("h", 640);
|
||||
JSONObject msg = new JSONObject();
|
||||
msg.put("config", config);
|
||||
browser.run(msg.toString(), msgHandler);
|
||||
browser.run(msg, msgHandler);
|
||||
} catch (InterruptedException e) {
|
||||
throw new IOException("Couldn't wait for login dialog", e);
|
||||
} finally {
|
||||
|
|
|
@ -34,7 +34,7 @@ public class LiveJasminElectronLoginDialog {
|
|||
config.put("h", 720);
|
||||
JSONObject msg = new JSONObject();
|
||||
msg.put("config", config);
|
||||
browser.run(msg.toString(), msgHandler);
|
||||
browser.run(msg, msgHandler);
|
||||
} catch (InterruptedException e) {
|
||||
throw new IOException("Couldn't wait for login dialog", e);
|
||||
} catch (IOException e) {
|
||||
|
|
Loading…
Reference in New Issue