|
| 1 | +package me.aa07.paradise.taskdaemon.core.modules.ip2asn; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.http.HttpClient; |
| 6 | +import java.net.http.HttpRequest; |
| 7 | +import java.net.http.HttpResponse; |
| 8 | +import java.net.http.HttpResponse.BodyHandlers; |
| 9 | +import java.time.LocalDateTime; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Optional; |
| 13 | +import me.aa07.paradise.taskdaemon.core.config.Ip2AsnSerivceConfig; |
| 14 | +import me.aa07.paradise.taskdaemon.core.database.DatabaseType; |
| 15 | +import me.aa07.paradise.taskdaemon.core.database.DbCore; |
| 16 | +import me.aa07.paradise.taskdaemon.core.models.ip2asn.Ip2AsnResponseModel; |
| 17 | +import me.aa07.paradise.taskdaemon.database.gamedb.Tables; |
| 18 | +import me.aa07.paradise.taskdaemon.database.gamedb.tables.records.Ip2groupRecord; |
| 19 | +import org.apache.logging.log4j.Logger; |
| 20 | +import org.jooq.DSLContext; |
| 21 | +import org.jooq.Record1; |
| 22 | +import org.jooq.Select; |
| 23 | +import org.jooq.types.UInteger; |
| 24 | +import org.quartz.DisallowConcurrentExecution; |
| 25 | +import org.quartz.Job; |
| 26 | +import org.quartz.JobDataMap; |
| 27 | +import org.quartz.JobExecutionContext; |
| 28 | +import org.quartz.JobExecutionException; |
| 29 | + |
| 30 | +@DisallowConcurrentExecution // NO |
| 31 | +public class Ip2AsnJob implements Job { |
| 32 | + |
| 33 | + @Override |
| 34 | + public void execute(JobExecutionContext event) throws JobExecutionException { |
| 35 | + JobDataMap datamap = event.getMergedJobDataMap(); |
| 36 | + |
| 37 | + // Get our logger - important |
| 38 | + Object raw_logger = datamap.get("LOGGER"); |
| 39 | + Optional<Logger> logger_holder = Optional.empty(); |
| 40 | + |
| 41 | + if (raw_logger instanceof Logger l2) { |
| 42 | + logger_holder = Optional.of(l2); |
| 43 | + } |
| 44 | + |
| 45 | + if (!logger_holder.isPresent()) { |
| 46 | + System.out.println("[Ip2Asn] LOGGER WAS SOMEHOW NULL - THIS IS VERY BAD"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + Logger logger = logger_holder.get(); |
| 51 | + |
| 52 | + // Now get our DB |
| 53 | + Object raw_db = datamap.get("DBCORE"); |
| 54 | + Optional<DbCore> dbcore_holder = Optional.empty(); |
| 55 | + |
| 56 | + if (raw_db instanceof DbCore db2) { |
| 57 | + dbcore_holder = Optional.of(db2); |
| 58 | + } |
| 59 | + |
| 60 | + if (!dbcore_holder.isPresent()) { |
| 61 | + logger.error("[Ip2Asn] DBCORE WAS SOMEHOW NULL - THIS IS VERY BAD"); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + DbCore dbcore = dbcore_holder.get(); |
| 66 | + |
| 67 | + // Now get our config |
| 68 | + Object raw_ip2asn_cfg = datamap.get("IP2ASNCFG"); |
| 69 | + Optional<Ip2AsnSerivceConfig> ip2asn_cfg_holder = Optional.empty(); |
| 70 | + |
| 71 | + if (raw_ip2asn_cfg instanceof Ip2AsnSerivceConfig ip2asnCfg2) { |
| 72 | + ip2asn_cfg_holder = Optional.of(ip2asnCfg2); |
| 73 | + } |
| 74 | + |
| 75 | + if (!ip2asn_cfg_holder.isPresent()) { |
| 76 | + logger.error("[Ip2Asn] IP2ASNCFG WAS SOMEHOW NULL - THIS IS VERY BAD"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + Ip2AsnSerivceConfig config = ip2asn_cfg_holder.get(); |
| 81 | + |
| 82 | + DSLContext ctx = dbcore.jooq(DatabaseType.GameDb); |
| 83 | + |
| 84 | + // So |
| 85 | + // JOOQ doesnt have INET_ATON, presumably because its a MySQL native |
| 86 | + // So we have to do the manual selects then work out the differences |
| 87 | + // To do this we need: |
| 88 | + // 1. All IPs from player seen in the last 30 days |
| 89 | + // 2. Then remove the IPs in ip2group that have been updated in the last 7 days |
| 90 | + // Then we need to update IP2Group or insert where required |
| 91 | + LocalDateTime last_30_days = LocalDateTime.now().minusDays(30); |
| 92 | + LocalDateTime last_7_days = LocalDateTime.now().minusDays(7); |
| 93 | + |
| 94 | + logger.info("[Ip2Asn] Pulling player IPs..."); |
| 95 | + |
| 96 | + // Get the player IPs |
| 97 | + List<String> player_ips = ctx.select(Tables.PLAYER.IP).from(Tables.PLAYER) |
| 98 | + .where(Tables.PLAYER.LASTSEEN.gt(last_30_days)).fetch() |
| 99 | + .getValues(Tables.PLAYER.IP, String.class); |
| 100 | + |
| 101 | + logger.info(String.format("[Ip2Asn] Pulled %s player IPs, now pulling ip2group IPs...", player_ips.size())); |
| 102 | + |
| 103 | + List<UInteger> ip2group_ips = ctx.select(Tables.IP2GROUP.IP).from(Tables.IP2GROUP) |
| 104 | + .where(Tables.IP2GROUP.DATE.gt(last_7_days)).fetch() |
| 105 | + .getValues(Tables.IP2GROUP.IP, UInteger.class); |
| 106 | + |
| 107 | + logger.info(String.format("[Ip2Asn] Pulled %s ip2group IPs, now parsing...", ip2group_ips.size())); |
| 108 | + |
| 109 | + // Now turn that to regular IPs |
| 110 | + ArrayList<String> parsed_ip2group_ips = new ArrayList<String>(); |
| 111 | + |
| 112 | + for (UInteger ip_uint : ip2group_ips) { |
| 113 | + String ip_str = uint2ip(ip_uint); |
| 114 | + parsed_ip2group_ips.add(ip_str); |
| 115 | + } |
| 116 | + |
| 117 | + ArrayList<String> ips_to_get = new ArrayList<String>(); |
| 118 | + |
| 119 | + // Start with the IPs in player - then see if they exist in the last 7 days in |
| 120 | + // ip2group |
| 121 | + for (String ip : player_ips) { |
| 122 | + if (!parsed_ip2group_ips.contains(ip)) { |
| 123 | + ips_to_get.add(ip); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + logger.info(String.format("[Ip2Asn] Found %s IPs to refresh groups for", ips_to_get.size())); |
| 128 | + |
| 129 | + if (ips_to_get.size() == 0) { |
| 130 | + logger.info("[Ip2Asn] No work to do"); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + // Now do the big refresh |
| 135 | + HttpClient client = HttpClient.newHttpClient(); |
| 136 | + Gson gson = new Gson(); |
| 137 | + for (String ip : ips_to_get) { |
| 138 | + try { |
| 139 | + // Get our AS - probably a more elegant way to do this but meh |
| 140 | + HttpRequest httpreq = HttpRequest.newBuilder().uri(new URI(String.format("%s%s", config.host, ip))) |
| 141 | + .GET().build(); |
| 142 | + |
| 143 | + // Send the request off |
| 144 | + HttpResponse<String> response = client.send(httpreq, BodyHandlers.ofString()); |
| 145 | + |
| 146 | + // Decode the shenanigans |
| 147 | + Ip2AsnResponseModel res_model = gson.fromJson(response.body(), Ip2AsnResponseModel.class); |
| 148 | + UInteger ip_uint = ip2uint(ip); |
| 149 | + UInteger asn = UInteger.valueOf(res_model.asn); |
| 150 | + |
| 151 | + // See if our row exists, we update if it does |
| 152 | + Select<Record1<UInteger>> fetch_test = ctx.select(Tables.IP2GROUP.IP).from(Tables.IP2GROUP) |
| 153 | + .where(Tables.IP2GROUP.IP.eq(ip_uint)); |
| 154 | + |
| 155 | + if (ctx.fetchExists(fetch_test)) { |
| 156 | + // It exists, update |
| 157 | + logger.info(String.format("[Ip2Asn] Updating row for %s", ip)); |
| 158 | + ctx.update(Tables.IP2GROUP) |
| 159 | + .set(Tables.IP2GROUP.GROUPSTR, asn) |
| 160 | + .set(Tables.IP2GROUP.DATE, dbcore.now()) |
| 161 | + .where(Tables.IP2GROUP.IP.eq(ip_uint)) |
| 162 | + .execute(); |
| 163 | + } else { |
| 164 | + // It dont exist, insert |
| 165 | + logger.info(String.format("[Ip2Asn] Inserting row for %s", ip)); |
| 166 | + Ip2groupRecord record = ctx.newRecord(Tables.IP2GROUP); |
| 167 | + record.setIp(ip_uint); |
| 168 | + record.setDate(dbcore.now()); |
| 169 | + record.setGroupstr(asn); |
| 170 | + record.store(); |
| 171 | + } |
| 172 | + |
| 173 | + } catch (Exception e) { |
| 174 | + logger.warn(String.format("[Ip2Asn] Failed to get AS info for IP %s - skipping", ip)); |
| 175 | + logger.warn(e); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + logger.info("[Ip2Asn] Processing complete"); |
| 180 | + } |
| 181 | + |
| 182 | + // Turns a uint IP into its string form |
| 183 | + private String uint2ip(UInteger ip) { |
| 184 | + long ip_long = ip.longValue(); |
| 185 | + return String.format("%d.%d.%d.%d", |
| 186 | + (ip_long >> 24 & 0xff), |
| 187 | + (ip_long >> 16 & 0xff), |
| 188 | + (ip_long >> 8 & 0xff), |
| 189 | + (ip_long & 0xff)); |
| 190 | + } |
| 191 | + |
| 192 | + // Turns a string IP into its uint form |
| 193 | + private UInteger ip2uint(String ip) { |
| 194 | + String[] octets = ip.split("\\."); |
| 195 | + |
| 196 | + long ip_long = 0; |
| 197 | + for (int i = 0; i < 4; i++) { |
| 198 | + int octet = Integer.parseInt(octets[i]); |
| 199 | + if (octet < 0 || octet > 255) { |
| 200 | + throw new IllegalArgumentException("Invalid octet in IP address: " + octets[i]); |
| 201 | + } |
| 202 | + // This does the shift magic with the for loop, |
| 203 | + // with each iteration shifting by 8 |
| 204 | + ip_long |= ((long) octet << (24 - 8 * i)); |
| 205 | + } |
| 206 | + |
| 207 | + return UInteger.valueOf(ip_long); |
| 208 | + } |
| 209 | +} |
0 commit comments