uses Classes, SysUtils, IdHTTP, Md5;
const
FRITZBOX_HOST = '
fritz.box';
type
TBoxInfo =
record
[....]
SID :
String;
TFritzBox =
class(TObject)
private
ffHTTP : TIdHTTP;
//INdy HTTP Componente
fError :
String;
fBox : TBoxInfo;
// Aktuelle FritzBox
[....]
function GetSID :
String;
public
constructor Create;
destructor Destroy;
override;
[....]
Procedure LogIn(ABenutzername:
string = '
'; APassword:
string = '
');
procedure Logout;
procedure WLanAusAn(aOn :
String);
[.....]
property SID :
String read GetSID;
end;
[.......]
Constructor TFritzBox.Create;
begin
ffHTTP := TIdHttp.Create(
nil);
end;
Destructor TFritzBox.Destroy;
begin
ffHTTp.Free;
inherited;
end;
procedure TFritzBox.LogIn(ABenutzername:
string = '
'; APassword:
string = '
');
var
challenge,response,sid :
String;
function InternalGetChallenge :
String;
begin
Result := ffHTTP.Get('
http://' + FRITZBOX_HOST + '
/login_sid.lua');
if Pos('
<challenge>',LowerCase(Result))=0
then
raise Exception.Create('
Fehler beim Initialisieren der Verbindung.');
System.Delete(Result,1,Pos('
<challenge>',LowerCase(Result))+10);
System.Delete(Result,Pos('
</',Result),Length(Result)-Pos('
</',Result)+1);
end;
function InternalGetResponse :
String;
var
Hash : TMD5_CTX;
Digest: TMD5digest;
str :
String;
i : Integer;
begin
str := challenge+'
-'+APassword;
MD5Init(Hash);
MD5Update(Hash, @str[1],Length(str)*2);
MD5Final(Hash,Digest);
Result := '
';
for i := 0
to 15
do
Result := Result + IntToHex(Digest[i],2);
Result := LowerCase(Result);
end;
function InternalGetSid :
String;
var
Params :
String;
begin
Result := '
';
Params:=('
?username=' + ABenutzername + '
&response=' + challenge + '
-' + response);
Result := ffHTTP.Get('
http://' + FRITZBOX_HOST + '
/login_sid.lua' + Params);
if Pos('
<sid>',LowerCase(Result))=0
then
raise Exception.Create('
Fehler beim Generieren der Sitzungs-ID.');
System.Delete(Result,1,Pos('
<sid>',LowerCase(Result))+4);
System.Delete(Result,Pos('
</',Result),Length(Result)-Pos('
</',Result)+1);
end;
begin
ffHTTP.HandleRedirects := true;
ffHTTP.Request.ContentLength := -1;
ffHTTP.Request.Accept := '
text/html, */*';
ffHTTP.HTTPOptions := [hoForceEncodeParams];
try
challenge := InternalGetChallenge;
response := InternalGetResponse;
sid := InternalGetSid;
FBox.SID := sid;
except
on E:
Exception do fError := E.
Message;
end;
end;
procedure TFritzBox.Logout;
begin
ffHTTP.HandleRedirects := true;
ffHTTP.Request.ContentLength := -1;
ffHTTP.Request.Accept := '
text/html, */*';
ffHTTP.HTTPOptions := [hoForceEncodeParams];
ffHTTP.Get('
http://'+ FRITZBOX_HOST + '
/login_sid.lua?logout=0&sid=' + fBox.SID);
fBox.SID := '
0000000000000000';
end;
procedure TFritzBox.WLanAusAn(aOn :
String):;
var hUrl :
String;
hParams : TStringlist;
Begin
LogIn('
xxx','
xxx');
ffHTTP.HandleRedirects := true;
ffHTTP.Request.ContentLength := -1;
ffHTTP.Request.Accept := '
text/html, */*';
ffHTTP.HTTPOptions := [hoForceEncodeParams];
hParams := Tstringlist.Create;
try
hParams.Clear;
hParams.Add('
wlan:settings/ap_enabled=' + aOn);
hParams.Add('
getpage=/wlan/wlan_settings.lua');
hUrl := '
http://'+ FRITZBOX_HOST + '
/wlan/wlan_settings.lua?sid=' + fBox.SID;
ffHTTP.Post(hUrl,hParams);
finally
hParams.Free;
end;
Logout;
end;