Properly find PA host

This commit is contained in:
Douglas Barone 2023-12-13 14:13:09 -04:00
parent ff01ff714a
commit e80daa85e2
3 changed files with 23 additions and 10 deletions

View File

@ -5,6 +5,8 @@ import { PaFirewallService } from '../services/PaFirewallService'
import { db } from '../prisma'
import { LoginResult } from '../schemas/LoginResult'
import { jwtService } from './jwt'
import { getNetworkForIP } from './netInfo'
import { PaHost } from '@prisma/client'
export async function login(
username: string,
@ -22,10 +24,22 @@ export async function login(
const paHosts = await db.paHost.findMany()
const pa = new PaFirewallService(paHosts[0].ip, paHosts[0].key)
const network = await getNetworkForIP(ip)
const paHost: PaHost | undefined = paHosts.find(paHost => {
return network?.contains(paHost.ip)
})
if (!paHost) throw new Error('Rede não suportada')
const pa = new PaFirewallService(paHost.ip, paHost.key)
await pa.login({ username, ip, domain: user.domain })
console.log(
`Mapped user ${user.domain}\\${username} to IP ${ip} on ${paHost.ip}`
)
const jwt = jwtService.generateToken({
displayName: user.displayName,
username: user.username,

View File

@ -82,16 +82,15 @@ const networks = networksInfo.map(network => {
export async function getNetworkForIP(ip: string) {
z.string().ip().parse(ip)
console.log(ip)
const blackList = ['10.7.0.10']
if (blackList.includes(ip))
return {
name: 'Endereço IP inválido',
shortName: 'XX',
network: '?',
isSupported: false
network: undefined,
isSupported: false,
contains: () => false
}
const network = networks.find(network => network.contains(ip))
@ -100,8 +99,9 @@ export async function getNetworkForIP(ip: string) {
return {
name: 'Rede desconhecida',
shortName: '--',
network: '?',
isSupported: false
network: undefined,
isSupported: false,
contains: () => false
}
}
@ -112,7 +112,8 @@ export async function getNetworkForIP(ip: string) {
)
return {
isSupported: !!paHost,
...network,
isSupported: !!paHost
contains: network.contains
}
}

View File

@ -42,8 +42,6 @@ export class PaFirewallService {
throw new Error('Failed to map user ID to IP')
}
console.log(`Mapped user ${domain}\\${username} to IP ${ip}`)
return true
}