mirror of
https://github.com/gnh1201/caterpillar.git
synced 2025-09-06 18:01:07 +00:00
Update server.py
This commit is contained in:
parent
aabf0d0d6d
commit
2288afe57d
108
server.py
108
server.py
|
@ -7,7 +7,7 @@
|
|||
# Namyheon Go (Catswords Research) <gnh1201@gmail.com>
|
||||
# https://github.com/gnh1201/caterpillar
|
||||
# Created at: 2022-10-06
|
||||
# Updated at: 2024-03-13
|
||||
# Updated at: 2024-05-20
|
||||
#
|
||||
|
||||
import argparse
|
||||
|
@ -33,6 +33,8 @@ from requests.auth import HTTPBasicAuth
|
|||
from urllib.parse import urlparse
|
||||
from decouple import config
|
||||
|
||||
from base import Extension, jsonrpc2_create_id, jsonrpc2_encode, jsonrpc2_result_encode
|
||||
|
||||
def extract_credentials(url):
|
||||
pattern = re.compile(r'(?P<scheme>\w+://)?(?P<username>[^:/]+):(?P<password>[^@]+)@(?P<url>.+)')
|
||||
match = pattern.match(url)
|
||||
|
@ -44,7 +46,7 @@ def extract_credentials(url):
|
|||
return username, password, scheme + url
|
||||
else:
|
||||
return None, None, url
|
||||
|
||||
|
||||
# initalization
|
||||
try:
|
||||
listening_port = config('PORT', default=5555, cast=int)
|
||||
|
@ -75,32 +77,15 @@ buffer_size = args.buffer_size
|
|||
accepted_relay = {}
|
||||
resolved_address_list = []
|
||||
|
||||
# set environment of Extension
|
||||
Extension.set_buffer_size(buffer_size)
|
||||
Extension.set_protocol('tcp')
|
||||
|
||||
# set basic authentication
|
||||
auth = None
|
||||
if _username:
|
||||
auth = HTTPBasicAuth(_username, _password)
|
||||
|
||||
def jsonrpc2_create_id(data):
|
||||
return hashlib.sha1(json.dumps(data).encode(client_encoding)).hexdigest()
|
||||
|
||||
def jsonrpc2_encode(method, params = None):
|
||||
data = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"params": params
|
||||
}
|
||||
id = jsonrpc2_create_id(data)
|
||||
data['id'] = id
|
||||
return (id, json.dumps(data))
|
||||
|
||||
def jsonrpc2_result_encode(result, id = ''):
|
||||
data = {
|
||||
"jsonrpc": "2.0",
|
||||
"result": result,
|
||||
"id": id
|
||||
}
|
||||
return json.dumps(data)
|
||||
|
||||
def parse_first_data(data):
|
||||
parsed_data = (b'', b'', b'', b'', b'')
|
||||
|
||||
|
@ -524,83 +509,6 @@ def start(): #Main Program
|
|||
print("\n[*] Graceful Shutdown")
|
||||
sys.exit(1)
|
||||
|
||||
class Extension():
|
||||
extensions = []
|
||||
|
||||
@classmethod
|
||||
def register(cls, f):
|
||||
cls.extensions.append(f)
|
||||
|
||||
@classmethod
|
||||
def get_filters(cls):
|
||||
filters = []
|
||||
for extension in cls.extensions:
|
||||
if extension.type == "filter":
|
||||
filters.append(extension)
|
||||
return filters
|
||||
|
||||
@classmethod
|
||||
def get_rpcmethod(cls, method):
|
||||
for extension in cls.extensions:
|
||||
is_exported_method = (method == extension.method) or (method in extension.exported_methods)
|
||||
if extension.type == "rpcmethod" and is_exported_method:
|
||||
return extension
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def dispatch_rpcmethod(cls, method, type, id, params, conn):
|
||||
rpcmethod = cls.get_rpcmethod(method)
|
||||
if rpcmethod:
|
||||
if rpcmethod.method == method:
|
||||
rpcmethod.dispatch(type, id, params, conn)
|
||||
else:
|
||||
f = getattr(rpcmethod, method, None)
|
||||
if f:
|
||||
f(type, id, params, conn)
|
||||
|
||||
@classmethod
|
||||
def get_connector(cls, connection_type):
|
||||
for extension in cls.extensions:
|
||||
if extension.type == "connector" and extension.connection_type == connection_type:
|
||||
return extension
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def send_accept(cls, conn, method, success = True):
|
||||
_, message = jsonrpc2_encode(f"{method}_accept", {
|
||||
"success": success
|
||||
})
|
||||
conn.send(message.encode(client_encoding))
|
||||
|
||||
@classmethod
|
||||
def readall(cls, conn):
|
||||
data = b''
|
||||
while True:
|
||||
try:
|
||||
chunk = conn.recv(buffer_size)
|
||||
if not chunk:
|
||||
break
|
||||
data += chunk
|
||||
except:
|
||||
pass
|
||||
|
||||
return data
|
||||
|
||||
def __init__(self):
|
||||
self.type = None
|
||||
self.method = None
|
||||
self.exported_methods = []
|
||||
self.connection_type = None
|
||||
|
||||
def test(self, filtered, data, webserver, port, scheme, method, url):
|
||||
raise NotImplementedError
|
||||
|
||||
def dispatch(self, type, id, params, method = None, conn = None):
|
||||
raise NotImplementedError
|
||||
|
||||
def connect(self, conn, data, webserver, port, scheme, method, url):
|
||||
raise NotImplementedError
|
||||
|
||||
if __name__== "__main__":
|
||||
# load extensions
|
||||
#Extension.register(importlib.import_module("plugins.fediverse").Fediverse())
|
||||
|
|
Loading…
Reference in New Issue
Block a user