Skip to content

Commit 98d02d2

Browse files
committed
fix: add cli-to-api example
1 parent 1b80679 commit 98d02d2

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

urlcrazy/Containerfile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM kalilinux/kali-last-release
2+
3+
RUN useradd -ms /bin/bash kali \
4+
&& apt-get update \
5+
&& apt-get install --no-install-recommends -y \
6+
urlcrazy \
7+
python3 \
8+
python3-venv \
9+
&& rm -rf /var/lib/apt/lists/* \
10+
&& mkdir -p /opt/urlcrazy \
11+
&& chown -R kali:kali /opt/urlcrazy
12+
13+
COPY --chown=kali:kali main.py /opt/urlcrazy/main.py
14+
COPY --chown=kali:kali pyproject.toml /opt/urlcrazy/pyproject.toml
15+
16+
USER kali
17+
18+
WORKDIR /opt/urlcrazy
19+
20+
RUN python3 -m venv env \
21+
&& env/bin/pip install --no-cache-dir -U pip .
22+
23+
ENV PATH="/opt/urlcrazy/env/bin:$PATH"
24+
25+
CMD [ "uvicorn", "--host", "0.0.0.0", "--port", "4000", "main:app" ]

urlcrazy/main.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import subprocess
2+
3+
from fastapi import FastAPI, HTTPException, Response
4+
5+
app = FastAPI()
6+
7+
8+
@app.get("/urlcrazy")
9+
def urlcrazy(
10+
keyboard: str = "qwerty",
11+
popularity: bool = False,
12+
no_resolve: bool = False,
13+
show_invalid: bool = False,
14+
format: str = "json",
15+
nocolor: bool = False,
16+
debug: bool = False,
17+
version: bool = False,
18+
help: bool = False,
19+
timeout: int = None,
20+
domain_name: str = None,
21+
):
22+
cmd = ["urlcrazy"]
23+
cmd.append(f"--keyboard={keyboard}")
24+
cmd.append(f"--format={format}")
25+
26+
if popularity:
27+
cmd.append("--popularity")
28+
29+
if no_resolve:
30+
cmd.append("--no-resolve")
31+
32+
if show_invalid:
33+
cmd.append("--show-invalid")
34+
35+
if nocolor:
36+
cmd.append("--nocolor")
37+
38+
if debug:
39+
cmd.append("--debug")
40+
41+
if version:
42+
cmd.append("--version")
43+
44+
if help:
45+
cmd.append("--help")
46+
47+
if domain_name:
48+
cmd.append(domain_name)
49+
50+
response = None
51+
try:
52+
response = subprocess.check_output(cmd, timeout=timeout)
53+
except Exception as e:
54+
raise HTTPException(status_code=500, detail=str(e))
55+
56+
resp_format = format.lower()
57+
58+
if help or version or not domain_name:
59+
resp_format = "text"
60+
61+
return Response(
62+
content=response.strip(),
63+
headers={"Content-Type": f"application/{resp_format}"},
64+
)

urlcrazy/pyproject.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "urlcrazy"
7+
version = "1.0.0"
8+
authors = [
9+
{ name="Castlecraft Ecommerce Pvt. Ltd.", email="[email protected]" },
10+
]
11+
description = "FastAPI for urlcrazy"
12+
requires-python = ">=3.7"
13+
dependencies = [
14+
"fastapi==0.95.0",
15+
"uvicorn==0.21.1",
16+
]

0 commit comments

Comments
 (0)