Skip to content

Commit 9027c6b

Browse files
committed
update ::ffff: ip4-prefix handling
1 parent b0a2395 commit 9027c6b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/api/main.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ def _get_src_ip() -> str:
5151
return request.remote_addr
5252

5353
if 'X-Real-IP' in request.headers:
54-
return request.headers['X-Real-IP'].replace('::ffff:', '')
54+
ip = request.headers['X-Real-IP']
5555

56-
if 'X-Forwarded-For' in request.headers:
57-
return request.headers['X-Forwarded-For'].replace('::ffff:', '')
56+
elif 'X-Forwarded-For' in request.headers:
57+
ip = request.headers['X-Forwarded-For']
5858

59-
return request.remote_addr
59+
else:
60+
ip = request.remote_addr
61+
62+
if ip.startswith('::ffff:'):
63+
return ip[7:]
64+
65+
return ip
6066

6167

6268
# curl -XPOST https://risk.oxl.app/api/report --data '{"ip": "1.1.1.1", "cat": "bot"}' -H 'Content-Type: application/json'
@@ -67,8 +73,12 @@ def report() -> Response:
6773

6874
data = request.get_json()
6975

70-
if 'ip' in data and data['ip'].startswith('::ffff:'):
71-
data['ip'] = data['ip'].replace('::ffff:', '')
76+
if 'ip' in data:
77+
if data['ip'].startswith('::ffff:'):
78+
data['ip'] = data['ip'][7:]
79+
80+
if data['ip'].endswith('.x'):
81+
data['ip'] = f"{data['ip'][:-1]}0"
7282

7383
if 'ip' not in data or not valid_public_ip(data['ip']):
7484
return _response_json(code=400, data={'msg': 'Invalid IP provided'})
@@ -101,7 +111,7 @@ def report() -> Response:
101111
@app.route('/api/ip/<ip>', methods=['GET'])
102112
def check(ip) -> Response:
103113
if ip.startswith('::ffff:'):
104-
ip = ip.replace('::ffff:', '')
114+
ip = ip[7:]
105115

106116
if not valid_public_ip(ip):
107117
return _response_json(code=400, data={'msg': 'Invalid IP provided'})
@@ -121,7 +131,7 @@ def check(ip) -> Response:
121131
@app.route('/api/net/<ip>', methods=['GET'])
122132
def check_net(ip) -> Response:
123133
if ip.startswith('::ffff:'):
124-
ip = ip.replace('::ffff:', '')
134+
ip = ip[7:]
125135

126136
if ip.find('/') != -1:
127137
ip = ip.split('/', 1)[0]

0 commit comments

Comments
 (0)