Debian Codegraph @

plugins/wfx/ftp/src/ftpproxy.pas

raw
1 {
2 Double commander
3 -------------------------------------------------------------------------
4 WFX plugin for working with File Transfer Protocol
5
6 Copyright (C) 2018 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 FtpProxy;
24
25 {$mode delphi}
26
27 interface
28
29 uses
30 Classes, SysUtils, IniFiles, ftpsend;
31
32 type
33 TProxyType = (
34 PROXY_NONE = 0,
35 PROXY_SOCKS4 = 1,
36 PROXY_SOCKS5 = 2,
37 PROXY_HTTP_CONNECT = 3
38 );
39
40 { TFtpProxy }
41
42 TFtpProxy = class
43 public
44 ID: String;
45 Host: String;
46 Port: String;
47 User: String;
48 Password: String;
49 ProxyType: TProxyType;
50 function Clone: TFtpProxy;
51 end;
52
53 var
54 ProxyList: TStringList;
55
56 procedure LoadProxyList(IniFile: TIniFile);
57 procedure SaveProxyList(IniFile: TIniFile);
58 procedure SetProxy(FtpSend: TFTPSend; ProxyID: String);
59
60 implementation
61
62 uses
63 FtpUtils, blcksock;
64
65 procedure LoadProxyList(IniFile: TIniFile);
66 var
67 Proxy: TFtpProxy;
68 I, Count: Integer;
69 sIndex: AnsiString;
70 begin
71 ProxyList.Clear;
72 Count := IniFile.ReadInteger('FTP', 'ProxyCount', 0);
73 for I := 1 to Count do
74 begin
75 sIndex := IntToStr(I);
76 Proxy := TFtpProxy.Create;
77 Proxy.ID := IniFile.ReadString('FTP', 'Proxy' + sIndex + 'ID', EmptyStr);
78 Proxy.Host := IniFile.ReadString('FTP', 'Proxy' + sIndex + 'Host', EmptyStr);
79 Proxy.Port := IniFile.ReadString('FTP', 'Proxy' + sIndex + 'Port', EmptyStr);
80 Proxy.User := IniFile.ReadString('FTP', 'Proxy' + sIndex + 'User', EmptyStr);
81 Proxy.Password := DecodeBase64(IniFile.ReadString('FTP', 'Proxy' + sIndex + 'Password', EmptyStr));
82 Proxy.ProxyType := TProxyType(IniFile.ReadInteger('FTP', 'Proxy' + sIndex + 'Type', Integer(PROXY_NONE)));
83 // Add proxy to proxy list
84 ProxyList.AddObject(Proxy.ID, Proxy);
85 end;
86 end;
87
88 procedure SaveProxyList(IniFile: TIniFile);
89 var
90 Proxy: TFtpProxy;
91 I, Count: Integer;
92 sIndex: AnsiString;
93 begin
94 Count:= ProxyList.Count;
95 IniFile.WriteInteger('FTP', 'ProxyCount', Count);
96 for I := 0 to Count - 1 do
97 begin
98 sIndex := IntToStr(I + 1);
99 Proxy := TFtpProxy(ProxyList.Objects[I]);
100 IniFile.WriteString('FTP', 'Proxy' + sIndex + 'ID', Proxy.ID);
101 IniFile.WriteString('FTP', 'Proxy' + sIndex + 'Host', Proxy.Host);
102 IniFile.WriteString('FTP', 'Proxy' + sIndex + 'Port', Proxy.Port);
103 IniFile.WriteString('FTP', 'Proxy' + sIndex + 'User', Proxy.User);
104 IniFile.WriteString('FTP', 'Proxy' + sIndex + 'Password', EncodeBase64(Proxy.Password));
105 IniFile.WriteInteger('FTP', 'Proxy' + sIndex + 'Type', Integer(Proxy.ProxyType));
106 end;
107 end;
108
109 procedure SetProxy(FtpSend: TFTPSend; ProxyID: String);
110 var
111 Index: Integer;
112 Proxy: TFtpProxy;
113 begin
114 Index:= ProxyList.IndexOf(ProxyID);
115 if (Index < 0) then
116 begin
117 FtpSend.Sock.HTTPTunnelIP:= EmptyStr;
118 FtpSend.Sock.HTTPTunnelUser:= EmptyStr;
119 FtpSend.Sock.HTTPTunnelPass:= EmptyStr;
120
121 FtpSend.DSock.HTTPTunnelIP:= EmptyStr;
122 FtpSend.DSock.HTTPTunnelUser:= EmptyStr;
123 FtpSend.DSock.HTTPTunnelPass:= EmptyStr;
124
125 FtpSend.Sock.SocksIP:= EmptyStr;
126 FtpSend.Sock.SocksUsername:= EmptyStr;
127 FtpSend.Sock.SocksPassword:= EmptyStr;
128
129 FtpSend.DSock.SocksIP:= EmptyStr;
130 FtpSend.DSock.SocksUsername:= EmptyStr;
131 FtpSend.DSock.SocksPassword:= EmptyStr;
132 end
133 else begin
134 Proxy:= TFtpProxy(ProxyList.Objects[Index]);
135 case Proxy.ProxyType of
136 PROXY_HTTP_CONNECT:
137 begin
138 FtpSend.Sock.HTTPTunnelIP:= Proxy.Host;
139 FtpSend.Sock.HTTPTunnelUser:= Proxy.User;
140 FtpSend.Sock.HTTPTunnelPass:= Proxy.Password;
141
142 FtpSend.DSock.HTTPTunnelIP:= Proxy.Host;
143 FtpSend.DSock.HTTPTunnelUser:= Proxy.User;
144 FtpSend.DSock.HTTPTunnelPass:= Proxy.Password;
145
146 if Length(Proxy.Port) > 0 then
147 begin
148 FtpSend.Sock.HTTPTunnelPort:= Proxy.Port;
149 FtpSend.DSock.HTTPTunnelPort:= Proxy.Port;
150 end;
151 end;
152 PROXY_SOCKS4,
153 PROXY_SOCKS5:
154 begin
155 if Proxy.ProxyType = PROXY_SOCKS4 then
156 begin
157 FtpSend.Sock.SocksType:= ST_Socks4;
158 FtpSend.DSock.SocksType:= ST_Socks4;
159 end
160 else begin
161 FtpSend.Sock.SocksType:= ST_Socks5;
162 FtpSend.DSock.SocksType:= ST_Socks5;
163 end;
164
165 FtpSend.Sock.SocksIP:= Proxy.Host;
166 FtpSend.Sock.SocksResolver:= False;
167 FtpSend.Sock.SocksUsername:= Proxy.User;
168 FtpSend.Sock.SocksPassword:= Proxy.Password;
169
170 FtpSend.DSock.SocksIP:= Proxy.Host;
171 FtpSend.DSock.SocksResolver:= False;
172 FtpSend.DSock.SocksUsername:= Proxy.User;
173 FtpSend.DSock.SocksPassword:= Proxy.Password;
174
175 if Length(Proxy.Port) > 0 then
176 begin
177 FtpSend.Sock.SocksPort:= Proxy.Port;
178 FtpSend.DSock.SocksPort:= Proxy.Port;
179 end;
180 end;
181 end;
182 end;
183 end;
184
185 { TFtpProxy }
186
187 function TFtpProxy.Clone: TFtpProxy;
188 begin
189 Result:= TFtpProxy.Create;
190 Result.ID:= ID;
191 Result.Host:= Host;
192 Result.Port:= Port;
193 Result.User:= User;
194 Result.Password:= Password;
195 Result.ProxyType:= ProxyType;
196 end;
197
198 initialization
199 ProxyList:= TStringList.Create;
200 ProxyList.OwnsObjects:= True;
201
202 finalization
203 ProxyList.Free;
204
205 end.
206