AS3 FTP Programming and the Socket and ByteArray Classes - actionscript-3

Sorry for the subject line sounding like an even nerdier Harry Potter title.
I'm trying to use AS3's Socket class to write a simple FTP program to export as an AIR app in Flex Builder 3. I'm using an FTP server on my local network to test the program. I can successfully connect to the server (the easy part) but I can't send any commands. I'm pretty sure that you have to use the ByteArray class to send these commands but there's some crucial piece of information that I'm missing apparently. Does anyone know how to do this? Thanks!
Dave

The FTP protocol predates UTF encoding. Switch to ANSI/ASCII for better results. If you do opt for writeMultiByte instead of writeUTFBytes, be aware that it is buggy in linux. Here's one way around it.
There's another question here where the line ending turns out to be the culprit, so make sure that you get it right (as suggested above).
As said before, if this is running from the web, all socket connections will require a crossdomain policy, but this is NOT file based over HTTP. Recent changes to the security rules mean that any socket based connection must first get a crossdomain from a policy server hosted on port 843 of the target host.
Quoting from Adobe:
A SWF file may no longer make a socket connection to its own domain without a socket policy file. Prior to version 9,0,115,0, a SWF file was permitted to make socket connections to ports 1024 or greater in its own domain without a policy file.
HTTP policy files may no longer be used to authorize socket connections. Prior to version 9,0,115,0, an HTTP policy file, served from the master location of /crossdomain.xml on port 80, could be used to authorize a socket connection to any port 1024 or greater on the same host.
Essentially, what this means is that you must be in control of the target FTP host, and install supplementary software on it to get this working.

Read this link too and maybe it can be useful this one too.
The first one is about policy files and the second is an example of a TELNET (so, no FTP here) client.

I've been able to get an FTP client working in a browser, but it's buggy. I had to get a listener running on port 843 to server the policy file so that Flash would be allowed to connect and transfer data. Then, I had to figure out how FTP actually works:
You have to open 2 sockets: a command socket and a data socket. The command socket is where you send your USER, PASS, CWD, and STOR commands. The data socket is where you write your ByteArray data to. Sending the PASV command will tell you what port your data socket must connect to.
Where it is buggy is on Mac, in both Safari and FF, when I call the "socket.close()" command, the server socket actually closes. On Windoze, it does not. This is a huge problem because the Event.CLOSE event is not fired until the SERVER closes the connection. This is in the livedocs.
This is where I'm at. I have no idea why it would work flawlessly on Mac and then be completely busted in 3 different browsers on Windows. The only thing I can come up with is that it's either something in my Windows configuration that's preventing proper communication with the server, or it's the Window Flash player that's causing the problem.
Any thoughts?

We will need more info to resolve this.. What you're saying here appears correct to me. You're using the Socket class to send data though, not ByteArray.
Are you sure data is not being sent? How are you receiving the response? It may be that it's working fine but you're just not aware of it? As i said, tell us more about what you're doing..
Lee Brimelow has a screencast on gotoAndLearn of writing an POP3 client. It's essentially the same of what you're doing so take a look.

Are you 100% sure the syntax is correct? I know with HTTP you'll have to an include extra linebreak after the request for it to go through. Without it you'll get nothing back. Not sure how it is with FTP though.

The FTP standard requires CRLF at the end of commands. Try using "\r\n" in place of the "\n" in your example.

You must serve the CrossDomain Policy File from your FTP server in order to conect correctly.

From what I've gathered, you have to send each command one at a time and validate the response before moving on. You should be getting something back against ProgressEvent.SOCKET_DATA
Try just this and see what you get in response.
socket.writeUTFBytes("USER "+user+"\n"); socket.flush();
You would then read the response out like this.
var response:String = mySocket.readUTFBytes(mySocket.bytesAvailable);

Related

Can I use html5 Websockets with windows domain authentication?

Our setup is like this: we use a coldfusion 10 server, and files are on a local intranet. Users use a domain login to access any files over https. I'm interested in using html5 websockets, but the first attempt gave me an error because no credentials were supplied. Is there a way around this? If not, is there a recommended technology for this scenario?
The user does log in on the client side. If it's possible, what I'd really like to do here is pass those credentials when making the connection to the server.
you should be able to supply the authentication header to your web socket server before the elevation to web socket read that and send it back in the headers for the elevation (first frame) then when the browser connects it should have the authentication it needs.
if your using a custom authentication E.G in page not authentication header you can also do this by passing any custom header to your server.
Or mandate that the first thing your web client sends is the authentication details this could be something like {username_hash}.{password_hash} if they don't close the socket to them.
Do not do this.
You're now responsible for sending and encrypting the authentication credentials yourself, bypassing something that already works and is tested.
Anyone can snoop on an unencrypted websocket port. Use HTTPs for an intranet, use stable solutions, don't reinvent this wheel because it tickles your fancy.
In a couple of years some colleague will have to maintain this and will have to figure out how your homebrew version works versus something that's solid like plain browser authentication.
My advice: Leave this to the browser and to well-tested coldfusion libraries.

How to create flash socket policy files to open port 843 for flash application in Apache server run on windows

I am creating a real time application and have come across HTML5 web-socket which is the ideal technique over ajax long polling. But web-socket is not guaranteed to run in every browser. I tested with IE-8 and IE-9, it is not supported in those versions.
I found web-socket-js which seems to be a perfect fallback for the browser not supporting modern web-socket. It uses flash for to carry out the task. But since flash 9, flash application are now required with flash socket policy file to access port 843. I have researchd in this article setting up a flash socket policy but could not find a solution that works with Apache installed in windows. How should I create this policy file and where should I put it. I am very much confused.
A policy file dictates the hosts from where .swf files can access services on the server. A policy file server is a TCP server that hosts a master policy file (an XML document) on port 843. The Flash Player first attempts to access the master policy file on this port, but if it doesn't find the policy file, requests for the file on the port on which it is trying to fetch the data.
That means the Flash Player actually makes 2 network requests. One goes to port 843 by default. If that request times out (in 3 seconds) then it makes a second request to the port you are making a connection to (presuming it's port 80 in your case).
If I remember correctly, the request is made to the following URL -
hostname:port//crossdomain.xml
In your case, I think it might just be a matter of putting the master policy file in the root of your web server. Apache uses the DocumentRoot directive in the .conf file to identify this directory.

Test Policy File service without Flash?

I have a Policy File service written in Node.js (Windows 7 environment). I simply want to test that the service returns the policy when a request is made over port 843, but without using Flash (i.e. Telnet, Browser, Powershell, Python, anything really.)
I've looked extensively but can't find any solution that does this. I have attempted with Telnet, but on connection, any keypress at all causes the server to return a bad response (presumably because the request didn't contain the requisite <policy-file-request/>\0 data.)
Is this possible?
You need to rebuild your service to wait for more data if there is incomplete data. Then you will be able to use Telnet to debug the service.

Send HTTP request through browser to a program that is listening on a port

So I am trying to build a C Program that handles HTTP requests through a web browser. The program is listening on a predefined port for incoming HTTP requests and responds appropriately. It is working totally fine when I place is it in the root directory but it doesnt work when I move it to directory on its own.
For instance, this works:
localhost:9000
but this doesnt:
localhost:9000/myprogram
Any clues on how I may fix this?
Hard to answer with so few details. But to get started I would check the following:
What does the web server log say?
Is the web server configured to allow execution of [CGI] in this directory?
Are you sure you haven't mixed up the web root with the directory root, thus placing your
program in the wrong place?
Are the directory and program access modes set correctly?

wso2 esb not running on linux server

I have some problem with my wso2-esb, it is on a remote linux server and seems to be succesfully started:
But when I try to access it, does not work:
I am not any expert in servers, it is the first time I do such thing, so I probably missed some basic step or something you might know.
Please suggest
Thanks a lot
SOLVED the server was firewalled, I had to add an exception to access it
Two things might be happening.
IE is masking an HTTP error response with its friendly errors.
Since this is a remote server, iptables could be running on the server, or there is another firewall in the way blocking that port.
To diagnose, I would start by disabling friendly error messages in IE, or using a different browser that doesn't do this. Instructions on how to disable it here: http://technet.microsoft.com/en-us/library/cc778248(v=ws.10).aspx
Next, if that doesn't resolve it, I would try running curl/wget on the server, and requesting the displayed URL. curl can be run with curl example.com and wget wget -qO- example.com, both will displayed the returned data (if any) on the terminal. If one returns a command not found, try the other. If that doesn't work, something is going on with your server. If it's returning something that looks like an error (e.g. a sever generated error page), I'd look into that too at this step.
If you appear to have connectivity issues, you can see if there's any iptables rules in place by running iptables -L on the server. A DROP all under Chain INPUT would cause this. You can read more about iptables here, and how to set it up for your needs here: https://help.ubuntu.com/community/IptablesHowTo (Even if you're not using ubuntu, this will still work for you, look in the "Allowing Incoming Traffic on Specific Ports" section, there's an example there you would need to adopt slightly for the non-standard port the server is running on). If there's an external firewall preventing access, you would need to talk with whoever is managing the sever.
Its seems some network issue. Can you try to ping the IP. If you cannt ping that means the machine is not reachable.
Regards,
/nuwan