Skip to content

Commit 8465ac6

Browse files
committed
update oxl-utils usage for api
1 parent 703555c commit 8465ac6

File tree

2 files changed

+10
-44
lines changed

2 files changed

+10
-44
lines changed

src/api/main.py

+8-43
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from flask import Flask, request, Response, json, redirect
1414
from waitress import serve
1515
import maxminddb
16+
from oxl_utils.valid.net import valid_ip4, valid_public_ip, valid_asn
1617

1718
app = Flask('risk-db')
1819
BASE_DIR = Path('/var/local/lib/risk-db')
@@ -33,42 +34,6 @@
3334
report_lock = Lock()
3435

3536

36-
def _valid_ipv4(ip: str) -> bool:
37-
try:
38-
IPv4Address(ip)
39-
return True
40-
41-
except AddressValueError:
42-
return False
43-
44-
45-
def _valid_public_ip(ip: str) -> bool:
46-
ip = str(ip)
47-
try:
48-
ip = IPv4Address(ip)
49-
return ip.is_global and \
50-
not ip.is_loopback and \
51-
not ip.is_reserved and \
52-
not ip.is_multicast and \
53-
not ip.is_link_local
54-
55-
except AddressValueError:
56-
try:
57-
ip = IPv6Address(ip)
58-
return ip.is_global and \
59-
not ip.is_loopback and \
60-
not ip.is_reserved and \
61-
not ip.is_multicast and \
62-
not ip.is_link_local
63-
64-
except AddressValueError:
65-
return False
66-
67-
68-
def _valid_asn(_asn: str) -> bool:
69-
return _asn.isdigit() and 0 <= int(_asn) <= 4_294_967_294
70-
71-
7237
def _safe_comment(cmt: str) -> str:
7338
return regex_replace(r'[^\sa-zA-Z0-9_=+.-]', '', cmt)[:50]
7439

@@ -82,14 +47,14 @@ def _response_json(code: int, data: dict) -> Response:
8247

8348

8449
def _get_ipv(ip: str) -> int:
85-
if _valid_ipv4(ip):
50+
if valid_ip4(ip):
8651
return 4
8752

8853
return 6
8954

9055

9156
def _get_src_ip() -> str:
92-
if _valid_public_ip(request.remote_addr):
57+
if valid_public_ip(request.remote_addr):
9358
return request.remote_addr
9459

9560
if 'X-Real-IP' in request.headers:
@@ -112,7 +77,7 @@ def report() -> Response:
11277
if 'ip' in data and data['ip'].startswith('::ffff:'):
11378
data['ip'] = data['ip'].replace('::ffff:', '')
11479

115-
if 'ip' not in data or not _valid_public_ip(data['ip']):
80+
if 'ip' not in data or not valid_public_ip(data['ip']):
11681
return _response_json(code=400, data={'msg': 'Invalid IP provided'})
11782

11883
if 'cat' not in data or data['cat'].lower() not in RISK_CATEGORIES:
@@ -123,7 +88,7 @@ def report() -> Response:
12388

12489
r = {
12590
'ip': data['ip'], 'cat': data['cat'].lower(), 'time': int(time()),
126-
'v': 4 if _valid_ipv4(data['ip']) else 6, 'cmt': None, 'token': None, 'by': _get_src_ip,
91+
'v': 4 if valid_ip4(data['ip']) else 6, 'cmt': None, 'token': None, 'by': _get_src_ip,
12792
}
12893

12994
if 'cmt' in data:
@@ -145,7 +110,7 @@ def check(ip) -> Response:
145110
if ip.startswith('::ffff:'):
146111
ip = ip.replace('::ffff:', '')
147112

148-
if not _valid_public_ip(ip):
113+
if not valid_public_ip(ip):
149114
return _response_json(code=400, data={'msg': 'Invalid IP provided'})
150115

151116
try:
@@ -168,7 +133,7 @@ def check_net(ip) -> Response:
168133
if ip.find('/') != -1:
169134
ip = ip.split('/', 1)[0]
170135

171-
if not _valid_public_ip(ip):
136+
if not valid_public_ip(ip):
172137
return _response_json(code=400, data={'msg': 'Invalid IP provided'})
173138

174139
ipv = _get_ipv(ip)
@@ -191,7 +156,7 @@ def check_net(ip) -> Response:
191156

192157
@app.route('/api/asn/<nr>', methods=['GET'])
193158
def check_asn(nr) -> Response:
194-
if not _valid_asn(nr):
159+
if not valid_asn(nr):
195160
return _response_json(code=400, data={'msg': 'Invalid ASN provided'})
196161

197162
try:

src/api/requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
flask
22
waitress
3-
maxminddb
3+
maxminddb
4+
oxl-utils

0 commit comments

Comments
 (0)