forked from Night-Stalkers/lm-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipcheck.py
More file actions
269 lines (211 loc) · 8.73 KB
/
ipcheck.py
File metadata and controls
269 lines (211 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
ipcheck.py
Copyright (C) 2020 Night-Stalkers
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
# *-!-* EXPERIMENTAL *-!- #
# *-!-* UNDER TESTING *-!- #
"""
ipcheck
REQUIRES requests AND IP.py
Checks player's IP addresses using the proxycheck.io API.
To function correctly, a file called "api_key.txt" containing
a valid proxycheck.io API key must exist inside the "ipcheck_data"
directory.
ipcheck automatically caches IP info requests for future queries,
this cache expires after a day, by default.
Needs to be tested a lot.
Changelog:
1.1.0:
* Now you can choose between using an API key file or just putting it directly in here. (USE_API_KEY_FILE)
"""
from IP import IP
import os
import requests
import json
from time import time
from commands import add, admin
from twisted.internet import reactor
NAME = "ipcheck"
VERSION = "1.1.0"
AUTHOR = "Hourai (Yui)"
# If True, the script will look for an API key file in DATA_DIR (default)
# If False, you must place your API Key into the API_KEY variable.
USE_API_KEY_FILE = True
API_KEY = "Replace this with your API key if USE_API_KEY_FILE is False."
DATA_DIR = "./scripts/ipcheck_data/"
API_KEY_FILENAME = "api_key.txt"
IP_CACHE_FILENAME = "ip_cache"
PRINT_API_KEY = False # Will print your API key in the server console if enabled, use with care.
REQUEST_FORMAT = "http://proxycheck.io/v2/%s?key=%s&risk=1&vpn=1"
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" \
"Chrome/42.0.2311.135 Safari/537.36 Edge/12.246"
HTTP_OK = 200
HTTP_FORBIDDEN = 403
HTTP_NOT_FOUND = 404
HTTP_INTERNAL_SERVER_ERROR = 500
HTTP_BAD_GATEWAY = 502
# Unix Time, seconds
ONE_MINUTE = 60
ONE_HOUR = 60 * ONE_MINUTE
ONE_DAY = 24 * ONE_HOUR
CACHE_EXPIRE_TIME = ONE_DAY
def log_msg(msg, protocol, print_name=True, warn=False, irc=True, info=False):
irc_relay = protocol.irc_relay
print "%s: %s" % (NAME, msg)
if print_name:
msg = "%s: %s" % (NAME, msg)
if warn: # sets red text color for IRC warning message
msg = '\x0304' + msg + '\x0f'
elif info:
msg = '\x0302' + msg + '\x0f'
try:
if irc and irc_relay.factory.bot and irc_relay.factory.bot.colors:
irc_relay.send(msg)
except AttributeError as e:
print "%s: irc bot not initialized." % NAME
return
class IPCheck(object):
def __init__(self, proto):
self.has_api_key = False
self.api_key = ""
self.enabled = True
self.proto = proto
self.ip_cache = dict()
self.current_timestamp = int(time())
self.last_timestamp = 0
log_msg("running version %s by %s" % (VERSION, AUTHOR), self.proto)
def start(self):
if USE_API_KEY_FILE:
self.has_api_key = self._load_api_key()
else:
log_msg("API key file disabled. Using API_KEY set in script file.", self.proto)
self.api_key = API_KEY
self.has_api_key = True
if not self.has_api_key:
self.enabled = False
log_msg("API KEY NOT FOUND, IPCHECK DISABLED!", self.proto, warn=True)
if PRINT_API_KEY:
log_msg("API_KEY = [%s]" % self.api_key, self.proto, irc=False)
self._load_ip_cache_file()
if "TS" in self.ip_cache:
self.last_timestamp = self.ip_cache["TS"]
else:
self.last_timestamp = self.current_timestamp
self.ip_cache["TS"] = self.current_timestamp
if self._check_timestamps(CACHE_EXPIRE_TIME):
log_msg("IP cache file is too old, cache file ignored.", self.proto)
self.ip_cache = {}
log_msg("ipcheck is now ready!", self.proto, info=True)
def check_ip(self, ip):
if self.enabled:
resp, cached = self._get_ip_info(ip)
resp[u'cached'] = cached
return resp
else:
return {u'IPCHECK DISABLED!'}
def dump(self):
log_msg("Saving cache file to disk.", self.proto)
self._dump_ip_cache_to_file()
def verify_current_cache(self, time_period):
log_msg("Verifying current IP cache...", self.proto)
current_time = int(time())
diff = current_time - self.last_timestamp
if diff > time_period:
log_msg("IP cache too old, emptying cache.", self.proto)
self.last_timestamp = current_time
self.current_timestamp = current_time
self.ip_cache = {"TS": current_time}
else:
log_msg("Current IP cache OK.", self.proto)
def _get_ip_info(self, ip):
if ip == IP("127.0.0.1") or ip == IP("255.255.255.255") or ip.ip[0] == 192:
return json.loads('{"status": "ok", "127.0.0.1": {"type":"localhost"}}'), False
if self._ip_already_exists(ip):
return self._get_ip_info_from_cache(ip), True
r = requests.get(REQUEST_FORMAT % (ip, self.api_key))
if r.status_code == HTTP_OK:
self._new_cache_entry(ip, r.json())
return r.json(), False
elif r.status_code == HTTP_FORBIDDEN:
return json.loads('{"status": "forbidden"}'), False
elif r.status_code == HTTP_BAD_GATEWAY:
return json.loads('{"status": "bad_gateway"}'), False
elif r.status_code == HTTP_NOT_FOUND:
return json.loads('{"status": "not_found"}'), False
elif r.status_code == HTTP_INTERNAL_SERVER_ERROR:
return json.loads('{"status": "internal_server_error"}'), False
def _load_ip_cache_file(self):
#log_msg(os.getcwd(), self.proto)
if os.path.exists(DATA_DIR + IP_CACHE_FILENAME):
log_msg("Cache file found.", self.proto, irc=True)
with open(DATA_DIR + IP_CACHE_FILENAME, "r") as fcache:
cache = fcache.read()
self.ip_cache = json.loads(cache)
log_msg("Cache file loaded.", self.proto, irc=True)
return True
else:
log_msg("Cache file not found.", self.proto, irc=True)
return False
def _dump_ip_cache_to_file(self):
#log_msg(os.getcwd(), self.proto)
with open(DATA_DIR + IP_CACHE_FILENAME, "w+") as fcache:
cache = json.dumps(self.ip_cache)
fcache.write(cache)
def _get_ip_info_from_cache(self, ip):
for ip_record in self.ip_cache:
if ip_record == ip.ip_str:
return self.ip_cache[ip_record]
def _ip_already_exists(self, ip):
return ip.ip_str in self.ip_cache
def _load_api_key(self):
if os.path.exists(DATA_DIR + API_KEY_FILENAME):
with open(DATA_DIR + API_KEY_FILENAME) as f:
lines = []
for line in f:
lines.append(line.rstrip())
self.api_key = lines[0]
log_msg("API KEY found!", self.proto)
return True
else:
log_msg("API key file not found.", self.proto, irc=False)
return False
def _new_cache_entry(self, ip, info):
self.ip_cache[ip.ip_str] = info
def _check_timestamps(self, time_period):
diff = self.current_timestamp - self.last_timestamp
if self.current_timestamp == self.last_timestamp:
return False
elif diff > time_period:
return True
else:
return False
def apply_script(protocol, connection, config):
class IPCheckProtocol(protocol):
def __init__(self, *args, **kwargs):
protocol.__init__(self, *args, **kwargs)
self.ipcheck = IPCheck(self)
self.test = 1
reactor.callLater(5, self.ipcheck.start)
def on_map_leave(self):
self.ipcheck.verify_current_cache(CACHE_EXPIRE_TIME)
self.ipcheck.dump()
return protocol.on_map_leave(self)
class IPCheckConnection(connection):
def __init__(self, *args, **kwargs):
connection.__init__(self, *args, **kwargs)
def on_login(self, name):
ip = IP(self.address[0])
result = self.protocol.ipcheck.check_ip(ip)
log_msg("Player \"%s\" IP ADRRESS INFO: %s" % (name, str(result)), self.protocol, info=True)
return connection.on_login(self, name)
return IPCheckProtocol, IPCheckConnection