2

I have an AWS EC2 instance and an app that I created. The app is for people who get migraines (tracks info, tells them what their trigger(s) are). Now I want to be able to send user input from my application to a server so that I can see trends. I am having difficulty connecting to the server and finding out how I would be able to write files to the server.

I have written this method:

- (void) sendDataToServer:(NSString *)url :(NSString *)key : (NSString *) content{

    // define your form fields here:
    //NSString *content = @"field1=42&field2=Hello";


    NSString *address = [NSString stringWithFormat:@"ssh -i %@ %@", key, url];
    NSLog(@"%@", address);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:address]];
    [request setHTTPMethod:@"POST"];
    NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:contentData];
    NSString *postLength = [NSString stringWithFormat:@"%d",[contentData length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    // generates an autoreleased NSURLConnection
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn){
        NSLog(@"connection");
    }
    //[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               //[self doSomethingWithData:data];
                               if (error){
                                   NSLog(@"ERROR");
                               }
                           }];

}

I put the private key from my key pair into the app. How would I use that to connect? Should I not be using my private key? Should I be doing this differently?

Wyetro
  • 8,439
  • 9
  • 46
  • 64

1 Answers1

4

Should I be doing this differently?

Absolutely, 100%, YES. You don't want to let people SSH into your server, especially by embedding your private key into an app binary. It's crazy easy for someone to get it, then wreak havoc upon your server.

Don't do this.

Instead, I would get a web server like Apache running on your instance (it's trivial), and write an application (in PHP, Rails (with Passenger), Python, whatever) that saves files to the server's hard drive. You'll also want to get an Elastic IP address so that it stays constant, as ashack mentioned.

In your iOS app, you'll want to send a POST request to your server. See Sending an HTTP POST request on iOS, it's essentially what you're doing now.

Don't publish your private SSH key. It's private for a good reason.

Community
  • 1
  • 1
Undo
  • 25,519
  • 37
  • 106
  • 129
  • So what would this look like in the Objective-C? – Wyetro Aug 01 '14 at 14:59
  • You'd want to make a POST request, @WyattMufson. Edited it into my answer. You have much of the code already, you just want to drop the SSH part. – Undo Aug 01 '14 at 15:00
  • Also, make sure you have assigned an Elastic IP address to your instance. Don't write your code to try and connect to the Public DNS URL. – ashack Aug 01 '14 at 15:02
  • So I would make the POST request to the Elastic IP address? – Wyetro Aug 01 '14 at 15:03
  • @Undo where would I find the resources to make an application that saves the files to the hard drive? – Wyetro Aug 01 '14 at 15:29
  • It depends, what language are you planning to use to make the server-side app @WyattMufson? – Undo Aug 01 '14 at 15:30
  • I was thinking on using Python/django – Wyetro Aug 01 '14 at 15:30
  • [This question](http://stackoverflow.com/questions/2491141/how-to-read-and-write-a-file-using-python) should help, @Wyatt. – Undo Aug 01 '14 at 15:31