Skip to content

Commit cf28d29

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

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

src/api/main.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
from ipaddress import IPv4Address, IPv6Address, AddressValueError, IPv4Interface, IPv6Interface
3+
from ipaddress import IPv4Interface, IPv6Interface
44
from re import sub as regex_replace
55
from threading import Lock
66
from json import dumps as json_dumps
@@ -13,7 +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
16+
from oxl_utils.valid.net import valid_ip4, valid_public_ip, valid_asn, get_ipv
1717

1818
app = Flask('risk-db')
1919
BASE_DIR = Path('/var/local/lib/risk-db')
@@ -46,13 +46,6 @@ def _response_json(code: int, data: dict) -> Response:
4646
)
4747

4848

49-
def _get_ipv(ip: str) -> int:
50-
if valid_ip4(ip):
51-
return 4
52-
53-
return 6
54-
55-
5649
def _get_src_ip() -> str:
5750
if valid_public_ip(request.remote_addr):
5851
return request.remote_addr
@@ -114,7 +107,7 @@ def check(ip) -> Response:
114107
return _response_json(code=400, data={'msg': 'Invalid IP provided'})
115108

116109
try:
117-
with maxminddb.open_database(RISKY_DB_FILE[_get_ipv(ip)]) as m:
110+
with maxminddb.open_database(RISKY_DB_FILE[get_ipv(ip)]) as m:
118111
r = m.get(ip)
119112
if r is None:
120113
return _response_json(code=404, data={'msg': 'Provided IP not reported'})
@@ -136,7 +129,7 @@ def check_net(ip) -> Response:
136129
if not valid_public_ip(ip):
137130
return _response_json(code=400, data={'msg': 'Invalid IP provided'})
138131

139-
ipv = _get_ipv(ip)
132+
ipv = get_ipv(ip)
140133

141134
if ipv == 4:
142135
net = IPv4Interface(f"{ip}/{NET_SIZE[ipv]}").network.network_address.compressed

0 commit comments

Comments
 (0)