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

disable_switch_port and 6.043 #92

Closed Choo-lux closed 3 years ago

Choo-lux commented 3 years ago

Hi all, just upgraded to latest controller where the 8443 port is no longer used (on the ck2).

While I can use the api with the list example, the default disable switch port example no longer works.

this shows main response is 404 Not Found (it does work on Ubuntu..)

> -----------LOGIN-------------
> Array
> (
>     [url] => https://192.168.1.254/api/auth/login
>     [content_type] => application/json; charset=utf-8
>     [http_code] => 200
>     [header_size] => 783
>     [request_size] => 262
>     [filetime] => -1
>     [ssl_verify_result] => 18
>     [redirect_count] => 0
>     [total_time] => 0.407374
>     [namelookup_time] => 1.7E-5
>     [connect_time] => 1.7E-5
>     [pretransfer_time] => 8.2E-5
>     [size_upload] => 54
>     [size_download] => 4624
>     [speed_download] => 11361
>     [speed_upload] => 132
>     [download_content_length] => 4624
>     [upload_content_length] => 54
>     [starttransfer_time] => 0.407355
>     [redirect_time] => 0
>     [redirect_url] => 
>     [primary_ip] => 192.168.1.254
>     [certinfo] => Array
>         (
>         )
>     [primary_port] => 443
>     [local_ip] => 192.168.1.245
>     [local_port] => 40842
>     [http_version] => 2
>     [protocol] => 2
>     [ssl_verifyresult] => 0
>     [scheme] => HTTPS
>     [appconnect_time_us] => 17
>     [connect_time_us] => 17
>     [namelookup_time_us] => 17
>     [pretransfer_time_us] => 82
>     [redirect_time_us] => 0
>     [starttransfer_time_us] => 407355
>     [total_time_us] => 407374
> )
> 
> ----------RESPONSE-----------
> HTTP/1.1 200 OK
> Vary: Origin
> X-DNS-Prefetch-Control: off
> X-Frame-Options: SAMEORIGIN
> Strict-Transport-Security: max-age=15552000; includeSubDomains
> X-Download-Options: noopen
> X-Content-Type-Options: nosniff
> X-XSS-Protection: 1; mode=block
> Accept-Ranges: bytes
> X-CSRF-Token: 90ee101e-cf05-40c3-9bf6-437ac5261f7f
> Content-Type: application/json; charset=utf-8
> Content-Length: 4624
> X-Response-Time: 403ms
> Set-Cookie: TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IcBHsZ9fOXnWQOW6LUvTJlAXFu_9eYhK4; path=/; secure; httponly
> Date: Tue, 19 Jan 2021 21:51:39 GMT
> Connection: keep-alive
> 
> 
>   "unique_id": "3ec98ef3-1620-4d40-b7b9eaed3cdd59e6",
>   "first_name": "admin",
>   "last_name": " ",
>   "full_name": "admin  ",
>   "email": "",
>   "email_status": "UNVERIFIED",
>   "phone": "",
>   "avatar_relative_path": "",
>   "status": "ACTIVE",
>   "employee_number": "",
>   "create_time": 1610670812,
>   "extras": {},
>   "username": "admin",
>   "local_account_exist": true,
>   "password_revision": 1610673928,
>   "sso_account": "",
>   "sso_uuid": "",
>   "sso_username": "",
>   "sso_picture": "",
>   "uid_sso_id": "",
>   "uid_sso_account": "",
>   "groups": [
>     {
>       "unique_id": "6ce5b511-772b-4e70-bf23-4594b13502b4",
>       "name": "UniFi-CloudKey-Gen2",
>       "up_id": "",
>       "up_ids": [],
>       "system_name": "UniFi-CloudKey-Gen2",
>       "create_time": "2021-01-15T10:33:32+10:00"
>     }
>   ],
>   "roles": [
>     {
>       "unique_id": "773a2026-1746-4ef8-9915-7fbe8ce0b2cd",
>       "name": "Owner",
>       "system_role": true,
>       "system_key": "owner",
>       "level": 1
>     }
>   ],
>   "permissions": {
>     "access.management": [
>       "admin"
>     ],
>     "connect.management": [
>       "admin"
>     ],
>     "led.management": [
>       "admin"
>     ],
>     "network.management": [
>       "admin"
>     ],
>     "protect.management": [
>       "admin"
>     ],
>     "system.management.location": [
>       "admin"
>     ],
>     "system.management.user": [
>       "admin"
>     ],
>     "talk.management": [
>       "admin"
>     ]
>   },
>   "scopes": [
>     "view:user_timezone",
>     "view:user",
>     "view:systemlog",
>     "view:system_info",
>     "view:sso",
>     "view:settings",
>     "view:role:viewer",
>     "view:role:admin",
>     "view:role",
>     "view:permission:viewer",
>     "view:permission:super_admin",
>     "view:permission:owner",
>     "view:permission:admin",
>     "view:permission",
>     "view:notification",
>     "view:location_policy",
>     "view:location_device",
>     "view:location_activity",
>     "view:location",
>     "view:holiday_timezone",
>     "view:holiday",
>     "view:group",
>     "view:door_group",
>     "view:controller:uid-agent",
>     "view:controller:talk",
>     "view:controller:protect",
>     "view:controller:network",
>     "view:controller:led",
>     "view:controller:connect",
>     "view:controller:access",
>     "view:controller",
>     "view:cloud_access",
>     "view:backup",
>     "view:app:users",
>     "view:app:settings",
>     "view:app:locations",
>     "view:access.visitor",
>     "view:access.systemlog",
>     "view:access.settings",
>     "view:access.schedule",
>     "view:access.policy",
>     "view:access.pin_code",
>     "view:access.nfc_card",
>     "view:access.face",
>     "view:access.device",
>     "view:access.dashboard",
>     "update:access.device",
>     "systemlog:user",
>     "systemlog:system",
>     "systemlog:location",
>     "systemlog:access",
>     "open:door",
>     "notify:user",
>     "notify:talk",
>     "notify:protect",
>     "notify:network",
>     "notify:location",
>     "notify:led",
>     "notify:access",
>     "manage:controller:talk",
>     "manage:controller:protect",
>     "manage:controller:network",
>     "manage:controller:led",
>     "manage:controller:connect",
>     "manage:controller:access",
>     "manage:controller",
>     "edit:user_timezone",
>     "edit:user_credential",
>     "edit:user",
>     "edit:systemlog",
>     "edit:system_info",
>     "edit:sso",
>     "edit:settings",
>     "edit:role:viewer",
>     "edit:role:admin",
>     "edit:role",
>     "edit:permission:viewer",
>     "edit:permission:super_admin",
>     "edit:permission:owner",
>     "edit:permission:admin",
>     "edit:permission",
>     "edit:notification",
>     "edit:location_policy",
>     "edit:location_device",
>     "edit:location_activity",
>     "edit:location",
>     "edit:holiday_timezone",
>     "edit:holiday",
>     "edit:group",
>     "edit:feedback",
>     "edit:door_group",
>     "edit:controller:uid-agent",
>     "edit:cloud_access",
>     "edit:backup",
>     "edit:access.visitor",
>     "edit:access.settings",
>     "edit:access.schedule",
>     "edit:access.policy",
>     "edit:access.pin_code",
>     "edit:access.nfc_card",
>     "edit:access.face",
>     "edit:access.device",
>     "delete:access.device",
>     "cloud_access:system",
>     "cloud_access:controller",
>     "assign:role_super_admin",
>     "assign:role:viewer",
>     "assign:role:admin",
>     "assign:role",
>     "adopt:access.device",
>     "access.open_door:location",
>     "access.open_door:floor",
>     "access.open_door:door_group",
>     "access.open_door:door",
>     "access.open_door:building"
>   ],
>   "cloud_access_granted": false,
>   "update_time": 1610673928,
>   "avatar": "",
>   "nfc_token": "",
>   "nfc_display_id": "",
>   "nfc_card_type": "",
>   "nfc_card_status": "",
>   "id": "3ec98ef3-1620-4d40-b7b9-eaed3cdd59e6",
>   "isOwner": true,
>   "isSuperAdmin": true
> }
> -----------------------------
> ---------cURL INFO-----------
> Array
> (
>     [url] => https://192.168.1.254/proxy/network/api/s/default/stat/device/74:83:c2:17:2f:db
>     [content_type] => application/json;charset=UTF-8
>     [http_code] => 200
>     [header_size] => 826
>     [request_size] => 421
>     [filetime] => -1
>     [ssl_verify_result] => 18
>     [redirect_count] => 0
>     [total_time] => 0.088461
>     [namelookup_time] => 1.6E-5
>     [connect_time] => 0.000726
>     [pretransfer_time] => 0.025379
>     [size_upload] => 0
>     [size_download] => 4816
>     [speed_download] => 54727
>     [speed_upload] => 0
>     [download_content_length] => -1
>     [upload_content_length] => -1
>     [starttransfer_time] => 0.087743
>     [redirect_time] => 0
>     [redirect_url] => 
>     [primary_ip] => 192.168.1.254
>     [certinfo] => Array
>         (
>         )
> 
>     [primary_port] => 443
>     [local_ip] => 192.168.1.245
>     [local_port] => 40844
>     [http_version] => 2
>     [protocol] => 2
>     [ssl_verifyresult] => 0
>     [scheme] => HTTPS
>     [appconnect_time_us] => 25360
>     [connect_time_us] => 726
>     [namelookup_time_us] => 16
>     [pretransfer_time_us] => 25379
>     [redirect_time_us] => 0
>     [starttransfer_time_us] => 87743
>     [total_time_us] => 88461
> )
> 
> -------URL & PAYLOAD---------
> https://192.168.1.254/proxy/network/api/s/default/stat/device/74:83:c2:17:2f:db
> empty payload
> ----------RESPONSE-----------
> {"meta":{"rc":"ok"},"data":[{"_id":"5d773594da689303933a64b6","ip":"192.168.1.7","mac":"74:83:c2:17:2f:db","model":"US24","type":"usw","version":"5.43.18.12487","adopted":true,"site_id":"5cb8f325da68930413ace89b","x_authkey":"10919d9315ba5cbe79197f5b77e7bec6","cfgversion":"e3837089580a1c94","config_network":rx_errors":12.0}},"tx_bytes":24159355216,"rx_bytes":2605732792,"bytes":26765088008,"num_sta":32,"user-num_sta":32,"guest-num_sta":0,"x_has_ssh_hostkey":true}]}
> -----------------------------
> -----------LOGIN-------------
> Array
> (
>     [url] => https://192.168.1.254/api/auth/login
>     [content_type] => text/plain; charset=utf-8
>     [http_code] => 404
>     [header_size] => 473
>     [request_size] => 538
>     [filetime] => -1
>     [ssl_verify_result] => 18
>     [redirect_count] => 0
>     [total_time] => 0.009806
>     [namelookup_time] => 1.6E-5
>     [connect_time] => 1.6E-5
>     [pretransfer_time] => 4.1E-5
>     [size_upload] => 54
>     [size_download] => 9
>     [speed_download] => 1000
>     [speed_upload] => 6000
>     [download_content_length] => 9
>     [upload_content_length] => 54
>     [starttransfer_time] => 0.009769
>     [redirect_time] => 0
>     [redirect_url] => 
>     [primary_ip] => 192.168.1.254
>     [certinfo] => Array
>         (
>         )
> 
>     [primary_port] => 443
>     [local_ip] => 192.168.1.245
>     [local_port] => 40848
>     [http_version] => 2
>     [protocol] => 2
>     [ssl_verifyresult] => 0
>     [scheme] => HTTPS
>     [appconnect_time_us] => 16
>     [connect_time_us] => 16
>     [namelookup_time_us] => 16
>     [pretransfer_time_us] => 41
>     [redirect_time_us] => 0
>     [starttransfer_time_us] => 9769
>     [total_time_us] => 9806
> )
> 
> ----------RESPONSE-----------
> HTTP/1.1 404 Not Found
> Vary: Origin
> X-DNS-Prefetch-Control: off
> X-Frame-Options: SAMEORIGIN
> Strict-Transport-Security: max-age=15552000; includeSubDomains
> X-Download-Options: noopen
> X-Content-Type-Options: nosniff
> X-XSS-Protection: 1; mode=block
> Accept-Ranges: bytes
> X-CSRF-Token: 90ee101e-cf05-40c3-9bf6-437ac5261f7f
> X-Response-Time: 5ms
> Content-Type: text/plain; charset=utf-8
> Content-Length: 9
> Date: Tue, 19 Jan 2021 21:51:39 GMT
> Connection: keep-alive
> 
> Not Found
> -----------------------------
> false
malle-pietje commented 3 years ago

Hmm, difficult to read this output but it is strange I don't see ant output from the set_device_settings_base() function/method.

You did make sure to use the latest version of the client (1.1.63)? The code was recently updated following this report: https://github.com/Art-of-WiFi/UniFi-API-client/issues/69

I'm afraid I can't replicate your environment; none of our direct clients where we manage the infrastructure are using Cloud Keys for Network Management, only for Protect.

Choo-lux commented 3 years ago

thanks. sorry for formatting

At this point it looks like Unifi OS is the issue at least with the disable port example

you can see above that it attempts login a second time whereas the list example does not

finally found the php error:

PHP Notice: JSON decode error: Syntax error, malformed JSON in /var/www/html/UniFi-API-client/src/Client.php on line 3606

malle-pietje commented 3 years ago

The PHP error is triggered because the response is non-json content. When I have access to a UniFi OS unit I’ll investigate this further.

Choo-lux commented 3 years ago

ok cool, thanks for the api, I should have another ck soon for testing

malle-pietje commented 3 years ago

Not yet tested this on a UniFi OS machine but after reformatting the original post I notice you’re logging in again after fetching the device details. That isn’t necessary if you still have an instance of the Client unless you previously logged off.

Can you try and see what happens without the second login?

Choo-lux commented 3 years ago

hi there, just got a UDM Pro on latest controller and it does a similar thing. (400 error not 404)

I'm using the exact example code you provide. Am not trying to login twice.

Can I get you access to this controller?

Choo-lux commented 3 years ago

not sure what happened here but its all good now...cheers

malle-pietje commented 3 years ago

Hmmm, interesting... thanks for the update!