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

Adding CURL feature #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ EXPOSE ${HTTP_PORT}
WORKDIR /app

RUN apt-get update
RUN apt-get install -y mtr-tiny iputils-ping
RUN apt-get install -y mtr-tiny iputils-ping curl

RUN pip install --no-cache-dir poetry
COPY pyproject.toml poetry.lock ./
Expand Down
29 changes: 27 additions & 2 deletions glass.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,29 @@ def api_mtr(target: str) -> str:
return do("mtr", target=target)


@app.route("/curl/<target>")
@app.route("/api/curl/<target>")
def api_curl(target: str) -> str:
log(request, method="curl", target=target)
return do("curl", target)



def do(method: str, target: str) -> str:
output = ""
result = None

target = sanitize(target)
if method == "curl":
target = sanitize2(target)
else:
target = sanitize(target)
print(target)
if target:
if method == "ping":
result = ping(target)
elif method == "mtr":
result = mtr(target)
elif method == "curl":
result = curl(target)
else:
output = "You gave me an invalid method!"

Expand All @@ -78,6 +91,10 @@ def mtr(dest: str, count: int = 10) -> CompletedProcess[bytes]:
["mtr", dest, "-r", "-w", "-c", str(count)], capture_output=True
)

def curl(dest: str, count: int = 10) -> CompletedProcess[bytes]:
return subprocess.run(
["curl", "-v", dest ], capture_output=True
)

def sanitize(dirty_target: str) -> str:
match = re.match("([\w\.\:\-\_]+)", dirty_target)
Expand All @@ -88,6 +105,14 @@ def sanitize(dirty_target: str) -> str:

return target

def sanitize2(dirty_target: str) -> str:
match = re.match("(http|https)://[\w\-]+(\.[\w\-]+)+\S*", dirty_target)
if match:
target = match.group(0)
else:
target = ""

return target

def log(
request: Request, method: Optional[str] = None, target: Optional[str] = None
Expand Down