mirror of
https://github.com/catspace-dev/unicheckbot.git
synced 2024-11-25 07:13:44 +03:00
tcp port check method
This commit is contained in:
parent
0efd433363
commit
16343d6ea0
|
@ -3,7 +3,7 @@ from gevent.pywsgi import WSGIServer
|
||||||
from helpers import access_token_required
|
from helpers import access_token_required
|
||||||
import config
|
import config
|
||||||
|
|
||||||
from checkers import HttpChecker, ICMPChecker
|
from checkers import HttpChecker, ICMPChecker, TCPPortChecker
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@ -22,6 +22,20 @@ def http_check():
|
||||||
return jsonify(checker.check())
|
return jsonify(checker.check())
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/tcp_port')
|
||||||
|
@access_token_required
|
||||||
|
def tcp_port_check():
|
||||||
|
target = request.args.get("target", None)
|
||||||
|
port = int(request.args.get("port", 80))
|
||||||
|
|
||||||
|
if not target:
|
||||||
|
abort(400)
|
||||||
|
|
||||||
|
checker = TCPPortChecker(target, port)
|
||||||
|
|
||||||
|
return jsonify(checker.check())
|
||||||
|
|
||||||
|
|
||||||
@app.route('/icmp')
|
@app.route('/icmp')
|
||||||
@access_token_required
|
@access_token_required
|
||||||
def icmp_check():
|
def icmp_check():
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
from .http import HttpChecker
|
from .http import HttpChecker
|
||||||
from .icmp import ICMPChecker
|
from .icmp import ICMPChecker
|
||||||
|
from .tcp_port import TCPPortChecker
|
||||||
|
|
|
@ -1,11 +1,42 @@
|
||||||
from core.coretypes import Response
|
from core.coretypes import Response, PortResponse, ResponseStatus, ErrorPayload, ErrorCodes
|
||||||
from .base import BaseChecker
|
from .base import BaseChecker
|
||||||
|
import socket
|
||||||
|
|
||||||
|
|
||||||
class TCPPortChecker(BaseChecker):
|
class TCPPortChecker(BaseChecker):
|
||||||
|
|
||||||
def __init__(self, target: str):
|
def __init__(self, target: str, port: int):
|
||||||
|
self.port = port
|
||||||
super().__init__(target)
|
super().__init__(target)
|
||||||
|
|
||||||
def check(self) -> Response:
|
def check(self) -> Response:
|
||||||
pass
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
# 2 seconds timeout...
|
||||||
|
sock.settimeout(2)
|
||||||
|
|
||||||
|
try:
|
||||||
|
res = sock.connect_ex((self.target, self.port))
|
||||||
|
except socket.gaierror:
|
||||||
|
return Response(
|
||||||
|
status=ResponseStatus.ERROR,
|
||||||
|
payload=ErrorPayload(
|
||||||
|
message="Invalid hostname",
|
||||||
|
code=ErrorCodes.InvalidHostname
|
||||||
|
),
|
||||||
|
node=self.node_info
|
||||||
|
)
|
||||||
|
if res == 0:
|
||||||
|
sock.close()
|
||||||
|
return Response(
|
||||||
|
status=ResponseStatus.OK,
|
||||||
|
payload=PortResponse(open=True),
|
||||||
|
node=self.node_info
|
||||||
|
)
|
||||||
|
sock.close()
|
||||||
|
return Response(
|
||||||
|
status=ResponseStatus.OK,
|
||||||
|
payload=PortResponse(open=False),
|
||||||
|
node=self.node_info
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,7 @@ class ResponseStatus(str, Enum):
|
||||||
class ErrorCodes(IntEnum):
|
class ErrorCodes(IntEnum):
|
||||||
ConnectError = 1
|
ConnectError = 1
|
||||||
ICMPHostNotAlive = 2
|
ICMPHostNotAlive = 2
|
||||||
TCPPortClosed = 3
|
InvalidHostname = 3
|
||||||
UDPPortClosed = 4
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -46,6 +45,11 @@ class APINodeInfo:
|
||||||
location: str
|
location: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class PortResponse(Payload):
|
||||||
|
open: bool
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Response:
|
class Response:
|
||||||
status: ResponseStatus
|
status: ResponseStatus
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "core"
|
name = "core"
|
||||||
version = "0.6.0"
|
version = "0.7.0"
|
||||||
description = "Types and other core functionality"
|
description = "Types and other core functionality"
|
||||||
authors = ["kiriharu <kiriharu@yandex.ru>"]
|
authors = ["kiriharu <kiriharu@yandex.ru>"]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user