Ver Mensaje Individual
  #4  
Antiguo 20-03-2024
Avatar de Casimiro Notevi
Casimiro Notevi Casimiro Notevi is online now
Moderador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.043
Reputación: 10
Casimiro Notevi Tiene un aura espectacularCasimiro Notevi Tiene un aura espectacular
Preguntando a chatgpt:

Código Delphi [-]
uses
  ComObj;

procedure AllowOnlySpecificIPs(const AllowedIPs: TStringList);
var
  fwPolicy2: OleVariant;
  fwRules: OleVariant;
  fwRule: OleVariant;
  i: Integer;
begin
  // Crear el objeto COM para administrar la política del Firewall de Windows
  fwPolicy2 := CreateOleObject('HNetCfg.FwPolicy2');
  
  // Obtener el objeto Rules
  fwRules := fwPolicy2.Rules;
  
  // Eliminar todas las reglas existentes
  fwRules.RemoveAll;
  
  // Crear una nueva regla de Firewall para cada IP permitida
  for i := 0 to AllowedIPs.Count - 1 do
  begin
    // Crear un nuevo objeto de regla de Firewall
    fwRule := CreateOleObject('HNetCfg.FWRule');
    
    // Establecer propiedades de la regla de Firewall
    fwRule.Action := NET_FW_ACTION_ALLOW;
    fwRule.Description := 'Permitir tráfico solo desde ' + AllowedIPs[i];
    fwRule.Direction := NET_FW_RULE_DIR_IN;
    fwRule.Enabled := True;
    fwRule.InterfaceTypes := 'All';
    fwRule.RemoteAddresses := AllowedIPs[i];
    
    // Agregar la regla al Firewall
    fwRules.Add(fwRule);
  end;
end;


Y así añades las IP permitidas:

Código Delphi [-]
var
  AllowedIPs: TStringList;
begin
  AllowedIPs := TStringList.Create;
  try
    AllowedIPs.Add('Dirección_IP_1');
    AllowedIPs.Add('Dirección_IP_2');
    // Agrega más direcciones IP si es necesario
    
    AllowOnlySpecificIPs(AllowedIPs);
  finally
    AllowedIPs.Free;
  end;
end;
Responder Con Cita