· 6 years ago · Sep 25, 2019, 02:50 PM
1package extensions.VoteEngine.auto;
2
3import java.io.BufferedReader;
4import java.io.InputStreamReader;
5import java.net.HttpURLConnection;
6import java.net.URL;
7import java.util.logging.Logger;
8
9import net.sf.l2j.Config;
10
11/**
12 * Usage if (VoteRewardHotzone.check(player.getClient().getConnection().getInetAddress().toString()) == true) means player has voted
13 * @author Nightwolf
14 */
15public class VoteRewardHotzone
16{
17 private static Logger _log = Logger.getLogger(VoteRewardHotzone.class.getName());
18
19 // Configurations.
20 private static boolean debug = true;
21 private static String player_ip;
22 private static String server_id;
23 private static String server_ip;
24 private static String server_api;
25
26 // TODO check for possible null player
27 public static boolean check(String ip)
28 {
29 if (debug)
30 _log.info("Server VOTE CHECK START");
31 // set configs
32 player_ip = ip;// value from player's IP [player.getClient().getConnection().getInetAddress().toString()]
33 server_id = Config.HOTZONE_SERVER_ID;// value from config (server id)
34 server_ip = Config.HOTZONE_SERVER_IP;// value from config (server IP)
35 server_api = Config.HOTZONE_SERVER_API;// value from config (server API)
36
37 return HotCheck();
38 }
39
40 private final static boolean HotCheck()
41 {
42 String _status = null;
43 String _error_code = null;
44 String _server_id = null;
45 String _server_ip = null;
46 String _server_api = null;
47 String _server_ipv = null;
48 Boolean _voted = false;
49
50 try
51 {
52 final URL connection = new URL("https://l2hotzone.com/api.php?API_KEY=" + server_api + "&SERVER_ID=" + server_id + "&IP=" + server_ip + "&VOTER_IP=" + player_ip);
53
54 final HttpURLConnection con = (HttpURLConnection) connection.openConnection();
55 con.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
56
57 final int responseCode = con.getResponseCode();
58 if (debug)
59 _log.info("Server RESPONSE CODE: " + responseCode);
60 if (responseCode == 200) // OK
61 {
62 // JsonObject json = new JsonObject(new URL("https://l2hotzone.com/api.php?API_KEY=" + server_api + "&SERVER_ID=" + server_id + "&IP=" + server_ip + "&VOTER_IP=" + ip));
63
64 try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())))
65 {
66 String line = null;
67 while ((line = br.readLine()) != null)
68 {
69 // remove characters "{" "}" "\" "result:" and "spaces"
70 line = line.replace("{", "");
71 line = line.replace("}", "");
72 line = line.replace("\"", "");
73 line = line.replace("result:", "");
74 line = line.replace(" ", "");
75 if (debug)
76 _log.info("Server clear string: " + line);
77
78 // split key:value, key:value, ...
79 String[] keySplit = line.split(",");
80
81 int i = 0;
82 while (keySplit != null)
83 {
84 String value = keySplit[i];
85 // split key and value
86 String[] valSplit = value.split(":");
87 String key = valSplit[0];// get key
88 String val = valSplit[1];// get val
89 // set values
90 if (key.equals("status"))
91 {
92 _status = val;
93 }
94 if (key.equals("error_code"))
95 {
96 _error_code = val;
97 }
98 if (key.equals("ServerId"))
99 {
100 _server_id = val;
101 }
102 if (key.equals("ipaddress"))
103 {
104 _server_ip = val;
105 }
106 if (key.equals("Api-Key"))
107 {
108 _server_api = val;
109 }
110 if (key.equals("Ipversion"))
111 {
112 _server_ipv = val;
113 }
114 if (key.equals("voted"))
115 {
116 _voted = Boolean.valueOf(val);
117 }
118
119 if (debug)
120 _log.info("Server READER: key(" + key + ") val(" + val + ")");
121
122 i++;
123 // exit while
124 if (i == 7)
125 break;
126
127 }
128 // validate the response messages
129 // check status and if not valid show error code message
130 if (_status == null || _server_id == null || _server_ip == null || _server_api == null || _server_ipv == null || _voted == null)
131 {
132 _log.info("Vote Error: null values:" + _status);
133 _log.info("Vote Error: null values:" + _server_id);
134 _log.info("Vote Error: null values:" + _server_ip);
135 _log.info("Vote Error: null values:" + _server_api);
136 _log.info("Vote Error: null values:" + _server_ipv);
137 _log.info("Vote Error: null values:" + _voted);
138 return false;
139 }
140 // check status
141 if (!_status.equals("valid"))
142 {
143 _log.info("Vote Error: " + _error_code);
144 return false;
145 }
146 // check server id
147 if (!_server_id.equals(server_id))
148 {
149 _log.info("Vote Error: server ID was not the same: " + _server_id + " should be: " + server_id);
150 return false;
151 }
152 if (!_server_ip.equals(server_ip))
153 {
154 _log.info("Vote Error: server IP was not the same: " + _server_ip + " should be: " + server_ip);
155 return false;
156 }
157 if (!_server_api.equals(server_api))
158 {
159 _log.info("Vote Error: server API was not the same: " + _server_api + " should be: " + server_api);
160 return false;
161 }
162 if (!_server_ipv.equals("IPversion4"))
163 {
164 _log.info("Vote Error: user IP Version was not 4: " + _server_ipv + " should be: IPversion4");
165 return false;
166 }
167
168 if (debug)
169 _log.info("Server Voted: " + _voted);
170
171 return _voted;
172 }
173 }
174 }
175 }
176 catch (Exception e)
177 {
178 e.printStackTrace();
179 }
180 return false;
181 }
182}