-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from ninoseki/query-lookup
feat: quick query lookup
- Loading branch information
Showing
19 changed files
with
803 additions
and
527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from fastapi import APIRouter | ||
|
||
from backend.api.endpoints import fingerprint | ||
from backend.api.endpoints import counters, fingerprint | ||
|
||
api_router = APIRouter() | ||
api_router.include_router( | ||
fingerprint.router, prefix="/fingerprint", tags=["fingerprint"] | ||
) | ||
api_router.include_router(counters.router, prefix="/counters", tags=["counters"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends, HTTPException, status | ||
|
||
from backend import schemas, services, settings | ||
|
||
router = APIRouter() | ||
|
||
|
||
def get_shodan(): | ||
if settings.SHODAN_API_KEY is None: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="Shodan is not configured correctly", | ||
) | ||
|
||
return services.Shodan(settings.SHODAN_API_KEY) | ||
|
||
|
||
def get_censys(): | ||
if settings.CENSYS_APP_ID is None or settings.CENSYS_SECRET is None: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="Censys is not configured correctly", | ||
) | ||
|
||
return services.Censys(settings.CENSYS_APP_ID, settings.CENSYS_SECRET) | ||
|
||
|
||
Shodan = Annotated[services.Shodan, Depends(get_shodan)] | ||
Censys = Annotated[services.Censys, Depends(get_censys)] | ||
|
||
|
||
@router.get("/shodan") | ||
async def shodan(query: str, *, shodan: Shodan) -> schemas.Count: | ||
return schemas.Count(count=await shodan.call(query)) | ||
|
||
|
||
@router.get("/censys") | ||
async def censys(query: str, *, censys: Censys): | ||
return schemas.Count(count=await censys.call(query)) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import httpx | ||
from starlette.datastructures import Secret | ||
|
||
|
||
class Shodan: | ||
def __init__(self, api_key: Secret): | ||
self.api_key = api_key | ||
|
||
async def call(self, query: str): | ||
async with httpx.AsyncClient(base_url="https://api.shodan.io") as client: | ||
res = await client.get( | ||
"/shodan/host/search", params={"key": str(self.api_key), "query": query} | ||
) | ||
res.raise_for_status() | ||
|
||
return res.json()["total"] | ||
|
||
|
||
class Censys: | ||
def __init__( | ||
self, | ||
app_id: Secret, | ||
secret: Secret, | ||
): | ||
self.app_id = app_id | ||
self.secret = secret | ||
|
||
async def call(self, query: str): | ||
auth = httpx.BasicAuth(username=str(self.app_id), password=str(self.secret)) | ||
|
||
async with httpx.AsyncClient( | ||
base_url="https://search.censys.io", auth=auth | ||
) as client: | ||
res = await client.get("/api/v2/hosts/search", params={"q": query}) | ||
res.raise_for_status() | ||
|
||
return res.json()["result"]["total"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.