somakeit / smib

1 stars 0 forks source link

Capture which smibhid did the thing #118

Open sjefferson99 opened 1 month ago

sjefferson99 commented 1 month ago

Might be useful to know which smibhid did the thing, use cases like which door is used more, is a smibhid needed there, etc.

@sam57719 Can the slack API determine this from the request or do we need to pass something like the hostname planned in #101 in the request to achieve this?

sam57719 commented 1 month ago

Hostname would be good - although I might be able to resolve it from the request IP. Will test tomororw at the space.

sam57719 commented 1 month ago

request IP accessible from the request object request.scope['client'] = ('127.0.0.1', 61198)

sam57719 commented 3 weeks ago

blocked by #101

sam57719 commented 2 days ago

101 completed, unblocked

Easy enough to implement with socket.gethostbyaddr(ip)

Will add this out in the log for all http events

Another issue to add and track in the DB should be created

sam57719 commented 1 day ago

Socket.gethostbyname is slow. Can be cached but means the first request for each device will be slow.

Looking at using nslookup or something

Otherwise just ip address.

@sjefferson99 could you maybe pass this in a header (not body) of the request? Maybe under ClientHostname ?

Thoughts?

sam57719 commented 2 hours ago

The below code seems to work great, and its quick. Also included a standard socket.gethostbyaddr() call which is significantly slower and does the same thing.

import re
import subprocess
from pprint import pprint
import socket
from more_itertools import map_except

def extract_info(text):
    server_match = re.search(r'Server:\s+(.*?)\s+Address:\s+(.*?)\s', text)
    device_match = re.search(r'Name:\s+(.*?)\s+Address:\s+(.*?)\s', text)

    server, server_address = server_match.groups() if server_match else (None, None)
    device, device_address = device_match.groups() if device_match else (None, None)

    return {
        "server": server,
        "server_address": server_address,
        "device": device,
        "device_address": device_address,
    }

def nslookup(host):
    result = subprocess.run(['nslookup', host], stdout=subprocess.PIPE)
    output = result.stdout.decode()
    info = extract_info(output)
    return info

ips = ['192.168.1.145', '192.168.1.221', '192.168.1.1', "192.168.1.148", "192.168.1.176"]

info = list(map(nslookup, ips))
pprint(info, sort_dicts=False)

info = list(map_except(socket.gethostbyaddr, ips, Exception))
pprint(info, sort_dicts=False)

Will implement the first method. Will need to sudo apt-get install dnsutils or RUN apk add --update --no-cache bind-tools into the container (dockerfile)