To use, tests must be able to run iptables, eg sudo chmod u+s iptables
| 26 | |
| 27 | /** To use, tests must be able to run iptables, eg sudo chmod u+s iptables */ |
| 28 | public class IpTables { |
| 29 | private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 30 | |
| 31 | private static final boolean ENABLED = Boolean.getBoolean("solr.tests.use.iptables"); |
| 32 | |
| 33 | private static final Set<Integer> BLOCK_PORTS = |
| 34 | Collections.synchronizedSet(new HashSet<Integer>()); |
| 35 | |
| 36 | public static void unblockPort(int port) throws IOException, InterruptedException { |
| 37 | if (ENABLED && BLOCK_PORTS.contains(port)) { |
| 38 | log.info("Unblock port with iptables: {}", port); |
| 39 | runCmd(("iptables -D INPUT -p tcp --dport " + port + " -j DROP").split("\\s")); |
| 40 | runCmd(("iptables -D OUTPUT -p tcp --dport " + port + " -j DROP").split("\\s")); |
| 41 | BLOCK_PORTS.remove(port); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public static void unblockAllPorts() throws IOException, InterruptedException { |
| 46 | if (ENABLED) { |
| 47 | log.info("Unblocking any ports previously blocked with iptables..."); |
| 48 | final Integer[] ports = BLOCK_PORTS.toArray(new Integer[0]); |
| 49 | for (Integer port : ports) { |
| 50 | IpTables.unblockPort(port); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private static void runCmd(String... cmd) throws IOException, InterruptedException { |
| 56 | final int exitCode = new ProcessBuilder(cmd).inheritIO().start().waitFor(); |
| 57 | if (exitCode != 0) { |
| 58 | throw new IOException( |
| 59 | "iptables process did not exit successfully, exit code was: " + exitCode); |
| 60 | } |
| 61 | } |
| 62 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…