Skip to content

Commit 717fc26

Browse files
authored
Added support for RouterOS 6.43+ API login method
1 parent f5972c9 commit 717fc26

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

RouterOSAPI.pas

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
33
Author: Pavel Skuratovich (aka Chupaka), Minsk, Belarus
44
Description: Implementation of MikroTik RouterOS API Client
5-
Version: 1.2
5+
Version: 1.3
66
77
Support: http://forum.mikrotik.com/viewtopic.php?t=31555
88
Dependencies: Uses Ararat Synapse Library (http://synapse.ararat.cz/)
9-
Legal issues: Copyright © by Pavel Skuratovich
9+
Legal issues: Copyright © by Pavel Skuratovich
1010
1111
This source code is provided 'as-is', without any express or
1212
implied warranty. In no event will the author be held liable
@@ -41,6 +41,9 @@
4141
********************************************************************************
4242
4343
Version history:
44+
1.3 June 04, 2018
45+
Added support for RouterOS 6.43+ API login method
46+
4447
1.2 June 12, 2013
4548
Added basic support for API over TLS
4649
@@ -257,19 +260,31 @@ function TRosApiClient.DoLogin(const Username, Password: AnsiString): Boolean;
257260
begin
258261
Result := False;
259262

260-
Res := Query(['/login'], True);
261-
if Res.Values[0].Name = '!done' then
262-
begin
263-
Res2 := Query(['/login', '=name=' + Username, '=response=00' +
264-
StrToHex(MD5(#0 + Password + HexToStr(Res['=ret'])))], True);
265-
if Res2.Trap then
266-
FSock.CloseSocket
267-
else
268-
Result := True;
269-
Res2.Free;
270-
end
263+
// post-6.43 login method
264+
Res := Query(['/login', '=name=' + Username, '=password=' + Password], True);
265+
if Res.Trap then
266+
// login error
267+
FSock.CloseSocket
271268
else
272-
raise Exception.Create('Invalid response: ''' + Res.Values[0].Name + ''', expected ''!done''');
269+
if Res.Done then
270+
begin
271+
if High(Res.Sentences) <> -1 then
272+
begin
273+
// fallback to pre-6.43 login method
274+
Res2 := Query(['/login', '=name=' + Username, '=response=00' +
275+
StrToHex(MD5(#0 + Password + HexToStr(Res['=ret'])))], True);
276+
if Res2.Trap then
277+
FSock.CloseSocket
278+
else
279+
Result := True;
280+
Res2.Free;
281+
end
282+
else
283+
Result := True;
284+
end
285+
else
286+
raise Exception.Create('Invalid response: ''' + Res.Values[0].Name + ''', expected ''!done''');
287+
273288
Res.Free;
274289
end;
275290

0 commit comments

Comments
 (0)