initial commit

This commit is contained in:
kiriharu 2021-01-03 00:56:55 +03:00
commit 68960dd75e
20 changed files with 345 additions and 0 deletions

128
.gitignore vendored Normal file
View File

@ -0,0 +1,128 @@
# Editors
.vscode/
.idea/
# Vagrant
.vagrant/
# Mac/OSX
.DS_Store
# Windows
Thumbs.db
# Source for the following rules: https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# poetry lock
poetry.lock

0
apps/__init__.py Normal file
View File

0
apps/api/README.rst Normal file
View File

0
apps/api/__init__.py Normal file
View File

0
apps/api/api/__init__.py Normal file
View File

34
apps/api/api/app.py Normal file
View File

@ -0,0 +1,34 @@
from flask import Flask, request, abort, jsonify
from gevent.pywsgi import WSGIServer
from gevent import monkey
from helpers import access_token_required
import config
monkey.patch_all()
# Monkey patch SSL in requests to prevent RecursionError! DO NOT REMOVE OR MOVE!
from checkers.http import HttpChecker
app = Flask(__name__)
@app.route('/http')
@access_token_required
def http_check():
target = request.args.get("target", None)
port = int(request.args.get("port", 80))
if not target:
abort(400)
checker = HttpChecker(target, port)
return jsonify(checker.check())
def main():
http = WSGIServer(('', config.APP_PORT), app.wsgi_app)
http.serve_forever()
if __name__ == '__main__':
main()

View File

View File

@ -0,0 +1,12 @@
from core.coretypes import Response
from abc import ABC
class BaseChecker(ABC):
def __init__(self, target: str, port: int):
self.target = target
self.port = port
def check(self) -> Response:
raise NotImplementedError

View File

@ -0,0 +1,49 @@
from core.coretypes import (
Response, HttpCheckerResponse,
ResponseStatus, HttpErrorCodes, ErrorPayload
)
from requests import Session
from requests.exceptions import ConnectionError
from .base import BaseChecker
import time
import re
class HttpChecker(BaseChecker):
default_schema = "http://"
default_schema_re = re.compile("^[hH][tT][tT][pP].*")
def __init__(self, target: str, port: int):
super(HttpChecker, self).__init__(target, port)
self.session = Session()
def check(self) -> Response:
url = f"{self.target}:{self.port}"
if not self.default_schema_re.match(url):
url = f"{self.default_schema}{url}"
start_time = time.time()
try:
request = self.session.get(
url
)
except ConnectionError:
return Response(
status=ResponseStatus.ERROR,
payload=ErrorPayload(
message="Failed to establish a new connection",
code=HttpErrorCodes.ConnectError
)
)
end_time = time.time()
return Response(
status=ResponseStatus.OK,
payload=HttpCheckerResponse(
time=end_time - start_time,
status_code=request.status_code
)
)

13
apps/api/api/config.py Normal file
View File

@ -0,0 +1,13 @@
import os
# APP PORT
APP_PORT = os.environ.get("PORT", 8080)
# Node name. Will be shown in tgbot
NODE_NAME = os.environ.get("NODE_NAME", "Default node")
# Node location. Will be shown in tgbot
NODE_LOCATION = os.environ.get("NODE_LOCATION", "Undefined Location")
# Access token. Will be used for requests
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN", "CHANGE_TOKEN_BY_ENV")

13
apps/api/api/helpers.py Normal file
View File

@ -0,0 +1,13 @@
from functools import wraps
from flask import request, abort
from config import ACCESS_TOKEN
def access_token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
if token := request.args.get('token', None):
if token == ACCESS_TOKEN:
return f(*args, **kwargs)
abort(403)
return decorated

19
apps/api/pyproject.toml Normal file
View File

@ -0,0 +1,19 @@
[tool.poetry]
name = "api"
version = "0.1.0"
description = "Host report API"
authors = ["kiriharu <kiriharu@yandex.ru>"]
[tool.poetry.dependencies]
python = "^3.8.2"
Flask = "^1.1.2"
gevent = "^20.12.1"
requests = "^2.25.1"
core = {path = "../core"}
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

0
apps/core/README.rst Normal file
View File

0
apps/core/__init__.py Normal file
View File

View File

@ -0,0 +1,33 @@
from dataclasses import dataclass
from enum import Enum, IntEnum
class Payload:
pass
class ResponseStatus(str, Enum):
OK = "ok"
ERROR = "error"
class HttpErrorCodes(IntEnum):
ConnectError = 1
@dataclass
class ErrorPayload(Payload):
message: str
code: HttpErrorCodes
@dataclass
class HttpCheckerResponse(Payload):
status_code: int
time: float
@dataclass
class Response:
status: ResponseStatus
payload: Payload

14
apps/core/pyproject.toml Normal file
View File

@ -0,0 +1,14 @@
[tool.poetry]
name = "core"
version = "0.1.0"
description = "Types and other core functionality"
authors = ["kiriharu <kiriharu@yandex.ru>"]
[tool.poetry.dependencies]
python = "^3.8"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

0
apps/tgbot/README.rst Normal file
View File

0
apps/tgbot/__init__.py Normal file
View File

16
apps/tgbot/pyproject.toml Normal file
View File

@ -0,0 +1,16 @@
[tool.poetry]
name = "tgbot"
version = "0.1.0"
description = "Telegram bot"
authors = ["kiriharu <kiriharu@yandex.ru>"]
[tool.poetry.dependencies]
python = "^3.8"
aiogram = "^2.11.2"
core = {path = "../core"}
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

14
pyproject.toml Normal file
View File

@ -0,0 +1,14 @@
[tool.poetry]
name = "host_report"
version = "0.1.0"
description = ""
authors = ["kiriharu <kiriharu@yandex.ru>"]
[tool.poetry.dependencies]
python = "^3.8.2"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"