Skip to content

Commit

Permalink
update oxl-utils usage for api
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Nov 4, 2024
1 parent 703555c commit 8465ac6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 44 deletions.
51 changes: 8 additions & 43 deletions src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from flask import Flask, request, Response, json, redirect
from waitress import serve
import maxminddb
from oxl_utils.valid.net import valid_ip4, valid_public_ip, valid_asn

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


def _valid_ipv4(ip: str) -> bool:
try:
IPv4Address(ip)
return True

except AddressValueError:
return False


def _valid_public_ip(ip: str) -> bool:
ip = str(ip)
try:
ip = IPv4Address(ip)
return ip.is_global and \
not ip.is_loopback and \
not ip.is_reserved and \
not ip.is_multicast and \
not ip.is_link_local

except AddressValueError:
try:
ip = IPv6Address(ip)
return ip.is_global and \
not ip.is_loopback and \
not ip.is_reserved and \
not ip.is_multicast and \
not ip.is_link_local

except AddressValueError:
return False


def _valid_asn(_asn: str) -> bool:
return _asn.isdigit() and 0 <= int(_asn) <= 4_294_967_294


def _safe_comment(cmt: str) -> str:
return regex_replace(r'[^\sa-zA-Z0-9_=+.-]', '', cmt)[:50]

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


def _get_ipv(ip: str) -> int:
if _valid_ipv4(ip):
if valid_ip4(ip):
return 4

return 6


def _get_src_ip() -> str:
if _valid_public_ip(request.remote_addr):
if valid_public_ip(request.remote_addr):
return request.remote_addr

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

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

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

r = {
'ip': data['ip'], 'cat': data['cat'].lower(), 'time': int(time()),
'v': 4 if _valid_ipv4(data['ip']) else 6, 'cmt': None, 'token': None, 'by': _get_src_ip,
'v': 4 if valid_ip4(data['ip']) else 6, 'cmt': None, 'token': None, 'by': _get_src_ip,
}

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

if not _valid_public_ip(ip):
if not valid_public_ip(ip):
return _response_json(code=400, data={'msg': 'Invalid IP provided'})

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

if not _valid_public_ip(ip):
if not valid_public_ip(ip):
return _response_json(code=400, data={'msg': 'Invalid IP provided'})

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

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

try:
Expand Down
3 changes: 2 additions & 1 deletion src/api/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
flask
waitress
maxminddb
maxminddb
oxl-utils

0 comments on commit 8465ac6

Please sign in to comment.