Skip to content

Commit

Permalink
Add endpoints for public IP address and request header debugging (#26)
Browse files Browse the repository at this point in the history
# Description
Add `util` router with `client_ip` and `headers` endpoints for
connection debugging
Allows all forwarded IPs to get real client IP address instead of proxy
IP

# Issues
Relates to
OpenVoiceOS/ovos-PHAL-plugin-ipgeo#12

# Other Notes
Currently deployed at hana.neonaialpha.com for testing

---------

Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored May 15, 2024
1 parent c57270b commit 77b8c09
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions neon_hana/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from neon_hana.app.routers.llm import llm_route
from neon_hana.app.routers.mq_backend import mq_route
from neon_hana.app.routers.auth import auth_route
from neon_hana.app.routers.util import util_route
from neon_hana.version import __version__


Expand All @@ -45,5 +46,6 @@ def create_app(config: dict):
app.include_router(proxy_route)
app.include_router(mq_route)
app.include_router(llm_route)
app.include_router(util_route)

return app
2 changes: 1 addition & 1 deletion neon_hana/app/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def main():
config = Configuration().get("hana", {})
app = create_app(config)
uvicorn.run(app, host=config.get('server_host', "0.0.0.0"),
port=config.get('port', 8080))
port=config.get('port', 8080), forwarded_allow_ips="*")


if __name__ == "__main__":
Expand Down
44 changes: 44 additions & 0 deletions neon_hana/app/routers/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
# All trademark and other rights reserved by their respective owners
# Copyright 2008-2021 Neongecko.com Inc.
# BSD-3
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from fastapi import APIRouter, Request
from starlette.responses import PlainTextResponse

from neon_hana.app.dependencies import client_manager

util_route = APIRouter(prefix="/util", tags=["utilities"])


@util_route.get("/client_ip", response_class=PlainTextResponse)
async def api_client_ip(request: Request) -> str:
client_manager.validate_auth("", request.client.host)
return request.client.host


@util_route.get("/headers")
async def api_headers(request: Request):
client_manager.validate_auth("", request.client.host)
return request.headers
2 changes: 1 addition & 1 deletion neon_hana/auth/client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def validate_auth(self, token: str, origin_ip: str) -> bool:
max_tokens=self._rpm))
if not self.rate_limiter.consume(origin_ip) and self._rpm > 0:
raise HTTPException(status_code=429,
detail=f"Requests limited to {self._rpm}/min"
detail=f"Requests limited to {self._rpm}/min "
f"per client connection")

if self._disable_auth:
Expand Down

0 comments on commit 77b8c09

Please sign in to comment.