Skip to content

add local service daemon option #1325

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion garak/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import sys

from garak import cli
from garak import api


def main():
cli.main(sys.argv[1:])
if "--remote" in sys.argv:
api.run()
else:
cli.main(sys.argv[1:])


if __name__ == "__main__":
Expand Down
33 changes: 33 additions & 0 deletions garak/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from garak.cli import main
from socket import socket, AF_INET, SOCK_STREAM
import sys

ADDRESS = "localhost"
PORT = 45345

def run():
garak_socket = socket(AF_INET, SOCK_STREAM)
garak_socket.bind((ADDRESS, PORT))
garak_socket.listen()
# Client needs to tell the api which port to send the output to.
while True:
client_socket, client = garak_socket.accept()

arguments = client_socket.recv(1024).decode()
arguments = arguments.split()

client_address = client[0]
client_port = int(arguments[0])

# This just makes sure that the output of the cli is returned to the client.
def redirectOut(port=0, host=0):
if port == 0 or host == 0:
raise ValueError("Client port or address weren't found")
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((host, port))
with sock.makefile("w") as file:
sys.stdout = file
main(arguments[1:])
return sock

redirectOut(client_port, client_address)
Loading