Proyecto

General

Perfil

Descargar (13,4 KB) Estadísticas
| Rama: | Revisión:
unit demonio ;

{$mode objfpc}{$H+}

interface

uses
unix,clservidor,globales,Classes , HTTPDefs, SysUtils , FileUtil , LResources, IdCmdTCPServer, IdCommandHandlers, IdContext;

type

{ TDataModule1 }

TDataModule1 = class (TDataModule )
IdCmdTCPServer1:TIdCmdTCPServer;
procedure activarAccesoInternetCommand(ASender: TIdCommand);
procedure activarWebCamCommand(ASender: TIdCommand);
procedure apagarEquipoCommand (ASender : TIdCommand );
procedure cierraServidorAlumnoCommand(ASender: TIdCommand);
procedure DataModuleCreate(Sender: TObject);
procedure datosEquipoCommand(ASender: TIdCommand);
procedure desactivarAccesoInternetCommand(ASender: TIdCommand);
procedure desbloquearMonitorOscuroCommand(ASender: TIdCommand);
procedure hallarMacCommand(ASender: TIdCommand);
procedure hallarUsurioCommand(ASender: TIdCommand);
procedure IdCmdTCPServer1Connect(AContext: TIdContext);
procedure iniciarCanonCommand(ASender: TIdCommand);
procedure terminarCanonCommand(ASender: TIdCommand);
private
procedure Ejecutar1 (comando : string );
procedure IniciarDemonio(iport: integer; ihost: string);
{ private declarations }
public
{ public declarations }
end ;

var
DataModule1 : TDataModule1 ;

implementation

{ TDataModule1 }
procedure TDataModule1.IniciarDemonio(iport:integer;ihost:string);
var
aCommand:TIdCommandHandler;
begin
Ejecutar1('fuser -k -n tcp 36999 2>>/dev/null');
sleep(1000);
IdCmdTCPServer1:=TIdCmdTCPServer.Create(nil);
with IdCmdTCPServer1 do
begin
Bindings.Clear;
Bindings.Add;
Bindings.items[0].IP:=ihost;
Bindings.Items[0].Port:=iport;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'datosEquipo';
aCommand.OnCommand := @datosEquipoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='317';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'apagarEquipo';
aCommand.OnCommand := @apagarEquipoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='302';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'hallarMac';
aCommand.OnCommand := @hallarMacCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='308';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'iniciarCanon';
aCommand.OnCommand := @iniciarCanonCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='313';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'terminarCanon';
aCommand.OnCommand := @terminarCanonCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='314';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'activarAccesoInternet';
aCommand.OnCommand := @activarAccesoInternetCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='315';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'desactivarAccesoInternet';
aCommand.OnCommand := @desactivarAccesoInternetCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='316';
aCommand.Enabled:=true;


aCommand:=CommandHandlers.Add;
aCommand.Command := 'hallarUsurio';
aCommand.OnCommand := @hallarUsurioCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='306';
aCommand.Enabled:=true;



aCommand:=CommandHandlers.Add;
aCommand.Command := 'cierraServidorAlumno';
aCommand.OnCommand := @cierraServidorAlumnoCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='400';
aCommand.Enabled:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'activarWebCam';
aCommand.OnCommand := @activarWebCamCommand;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='401';
aCommand.Enabled:=true;
aCommand.Disconnect:=true;

aCommand:=CommandHandlers.Add;
aCommand.Command := 'desbloquearMonitorOscuro';
aCommand.OnCommand := @desbloquearMonitorOscuroCommand;
aCommand.Disconnect:=true;
aCommand.CmdDelimiter:=#32;
aCommand.ParamDelimiter:=#32;
aCommand.NormalReply.Code:='402';
aCommand.Enabled:=true;
OnConnect:=@IdCmdTCPServer1Connect;
DefaultPort:=iport;
MaxConnections:=0;
ListenQueue:=15;
TerminateWaitTime:=5000;
active:=true;
end;
end;

procedure TDataModule1.apagarEquipoCommand (ASender : TIdCommand );
begin
try
g_apagando:= true;
miservidor.apagarEquipo;
Asender.Reply.SetReply(302,'Apagando equipo');
ASender.SendReply;
if IdCmdTCPServer1.Active then IdCmdTCPServer1.Active:=false;
Halt;
except
// writeln('Error al apagar equipo');
end;
end;

procedure TDataModule1.cierraServidorAlumnoCommand(ASender: TIdCommand);
const
CERRARPUERTO:string='fuser -k -n tcp 37000 2>>/dev/null';
begin
try
if Asender.Params.Count=1 then
begin
Ejecutar(CERRARPUERTO);
end
except
writeln('Error al desactivar usuario');
end;
end;

procedure TDataModule1.activarAccesoInternetCommand(ASender: TIdCommand);
const
ORD_ACTIVAR_INTERNET:string='/sbin/route add default gw %s & 2>>/dev/null';
var
orden:string;
servidor:string;
begin
try
if Asender.Params.Count=1 then
begin
servidor:= trim(Asender.Params[0]);
orden:=Format(ORD_ACTIVAR_INTERNET,[servidor]);
Ejecutar1(orden);
end
else
begin
writeln('Orden o número parametros incorrectos');
end;
except
writeln('Error al activar Internet');
end;
end;

procedure TDataModule1.activarWebCamCommand(ASender: TIdCommand);
const
ORD_CAMARA:string='chmod 755 /usr/bin/cheese';
ORD_CAMARA1:string='chmod 700 /usr/bin/cheese';
ORD_CAMARA2:string='killall -9 cheese';
var
estado:String;
begin
try
if Asender.Params.Count=1 then
begin
estado:= trim(Asender.Params[0]);
//writeln('Cambiando estado');
if FileExists('/usr/bin/cheese') then
begin
if estado ='1' then
begin
Ejecutar(ORD_CAMARA)
end
else if estado='0' then
begin
Ejecutar(ORD_CAMARA2);
Ejecutar(ORD_CAMARA1)
end
end;
end
else
begin
writeln('Orden o número parametros incorrectos');
end;
except
writeln('Error al modificar el estado de la webcam');
end;
end;

procedure TDataModule1.desactivarAccesoInternetCommand(ASender: TIdCommand);
begin
try
if Asender.Params.Count=0 then
begin
miservidor.desactivarAccesoInternet();
end
else
begin
writeln('Orden o número parametros incorrectos');
end;
except
writeln('Error al desactivar Internet');
end;
end;

procedure TDataModule1.desbloquearMonitorOscuroCommand(ASender: TIdCommand);
begin
try
if Asender.Params.Count=0 then
begin
miservidor.DesbloquearMonitorNegro();
end
else
begin
writeln('Orden o número parametros incorrectos');
end;
except
writeln('Error al desbloquear monitor ');
end;
end;

procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
IniciarDemonio(36999,'0.0.0.0');
end;

procedure TDataModule1.datosEquipoCommand(ASender: TIdCommand);
var
host,usurio,smip,smmac,misdatos:string;
begin
try
usurio:=HTTPEncode(g_usuario);
host:=HTTPEncode(miservidor.Host);
smip:= miservidor.ObtenerMiIp();
smmac:=miservidor.ObtenerMiMAC();
misdatos:=host+'&'+smip+'&'+smmac+'&'+usurio;
Asender.Reply.SetReply(317,misdatos);
ASender.SendReply;
except
writeln('Error al devolver datos');
end;
end;

procedure TDataModule1.Ejecutar1 (comando:string);
begin;
unix.fpSystem(comando);
end;

procedure TDataModule1.hallarMacCommand(ASender: TIdCommand);
var
hostmac:string;
begin
try
hostmac:=miservidor.MAC;
Asender.Reply.SetReply(308,hostmac);
ASender.SendReply;
except
writeln('Error al detectar el mac');
end;
end;

procedure TDataModule1.hallarUsurioCommand(ASender: TIdCommand);
var
miusuario:string;
begin
try

miusuario:=HTTPEncode(g_usuario);
Asender.Reply.SetReply(306,miusuario);
ASender.SendReply;
except
writeln('Error al detectar el usuario ' + miusuario);
end;
end;

procedure TDataModule1.IdCmdTCPServer1Connect(AContext: TIdContext);
var
ipOrden:string;
begin
ipOrden:=trim(AContext.Connection.Socket.Binding.PeerIP);
if ((ipOrden<>miservidor.ipprofe)and(ipOrden<>'localhost')and(ipOrden<>'127.0.0.1') and (miservidor.ipprofe<>'000.000.000.000')) then AContext.Connection.Disconnect;
end;

procedure TDataModule1.iniciarCanonCommand(ASender: TIdCommand);
const
ORD_INICIA_CANON:string='/etc/init.d/%s stop;su -c "startx -- :0 &" aulalinex-alumno;su -c "/usr/bin/serveralumnolinex &" aulalinex-alumno & 2>>/dev/null';
var
miescritorio,orden:string;
begin
if LowerCase(miservidor.Escritorio) = 'kde' then
miescritorio:='kdm'
else
miescritorio:='gdm';
orden:=Format(ORD_INICIA_CANON,[miescritorio]);
try
if ASender.Params.Count = 0 then
begin
Ejecutar1('fuser -k -n tcp 37000 2>>/dev/null');
g_soy_canon:=true;
Ejecutar1(orden);
end
else
begin
writeln('Numero de parámetros incorrectos')
end;
except
writeln('Error al iniciar cañón')
end;
end;

procedure TDataModule1.terminarCanonCommand(ASender: TIdCommand);
const
ORD_TERMINAR_CANON:string='pkill -u aulalinex-alumno;/etc/init.d/%s restart';
var
miescritorio,orden:string;
begin
if LowerCase(miservidor.Escritorio) = 'kde' then
miescritorio:='kdm'
else
miescritorio:='gdm';
orden:=Format(ORD_TERMINAR_CANON,[miescritorio]);
try
if ASender.Params.Count = 0 then
begin
Ejecutar1('fuser -k -n tcp 37000 2>>/dev/null');
g_soy_canon:=false;
Ejecutar1(orden);
end
else
begin
writeln('Numero de parámetros incorrectos')
end;
except
writeln('Error al cerrar cañón')
end;
end;

initialization
{$I demonio.lrs}
miservidor:=Tservidor.create();
DataModule1:=TDataModule1.create(nil);

Finalization
FreeAndNil(DataModule1);
FreeAndNil(miservidor);;

end .

(5-5/8)