如何连接到EC2实例中的iOS应用程序 [英] How to connect to EC2 instance within an iOS App

查看:170
本文介绍了如何连接到EC2实例中的iOS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AWS EC2实例和我创建了一个应用程序。该应用程序是谁的人得到偏头痛(曲目信息,告诉他们什么他们的触发器(S)是)。现在,我希望能够从我的应用程序发送用户输入到服务器,这样我可以看到的趋势。我有困难连接到服务器,并找出如何我能写文件到服务器。

我写了这个方法:

   - (无效)sendDataToServer:(的NSString *)网址:(的NSString *)键:(的NSString *)内容{

    //这里定义表单字段:
    //的NSString *内容= @字段1 = 42安培;场2 =你好;


    的NSString *地址= [NSString的stringWithFormat:@的ssh -i%@%@键,网址]
    的NSLog(@%@,地址);
    NSMutableURLRequest *请求= [[NSMutableURLRequest页头] initWithURL:[NSURL URLWithString:地址]];
    [请求setHTTPMethod:@POST];
    的NSData * contentData内容= [内容dataUsingEncoding:NSUTF8StringEncoding]。
    [请求setHTTPBody:contentData内容]
    的NSString * postLength = [NSString的stringWithFormat:@%D,[contentData内容长度];
    [请求的setValue:postLength forHTTPHeaderField:@内容长度];

    //生成一个自动释放NSURLConnection的
    NSURLConnection的*康恩= [[NSURLConnection的页头] initWithRequest:要求委托:个体经营];
    如果(康涅狄格州){
        的NSLog(@连接);
    }
    // [NSURLConnection的sendSynchronousRequest:请求returningResponse:无错误:无]
    [NSURLConnection的sendAsynchronousRequest:要求
                                       队列:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *响应的NSData *数据,NSError *错误){
                               // [自doSomethingWithData:数据]
                               如果(错误){
                                   的NSLog(@错误);
                               }
                           }];

}
 

我把私钥从我的密钥对到应用程序。我将如何用它来连接?如果我不使用我的私钥?我应该不同要这样做?

解决方案
  

我应该不同要这样做?

当然,100%,YES。你不想让别人SSH到你的服务器,尤其的嵌入你的私钥到一个应用程序二进制文件。这很疯狂很容易的人得到它,然后发泄在你的服务器的严重破坏。

别这样。

而不是,我会得到一个Web服务器如阿帕奇您的实例运行(这是微不足道的),并且编写一个应用程序(在PHP中,滑轨(带客),Python的,等等)是将文件保存到服务器的硬盘驱动器。你也想​​获得一个弹性IP地址,使其保持不变,为ashack提及。

在你的iOS应用程序,你会想要一个POST请求发送到服务器。请参见发送在iOS HTTP POST请求,它基本上是你重新做的事情。

不要发布您的SSH私钥。它是私有的一个很好的理由。

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?

解决方案

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.

这篇关于如何连接到EC2实例中的iOS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆