diff --git a/server/src/utils/paloalto.js b/server/src/utils/paloalto.js index fed2063..1243f74 100644 --- a/server/src/utils/paloalto.js +++ b/server/src/utils/paloalto.js @@ -1,3 +1,5 @@ +// Ref.: https://docs.paloaltonetworks.com/pan-os/9-1/pan-os-panorama-api/pan-os-xml-api-request-types/apply-user-id-mapping-and-populate-dynamic-address-groups-api.html + import axios from 'axios' import prisma from '../prisma' import https from 'https' @@ -9,10 +11,10 @@ const httpsAgent = new https.Agent({ let working = false -const DEBOUNCE_TIME_MS = 10000 +const DEBOUNCE_TIME_IN_MS = 5000 async function updateUserIdMappings() { - if (working) return 0 + if (working) return -1 working = true @@ -29,43 +31,29 @@ async function updateUserIdMappings() { }) const entries = wifiDevices.reduce( - (entries, device, i) => + (entries, device) => (entries += ``), '' ) - const cmd = ` + const cmd = ` 1.0 update - ${entries} + ${entries} - ` + ` - /* - https://docs.paloaltonetworks.com/pan-os/9-1/pan-os-panorama-api/pan-os-xml-api-request-types/apply-user-id-mapping-and-populate-dynamic-address-groups-api.html - Use a GET request if the URL query size is less than 2K and a POST request if the request size is between 2K to 5MB. Limit the query size to 5MB. - When multiple login or logout events are generated at the same time, make sure to follow these guidelines to ensure optimal firewall performance: - Design your application to queue events and perform batch API updates instead of sending single event or mapping updates. - Limit the number of concurrent API calls to five. This limit ensures that there is no performance impact to the firewall web interface as the management plane web server handles requests from both the API and the web interface. - */ - - const result = await axios({ - method: 'POST', - data: qs.stringify({ - cmd - }), + await axios({ url: `https://${process.env.PA_HOST}/api/`, - params: { - type: 'user-id' - }, - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, + method: 'POST', + params: { type: 'user-id' }, + data: qs.stringify({ cmd }), + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, auth: { username: process.env.PA_USER, password: process.env.PA_PASSWORD @@ -75,12 +63,12 @@ async function updateUserIdMappings() { return wifiDevices.length } catch (e) { - console.log('Error updating user-id mappings:', e) + console.log('Error updating user-id mappings:', e.message) return 'Não foi possível atualizar. Veja o log do servidor' } finally { setTimeout(() => { working = false - }, DEBOUNCE_TIME_MS) + }, DEBOUNCE_TIME_IN_MS) } }