Still working with Flex FTP and this time I was missing EPSV support. EPSV is roughly the same as PASV, exect that you only get a port number to connect to back and not an IP address. You can read up on RFC 2428 “FTP Extensions for IPv6 and NATs” if you want more details.
The first thing I did to add support for EPSV in Flex FTP is adding a response to the Responses class.
- public static const ENTERING_EPSV:int = 229; //Entering Extended Passive Mode.
Second was the commands class, where I added the following line:
- public static const EPSV:String = "EPSV";
Then I added an extra argument to the FTPClient class’ constructor that will indicate whether or not to use ESPV.
- public function FTPClient (host:String="", port:int=21, useEpsv:Boolean = false)
I store the value internally and use a getter method to determine if an Invoker class (i.e ListInv) needs to use EPSV.
Based on the client epsv setting any invoker can be updated to send the EPSV command instead of the PASV command when needed, i.e. my implementation of ListInv’s startSequence method:
- override protected function startSequence ():void {
- if(client.useEpsv) {
- sendCommand(new FTPCommand(Commands.EPSV));
- } else {
- sendCommand(new FTPCommand(Commands.PASV));
- }
- }
I also update the responseHandler method in ListInv to include the following case:
- case Responses.ENTERING_EPSV:
- passiveSocket =Â Â Â PassiveSocketInfo.createPassiveSocket(evt.response.message,
- handlePassiveConnect,
- handleListing, ioErrorHandler,
- null, true, client.hostname);
- break;
On top of that I made some tweaks to the PassiveSocketInfo class.
The parseFromResponse method now looks like this:
- public static function parseFromResponse (pasvResponse:String, usingEpsv:Boolean = false, hostName:String = ""):PassiveSocketInfo {
- var host:String;
- var port:int;
- if (usingEpsv) {
- host = hostName;
- port = pasvResponse.match(/\d+/)[0];
- } else {
- var match:Array = pasvResponse.match(/(\d{1,3},){5}\d{1,3}/);
- if (match == null)
- throw new Error("Error parsing passive port! (Response: "+pasvResponse+")");
- var data:Array = match[0].split(",");
- host = data.slice(0,4).join(".");
- port = parseInt(data[4])*256+parseInt(data[5]);
- }
- return new PassiveSocketInfo(host, port);
- }
The createPassiveSocket method now takes two extra optional arguments (usingEpsv:Boolean and host:String) and just passes those on to the parseFromResponse method.
Hope this helps anybody looking for EPSV support for Flex FTP.
PS: It’s been a while since I added EPSV support, so let me know about any gaps.