Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持配置获取IP的请求头 #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class ConfigReadException(Exception):
"127.0.0.1",
],
"_whitelist_host-desc": "强制白名单HOST,不需要加端口号(即不受其他安全设置影响的HOST)",
"get_ip_header": "X-Real-IP",
"_get_ip_header-desc": "获取真实IP的请求头,不懂请保持默认",
"check_lxm": False,
"_check_lxm-desc": "是否检查lxm请求头(正常的LX Music请求时都会携带这个请求头)",
"lxm_ban": True,
Expand Down
4 changes: 2 additions & 2 deletions main-flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def _404(_):
@app.before_request
def check():
# nginx proxy header
if (request.headers.get("X-Real-IP")):
request.remote_addr = request.headers.get("X-Real-IP")
if (request.headers.get(config.read_config("security.get_ip_header"))):
request.remote_addr = request.headers.get(config.read_config("security.get_ip_header"))
# check ip
if (config.check_ip_banned(request.remote_addr)):
return handleResult({"code": 1, "msg": "您的IP已被封禁", "data": None}), 403
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ async def handle_before_request(app, handler):
async def handle_request(request):
try:
# nginx proxy header
if (request.headers.get("X-Real-IP")):
request.remote_addr = request.headers.get("X-Real-IP")
if (request.headers.get(config.read_config("security.get_ip_header"))):
request.remote_addr = request.headers.get(config.read_config("security.get_ip_header"))
else:
request.remote_addr = request.remote
# check ip
Expand Down