| 1 |
{ |
| 2 |
Double commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
WFX plugin for working with File Transfer Protocol |
| 5 |
|
| 6 |
Copyright (C) 2009-2024 Alexander Koblov ([email protected]) |
| 7 |
|
| 8 |
This library is free software; you can redistribute it and/or |
| 9 |
modify it under the terms of the GNU Lesser General Public |
| 10 |
License as published by the Free Software Foundation; either |
| 11 |
version 2.1 of the License, or (at your option) any later version. |
| 12 |
|
| 13 |
This library is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
Lesser General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU Lesser General Public |
| 19 |
License along with this library; if not, write to the Free Software |
| 20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
} |
| 22 |
|
| 23 |
unit FtpUtils; |
| 24 |
|
| 25 |
{$IFDEF FPC} |
| 26 |
{$mode objfpc}{$H+} |
| 27 |
{$ENDIF} |
| 28 |
|
| 29 |
interface |
| 30 |
|
| 31 |
uses |
| 32 |
Classes, SysUtils, WfxPlugin; |
| 33 |
|
| 34 |
const |
| 35 |
cFtpsPort = '990'; |
| 36 |
|
| 37 |
function IsIpPrivate(Value: String): Boolean; |
| 38 |
|
| 39 |
function EncodeBase64(Data: AnsiString): AnsiString; |
| 40 |
function DecodeBase64(Data: AnsiString): AnsiString; |
| 41 |
|
| 42 |
function RepairConnectionName(Connection: AnsiString): AnsiString; |
| 43 |
function ExtractConnectionHost(Connection: AnsiString): AnsiString; |
| 44 |
function ExtractConnectionPort(Connection: AnsiString): AnsiString; |
| 45 |
function ExtractConnectionProt(Connection: AnsiString): AnsiString; |
| 46 |
|
| 47 |
function FormatMachineTime(const Time: TFileTime): String; |
| 48 |
function DecodeMachineTime(const Time: String): TDateTime; |
| 49 |
|
| 50 |
implementation |
| 51 |
|
| 52 |
uses |
| 53 |
Base64, DateUtils, synautil, synaip |
| 54 |
{$IFDEF MSWINDOWS} |
| 55 |
, Windows |
| 56 |
{$ELSE} |
| 57 |
, UnixUtil |
| 58 |
{$ENDIF} |
| 59 |
; |
| 60 |
|
| 61 |
function IsIPv6(Value: String): Boolean; |
| 62 |
var |
| 63 |
Index: Integer; |
| 64 |
begin |
| 65 |
Index:= Pos('[', Value); |
| 66 |
if Index = 1 then |
| 67 |
begin |
| 68 |
Index:= Pos(']', Value, Index + 1); |
| 69 |
if Index > 0 then |
| 70 |
begin |
| 71 |
Value:= Copy(Value, 2, Index - 2); |
| 72 |
end; |
| 73 |
end; |
| 74 |
Result:= IsIP6(Value); |
| 75 |
end; |
| 76 |
|
| 77 |
function StrToIp(Value: String): LongWord; |
| 78 |
var |
| 79 |
S: String; |
| 80 |
I, X: LongWord; |
| 81 |
begin |
| 82 |
Result := 0; |
| 83 |
for X := 0 to 3 do |
| 84 |
begin |
| 85 |
S := Fetch(Value, '.'); |
| 86 |
I := StrToIntDef(S, 0); |
| 87 |
Result := (256 * Result) + I; |
| 88 |
end; |
| 89 |
end; |
| 90 |
|
| 91 |
function IsIpPrivate(Value: String): Boolean; |
| 92 |
var |
| 93 |
Index: Integer; |
| 94 |
Binary: LongWord; |
| 95 |
const |
| 96 |
PrivAddr: array [0..4, 0..1] of LongWord = ( |
| 97 |
// 10.0.0.0 - 10.255.255.255 |
| 98 |
(167772160, 184549375), // Single Class A network |
| 99 |
// 172.16.0.0 - 172.31.255.255 |
| 100 |
(2886729728, 2887778303), // Contiguous range of 16 Class B blocks |
| 101 |
// 192.168.0.0 - 192.168.255.255 |
| 102 |
(3232235520, 3232301055), // Contiguous range of 256 Class C blocks |
| 103 |
// 169.254.0.0 - 169.254.255.255 |
| 104 |
(2851995648, 2852061183), // Link-local address |
| 105 |
// 127.0.0.0 - 127.255.255.255 |
| 106 |
(2130706432, 2147483647) // Loopback (localhost) |
| 107 |
); |
| 108 |
begin |
| 109 |
Binary:= StrToIp(Value); |
| 110 |
for Index:= 0 to 4 do |
| 111 |
begin |
| 112 |
if (Binary >= PrivAddr[Index][0]) and (Binary <= PrivAddr[Index][1]) then |
| 113 |
Exit(True) |
| 114 |
end; |
| 115 |
Result:= False; |
| 116 |
end; |
| 117 |
|
| 118 |
function EncodeBase64(Data: AnsiString): AnsiString; |
| 119 |
var |
| 120 |
StringStream1, |
| 121 |
StringStream2: TStringStream; |
| 122 |
begin |
| 123 |
Result:= EmptyStr; |
| 124 |
if Data = EmptyStr then Exit; |
| 125 |
StringStream1:= TStringStream.Create(Data); |
| 126 |
try |
| 127 |
StringStream1.Position:= 0; |
| 128 |
StringStream2:= TStringStream.Create(EmptyStr); |
| 129 |
try |
| 130 |
with TBase64EncodingStream.Create(StringStream2) do |
| 131 |
try |
| 132 |
CopyFrom(StringStream1, StringStream1.Size); |
| 133 |
finally |
| 134 |
Free; |
| 135 |
end; |
| 136 |
Result:= StringStream2.DataString; |
| 137 |
finally |
| 138 |
StringStream2.Free; |
| 139 |
end; |
| 140 |
finally |
| 141 |
StringStream1.Free; |
| 142 |
end; |
| 143 |
end; |
| 144 |
|
| 145 |
function DecodeBase64(Data: AnsiString): AnsiString; |
| 146 |
var |
| 147 |
StringStream1, |
| 148 |
StringStream2: TStringStream; |
| 149 |
Base64DecodingStream: TBase64DecodingStream; |
| 150 |
begin |
| 151 |
Result:= EmptyStr; |
| 152 |
if Data = EmptyStr then Exit; |
| 153 |
StringStream1:= TStringStream.Create(Data); |
| 154 |
try |
| 155 |
StringStream1.Position:= 0; |
| 156 |
StringStream2:= TStringStream.Create(EmptyStr); |
| 157 |
try |
| 158 |
Base64DecodingStream:= TBase64DecodingStream.Create(StringStream1); |
| 159 |
with StringStream2 do |
| 160 |
try |
| 161 |
CopyFrom(Base64DecodingStream, Base64DecodingStream.Size); |
| 162 |
finally |
| 163 |
Base64DecodingStream.Free; |
| 164 |
end; |
| 165 |
Result:= StringStream2.DataString; |
| 166 |
finally |
| 167 |
StringStream2.Free; |
| 168 |
end; |
| 169 |
finally |
| 170 |
StringStream1.Free; |
| 171 |
end; |
| 172 |
end; |
| 173 |
|
| 174 |
function RepairConnectionName(Connection: AnsiString): AnsiString; |
| 175 |
var |
| 176 |
Index: Integer; |
| 177 |
DenySym: set of AnsiChar; |
| 178 |
begin |
| 179 |
Result:= Connection; |
| 180 |
DenySym:= AllowDirectorySeparators + AllowDriveSeparators + ['<']; |
| 181 |
for Index:= 1 to Length(Result) do |
| 182 |
begin |
| 183 |
if Result[Index] in DenySym then |
| 184 |
begin |
| 185 |
Result[Index]:= '_'; |
| 186 |
end; |
| 187 |
end; |
| 188 |
end; |
| 189 |
|
| 190 |
function ExtractConnectionHost(Connection: AnsiString): AnsiString; |
| 191 |
var |
| 192 |
Index: Integer; |
| 193 |
begin |
| 194 |
Index:= Pos('://', Connection); |
| 195 |
if Index > 0 then Delete(Connection, 1, Index + 2); |
| 196 |
|
| 197 |
if IsIPv6(Connection) then |
| 198 |
begin |
| 199 |
Index:= Pos('[', Connection); |
| 200 |
if Index = 1 then |
| 201 |
begin |
| 202 |
Index:= Pos(']', Connection, Index + 1); |
| 203 |
if Index > 0 then |
| 204 |
begin |
| 205 |
Connection:= Copy(Connection, 2, Index - 2); |
| 206 |
end; |
| 207 |
end; |
| 208 |
end |
| 209 |
else begin |
| 210 |
Index:= Pos(':', Connection); |
| 211 |
if Index > 0 then Connection:= Copy(Connection, 1, Index - 1) |
| 212 |
end; |
| 213 |
Result:= Connection; |
| 214 |
end; |
| 215 |
|
| 216 |
function ExtractConnectionPort(Connection: AnsiString): AnsiString; |
| 217 |
var |
| 218 |
I, J: Integer; |
| 219 |
begin |
| 220 |
I:= Pos('://', Connection); |
| 221 |
if I > 0 then Delete(Connection, 1, I + 2); |
| 222 |
|
| 223 |
if IsIPv6(Connection) then |
| 224 |
begin |
| 225 |
I:= Pos(']:', Connection); |
| 226 |
if I > 0 then Delete(Connection, 1, I + 1); |
| 227 |
end |
| 228 |
else begin |
| 229 |
I:= Pos(':', Connection); |
| 230 |
if I > 0 then Delete(Connection, 1, I); |
| 231 |
end; |
| 232 |
|
| 233 |
if I = 0 then |
| 234 |
Result:= EmptyStr |
| 235 |
else begin |
| 236 |
J:= Pos('/', Connection); |
| 237 |
if J = 0 then J:= MaxInt; |
| 238 |
Result:= Trim(Copy(Connection, 1, J - 1)); |
| 239 |
end; |
| 240 |
end; |
| 241 |
|
| 242 |
function ExtractConnectionProt(Connection: AnsiString): AnsiString; |
| 243 |
var |
| 244 |
I: Integer; |
| 245 |
begin |
| 246 |
Result:= LowerCase(Connection); |
| 247 |
I:= Pos('://', Result); |
| 248 |
if I = 0 then |
| 249 |
Result:= EmptyStr |
| 250 |
else begin |
| 251 |
Result:= Copy(Result, 1, I - 1); |
| 252 |
end; |
| 253 |
end; |
| 254 |
|
| 255 |
function FormatMachineTime(const Time: TFileTime): String; |
| 256 |
var |
| 257 |
FileTime: TDateTime; |
| 258 |
begin |
| 259 |
FileTime:= (Int64(Time) / 864000000000.0) - 109205.0; |
| 260 |
Result:= FormatDateTime('yyyymmddhhnnss', FileTime); |
| 261 |
end; |
| 262 |
|
| 263 |
function DecodeMachineTime(const Time: String): TDateTime; |
| 264 |
var |
| 265 |
Year, Month, Day: Word; |
| 266 |
Hour, Minute, Second: Word; |
| 267 |
begin |
| 268 |
try |
| 269 |
Year:= StrToIntDef(Copy(Time, 1, 4), 1970); |
| 270 |
Month:= StrToIntDef(Copy(Time, 5, 2), 1); |
| 271 |
Day:= StrToIntDef(Copy(Time, 7, 2), 1); |
| 272 |
Hour:= StrToIntDef(Copy(Time, 9, 2), 0); |
| 273 |
Minute:= StrToIntDef(Copy(Time, 11, 2), 0); |
| 274 |
Second:= StrToIntDef(Copy(Time, 13, 2), 0); |
| 275 |
Result:= EncodeDate(Year, Month, Day) + EncodeTime(Hour, Minute, Second, 0); |
| 276 |
Result:= UniversalTimeToLocal(Result, TimeZoneBias); |
| 277 |
except |
| 278 |
Result:= MinDateTime; |
| 279 |
end; |
| 280 |
end; |
| 281 |
|
| 282 |
end. |
| 283 |
|