david.ginn524
Posts: 5
Joined: Fri Sep 07, 2012 2:34 pm

turn on script with ftp holding

Fri Sep 07, 2012 2:52 pm

Ok I have an idea that I have been wanting to implement but not entirely sure how to. Here is a basic description.
I have a server that gets used for downloading files where ever i want. It's like dropbox but my own personal one. Unfortunately me keeping it on 24/7 is a lot of electricity/money. I ordered a pi thinking I can do this which i am hoping it is possible. My server is just a giant ftp server.

My idea: If no one is connected it goes to sleep to save power. then when someone tries to connect it starts up and lets the person in.
Since Rpi's don't use much I chose this as a viable option to run 24/7. My plan is to create a script /ftp server on the Rpi. When someone tries to connect the Rpi see's that catches the connection and tells my server to wake up. While it is doing that it "holds" the connection then once the server is up it will forward the connection to the server as if it was on originally.
Hopefully this makes sense? yes?

Right now i have scripts in place on my main server that checks every 5 min to see if someone is connected if not it goes to sleep
I also found an article on how to use the Rpi's GPIO pins to turn on a LED which is written in python.
http://tinyurl.com/7ww3gkw
This is why i posted it in here. In theory I just have to modify the code a tiny to have it turn on my server via the regular power button.
I can easily setup an ftp server on the RPi.

The problem I have is, I have no clue how to "hold" the connection of the incoming FTP then forward it.
There are a few requirements for this pretty much as I see it.
1 it has to accept any ftp connection with any username and password
2 once it runs the script to power on the server it has to check to see when it is ready
3 while its powering up it has to "hold the connection"; maybe give a message say please wait.
4 once it's up and running it has to take the existing connection and forward it to the FTP server; then the other FTP server will check the username and password and continue like it was never off.
5 if the server is already on the RPi should automatically forward the connection without a message /run the scripts

Do you guys have any suggestions on how to even start the FTP section? I think I can manage with the turn on part/script through the GPIO pins

User avatar
jecxjo
Posts: 158
Joined: Sat May 19, 2012 5:22 pm
Location: Minneapolis, MN (USA)

Re: turn on script with ftp holding

Fri Sep 07, 2012 6:52 pm

It looks like you will need to have your Pi running a program that receives messages and stores them until it can forward them to the FTP server. This should be as simple as the typical "Echo Server" code you find in tutorials on TCP communication. But instead of echoing right away, you wait until the server it up and then send the cached and future messages along.

So looking at your steps.

1. You should accept all messages generically. When you receive a packet (or if you are doing TCP, when you get a connection) to trigger your FTP Server to start. Stores the messages in a queue, keep them in order.

2. This can be as simple as just trying to connect to the FTP server from your Pi. Keep trying connections until one get accepted. That will tell you the service is up. (This is how port sniffers work, send packets to a port until you "hear" something).

3. You could look into doing some TCP magic by manipulating the flags in your packets but this would be sort of difficult. I'll make more comments at the end as this step is probably the fault point that will make this less possible.

4. Your Pi doesnt need to actually log in. As stated in step 2, just open a connection to the server and once one is accepted start sending the packets stored in your queue. This code should be structured something like:

Code: Select all

Open World Socket
Listen for connection on World Socket
Loop While World Socket is Open
{
  Receive World Message
  If FTP Socket is Closed
  {
    Queue Message
    Trigger FTP Wakeup
    Loop While FTP Socket is Closed
    {
        Try to open FTP Socket
    }
  }
  If FTP Socket is Open
  {
    If Queue is Empty, Send Message
    Else
    {
        Queue Message
         Send oldest message in Queue
    }
}
You'll want to use multiple threads, one for opening, closing and sending message to FTP and one for listening, receiving and responding to message from the world.

5. If you follow something like the pseudo code I listed above you'll attempt to connect to the FTP server and should just move on to the forwarding phase right away.

So back to Step 3's issue. What are you looking at for start-up time on the FTP Server? If you are talking about a "beige box" booting up you're probably going into timeout issues where the FTP request is sent to the Pi and no response is heard for a while. Manipulating TCP flags will be difficult and might not even work (Delaying SYN/ACK packets to force socket re-connections, or sending PUSH and RST to cause some delay). In the end, unless the FTP Server is going to be up quickly you'll probably have to connect to the Pi, expect a failure of connection, reconnect and have it up.

One possible design change could be to host the FTP Server on the Pi and have your Server power up and create a SSHFS or NFS connection between Pi and Server. You'd still have to script up some code to listen for an FTP connection from the world to your pi, but at least at that point you'd be able to connect and just not see directories until the server is up. I'd look into systemd (a new replacement for SysV Init Scripts). It will start up your FTP Server on the Pi and there might be a way to get it to trigger your FTP Server Wakeup script when a connection is made. Read up on Socket Activated Services and see if systemd allows you to configure a "When a socket is connected for XXX Service, run this script as well as start up the service"
xmpp: [email protected]
Blog: http://jecxjo.motd.org/code

david.ginn524
Posts: 5
Joined: Fri Sep 07, 2012 2:34 pm

Re: turn on script with ftp holding

Fri Sep 07, 2012 7:52 pm

holy cow thanks for your response. I understand everything your saying along with the pseudo code which is actually better than was i was originally thinking. The only problem i've never done any programming with TCP connections etc. I will take all you said into consideration.

I just posted this to see if anyone had any ideas. Right now I am going to get the Power on and off scripts working perfectly through the GPIO pins then I will report back to see if i can set up some time of java application or something to work with the FTP.
I will also see how long it takes to wake up from sleep.
Honestly wish i knew more about programming because i have so many cool ideas i want to do, LOL i even work for a software company but i only test the product not code it. There are not enough hours in the day to do everything i want to. It's probably going to be a few weeks before I am able to code stuff for it :( but i did set up my other Rpi as a media server like everyone else :)
Thanks again I shall look further into this type of thing it gives me hope that it can actually be done

david.ginn524
Posts: 5
Joined: Fri Sep 07, 2012 2:34 pm

Re: turn on script with ftp holding

Sun Sep 09, 2012 9:44 pm

@ jecxjo

for an echo server are you talking something similar to this?
http://www.paulgriffiths.net/program/c/ ... rvsrc.html

i have gotten the wake up script to work. now working on the sleep script. also going to test the sleep recover time . Then i will start on what you referred to
Thanks again

david.ginn524
Posts: 5
Joined: Fri Sep 07, 2012 2:34 pm

Re: turn on script with ftp holding

Sun Sep 09, 2012 10:07 pm

well apparently i thought i had the wake up script working. and while it does work it code it does not function as i had intended. since GPIO4 and ground are always "on" even without a voltage of 3 or w/e it goes up to. my server still see's it as time to go to sleep. I guess i need a small relay in there :( so it keeps circuit off then when my script sends the voltage on GPIO4 it opens the gate and sends a quick sleep command via the power button

User avatar
jecxjo
Posts: 158
Joined: Sat May 19, 2012 5:22 pm
Location: Minneapolis, MN (USA)

Re: turn on script with ftp holding

Mon Sep 10, 2012 4:52 pm

david.ginn524 wrote:@ jecxjo

for an echo server are you talking something similar to this?
http://www.paulgriffiths.net/program/c/ ... rvsrc.html

i have gotten the wake up script to work. now working on the sleep script. also going to test the sleep recover time . Then i will start on what you referred to
Thanks again
Yep that is exactly what I'm talking about. Gives you a good intro into how to create a listening socket, receive data and send data. Now you'll just have to setup two sockets (one for listening to FTP Requests and one to connect to the FTP Server) and possibly put them in separate threads to make it a little more usable.
xmpp: [email protected]
Blog: http://jecxjo.motd.org/code

david.ginn524
Posts: 5
Joined: Fri Sep 07, 2012 2:34 pm

Re: turn on script with ftp holding

Wed Sep 19, 2012 7:39 pm

ok well I started writing something, This was a snippet i got off a website and modified it a little.

currently this is what I have, I am able to telnet into it at a specified port and type w/ then it writes it to a log. A few things that I don't know are
1: when I run it the first like will always have "ÿûÿû ÿûÿû'ÿýÿûÿý" in front of it then what ever i type. i am not sure what that is. I am guessing that it would be because either 1 its null or 2 that is what telnet sends to connect to the socket. At any rate is there anyway to clear/flush this?

2 I have tried to look up some java ftp libraries but all i find are client libraries is there any libraries out there that you know of that I could use? because when i try to connect to the socket via FTP it times out because it doesn't get a response. (obviously since i haven't set it up.)

Code: Select all

import java.net.*;
import java.io.*;
 
public class PortMonitor {
 
    /**
     * JavaProgrammingForums.com
     */
    public static void main(String[] args) throws Exception {
 
        //Port to monitor
        final int myPort = 1901;
        ServerSocket ssock = new ServerSocket(myPort);
        System.out.println("port " + myPort + " opened");
 
        Socket sock = ssock.accept();
        System.out.println("Someone has made socket connection");
 
        OneConnection client = new OneConnection(sock);
        String s = client.getRequest();
 
    }
 
}
 
class OneConnection {
    Socket sock;
    BufferedReader in = null;
    DataOutputStream out = null;
 
    OneConnection(Socket sock) throws Exception {
        this.sock = sock;
        
        in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        out = new DataOutputStream(sock.getOutputStream());
     
    }

    String getRequest() throws Exception {
        String s = null;
        
        while ((s = in.readLine()) != null) {
        	
       
        	 FileWriter fstream = new FileWriter("log.txt",true);
        	  BufferedWriter out = new BufferedWriter(fstream);
        	System.out.println("got: " +s);
        	out.write(s);
        
        	 out.close();
        
        }
       
        return s;
        
    }
}

User avatar
jecxjo
Posts: 158
Joined: Sat May 19, 2012 5:22 pm
Location: Minneapolis, MN (USA)

Re: turn on script with ftp holding

Sat Sep 22, 2012 3:32 am

The best thing to do for figuring out what network traffic is flying by is to install Wireshark. Simple to use, very helpful.

http://www.wireshark.org/
xmpp: [email protected]
Blog: http://jecxjo.motd.org/code

Return to “Python”