NRCHKB / node-red-contrib-unifi-os

Nodes to access UniFi data using endpoints and websockets
Apache License 2.0
27 stars 1 forks source link

[Enhancement] : Allow custom WS port #54

Closed ys333193 closed 1 year ago

ys333193 commented 1 year ago

It appears WebSocket interface only uses port 443, and doesn't use port configured in the configuration node. My Unifi controller runs on port 8443, and WebSocket interface bound to the same port (tested with Python script).

Please expose WebSocket port through config node.

Shaquu commented 1 year ago

Hey, so to clarify. The WebSocket port is configurable from the unifi side. But on the node side, there is no way at the moment to use a custom port.

SgtErnestBilko commented 1 year ago

I can find no setting to change the websocket port in the legacy or new interfaces of UniFi Network Application.

I can connect using the request node (e.g. endpoint /api/self) on port 8443.

If I try a websocket request using 8443 in NodeRed controller, I get error 1006 in NodeRed and the following in the export DEBUG=UniFi & node-red log.

28 Jan 07:04:17 UniFi-Error:WebSocket:466be400826b54fa53338f2385f4a769 [UniFi:b24398dbbc492294:b24398dbbc492294] Is UniFi server down? +1ms
28 Jan 07:04:17 - [error] [unifi-web-socket:UniFi:b24398dbbc492294] Error: connect ECONNREFUSED ::1:443
28 Jan 07:04:17 UniFi-Error:WebSocket:5a1259bfaddf9e7669c5bee34f498ffb [UniFi:b24398dbbc492294:b24398dbbc492294] Error: connect ECONNREFUSED ::1:443 +0ms

if I use 443 in NodeRed controller I get

28 Jan 07:15:11 UniFi-Error Bad response from: https:/mymac.local:443/api/login +5s

Hope that helps. Let me know if there is anything else you would like me to test.

ys333193 commented 1 year ago

Thanks for checking! I was able to call API, but was getting Connection Reset (1006) error using WebSockets. I have a firewall between Unifi controller and NodeRed server, and both 443 and 8443 ports were opened. I didn't capture any firewall violations either. I switched to a different node red contrib node (node-red-contrib-unifi-events) and its WebSocket implementation is working without any errors.

crxporter commented 1 year ago

I switched to a different node red contrib node (node-red-contrib-unifi-events)

While I certainly wouldn't recommend sticking with a 3 years old abandoned project based on a 5 years old project...

I dug a little there and it looks like yes - the websockets there are using port 8443 @Shaquu or @marcus-j-davies see here

Perhaps we need to add a field for port in the websocket node. UniFi OS is happy using ws default port 443 but the standalone controllers appear to keep that on 8443.

Maybe we also add a "default ws port" setting to our Endpoints.ts file?

crxporter commented 1 year ago

@SgtErnestBilko or @ys333193

Can one of you try the current websocket node pointed to this endpoint- :8443/wss/s/default/events image

I'm looking at how the connection is made... that might work.

SgtErnestBilko commented 1 year ago

That does work :-)

ys333193 commented 1 year ago

@SgtErnestBilko or @ys333193

Can one of you try the current websocket node pointed to this endpoint- :8443/wss/s/default/events

Works for me too!

crxporter commented 1 year ago

Awesome!

I'm glad we have found the problem and have a temporary fix. We should leave this issue open until a more permanent solution is added to the node as discussed above.

marcus-j-davies commented 1 year ago

@crxporter, @SgtErnestBilko, @ys333193

The below commit now adds the ability to specify a custom WS port. The defaults are applied according to your selected appliance

https://github.com/NRCHKB/node-red-contrib-unifi-os/commit/58ad3bf7a8e2d12d06803be6608ab877c1b63598

Screenshot 2023-02-16 at 16 51 28

crxporter commented 1 year ago

Quick note for those who have used my work-around...

This will break your setup unless you now remove the port number from the websocket endpoints. This is the better solution and should be ready in the next beta version.

marcus-j-davies commented 1 year ago

Fixed.