-
Notifications
You must be signed in to change notification settings - Fork 1
Adds ACL cleanup job #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
466b4fe
proxyDaemon initial commit
Burzah 5f02aba
alphabetize and commenting
Burzah 2a52ca2
Update TaskDaemon.Core/src/me/aa07/paradise/taskdaemon/core/Core.java
Burzah d8a20b7
alphabetize and commenting part dos
Burzah dda20ba
Update TaskDaemon.Core/src/me/aa07/paradise/taskdaemon/core/modules/a…
Burzah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
TaskDaemon.Core/src/me/aa07/paradise/taskdaemon/core/config/PfsenseConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package me.aa07.paradise.taskdaemon.core.config; | ||
|
|
||
| public class PfsenseConfig{ | ||
| public String host; | ||
| public int port; | ||
| } |
192 changes: 192 additions & 0 deletions
192
TaskDaemon.Core/src/me/aa07/paradise/taskdaemon/core/modules/aclcleanup/AclCleanupJob.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| package me.aa07.paradise.taskdaemon.core.modules.aclcleanup; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.Socket; | ||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import me.aa07.paradise.taskdaemon.core.config.PfsenseConfig; | ||
| import me.aa07.paradise.taskdaemon.core.database.DatabaseType; | ||
| import me.aa07.paradise.taskdaemon.core.database.DbCore; | ||
| import me.aa07.paradise.taskdaemon.database.gamedb.Tables; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.jooq.DSLContext; | ||
| import org.quartz.Job; | ||
| import org.quartz.JobDataMap; | ||
| import org.quartz.JobExecutionContext; | ||
| import org.quartz.JobExecutionException; | ||
|
|
||
| /** | ||
| * Scheduled job that cleans up ACL by removing inactive player IPs | ||
| */ | ||
| public class AclCleanupJob implements Job { | ||
|
|
||
| /** | ||
| * Main job execution - fetches current ACL, checks database for inactive IPs, and removes them | ||
| */ | ||
| @Override | ||
| public void execute(JobExecutionContext context) throws JobExecutionException { | ||
| JobDataMap datamap = context.getMergedJobDataMap(); | ||
|
|
||
| // Get our logger - important | ||
| Object raw_logger = datamap.get("LOGGER"); | ||
| Optional<Logger> logger_holder = Optional.empty(); | ||
|
|
||
| if (raw_logger instanceof Logger l2) { | ||
| logger_holder = Optional.of(l2); | ||
| } | ||
|
|
||
| if (!logger_holder.isPresent()) { | ||
| System.out.println("[AclCleanup] LOGGER WAS SOMEHOW NULL - THIS IS VERY BAD"); | ||
| return; | ||
| } | ||
|
|
||
| Logger logger = logger_holder.get(); | ||
|
|
||
| // Now get our DB | ||
| Object raw_db = datamap.get("DBCORE"); | ||
| Optional<DbCore> dbcore_holder = Optional.empty(); | ||
|
|
||
| if (raw_db instanceof DbCore db2) { | ||
| dbcore_holder = Optional.of(db2); | ||
| } | ||
|
|
||
| if (!dbcore_holder.isPresent()) { | ||
| logger.error("[AclCleanup] DBCORE WAS SOMEHOW NULL - THIS IS VERY BAD"); | ||
| return; | ||
| } | ||
|
|
||
| DbCore dbcore = dbcore_holder.get(); | ||
|
|
||
| // Get pfSense configuration | ||
| Object raw_pfs_cfg = datamap.get("PFSENSE_CFG"); | ||
| Optional<PfsenseConfig> pfs_cfg_holder = Optional.empty(); | ||
|
|
||
| if (raw_pfs_cfg instanceof PfsenseConfig pfsCfg) { | ||
| pfs_cfg_holder = Optional.of(pfsCfg); | ||
| } | ||
|
|
||
| if (pfs_cfg_holder.isEmpty()) { | ||
| logger.error("[AclCleanup] PFSENSE_CFG WAS SOMEHOW NULL - THIS IS VERY BAD"); | ||
| return; | ||
| } | ||
|
|
||
| PfsenseConfig pfs_cfg = pfs_cfg_holder.get(); | ||
|
|
||
| try { | ||
| List<String> current_acl_ips = fetchAcl(logger, pfs_cfg); | ||
| logger.info("IPs in ACL ({}): {}", current_acl_ips.size(), String.join(" ", current_acl_ips)); | ||
|
|
||
| List<String> ips_to_remove = checkIpsInDatabase(current_acl_ips, logger, dbcore); | ||
|
|
||
| if (ips_to_remove.isEmpty()) { | ||
| logger.info("No IPs to remove from ACL"); | ||
| } else { | ||
| logger.info("IPs to remove from ACL ({}): {}", ips_to_remove.size(), String.join(" ", ips_to_remove)); | ||
|
|
||
| for (String ip : ips_to_remove) { | ||
| removeIpFromAcl(ip, logger, pfs_cfg); | ||
| } | ||
|
|
||
| logger.info("Removed {} IPs from ACL", ips_to_remove.size()); | ||
| } | ||
|
|
||
| List<String> updated_acl_ips = fetchAcl(logger, pfs_cfg); | ||
| logger.info("IPs now in ACL ({}): {}", updated_acl_ips.size(), String.join(" ", updated_acl_ips)); | ||
|
|
||
| } catch (IOException e) { | ||
| logger.error("Error in AclCleanupJob!"); | ||
| logger.error(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks which IPs should be removed from ACL based on database activity | ||
| * Removes IPs that don't exist in DB or haven't been seen in last 10 minutes | ||
| */ | ||
| public List<String> checkIpsInDatabase(List<String> ips, Logger logger, DbCore dbcore) { | ||
| List<String> to_remove = new ArrayList<>(); | ||
|
|
||
| DSLContext ctx = dbcore.jooq(DatabaseType.GameDb); | ||
| LocalDateTime ten_mins_ago = dbcore.now().minusMinutes(10); | ||
|
|
||
| for (String ip : ips) { | ||
| if (!ctx.fetchExists(ctx.select(Tables.PLAYER.CKEY).from(Tables.PLAYER).where(Tables.PLAYER.IP.eq(ip)))) { | ||
| logger.info("IP {} no longer in database.", ip); | ||
| to_remove.add(ip); | ||
| continue; | ||
| } | ||
|
|
||
| boolean seen_in_last_10m = ctx.fetchExists( | ||
| ctx.select(Tables.PLAYER.CKEY).from(Tables.PLAYER).where(Tables.PLAYER.IP.eq(ip)) | ||
| .and(Tables.PLAYER.LASTSEEN.lt(ten_mins_ago)) | ||
| ); | ||
|
|
||
| if (!seen_in_last_10m) { | ||
| logger.info("IP {} not active within last 10 minutes.", ip); | ||
| to_remove.add(ip); | ||
| } | ||
| } | ||
|
|
||
| return to_remove; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves current IP list from ACL via socket connection | ||
| */ | ||
| public List<String> fetchAcl(Logger logger, PfsenseConfig cfg) throws IOException { | ||
| List<String> ip_list = new ArrayList<>(); | ||
|
|
||
| Socket socket = new Socket(cfg.host, cfg.port); | ||
| socket.setSoTimeout(10000); | ||
|
|
||
| OutputStream out = socket.getOutputStream(); | ||
| InputStream in = socket.getInputStream(); | ||
|
|
||
| String command = "show acl #1\n"; | ||
| out.write(command.getBytes("ascii")); | ||
|
|
||
| StringBuilder retval = new StringBuilder(); | ||
| byte[] buffer = new byte[16]; | ||
| int bytes_read; | ||
|
|
||
| while ((bytes_read = in.read(buffer)) != -1) { | ||
| retval.append(new String(buffer, 0, bytes_read)); | ||
| } | ||
|
|
||
| socket.close(); | ||
|
|
||
| String[] raw_ips = retval.toString().split("\n"); | ||
| for (String ip : raw_ips) { | ||
| if (ip.trim().isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| String[] parts = ip.split(" "); | ||
| if (parts.length >= 2) { | ||
| ip_list.add(parts[1]); | ||
| } | ||
| } | ||
|
|
||
| return ip_list; | ||
| } | ||
|
|
||
| /** | ||
| * Removes a specific IP from ACL #1 via socket command | ||
| */ | ||
| public void removeIpFromAcl(String ip, Logger logger, PfsenseConfig cfg) throws IOException { | ||
| logger.info("Removing {} from ACL", ip); | ||
|
|
||
| Socket socket = new Socket(cfg.host, cfg.port); | ||
| OutputStream out = socket.getOutputStream(); | ||
|
|
||
| String command = "del acl #1 " + ip + "\n"; | ||
| out.write(command.getBytes("ascii")); | ||
|
|
||
| socket.close(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.