|
| 1 | +package me.aa07.paradise.taskdaemon.core.modules.aclcleanup; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.net.Socket; |
| 7 | +import java.time.LocalDateTime; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Optional; |
| 11 | + |
| 12 | +import me.aa07.paradise.taskdaemon.core.config.PfsenseConfig; |
| 13 | +import me.aa07.paradise.taskdaemon.core.database.DatabaseType; |
| 14 | +import me.aa07.paradise.taskdaemon.core.database.DbCore; |
| 15 | +import me.aa07.paradise.taskdaemon.database.gamedb.Tables; |
| 16 | +import org.apache.logging.log4j.Logger; |
| 17 | +import org.jooq.DSLContext; |
| 18 | +import org.quartz.Job; |
| 19 | +import org.quartz.JobDataMap; |
| 20 | +import org.quartz.JobExecutionContext; |
| 21 | +import org.quartz.JobExecutionException; |
| 22 | + |
| 23 | +/** |
| 24 | + * Scheduled job that cleans up ACL by removing inactive player IPs |
| 25 | + */ |
| 26 | +public class AclCleanupJob implements Job { |
| 27 | + |
| 28 | + /** |
| 29 | + * Main job execution - fetches current ACL, checks database for inactive IPs, and removes them |
| 30 | + */ |
| 31 | + @Override |
| 32 | + public void execute(JobExecutionContext context) throws JobExecutionException { |
| 33 | + JobDataMap datamap = context.getMergedJobDataMap(); |
| 34 | + |
| 35 | + // Get our logger - important |
| 36 | + Object raw_logger = datamap.get("LOGGER"); |
| 37 | + Optional<Logger> logger_holder = Optional.empty(); |
| 38 | + |
| 39 | + if (raw_logger instanceof Logger l2) { |
| 40 | + logger_holder = Optional.of(l2); |
| 41 | + } |
| 42 | + |
| 43 | + if (!logger_holder.isPresent()) { |
| 44 | + System.out.println("[AclCleanup] LOGGER WAS SOMEHOW NULL - THIS IS VERY BAD"); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + Logger logger = logger_holder.get(); |
| 49 | + |
| 50 | + // Now get our DB |
| 51 | + Object raw_db = datamap.get("DBCORE"); |
| 52 | + Optional<DbCore> dbcore_holder = Optional.empty(); |
| 53 | + |
| 54 | + if (raw_db instanceof DbCore db2) { |
| 55 | + dbcore_holder = Optional.of(db2); |
| 56 | + } |
| 57 | + |
| 58 | + if (!dbcore_holder.isPresent()) { |
| 59 | + logger.error("[AclCleanup] DBCORE WAS SOMEHOW NULL - THIS IS VERY BAD"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + DbCore dbcore = dbcore_holder.get(); |
| 64 | + |
| 65 | + // Get pfSense configuration |
| 66 | + Object raw_pfs_cfg = datamap.get("PFSENSE_CFG"); |
| 67 | + Optional<PfsenseConfig> pfs_cfg_holder = Optional.empty(); |
| 68 | + |
| 69 | + if (raw_pfs_cfg instanceof PfsenseConfig pfsCfg) { |
| 70 | + pfs_cfg_holder = Optional.of(pfsCfg); |
| 71 | + } |
| 72 | + |
| 73 | + if (pfs_cfg_holder.isEmpty()) { |
| 74 | + logger.error("[AclCleanup] PFSENSE_CFG WAS SOMEHOW NULL - THIS IS VERY BAD"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + PfsenseConfig pfs_cfg = pfs_cfg_holder.get(); |
| 79 | + |
| 80 | + try { |
| 81 | + List<String> current_acl_ips = fetchAcl(logger, pfs_cfg); |
| 82 | + logger.info("IPs in ACL ({}): {}", current_acl_ips.size(), String.join(" ", current_acl_ips)); |
| 83 | + |
| 84 | + List<String> ips_to_remove = checkIpsInDatabase(current_acl_ips, logger, dbcore); |
| 85 | + |
| 86 | + if (ips_to_remove.isEmpty()) { |
| 87 | + logger.info("No IPs to remove from ACL"); |
| 88 | + } else { |
| 89 | + logger.info("IPs to remove from ACL ({}): {}", ips_to_remove.size(), String.join(" ", ips_to_remove)); |
| 90 | + |
| 91 | + for (String ip : ips_to_remove) { |
| 92 | + removeIpFromAcl(ip, logger, pfs_cfg); |
| 93 | + } |
| 94 | + |
| 95 | + logger.info("Removed {} IPs from ACL", ips_to_remove.size()); |
| 96 | + } |
| 97 | + |
| 98 | + List<String> updated_acl_ips = fetchAcl(logger, pfs_cfg); |
| 99 | + logger.info("IPs now in ACL ({}): {}", updated_acl_ips.size(), String.join(" ", updated_acl_ips)); |
| 100 | + |
| 101 | + } catch (IOException e) { |
| 102 | + logger.error("Error in AclCleanupJob!"); |
| 103 | + logger.error(e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Checks which IPs should be removed from ACL based on database activity |
| 109 | + * Removes IPs that don't exist in DB or haven't been seen in last 10 minutes |
| 110 | + */ |
| 111 | + public List<String> checkIpsInDatabase(List<String> ips, Logger logger, DbCore dbcore) { |
| 112 | + List<String> to_remove = new ArrayList<>(); |
| 113 | + |
| 114 | + DSLContext ctx = dbcore.jooq(DatabaseType.GameDb); |
| 115 | + LocalDateTime ten_mins_ago = dbcore.now().minusMinutes(10); |
| 116 | + |
| 117 | + for (String ip : ips) { |
| 118 | + if (!ctx.fetchExists(ctx.select(Tables.PLAYER.CKEY).from(Tables.PLAYER).where(Tables.PLAYER.IP.eq(ip)))) { |
| 119 | + logger.info("IP {} no longer in database.", ip); |
| 120 | + to_remove.add(ip); |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + boolean seen_in_last_10m = ctx.fetchExists( |
| 125 | + ctx.select(Tables.PLAYER.CKEY).from(Tables.PLAYER).where(Tables.PLAYER.IP.eq(ip)) |
| 126 | + .and(Tables.PLAYER.LASTSEEN.lt(ten_mins_ago)) |
| 127 | + ); |
| 128 | + |
| 129 | + if (!seen_in_last_10m) { |
| 130 | + logger.info("IP {} not active within last 10 minutes.", ip); |
| 131 | + to_remove.add(ip); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return to_remove; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Retrieves current IP list from ACL via socket connection |
| 140 | + */ |
| 141 | + public List<String> fetchAcl(Logger logger, PfsenseConfig cfg) throws IOException { |
| 142 | + List<String> ip_list = new ArrayList<>(); |
| 143 | + |
| 144 | + Socket socket = new Socket(cfg.host, cfg.port); |
| 145 | + socket.setSoTimeout(10000); |
| 146 | + |
| 147 | + OutputStream out = socket.getOutputStream(); |
| 148 | + InputStream in = socket.getInputStream(); |
| 149 | + |
| 150 | + String command = "show acl #1\n"; |
| 151 | + out.write(command.getBytes("ascii")); |
| 152 | + |
| 153 | + StringBuilder retval = new StringBuilder(); |
| 154 | + byte[] buffer = new byte[16]; |
| 155 | + int bytes_read; |
| 156 | + |
| 157 | + while ((bytes_read = in.read(buffer)) != -1) { |
| 158 | + retval.append(new String(buffer, 0, bytes_read)); |
| 159 | + } |
| 160 | + |
| 161 | + socket.close(); |
| 162 | + |
| 163 | + String[] raw_ips = retval.toString().split("\n"); |
| 164 | + for (String ip : raw_ips) { |
| 165 | + if (ip.trim().isEmpty()) { |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + String[] parts = ip.split(" "); |
| 170 | + if (parts.length >= 2) { |
| 171 | + ip_list.add(parts[1]); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return ip_list; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Removes a specific IP from ACL #1 via socket command |
| 180 | + */ |
| 181 | + public void removeIpFromAcl(String ip, Logger logger, PfsenseConfig cfg) throws IOException { |
| 182 | + logger.info("Removing {} from ACL", ip); |
| 183 | + |
| 184 | + Socket socket = new Socket(cfg.host, cfg.port); |
| 185 | + OutputStream out = socket.getOutputStream(); |
| 186 | + |
| 187 | + String command = "del acl #1 " + ip + "\n"; |
| 188 | + out.write(command.getBytes("ascii")); |
| 189 | + |
| 190 | + socket.close(); |
| 191 | + } |
| 192 | +} |
0 commit comments