|
| 1 | +package com.tamashenning.forgeanalytics.client; |
| 2 | + |
| 3 | +import java.security.MessageDigest; |
| 4 | +import java.security.NoSuchAlgorithmException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.apache.http.HttpResponse; |
| 11 | +import org.apache.http.NameValuePair; |
| 12 | +import org.apache.http.client.HttpClient; |
| 13 | +import org.apache.http.client.entity.UrlEncodedFormEntity; |
| 14 | +import org.apache.http.client.methods.HttpPost; |
| 15 | +import org.apache.http.entity.StringEntity; |
| 16 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 17 | +import org.apache.http.message.BasicNameValuePair; |
| 18 | + |
| 19 | +import com.google.gson.JsonObject; |
| 20 | +import com.google.gson.JsonPrimitive; |
| 21 | +import com.tamashenning.forgeanalytics.events.AnalyticsEvent; |
| 22 | +import com.tamashenning.forgeanalytics.models.AnalyticsModel; |
| 23 | + |
| 24 | +import net.minecraft.client.Minecraft; |
| 25 | +import net.minecraft.server.MinecraftServer; |
| 26 | +import net.minecraft.server.dedicated.DedicatedServer; |
| 27 | +import net.minecraftforge.common.ForgeVersion; |
| 28 | +import net.minecraftforge.common.MinecraftForge; |
| 29 | +import net.minecraftforge.common.config.Configuration; |
| 30 | +import net.minecraftforge.fml.common.FMLCommonHandler; |
| 31 | + |
| 32 | +public class AnalyticsCommon { |
| 33 | + |
| 34 | + public boolean UploadModel(AnalyticsModel model, boolean isClient) throws Exception { |
| 35 | + |
| 36 | + if (!FireEvent()) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + model.Properties.putAll(ForgeAnalyticsConstants.CustomProperties); |
| 41 | + |
| 42 | + // Code to send to Tamas backend... |
| 43 | + |
| 44 | + JsonObject data = new JsonObject(); |
| 45 | + data.add("PartitionKey", new JsonPrimitive(model.PartitionKey)); |
| 46 | + data.add("ClientDateTimeEpoch", new JsonPrimitive(model.ClientDateTimeEpoch)); |
| 47 | + data.add("Table", new JsonPrimitive(model.Table)); |
| 48 | + |
| 49 | + JsonObject propertiesMap = new JsonObject(); |
| 50 | + |
| 51 | + for (Map.Entry<String, String> entry : model.Properties.entrySet()) { |
| 52 | + // if the user opted out of the property, respect it... |
| 53 | + if (!ForgeAnalyticsConstants.dataConfig |
| 54 | + .get(Configuration.CATEGORY_GENERAL, entry.getKey() + "_OptOut", false).getBoolean()) { |
| 55 | + |
| 56 | + propertiesMap.add(entry.getKey(), new JsonPrimitive(entry.getValue())); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + data.add("Properties", propertiesMap); |
| 61 | + |
| 62 | + String json = data.toString(); |
| 63 | + |
| 64 | + // Code to send to Lex' backend |
| 65 | + |
| 66 | + JsonObject dataForge = new JsonObject(); |
| 67 | + dataForge.add("cmd", new JsonPrimitive(model.PartitionKey)); |
| 68 | + |
| 69 | + for (Map.Entry<String, String> entry : model.Properties.entrySet()) { |
| 70 | + // if the user opted out of the property, respect it... |
| 71 | + if (!ForgeAnalyticsConstants.dataConfig |
| 72 | + .get(Configuration.CATEGORY_GENERAL, entry.getKey() + "_OptOut", false).getBoolean()) { |
| 73 | + |
| 74 | + dataForge.add(entry.getKey(), new JsonPrimitive(entry.getValue())); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // System.out.println(json); |
| 79 | + this.UploadForge(dataForge.toString()); |
| 80 | + return this.UploadModel(json, isClient); |
| 81 | + } |
| 82 | + |
| 83 | + public AnalyticsModel CreateKeepAlivePing() { |
| 84 | + AnalyticsModel am = new AnalyticsModel(); |
| 85 | + am.Table = ForgeAnalyticsConstants.pingServerTable; |
| 86 | + am.Properties = new HashMap<String, String>(); |
| 87 | + am.PartitionKey = ForgeAnalyticsConstants.pingServerKeepAlive; |
| 88 | + am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L; |
| 89 | + am.Properties.putAll(this.getCommonValues()); |
| 90 | + return am; |
| 91 | + } |
| 92 | + |
| 93 | + public AnalyticsModel CreateServerStartupPing() { |
| 94 | + AnalyticsModel am = new AnalyticsModel(); |
| 95 | + am.Table = ForgeAnalyticsConstants.pingServerTable; |
| 96 | + am.Properties = new HashMap<String, String>(); |
| 97 | + am.PartitionKey = ForgeAnalyticsConstants.pingServerStartCommand; |
| 98 | + am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L; |
| 99 | + am.Properties.putAll(this.getCommonValues()); |
| 100 | + |
| 101 | + return am; |
| 102 | + } |
| 103 | + |
| 104 | + public AnalyticsModel CreateServerStoppedPing() { |
| 105 | + AnalyticsModel am = new AnalyticsModel(); |
| 106 | + am.Table = ForgeAnalyticsConstants.pingServerTable; |
| 107 | + am.Properties = new HashMap<String, String>(); |
| 108 | + am.PartitionKey = ForgeAnalyticsConstants.pingServerStopCommand; |
| 109 | + am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L; |
| 110 | + am.Properties.putAll(this.getCommonValues()); |
| 111 | + |
| 112 | + return am; |
| 113 | + } |
| 114 | + |
| 115 | + public boolean FireEvent() { |
| 116 | + try { |
| 117 | + MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance(); |
| 118 | + if (server.getClass().equals(DedicatedServer.class)) { |
| 119 | + DedicatedServer ds = (DedicatedServer) server; |
| 120 | + if (!ds.isSnooperEnabled()) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + MinecraftForge.EVENT_BUS.post(new AnalyticsEvent(net.minecraftforge.fml.relauncher.Side.SERVER)); |
| 126 | + } catch (Exception e) { |
| 127 | + // First time server init??? |
| 128 | + return false; |
| 129 | + } |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + protected Map<String, String> getCommonValues() { |
| 134 | + Map<String, String> commonValues = new HashMap<String, String>(); |
| 135 | + |
| 136 | + String activeModListCount = Integer |
| 137 | + .toString(net.minecraftforge.fml.common.Loader.instance().getActiveModList().size()); |
| 138 | + |
| 139 | + String modListCount = Integer.toString(net.minecraftforge.fml.common.Loader.instance().getModList().size()); |
| 140 | + // String modList = ""; |
| 141 | + |
| 142 | + commonValues.put("JavaVersion", System.getProperty("java.version")); |
| 143 | + commonValues.put("JavaMaxRAM", Long.toString(Runtime.getRuntime().maxMemory())); |
| 144 | + commonValues.put("JavaAllocatedRAM", Long.toString(Runtime.getRuntime().totalMemory())); |
| 145 | + // TODO: Remove once fully switched over. |
| 146 | + commonValues.put("SessionID", ForgeAnalyticsSingleton.getInstance().SessionID); |
| 147 | + commonValues.put("AdID", ForgeAnalyticsConstants.AdID); |
| 148 | + commonValues.put("session_id", ForgeAnalyticsSingleton.getInstance().SessionUUID.toString()); |
| 149 | + commonValues.put("instance_id", ForgeAnalyticsConstants.InstanceUUID.toString()); |
| 150 | + commonValues.put("MinecraftVersion", net.minecraftforge.fml.common.Loader.instance().getMCVersionString()); |
| 151 | + commonValues.put("ForgeVersion", ForgeVersion.getVersion()); |
| 152 | + commonValues.put("MCPVersion", net.minecraftforge.fml.common.Loader.instance().getMCPVersionString()); |
| 153 | + commonValues.put("ActiveModCount", activeModListCount); |
| 154 | + commonValues.put("ModCount", modListCount); |
| 155 | + commonValues.put("ModPack", ForgeAnalyticsConstants.modPack); |
| 156 | + |
| 157 | + return commonValues; |
| 158 | + } |
| 159 | + |
| 160 | + protected boolean UploadModel(String json, boolean isClient) throws Exception { |
| 161 | + |
| 162 | + HttpClient httpClient = HttpClientBuilder.create().build(); |
| 163 | + |
| 164 | + try { |
| 165 | + HttpPost request = new HttpPost(ForgeAnalyticsConstants.serverUrl); |
| 166 | + |
| 167 | + StringEntity params = new StringEntity(json); |
| 168 | + request.addHeader("content-type", "application/json"); |
| 169 | + request.setEntity(params); |
| 170 | + HttpResponse response = httpClient.execute(request); |
| 171 | + |
| 172 | + } catch (Exception ex) { |
| 173 | + ex.printStackTrace(); |
| 174 | + } |
| 175 | + |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + protected void UploadForge(String json) throws Exception { |
| 180 | + |
| 181 | + HttpClient httpClient = HttpClientBuilder.create().build(); |
| 182 | + |
| 183 | + try { |
| 184 | + HttpPost request = new HttpPost(ForgeAnalyticsConstants.forgeServerUrl); |
| 185 | + |
| 186 | + List<NameValuePair> nvp = new ArrayList<NameValuePair>(); |
| 187 | + nvp.add(new BasicNameValuePair("stat", json)); |
| 188 | + |
| 189 | + request.setEntity(new UrlEncodedFormEntity(nvp)); |
| 190 | + HttpResponse response = httpClient.execute(request); |
| 191 | + |
| 192 | + } catch (Exception ex) { |
| 193 | + // handle exception here |
| 194 | + ex.printStackTrace(); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + public String Anonymize(String data) throws NoSuchAlgorithmException { |
| 199 | + MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); |
| 200 | + byte[] dataBytes = data.getBytes(); |
| 201 | + for (int i = 0; i < ForgeAnalyticsConstants.HASHCOUNT; i++) { |
| 202 | + dataBytes = sha256.digest(dataBytes); |
| 203 | + } |
| 204 | + |
| 205 | + return this.bytesToHex(dataBytes); |
| 206 | + } |
| 207 | + |
| 208 | + final protected char[] hexArray = "0123456789abcdef".toCharArray(); |
| 209 | + |
| 210 | + protected String bytesToHex(byte[] bytes) { |
| 211 | + char[] hexChars = new char[bytes.length * 2]; |
| 212 | + for (int j = 0; j < bytes.length; j++) { |
| 213 | + int v = bytes[j] & 0xFF; |
| 214 | + hexChars[j * 2] = hexArray[v >>> 4]; |
| 215 | + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; |
| 216 | + } |
| 217 | + return new String(hexChars); |
| 218 | + } |
| 219 | + |
| 220 | +} |
0 commit comments