From c2435b3b07f7dc9125180dc77bfddbee9399ef57 Mon Sep 17 00:00:00 2001 From: Edi Feschiyan Date: Wed, 1 Apr 2020 16:16:47 +0300 Subject: figuring out listening port for agl service --- aglbaseservice.py | 40 ++++++++++++++++++++-------------- gps.py | 65 +++++++++++++++++++++++++++++++++++++++++-------------- mediaplayer.py | 1 + 3 files changed, 74 insertions(+), 32 deletions(-) diff --git a/aglbaseservice.py b/aglbaseservice.py index 972b6bb..7c134e2 100644 --- a/aglbaseservice.py +++ b/aglbaseservice.py @@ -6,14 +6,10 @@ import sys import asyncio from random import randint from websockets import connect +import logging +import asyncssh - - -# IPADDR = '127.0.0.1' -# PORT = '30000' -# TOKEN = 'HELLO' -# UUID = 'magic' -# URL = f'ws://{IPADDR}:{PORT}/api?token={TOKEN}&uuid={UUID}' +from typing import Union class AFBT(IntEnum): REQUEST = 2, @@ -37,14 +33,17 @@ class AGLBaseService: port = None token = None uuid = None + service = None - def __init__(self, api, ip, port, url=None, token='HELLO', uuid='magic'): + def __init__(self, api: str, ip: str, port: str, url: str = None, + token: str = 'HELLO', uuid: str = 'magic', service: str = None): self.api = api self.url = url self.ip = ip self.port = port self.token = token self.uuid = uuid + self.service = service def __await__(self): return self._async_init().__await__() @@ -72,6 +71,11 @@ class AGLBaseService: async def receive(self): return await self.websocket.recv() + async def portfinder(self): + with asyncssh.connect(self.ip) as c: + data = await c.run('ls -lah /', check=True) + print(data) + async def listener(self): try: while True: @@ -95,14 +99,18 @@ class AGLBaseService: except Exception as e: print("vote du phoque?!?!? : " + str(e)) + async def request(self, + verb: str, + values: Union[str, dict] = "", + msgid: int = randint(0, 9999999), + waitresponse: bool = False): + l = json.dumps([AFBT.REQUEST, str(msgid), f'{self.api}/{verb}', values]) + await self.send(l) + if waitresponse: + return await self.receive() + async def subscribe(self, event): - msgid = randint(0, 999999) - msg = f'[{AFBT.REQUEST},"{msgid}","{self.api}/subscribe",{{"value": "{event}"}}]' - await self.send(msg) + await self.request('subscribe', {'value': f'{event}'}) async def unsubscribe(self, event): - verb = 'unsubscribe' - msgid = randint(0, 999999) - msg = f'[2,"{msgid}","{self.api}/{verb}",{{"value": "{event}"}}]' - addrequest(msgid, msg) - await self.send(msg) + await self.request('unsubscribe', {'value': f'{event}'}) diff --git a/gps.py b/gps.py index 37caa4c..fa121e5 100644 --- a/gps.py +++ b/gps.py @@ -1,24 +1,19 @@ -from random import randint -import sys import asyncio -from random import randint -from websockets import connect -from websockets.exceptions import ConnectionClosedError import os -import json -import concurrent +import re +from socket import htons +import asyncssh from aglbaseservice import AGLBaseService - +import collections +from collections import namedtuple +from parse import * class GPSService(AGLBaseService): def __init__(self, ip, port): - super().__init__(api='gps', ip=ip, port=port) + super().__init__(api='gps', ip=ip, port=port, service='agl-service-gps') async def location(self): - verb = 'location' - msgid = randint(0, 999999) - await self.send(f'[2,"{msgid}","{self.api}/{verb}",""]') - return await self.receive() + return await self.request('location',waitresponse=True) async def subscribe(self, event='location'): await super().subscribe(event=event) @@ -31,12 +26,50 @@ async def main(loop): addr = os.environ.get('AGL_TGT_IP', 'localhost') port = os.environ.get('AGL_TGT_PORT', '30011') - gpss = await GPSService(ip=addr, port=port) + # gpss = await GPSService(ip=addr, port=port) + async with asyncssh.connect(addr,username='root') as c: + # find the name of the service since it is dynamically generated every time + servicename = await c.run(f"systemctl | grep agl-service-gps | awk '{{print $1}}'", check=False) + print(f"Found service name: {servicename.stdout}") - await gpss.location() - # listener = loop.create_task(gpss.listener()) + # get the pid + pid = await c.run(f'systemctl show --property MainPID --value {servicename.stdout}') + print(f'Service PID: {pid.stdout}') + + # get all sockets in the process' fd directory and their respective inodes + sockets = await c.run(f'find /proc/{pid.stdout.strip()}/fd/ | xargs readlink | grep socket') + inodes = re.findall('socket:\[(.*)\]', sockets.stdout) + print(f"Socket inodes: {inodes}") + + alltcp = await c.run('cat /proc/net/tcp') + fieldsstr = ' '.join(alltcp.stdout.strip().splitlines()[0].strip().split()) + ' misc' + # https://www.kernel.org/doc/Documentation/networking/proc_net_tcp.txt + # ['sl', 'local_address', 'rem_address', 'st', 'tx_queue:rx_queue', 'tr:tm->when', 'retrnsmt', 'uid', 'timeout', 'inode', 'sref_cnt', 'memloc', 'rto', 'pred_sclk', 'ackquick', 'congest', 'slowstart' ] + # '0: 00000000:753E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1001 0 20062 1 0000000095c038d6 100 0 0 10 0' + # {'sl':'0', # connection state + # 'local_address': '00000000:753E', + # 'rem_address':'00000000:0000', + # 'st':'0A', + # 'tx_queue':'00000000:00000000', + # 'rx_queue':'00:00000000', + # 'tr':'00000000', 'tm->when':'1001', 'retrnsmt': '0', + # '20062 1 0000000095c038d6 100 0 0 10 0 + fields = fieldsstr.split() + tcpsockets = alltcp.stdout.splitlines()[1:] + print(tcpsockets) + # print(' '.join(alltcp.stdout.strip().splitlines()[1].strip().split())) + # result = findall('{}: {}:{} {} {} {} {} {} {} ') + + # serviceport = await c.run(f'journalctl -u {servicename.stdout}') + # print(serviceport.stdout) + # matches = re.findall('Listening interface \*:(.*) \[',serviceport.stdout) + # print(matches) + + # print(await gpss.location()) + + # listener = loop.create_task(gpss.listener()) if __name__ == '__main__': loop = asyncio.get_event_loop() diff --git a/mediaplayer.py b/mediaplayer.py index dcad2c2..21f9801 100644 --- a/mediaplayer.py +++ b/mediaplayer.py @@ -22,6 +22,7 @@ class MediaPlayerService(AGLBaseService): super().__init__(api='mediaplayer', ip=ip, port=port) async def playlist(self): + self.request('playlist') verb = 'playlist' msgid = randint(0, 999999) -- cgit 1.2.3-korg