summaryrefslogtreecommitdiffstats
path: root/ogclient
blob: 8798ed6682b73f61b1f05ee071009f5409d18c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python3

#
# Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
#
# 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.

import json
import logging
import argparse
import platform
import subprocess
try:
	from signal import SIG_DFL, SIGPIPE
except ImportError:
	from signal import SIG_DFL


from src.ogClient import *
from src.log import configure_logging


def send_event_dgram(msg, ip='127.0.0.1', port=55885):
	sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	sock.sendto(bytes(msg, "utf-8"), (ip, port))


def create_parser():
	events = ['login', 'logout']
	parser = argparse.ArgumentParser()
	parser.set_defaults(func=None)
	subparsers = parser.add_subparsers()

	parser_event = subparsers.add_parser('event')

	subparsers_event = parser_event.add_subparsers()
	parser_event_login = subparsers_event.add_parser('login')
	parser_event_login.set_defaults(func=lambda x: send_event_dgram(f'session start {x.user}'))
	parser_event_login.add_argument('user', type=str)
	parser_event_logout = subparsers_event.add_parser('logout')
	parser_event_logout.set_defaults(func=lambda x: send_event_dgram(f'session stop {x.user}'))
	parser_event_logout.add_argument('user', type=str)

	parser.add_argument('-c', '--config', default="",
			    help='ogClient JSON config file path')
	parser.add_argument('--debug', default=False,
			    action='store_true',
			    help='enables debug log level')

	return parser


def main():
	parser = create_parser()
	args = parser.parse_args(sys.argv[1:])
	if args.func:
		args.func(args)
		return

	if args.config:
		config_path = args.config
	elif platform.system().lower() == 'linux':
		config_path = f'{ogClient.OG_PATH}ogclient/cfg/ogclient.json'
	else:
		config_path = './cfg/ogclient.json'

	try:
		with open(config_path, 'r') as f:
			CONFIG = json.load(f)
	except:
		print('Error: Parsing configuration file')
		return 0

	MODE = CONFIG['opengnsys']['mode']
	URL = CONFIG['opengnsys']['url']
	LOGLEVEL = CONFIG['opengnsys']['log']

	if MODE == 'live':
		proc = subprocess.Popen(["browser", "-qws", URL])
	if MODE != 'windows':
		signal.signal(SIGPIPE, SIG_DFL)

	configure_logging(MODE, LOGLEVEL)

	if args.debug:
		logging.getLogger().setLevel('DEBUG')

	client = ogClient(config=CONFIG)
	client.connect()
	client.run()

if __name__ == "__main__":
	main()