Skip to content

Commit

Permalink
Cmdline moved to be a config style object, rather than a runner
Browse files Browse the repository at this point in the history
  • Loading branch information
rain-on authored Mar 14, 2019
1 parent ad6bee9 commit a2d80d8
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,83 @@
*/
package tech.pegasys.ethfirewall;

import picocli.CommandLine.RunLast;
import tech.pegasys.ethfirewall.jsonrpcproxy.TransactionSigner;

import java.io.IOException;
import java.nio.file.Files;
import java.util.Optional;

import com.google.common.base.Charsets;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.client.WebClientOptions;
import org.apache.logging.log4j.core.config.Configurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.crypto.CipherException;

public final class EthFirewall {
private static final int ERROR_EXIT_CODE = 1;

private static final Logger LOG = LoggerFactory.getLogger(EthFirewall.class);

private final EthFirewallConfig config;
private final RunnerBuilder runnerBuilder;

public EthFirewall(final EthFirewallConfig config, final RunnerBuilder runnerBuilder) {
this.config = config;
this.runnerBuilder = runnerBuilder;
}

public void run() {
// set log level per CLI flags
System.out.println("Setting logging level to " + config.getLogLevel().name());
Configurator.setAllLevels("", config.getLogLevel());

Optional<String> password = readPasswordFromFile();
if (!password.isPresent()) {
LOG.error("Unable to extract password from supplied password file.");
return;
}

try {
runnerBuilder.setTransactionSigner(
TransactionSigner.createFrom(config.getKeyPath().toFile(), password.get()));
runnerBuilder.setClientOptions(
new WebClientOptions()
.setDefaultPort(config.getDownstreamHttpPort())
.setDefaultHost(config.getDownstreamHttpHost()));
runnerBuilder.setServerOptions(
new HttpServerOptions()
.setPort(config.getHttpListenPort())
.setHost(config.getHttpListenHost())
.setReuseAddress(true)
.setReusePort(true));

runnerBuilder.build().start();
} catch (IOException ex) {
LOG.info(
"Unable to access supplied keyfile, or file does not conform to V3 keystore standard.");
} catch (CipherException ex) {
LOG.info("Unable to decode keyfile with supplied passwordFile.");
}
}

private Optional<String> readPasswordFromFile() {
try {
byte[] fileContent = Files.readAllBytes(config.getPasswordFilePath());
return Optional.of(new String(fileContent, Charsets.UTF_8));
} catch (IOException ex) {
LOG.debug("Failed to read password from password file, {}", ex);
return Optional.empty();
}
}

public static void main(final String... args) {
final EthFirewallCommand command = new EthFirewallCommand();
final EthFirewallCommandLineConfig config = new EthFirewallCommandLineConfig(System.out);
if (!config.parse(args)) {
return;
}

command.parse(new RunLast(), command.exceptionHandler().andExit(ERROR_EXIT_CODE), args);
final EthFirewall ethFirewall = new EthFirewall(config, new RunnerBuilder());
ethFirewall.run();
}
}

This file was deleted.

Loading

0 comments on commit a2d80d8

Please sign in to comment.