dcache/test.py

179 lines
5.2 KiB
Python
Raw Permalink Normal View History

2023-05-26 00:44:44 +05:30
#!/bin/env /bin/python
# mCaptcha - A proof of work based DoS protection system
# Copyright © 2023 Aravinth Manivannan <realravinth@batsense.net>
2023-05-27 10:29:05 +05:30
#
2023-05-26 00:44:44 +05:30
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2023-05-27 10:29:05 +05:30
#
2023-05-26 00:44:44 +05:30
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2023-05-27 10:29:05 +05:30
#
2023-05-26 00:44:44 +05:30
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pprint import pprint
import requests
2023-12-27 19:48:51 +05:30
import grpc
import json
from dcache_py import dcache_pb2 as dcache
from dcache_py.dcache_pb2 import RaftRequest
2023-12-27 19:48:51 +05:30
from dcache_py.dcache_pb2_grpc import DcacheServiceStub
# import dcache_py.dcache_resources
2023-05-26 00:44:44 +05:30
def init(host: str):
resp = requests.post(f"http://{host}/init")
print(f"Initialization status: {resp.status_code}")
def add_host(host: str, id: int, peer: str):
params = [id, peer]
resp = requests.post(f"http://{host}/add-learner", json=params)
print(f"Adding host {peer}. Status: {resp.status_code}")
def switch_to_cluster(host: str, nodes: [int]):
resp = requests.post(f"http://{host}/change-membership", json=nodes)
print(f"Switching to cluster. Status: {resp.status_code}")
def metrics(host: str):
resp = requests.get(f"http://{host}/metrics")
data = resp.json()
pprint(data)
def write(host, data):
resp = requests.post(f"http://{host}/write", json=data)
print(f"RPC Status: {resp.status_code}")
2023-05-27 10:29:05 +05:30
resp = resp.json()
if "Err" in resp:
leader = resp["Err"]["APIError"]["ForwardToLeader"]["leader_node"]["addr"]
print(f"Forwarding write to leader {leader}")
return write(leader, data)
return resp["Ok"]["data"]
2023-05-26 00:44:44 +05:30
def add_vote(host: str, captcha_id: str):
resp = write(host, data={"AddVisitor": captcha_id})
pprint(resp)
def add_captcha(host: str, captcha_id: str):
params = {
"AddCaptcha": {
"id": captcha_id,
"mcaptcha": {
"visitor_threshold": 0,
"defense": {
"levels": [
{"visitor_threshold": 50, "difficulty_factor": 500},
{"visitor_threshold": 5000, "difficulty_factor": 50000},
],
"current_visitor_threshold": 0,
},
"duration": 30,
},
}
}
resp = write(host, data=params)
pprint(f"Captcha added {captcha_id}: {resp}")
2023-05-27 10:29:05 +05:30
host = "localhost:9001"
peers = [(2, "localhost:9002"), (3, "localhost:9003"), (4, "localhost:9004")]
captcha_id = "test_1"
2023-05-27 10:29:05 +05:30
def initialize_cluster():
init(host)
for peer_id, peer in peers:
add_host(host=host, id=peer_id, peer=peer)
switch_to_cluster(host, nodes=[1, 2, 3, 4])
2023-05-27 10:29:05 +05:30
2023-05-26 00:44:44 +05:30
add_captcha(host, captcha_id)
add_vote(host, captcha_id)
2023-05-27 10:29:05 +05:30
for _ in range(0, 600):
2023-05-26 00:44:44 +05:30
add_vote(host, captcha_id)
2023-05-27 10:29:05 +05:30
2023-12-27 19:48:51 +05:30
def grpc_add_vote(stub: DcacheServiceStub, captcha_id: str):
msg = dcache.CaptchaID(id=captcha_id)
# msg = RaftRequest(data=json.dumps({"AddVisitor": captcha_id}))
# resp = stub.Write(msg)
resp = stub.AddVisitor(msg)
2023-12-27 19:48:51 +05:30
pprint(resp)
2023-12-27 19:48:51 +05:30
def grpc_add_captcha(stub: DcacheServiceStub, captcha_id: str):
msg = dcache.AddCaptchaRequest(
id=captcha_id,
mcaptcha=dcache.MCaptcha(
duration=30,
defense=dcache.Defense(
levels=[
dcache.Level(visitor_threshold=50, difficulty_factor=500),
dcache.Level(visitor_threshold=5000, difficulty_factor=50000),
]
),
),
)
# params = {
# "AddCaptcha": {
# "id": captcha_id,
# "mcaptcha": {
# "defense": {
# "levels": [
# {"visitor_threshold": 50, "difficulty_factor": 500},
# {"visitor_threshold": 5000, "difficulty_factor": 50000},
# ],
# "current_visitor_threshold": 0,
# },
# "duration": 30,
# },
# }
# }
# msg = RaftRequest(data = json.dumps(params))
resp = stub.AddCaptcha(msg)
2023-12-27 19:48:51 +05:30
pprint(f"Captcha added {captcha_id}: {resp}")
2023-12-28 19:15:07 +05:30
msgs = []
for _ in range(0,1000):
msgs.append(
dcache.DcacheRequest(addVisitor=dcache.CaptchaID(id=captcha_id)),
)
msgs = dcache.DcacheBatchRequest(requests=msgs)
def grpc_pipeline_add_vote(stub):
responses = stub.PipelineDcacheOps(msgs)
for r in responses.responses:
print(f"received respo: {r}")
2023-12-27 19:48:51 +05:30
def grpc_run():
with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel)
grpc_add_captcha(stub, captcha_id)
2023-12-28 19:15:07 +05:30
grpc_pipeline_add_vote(stub)
#grpc_add_vote(stub, captcha_id)
2023-12-27 19:48:51 +05:30
if __name__ == "__main__":
grpc_run()
# add_vote("localhost:9002", captcha_id)