This commit is contained in:
Douglas Barone 2021-01-14 10:07:29 -04:00
parent 00097c68d2
commit a8a4bf7f6a

View File

@ -7,47 +7,40 @@ import { subMinutes } from 'date-fns'
import qs from 'qs' import qs from 'qs'
import { logError, logSuccess } from './logger' import { logError, logSuccess } from './logger'
const DEBOUNCE_TIME_IN_MS = 5000
const TIMEOUT_IN_MINUTES = '3' const TIMEOUT_IN_MINUTES = '3'
const httpsAgent = new https.Agent({ const httpsAgent = new https.Agent({
rejectUnauthorized: false rejectUnauthorized: false
}) })
let working = false async function getDevicesWithUserFromNet(network) {
async function updateUserIdMappings() {
if (working) return -1
working = true
const now = new Date() const now = new Date()
const timeoutThreshold = subMinutes(now, TIMEOUT_IN_MINUTES) const timeoutThreshold = subMinutes(now, TIMEOUT_IN_MINUTES)
try {
const wifiDevices = await prisma.wifiDevice.findMany({ const wifiDevices = await prisma.wifiDevice.findMany({
where: { where: {
userId: { not: null }, userId: { not: null },
status: 'ONLINE', status: 'ONLINE',
lastSeen: { gt: timeoutThreshold }, lastSeen: { gt: timeoutThreshold },
ip: { startsWith: process.env.PA_NET } ip: { startsWith: network }
}, },
select: { select: {
ip: true, ip: true,
user: { select: { sAMAccountName: true } } user: { select: { sAMAccountName: true } }
} }
}) })
return wifiDevices
}
if (wifiDevices.length == 0) return 0 function createCommand(devices) {
const entries = devices.reduce(
const entries = wifiDevices.reduce(
(entries, device) => (entries, device) =>
entries + entries +
`<entry name="ifms\\${device.user.sAMAccountName}" ip="${device.ip}" timeout="${TIMEOUT_IN_MINUTES}"/>\n`, `<entry name="ifms\\${device.user.sAMAccountName}" ip="${device.ip}" timeout="${TIMEOUT_IN_MINUTES}"/>\n`,
'' ''
) )
const cmd = ` return `
<uid-message> <uid-message>
<version>1.0</version> <version>1.0</version>
<type>update</type> <type>update</type>
@ -57,6 +50,15 @@ async function updateUserIdMappings() {
</login> </login>
</payload> </payload>
</uid-message>` </uid-message>`
}
async function updateUserIdMappings() {
try {
const devices = await getDevicesWithUserFromNet(process.env.PA_NET)
if (devices.length == 0) return 0
const cmd = createCommand(devices)
await axios({ await axios({
url: `https://${process.env.PA_HOST}/api/`, url: `https://${process.env.PA_HOST}/api/`,
@ -73,11 +75,11 @@ async function updateUserIdMappings() {
logSuccess({ logSuccess({
tags: ['user-id', 'paloalto'], tags: ['user-id', 'paloalto'],
message: `Updated ${wifiDevices.length} user-id mappings`, message: `Updated ${devices.length} user-id mappings`,
data: wifiDevices data: devices
}) })
return wifiDevices.length return devices.length
} catch (e) { } catch (e) {
logError({ logError({
tags: ['paloalto', 'user-id'], tags: ['paloalto', 'user-id'],
@ -86,10 +88,6 @@ async function updateUserIdMappings() {
}) })
return 'Não foi possível atualizar. Veja o log do servidor' return 'Não foi possível atualizar. Veja o log do servidor'
} finally {
setTimeout(() => {
working = false
}, DEBOUNCE_TIME_IN_MS)
} }
} }