Art-of-WiFi / UniFi-API-client

A PHP API client class to interact with Ubiquiti's UniFi Controller API
MIT License
1.09k stars 217 forks source link

Getting "Couldn't connect to server" error #207

Closed Fairydx closed 6 months ago

Fairydx commented 6 months ago

Good afternoon,

I'm experiencing an error after upgrading my UDM to version v3.2.7. When attempting to log in to the API, I receive the following error message. I have already updated the API Client to the latest version from this repository.

Please find the error below.

{
  "message": "cURL error: Failed to connect to x.x.x.x port 8443 after 0 ms: Couldn't connect to server",
  "exception": "ErrorException",
  "file": "/var/www/html/captive.controlink.pt/vendor/art-of-wifi/unifi-api-client/src/Client.php",
  "line": 154,
  "trace": [
    {
      "file": "/var/www/html/captive.controlink.pt/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php",
      "line": 254,
      "function": "handleError",
      "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
      "type": "->"
    },
    {
      "function": "Illuminate\\Foundation\\Bootstrap\\{closure}",
      "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
      "type": "->"
    },
    {
      "file": "/var/www/html/captive.controlink.pt/vendor/art-of-wifi/unifi-api-client/src/Client.php",
      "line": 154,
      "function": "trigger_error"
    },
    {
      "file": "/var/www/html/captive.controlink.pt/app/Http/Controllers/StoresController.php",
      "line": 170,
      "function": "login",
      "class": "UniFi_API\\Client",
      "type": "->"
    }
}

Can you help me with this?

Best regards, Fairydx

malle-pietje commented 6 months ago

Please use port 443 per the instructions unless you are handling this through NAT.

Fairydx commented 6 months ago

I have this configured on my Port Forward options: image

I can access the unify panel using the 8443 port.

malle-pietje commented 6 months ago

Okay, but the "Couldn't connect to server" suggested something is wrong at the TCP level. We also use the API client in Laravel (up to version 10) stacks and don't see such errors. Here's an entry from one of our apps: image

malle-pietje commented 6 months ago

Wait, did you not create a specific firewall rule to allow access to port 443 (local) from the WAN interface? In that case, please follow these instructions: https://artofwifi.net/blog/how-to-access-the-unifi-controller-by-wan-ip-or-hostname-on-a-udm-pro

Fairydx commented 6 months ago

I have that enabled.

image

Should I need to create a specific firewall rule for the 8443 port?

Fairydx commented 6 months ago

I'm seeing now that I can't even disable the Remote Direct Access feature. image

However I'm using a WAN IP address on the UDM: image

malle-pietje commented 6 months ago

Something is not set up correctly with your firewall rules. I can’t reproduce this here.

malle-pietje commented 6 months ago

I have that enabled.

image

Should I need to create a specific firewall rule for the 8443 port?

No, simply create a firewall based on the second method described in that blog post. You might consider creating a backup and resetting the unit to factory defaults…

Fairydx commented 6 months ago

I'm having the same problem with all the versions of unifi management on the web, I have a server that has the Unifi Management installed to connect to old ubiquiti devices, and I have the same error here.. I believe that this is not a device problem, because if I try to connect via API in another device, it works fine.

Do you know what can be blocking the connection?

malle-pietje commented 6 months ago

I'm afraid this issue is unrelated to the API client package. I recommend opening a topic on the Ubiquiti community forums instead. It helps if you can validate the issues by simply trying to connect to the web interface of your UniFi OS console from the "outside".

This might be related to the "Direct Remote Connection" feature which is greyed out but Ubiquiti would need to confirm that.

I will close this issue for now, please re-open it again in case it becomes clear the issue is related to the API client.

Fairydx commented 6 months ago

This is not related with a device itself, it's related with the connection between my server and the ubiquiti devices.

It's not only one device it's all of them

And one of them it's the Unifi Management OS that has a lot of sites with other gateways

malle-pietje commented 6 months ago

I'm afraid I cannot reproduce this at all.

Fairydx commented 6 months ago

I moved the captive portal to another server, and this problem goes away. If you know any way to understand or debug what is going on with the server, please share it.

malle-pietje commented 6 months ago

There are so many things that can affect the connectivity... One thing to check is whether the server is running Selinux. This can block outgoing connections.