|
| 1 | +package org.codejive.webreplay; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.nio.file.Paths; |
| 6 | + |
| 7 | +/** |
| 8 | + * Main entry point for running the WebReplayProxy as a standalone application. |
| 9 | + * |
| 10 | + * <p>Usage: |
| 11 | + * |
| 12 | + * <pre> |
| 13 | + * java -jar webreplay.jar [options] |
| 14 | + * |
| 15 | + * Options: |
| 16 | + * -p, --port <port> Port to run the proxy on (default: 3128) |
| 17 | + * -d, --dir <directory> Directory for storing cached requests (default: proxy-cache) |
| 18 | + * -m, --mode <mode> Replay mode: RECORD, CACHE, or REPLAY (default: CACHE) |
| 19 | + * -h, --help Show this help message |
| 20 | + * </pre> |
| 21 | + */ |
| 22 | +public class Main { |
| 23 | + private static final int DEFAULT_PORT = 3128; |
| 24 | + private static final String DEFAULT_DIRECTORY = "proxy-cache"; |
| 25 | + private static final ReplayMode DEFAULT_MODE = ReplayMode.CACHE; |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + try { |
| 29 | + Config config = parseArgs(args); |
| 30 | + |
| 31 | + if (config.showHelp) { |
| 32 | + showHelp(); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + startProxy(config); |
| 37 | + } catch (IllegalArgumentException e) { |
| 38 | + System.err.println("Error: " + e.getMessage()); |
| 39 | + System.err.println(); |
| 40 | + showHelp(); |
| 41 | + System.exit(1); |
| 42 | + } catch (IOException e) { |
| 43 | + System.err.println("Failed to start proxy: " + e.getMessage()); |
| 44 | + e.printStackTrace(); |
| 45 | + System.exit(1); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void startProxy(Config config) throws IOException { |
| 50 | + System.out.println("Starting WebReplayProxy..."); |
| 51 | + System.out.println(" Mode: " + config.mode); |
| 52 | + System.out.println(" Port: " + config.port); |
| 53 | + System.out.println(" Storage: " + config.storageDirectory); |
| 54 | + System.out.println(); |
| 55 | + |
| 56 | + WebReplayProxy proxy = |
| 57 | + WebReplayProxy.builder() |
| 58 | + .mode(config.mode) |
| 59 | + .storageDirectory(config.storageDirectory) |
| 60 | + .build(); |
| 61 | + |
| 62 | + proxy.start(config.port); |
| 63 | + |
| 64 | + System.out.println("Proxy started successfully!"); |
| 65 | + System.out.println( |
| 66 | + "Configure your browser or application to use proxy: localhost:" + config.port); |
| 67 | + System.out.println("Press Ctrl+C to stop"); |
| 68 | + |
| 69 | + // Add shutdown hook to gracefully stop the proxy |
| 70 | + Runtime.getRuntime() |
| 71 | + .addShutdownHook( |
| 72 | + new Thread( |
| 73 | + () -> { |
| 74 | + System.out.println("\nStopping proxy..."); |
| 75 | + proxy.stop(); |
| 76 | + })); |
| 77 | + |
| 78 | + // Keep the main thread alive |
| 79 | + try { |
| 80 | + Thread.currentThread().join(); |
| 81 | + } catch (InterruptedException e) { |
| 82 | + Thread.currentThread().interrupt(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static Config parseArgs(String[] args) { |
| 87 | + Config config = new Config(); |
| 88 | + |
| 89 | + for (int i = 0; i < args.length; i++) { |
| 90 | + String arg = args[i]; |
| 91 | + |
| 92 | + switch (arg) { |
| 93 | + case "-h": |
| 94 | + case "--help": |
| 95 | + config.showHelp = true; |
| 96 | + return config; |
| 97 | + |
| 98 | + case "-p": |
| 99 | + case "--port": |
| 100 | + if (i + 1 >= args.length) { |
| 101 | + throw new IllegalArgumentException("Missing value for " + arg); |
| 102 | + } |
| 103 | + try { |
| 104 | + config.port = Integer.parseInt(args[++i]); |
| 105 | + if (config.port < 1 || config.port > 65535) { |
| 106 | + throw new IllegalArgumentException("Port must be between 1 and 65535"); |
| 107 | + } |
| 108 | + } catch (NumberFormatException e) { |
| 109 | + throw new IllegalArgumentException("Invalid port number: " + args[i]); |
| 110 | + } |
| 111 | + break; |
| 112 | + |
| 113 | + case "-d": |
| 114 | + case "--dir": |
| 115 | + if (i + 1 >= args.length) { |
| 116 | + throw new IllegalArgumentException("Missing value for " + arg); |
| 117 | + } |
| 118 | + config.storageDirectory = Paths.get(args[++i]); |
| 119 | + break; |
| 120 | + |
| 121 | + case "-m": |
| 122 | + case "--mode": |
| 123 | + if (i + 1 >= args.length) { |
| 124 | + throw new IllegalArgumentException("Missing value for " + arg); |
| 125 | + } |
| 126 | + try { |
| 127 | + config.mode = ReplayMode.valueOf(args[++i].toUpperCase()); |
| 128 | + } catch (IllegalArgumentException e) { |
| 129 | + throw new IllegalArgumentException( |
| 130 | + "Invalid mode: " + args[i] + ". Must be RECORD, CACHE, or REPLAY"); |
| 131 | + } |
| 132 | + break; |
| 133 | + |
| 134 | + default: |
| 135 | + throw new IllegalArgumentException("Unknown option: " + arg); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return config; |
| 140 | + } |
| 141 | + |
| 142 | + private static void showHelp() { |
| 143 | + System.out.println( |
| 144 | + "WebReplayProxy - HTTP/HTTPS proxy with recording and replay capabilities"); |
| 145 | + System.out.println(); |
| 146 | + System.out.println("Usage: java -jar webreplay.jar [options]"); |
| 147 | + System.out.println(); |
| 148 | + System.out.println("Options:"); |
| 149 | + System.out.println( |
| 150 | + " -p, --port <port> Port to run the proxy on (default: " |
| 151 | + + DEFAULT_PORT |
| 152 | + + ")"); |
| 153 | + System.out.println( |
| 154 | + " -d, --dir <directory> Directory for storing cached requests (default: " |
| 155 | + + DEFAULT_DIRECTORY |
| 156 | + + ")"); |
| 157 | + System.out.println( |
| 158 | + " -m, --mode <mode> Replay mode: RECORD, CACHE, or REPLAY (default: " |
| 159 | + + DEFAULT_MODE |
| 160 | + + ")"); |
| 161 | + System.out.println(" -h, --help Show this help message"); |
| 162 | + System.out.println(); |
| 163 | + System.out.println("Modes:"); |
| 164 | + System.out.println(" RECORD - All requests pass through and are recorded"); |
| 165 | + System.out.println( |
| 166 | + " CACHE - Cached responses returned when available, otherwise pass through and" |
| 167 | + + " record"); |
| 168 | + System.out.println( |
| 169 | + " REPLAY - Only cached responses returned; non-matching requests return 404"); |
| 170 | + System.out.println(); |
| 171 | + System.out.println("Examples:"); |
| 172 | + System.out.println(" # Start with defaults (CACHE mode on port 3128)"); |
| 173 | + System.out.println(" java -jar webreplay.jar"); |
| 174 | + System.out.println(); |
| 175 | + System.out.println(" # Start in RECORD mode on port 8080"); |
| 176 | + System.out.println(" java -jar webreplay.jar --port 8080 --mode RECORD"); |
| 177 | + System.out.println(); |
| 178 | + System.out.println(" # Use custom storage directory"); |
| 179 | + System.out.println(" java -jar webreplay.jar --dir /path/to/cache"); |
| 180 | + } |
| 181 | + |
| 182 | + private static class Config { |
| 183 | + int port = DEFAULT_PORT; |
| 184 | + Path storageDirectory = Paths.get(DEFAULT_DIRECTORY); |
| 185 | + ReplayMode mode = DEFAULT_MODE; |
| 186 | + boolean showHelp = false; |
| 187 | + } |
| 188 | +} |
0 commit comments