Skip to content
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

add a unicast mode #13

Merged
merged 1 commit into from
Jan 5, 2022
Merged
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: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ class Config:

multicast_address: str
multicast_port: int
unicast_address: str
unicast_port: int
interface: str
verbose: bool = False
multicast_enabled: bool = True

@classmethod
def from_dict(cls, cfg: Dict[str, str]) -> "Config":
Expand All @@ -52,8 +55,11 @@ def from_dict(cls, cfg: Dict[str, str]) -> "Config":
controller_port=cfg["controller_port"],
username=cfg["username"],
password=cfg["password"],
multicast_enabled=cfg["multicast_enabled"],
multicast_address=cfg["multicast_address"],
multicast_port=cfg["multicast_port"],
unicast_address=cfg["unicast_address"],
unicast_port=cfg["unicast_port"],
interface=cfg["interface"],
verbose=cfg["verbose"],
)
Expand Down
43 changes: 31 additions & 12 deletions respondd_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,25 +254,44 @@ def start(self):
socket.SO_BINDTODEVICE,
bytes(self._config.interface.encode()),
)
self._sock.bind(("::", self._config.multicast_port))
if self._config.multicast_enabled:
self._sock.bind(("::", self._config.multicast_port))

self.joinMCAST(
self._sock, self._config.multicast_address, self._config.interface
)
self.joinMCAST(
self._sock, self._config.multicast_address, self._config.interface
)

while True:
msg, sourceAddress = self._sock.recvfrom(2048)
responseStruct = {}

msgSplit = str(msg, "UTF-8").split(" ")
if self._config.multicast_enabled:
msg, sourceAddress = self._sock.recvfrom(2048)

msgSplit = str(msg, "UTF-8").split(" ")

if msgSplit[0] == "GET": # multi_request
for request in msgSplit[1:]:
responseStruct[request] = self.buildStruct(request)
self.sendStruct(sourceAddress, responseStruct, True)
else: # single_request
responseStruct = self.buildStruct(msgSplit[0])
self.sendStruct(sourceAddress, responseStruct, False)
else:
timeStart = time.time()
msgSplit = ['GET', 'nodeinfo', 'statistics']

responseStruct = {}
if msgSplit[0] == "GET": # multi_request
for request in msgSplit[1:]:
responseStruct[request] = self.buildStruct(request)
self.sendStruct(sourceAddress, responseStruct, True)
else: # single_request
responseStruct = self.buildStruct(msgSplit[0])
self.sendStruct(sourceAddress, responseStruct, False)
self.sendStruct((self._config.unicast_address, self._config.unicast_port), responseStruct, True)

if self._config.verbose:
print ("data was send")

timeStop = time.time()
timeSleep = int (60 - (timeStop-timeStart) % 60)
if self._config.verbose:
print("will now sleep " + str(timeSleep) + " seconds")
time.sleep(timeSleep)

def merge_node(self, responseStruct):
"""This method merges the node information of all APs to their corresponding node_id."""
Expand Down
5 changes: 4 additions & 1 deletion unifi_respondd.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ controller_url: unifi.lan
controller_port: 8443
username: ubnt
password: ubnt
multicast_enabled: false
multicast_address: ff05::2:1001
multicast_port: 1001
unicast_address: fe80::68ff:94ff:fe00:1504
unicast_port: 10001
interface: eth0
verbose: true
verbose: true