|
unit clservidor;
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
unix,Classes, SysUtils,IniFiles,process,clcomandosroot, HTTPDefs;
|
|
type
|
|
|
|
{ Tservidor }
|
|
|
|
Tservidor = class
|
|
private
|
|
fGateway:string;
|
|
fEscritorio:string;
|
|
fCamara:Boolean;
|
|
fInterface:string;
|
|
fIPVLC:string;
|
|
fIP:String;
|
|
fMAC:string;
|
|
fHost:string;
|
|
fUsuarioAlumno:String;
|
|
fInternetDesactivado:Boolean;
|
|
fIPProfe:String;
|
|
procedure activarWebcab();
|
|
procedure AnadeIPVLC();
|
|
procedure eliminaBloqueoPuppet();
|
|
procedure LeerSetupIni(archivo: string);
|
|
public
|
|
constructor create (archivoini:string='/etc/aulalinex-red.conf');
|
|
property UsuarioAlumno:String read fUsuarioAlumno write fUsuarioAlumno;
|
|
property IPVLC:String read fIPVLC;
|
|
property IPProfe:String read fIPProfe write fIPProfe;
|
|
property Escritorio:String read fEscritorio;
|
|
property Gateway:String read fGateway;
|
|
property IP:String read fIP;
|
|
property MAC:String read fMAC;
|
|
property Host:String read fHost;
|
|
procedure ApagarEquipo();
|
|
function ObtenerHost():string;
|
|
procedure Ejecutar(comando: string);
|
|
function HallarDato(Cmd: string): string;
|
|
function ObtenerNombreUsuario(): string;
|
|
procedure desactivarAccesoInternet();
|
|
procedure EnviarDatosEquipo();
|
|
procedure actualizaEstadoEquipo();
|
|
function ObtenerMiIp():string;
|
|
function ObtenerMiMAC():string;
|
|
procedure DesbloquearMonitorNegro();
|
|
end;
|
|
|
|
implementation
|
|
uses globales;
|
|
procedure Tservidor.AnadeIPVLC();
|
|
const
|
|
ORD_ANADIR_IPVLC:String='/sbin/route add -net %s/24 gw %s 2>>/dev/null';
|
|
var
|
|
miorden:string;
|
|
begin
|
|
try
|
|
miorden:=Format(ORD_ANADIR_IPVLC,[fIPVLC,fGateway]);
|
|
Ejecutar(miorden);
|
|
except
|
|
//writeln('Error al añadir ip vlc');
|
|
end;
|
|
end;
|
|
|
|
procedure Tservidor.LeerSetupIni(archivo:string);
|
|
var
|
|
FicheroIni:TMemIniFile;
|
|
begin
|
|
try
|
|
FicheroIni:=TMemIniFile.create(archivo);
|
|
try
|
|
with FicheroIni do
|
|
begin
|
|
fInternetDesactivado:=ReadBool('Internet','Internet desactivado',false);
|
|
fIPVLC:=ReadString('Video','IP VLC','239.255.255.0');
|
|
fGateway:=ReadString('Servidor','IP Gateway','192.168.0.254');
|
|
fIPProfe:=ReadString('Profesor','IP Profesor','192.168.0.254');
|
|
fInterface:=ReadString('Red','Interface','eth0');
|
|
fEscritorio:=ReadString('Entorno','Escritorio','gdm');
|
|
fCamara:=ReadBool('Entorno','Webcam',true);
|
|
|
|
end;
|
|
finally
|
|
FicheroIni.free;
|
|
end;
|
|
except
|
|
writeLn('Error al cargar datos');
|
|
end;
|
|
end;
|
|
|
|
function Tservidor.ObtenerNombreUsuario():string;
|
|
begin
|
|
result:=g_usuario;
|
|
end;
|
|
|
|
procedure Tservidor.activarWebcab();
|
|
const
|
|
ORD_CAMARA:string='chmod 755 /usr/bin/cheese';
|
|
ORD_CAMARA1:string='chmod 700 /usr/bin/cheese';
|
|
begin
|
|
try
|
|
if FileExists('/usr/bin/cheese') then
|
|
begin
|
|
if fCamara then
|
|
Ejecutar(ORD_CAMARA)
|
|
else
|
|
Ejecutar(ORD_CAMARA1)
|
|
end;
|
|
except
|
|
//writeln('Error al activab webcam');
|
|
end
|
|
end;
|
|
function Tservidor.ObtenerHost():string;
|
|
const
|
|
ORD_HOST:string='hostname | cut -f1 -d"."';
|
|
begin
|
|
try
|
|
result:=HallarDato(ORD_HOST);
|
|
except
|
|
result:=''
|
|
end;
|
|
end;
|
|
|
|
procedure Tservidor.Ejecutar (comando:string);
|
|
begin;
|
|
try
|
|
unix.fpSystem(comando);
|
|
except
|
|
//writeln('Error al ejecutar comando');
|
|
end;
|
|
end;
|
|
|
|
function Tservidor.HallarDato(Cmd:string):String;
|
|
var
|
|
file1: TextFile;
|
|
s,t: ansistring;
|
|
n: longint;
|
|
j:integer;
|
|
begin
|
|
try
|
|
j:=0;
|
|
n:=popen(file1, Cmd, 'r');
|
|
if n=-1 then
|
|
begin
|
|
result:='';
|
|
Exit;
|
|
end;
|
|
t:='';
|
|
while not eof(file1) do begin
|
|
Readln(File1,s);
|
|
t:=t+s+Chr(10);
|
|
inc(j);
|
|
end;
|
|
pclose(file1);
|
|
result := trim(t);
|
|
except
|
|
result:='';
|
|
end;
|
|
end;
|
|
constructor Tservidor.create(archivoini:string='/etc/aulalinex-red.conf');
|
|
begin
|
|
inherited create;
|
|
fInterface:='eth0';
|
|
fIPVLC:='239.255.255.0';
|
|
fGateway:='192.168.0.254';
|
|
fIPProfe:='192.168.0.254';
|
|
fCamara:=true;
|
|
fInternetDesactivado:=false;
|
|
fEscritorio:='gdm';
|
|
eliminaBloqueoPuppet();
|
|
if FileExists(archivoini) then
|
|
LeerSetupIni(archivoini);
|
|
fIP:=ObtenerMiIP();
|
|
fMAC:=ObtenerMiMAC();
|
|
fHost:=ObtenerHost();
|
|
if fInternetDesactivado then
|
|
desactivarAccesoInternet();
|
|
activarWebcab();
|
|
AnadeIPVLC();
|
|
end;
|
|
|
|
procedure Tservidor.apagarEquipo;
|
|
const
|
|
ORDEN_APAGAR:string='/sbin/poweroff';
|
|
begin
|
|
Ejecutar(ORDEN_APAGAR);
|
|
end;
|
|
|
|
procedure Tservidor.eliminaBloqueoPuppet();
|
|
var
|
|
fch1:TDateTime;
|
|
fechaArchivo:String;
|
|
fechaActual:String;
|
|
begin
|
|
try
|
|
if FileExists('/var/lib/puppet/state/pupptlock') then
|
|
begin
|
|
fch1:=FileDateToDateTime(FileAge('/var/lib/puppet/state/pupptlock'));
|
|
fechaArchivo:=FormatDateTime('dd/mm/yyyy',fch1);
|
|
fechaActual:=FormatDateTime('dd/mm/yyyy',Now());
|
|
if ( fechaActual<> fechaArchivo) then
|
|
begin
|
|
Ejecutar('rm -f /var/lib/puppet/state/pupptlock' );
|
|
end;
|
|
end;
|
|
except
|
|
//writeln('Error al desbloquear fichero bloqueado de puppet');
|
|
end;
|
|
end;
|
|
|
|
procedure Tservidor.DesbloquearMonitorNegro();
|
|
Const
|
|
ORD_DESBLOQUEAR_MONITOR='/etc/init.d/gdm stop;rm -fr /home/alumno/%s/.local/share/gvfs-metadata/*;/etc/init.d/gdm start';
|
|
var
|
|
dirborrar:String;
|
|
orden:String;
|
|
begin
|
|
try
|
|
dirBorrar:=Format('/home/alumno/%s/.local/share/gvfs-metadata',[g_usuario]);
|
|
if DirectoryExists(dirborrar) then
|
|
begin
|
|
orden:=Format(ORD_DESBLOQUEAR_MONITOR,[g_usuario]);
|
|
Ejecutar(orden);
|
|
end;
|
|
except
|
|
//writeln('Error al desbloquear monitor');
|
|
end;
|
|
end;
|
|
|
|
function Tservidor.ObtenerMiIp():string;
|
|
const
|
|
ORDEN_IP:String='ifconfig %s 2>> /dev/null| grep -oiE "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 255 | grep -v 127.0.0.1 2>> /dev/null';
|
|
var
|
|
i:integer;
|
|
orden:String;
|
|
mis_tarjetas:array[0..4] of String=('','wlan0','wlan1','eth0','eth1');
|
|
mip:String;
|
|
begin
|
|
try
|
|
mis_tarjetas[0]:=fInterface;
|
|
g_tarjeta_red:='';
|
|
mip:='';
|
|
for i:=0 to Length(mis_tarjetas)-1 do
|
|
begin
|
|
orden:=Format( ORDEN_IP,[ mis_tarjetas[i]]);
|
|
mip:=trim(HallarDato(orden));
|
|
if (mip<>'') then
|
|
begin
|
|
g_tarjeta_red:= mis_tarjetas[i];
|
|
break;
|
|
end
|
|
end;
|
|
result:=mip;
|
|
except
|
|
result:='';
|
|
end;
|
|
|
|
end;
|
|
|
|
function Tservidor.ObtenerMiMAC():string;
|
|
var
|
|
mimac:string;
|
|
begin
|
|
try
|
|
mimac:=Hallardato('/sbin/ifconfig ' +g_tarjeta_red+' | grep -oiE "([0-9A-F]{2}:){5}[0-9A-F]{2}"');
|
|
result:=mimac;
|
|
except
|
|
result:='00:00:00:00:00:00';
|
|
end;
|
|
end;
|
|
|
|
procedure TServidor.desactivarAccesoInternet();
|
|
const
|
|
ORD_DESACTIVAR_INTERNET:string='/sbin/route del default & 2>> /dev/null';
|
|
begin
|
|
try
|
|
Ejecutar(ORD_DESACTIVAR_INTERNET);
|
|
except
|
|
//writeln('Error al enviar datos');
|
|
end;
|
|
end;
|
|
|
|
procedure TServidor.EnviarDatosEquipo();
|
|
var
|
|
mi_ip,mi_mac,mi_nombre,mis_datos,host_profe, orden_actualizar_datos:String;
|
|
begin
|
|
try
|
|
mi_ip:=ObtenerMiIp();
|
|
mi_mac:=ObtenerMiMAC();
|
|
mi_nombre:=ObtenerHost();
|
|
mi_nombre:=HTTPEncode(mi_nombre);
|
|
mis_datos:=mi_nombre+'&'+mi_ip+'&'+mi_mac;
|
|
host_profe:=fIPProfe;
|
|
if host_profe='000.000.000.000' then host_profe:='192.168.0.254';
|
|
orden_actualizar_datos:='actualizarDatosEquipo '+ mis_datos;
|
|
with ThrComandosRoot.create(host_profe,313,orden_actualizar_datos,37001) do Resume;
|
|
except
|
|
writeln('Error al enviar datos');
|
|
end;
|
|
end;
|
|
|
|
procedure TServidor.actualizaEstadoEquipo();
|
|
var
|
|
mi_usuario,mi_ip,mi_mac,mi_nombre,datos_equipo_alumno,host_profe, orden_actualizar_mis_datos:String;
|
|
begin
|
|
try
|
|
if g_apagando then exit;
|
|
mi_ip:=f_IpEquipo;
|
|
mi_mac:=ObtenerMiMAC();
|
|
mi_nombre:=ObtenerHost();
|
|
mi_nombre:=HTTPEncode(mi_nombre);
|
|
mi_usuario:=HTTPEncode(g_usuario);
|
|
datos_equipo_alumno:=mi_nombre+'&'+mi_ip+'&'+mi_mac+'&'+mi_usuario;
|
|
host_profe:=fIPProfe;
|
|
if host_profe='000.000.000.000' then host_profe:='192.168.0.254';
|
|
orden_actualizar_mis_datos:='actualizarDatosEquipo '+ datos_equipo_alumno;
|
|
with ThrComandosRoot.create(host_profe,313,orden_actualizar_mis_datos,37002) do Resume;
|
|
except
|
|
writeln('Error al datos');
|
|
end;
|
|
end;
|
|
|
|
end.
|
|
|
|
|