static int check_contents(request_rec *req)
{
    llzr_config *conf = ap_get_module_config (c->base_server->module_config,  &llzr_module);
    GTable *post_data = get_post_data(req);
    redis = conf->redisconn;
    reply = conf->redisreply;

    /*
        Things of interest:

        request_rec->uri
        request_rec->useragent_ip
        request_rec->request_time
        request_rec->method
    */

    char *request_remote_ip         = recq->useragent_ip;
    apr_time_t request_timestamp    = recq->request_time;
    char *request_uri               = recq->uri;
    char *request_method            = recq->method;

   /*

        @TODO: look through post request for username and password

        if they are included, create a token id, and insert into redis database
        send back a 402 payment required or 307 to /login/token
        ask the user to authorize login...
        on resubmission ony the token is passed and if exists, login is sucessful
        if not, the entry is logged and evaluated

   */

    /*
    ...
     */
    if (post_data) {

        /* ... hash the password to be secure in memory even.. */
        size_t length = sizeof(post_data['USERNAME_FIELD']);
        unsigned char hash[SHA_DIGEST_LENGTH];
        SHA1(post_data['USERNAME_FIELD'], length, hash);

        reply = redisCommand(redis, "HMSET %s %s %s", request_remote_ip, username, hash);

    }
    /*
    ...
     */

    redisFreeReply(reply);
    redisFree(redis);

    return OK;
}
示例#2
0
int proxy_server()
{
    int rx_bytes = 0, post_data_size = 0;
    int lsock = 0, csock = 0, client_data_size = 0;
    int result_size = 0, err_code = 0, id = 0;
    int addrlen = sizeof(struct sockaddr_in);
    struct sockaddr_in clientaddr;
    char *buffer = NULL, *post_data = NULL;
    char *host = NULL, *url = NULL;
    char *query = NULL, *fifo_file = NULL;
    char *get_data = NULL;
    char *client_data = NULL, *headers = NULL;
    char *server_ip = NULL;

    memset((void *) &clientaddr,0,addrlen);

    server_ip = config_get_server_ip();

    /* Create TCP socket */
    if((lsock = create_socket(server_ip,PROXY_PORT,SOCK_STREAM)) == SOCK_FAIL) {
        glog("Failed to create TCP socket for proxy server",LOG_ERROR_TYPE);
        if(server_ip) free(server_ip);
        return EXIT_FAILURE;
    }
    if(server_ip) free(server_ip);

    /* Infinite receive loop */
    while(1) {

        /* Accept incoming connection */
        if((csock = accept(lsock,(struct sockaddr *) &clientaddr,(socklen_t *) &addrlen)) < 0) {
            glog("Failed to accept TCP connection to proxy server",LOG_ERROR_TYPE);
            if(buffer) free(buffer);
            return EXIT_FAILURE;
        }

        if(!fork()) {
            /* Receive client request */
            if((buffer = receive(lsock,SOCK_STREAM,&rx_bytes,csock,&clientaddr)) == NULL) {
                glog("Failed to read data from client request sent to proxy server",LOG_ERROR_TYPE);
                exit(EXIT_FAILURE);
            }

            if(is_using_proxy(buffer)) {

                /* Get the target's IP address */
                host = get_host_name(buffer);

                /* Get the target URL path */
                url = get_url(buffer);

                /* Get POST data, if any */
                post_data = get_post_data(buffer,rx_bytes,&post_data_size);

                /* Get HTTP headers from request */
                headers = get_headers(buffer);

                /* If the CONSOLE_HOST is requested, then display the Web console interface */
                if(memcmp(host,CONSOLE_HOST,CONSOLE_HOST_SIZE) == 0) {
                    show_web_ui(csock,url);
                    close_socket(csock);
                } else {

                    /* Make sure the requested host is in our clients list */
                    query = sqlite3_mprintf("SELECT id FROM %s WHERE strftime('%%s',callback_time) >= strftime('%%s','now') AND ip = %Q LIMIT 1",CLIENTS_TABLE,host);
                    sql_exec(query,&result_size,&err_code);
                    sqlite3_free(query);

                    if(result_size > 0) {
                        /* Don't allow requests for filtered file extensions */
                        if(!is_url_filtered(url)) {

                            fifo_file = create_fifo(host);
                            if(!fifo_file) {
                                glog("Failed to create fifo file",LOG_ERROR_TYPE);
                            } else {
                                /* Insert query into queue table */
                                query = sqlite3_mprintf("INSERT INTO %s (fifo,host,url,headers,pdata,sent) VALUES (%Q,%Q,%Q,%Q,%Q,0)",QUEUE_TABLE,fifo_file,host,url,headers,post_data);
                                sql_exec(query,&result_size,&err_code);
                                sqlite3_free(query);
                                if(err_code != SQLITE_OK) {
                                    sql_log_error();
                                } else {

                                    /* When the client data has returned, the callback server will write the ID of the callback to the FIFO */
                                    id = read_from_fifo(fifo_file);

                                    /* Extract the data from the DB */
                                    get_data = sqlite3_mprintf("SELECT rdata FROM %s WHERE id = '%d'",QUEUE_TABLE,id);
                                    client_data = sql_exec(get_data,&client_data_size,&err_code);
                                    sqlite3_free(get_data);

                                    if(err_code != SQLITE_OK) {
                                        sql_log_error();
                                    } else {

                                        /* Write data to socket */
                                        if(write(csock,client_data,client_data_size) != client_data_size) {
                                            glog("Proxy socket write failed",LOG_ERROR_TYPE);
                                        }
                                    }
                                    if(client_data) free(client_data);

                                    /* Make sure the fifo gets deleted */
                                    destroy_fifo(fifo_file);
                                }
                            }
                        }
                    }
                }
            }

            /* Exit the child process */
            close_socket(csock);
            if(fifo_file) free(fifo_file);
            if(buffer) free(buffer);
            if(host) free(host);
            if(url) free(url);
            if(post_data) free(post_data);
            if(headers) free(headers);
            exit(EXIT_SUCCESS);
        }
    }

    /* Close up shop */
    close_socket(csock);
    close_socket(lsock);

    return EXIT_FAILURE;
}
示例#3
0
static int handle_request(struct mg_connection *conn)
{
    char * json_data;
    cJSON *root;
    char *json;
    char *title;
    char *description;
    int completed;
    if (!strcmp(conn->request_method, "POST") && !strncmp(conn->uri, "/api/v1",strlen("/api/v1"))) 
    {
        printf("api v1\n");
        json_data = get_post_data(conn);
        if(json_data == NULL)
        {
            printf("Error json data: NULL\n");
            mg_printf_data(conn, "%s","Error jason data:NULL");
            free(json_data);
            return MG_TRUE;   // Tell mongoose to close this connection
        }
        printf("input json data: %s\n", json_data);
        root = cJSON_Parse(json_data);
        if (root == NULL)
        {
            mg_printf_data(conn, "%s","Error jason data:Wrong format");
            free(json_data);
            return MG_TRUE;
        }

        if (!strncmp(conn->uri,"/api/v1/cars", strlen("/api/v1/cars")))
        {   
            int id;
            char *name;
            int price;
            sqlite3 *db;
            char *err_msg = 0; 
            id = cJSON_GetObjectItem(root,"Id")->valueint;
            name = cJSON_GetObjectItem(root,"Name")->valuestring;
            price = cJSON_GetObjectItem(root,"Price")->valueint;
            printf("id = %d,Name = %s, Price=%d",id,name,price);
            int rc = sqlite3_open("quanta.db", &db);
            if (rc != SQLITE_OK) 
            {
            
                fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
                sqlite3_close(db);        
                return MG_TRUE;   // Tell mongoose to close this connection
            }
            char sql[100];
            sprintf(sql,"INSERT INTO Cars VALUES(%d, '%s', %d);",id,name,price);
            rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
            if (rc != SQLITE_OK ) {
        
                fprintf(stderr, "SQL error: %s\n", err_msg);
        
                sqlite3_free(err_msg);        
                sqlite3_close(db);
        
                return MG_TRUE;   // Tell mongoose to close this connection
            } 
            sqlite3_free(err_msg);        
            sqlite3_close(db);
        }
        else
        {
            description = cJSON_GetObjectItem(root,"description")->valuestring; 
            title = cJSON_GetObjectItem(root,"title")->valuestring; 
            completed = cJSON_GetObjectItem(root,"completed")->valueint; 
            printf("title = %s,description = %s, completed = %d\n",title,description,completed);
        }
        json = cJSON_PrintUnformatted(root);
        write_json_result(conn, 0, json);
        free(json_data);
        return MG_TRUE;   // Tell mongoose to close this connection
    }
    else if (!strcmp(conn->request_method, "GET") && !strncmp(conn->uri, "/api/v1",strlen("/api/v1"))) 
    {
        sqlite3 *db;
        char *err_msg = 0; 
        char sql[100];
        int id;
        char response_buff[100];
        printf("get api v1\n");
        if(!strncmp(conn->uri,"/api/v1/cars",strlen("/api/v1/cars")))
        {
            sscanf(conn->uri,"/api/v1/cars/%d/",&id);
            int rc = sqlite3_open("quanta.db", &db);
            if (rc != SQLITE_OK) 
            {
            
                fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
                sqlite3_close(db);        
                return MG_TRUE;   // Tell mongoose to close this connection
            }
            sprintf(sql,"SELECT * FROM Cars WHERE Id = %d",id);
            rc = sqlite3_exec(db,sql, callback, response_buff, &err_msg);
        
            if (rc != SQLITE_OK ) {
                
                fprintf(stderr, "Failed to select data\n");
                fprintf(stderr, "SQL error: %s\n", err_msg);

                sqlite3_free(err_msg);
                sqlite3_close(db);
                
                return MG_TRUE;   // Tell mongoose to close this connection
            } 
            else
            {
                mg_printf_data(conn,"%s",response_buff);
                return MG_TRUE;   // Tell mongoose to close this connection
            }
            sqlite3_close(db);
        }
        else
        {
            mg_printf_data(conn, "%s","Not Support");
            return MG_TRUE;   // Tell mongoose to close this connection
        }
    }
    else if (!strcmp(conn->request_method, "DELETE") && !strncmp(conn->uri, "/api/v1/cars/",strlen("/api/v1/cars/"))) 
    {
        char sql[100];
        int id;
        sqlite3 *db;
        char *err_msg = 0; 
        sscanf(conn->uri,"/api/v1/cars/%d",&id);
        if (id != 0)
        {
            int rc = sqlite3_open("quanta.db", &db);
            if (rc != SQLITE_OK) 
            {
            
                fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
                sqlite3_close(db);        
                return MG_TRUE;   // Tell mongoose to close this connection
            }
            sprintf(sql,"DELETE FROM Cars WHERE ID = %d;",id);
            rc = sqlite3_exec(db,sql, 0, 0, &err_msg);
            if (rc != SQLITE_OK ) {
                
                fprintf(stderr, "Failed to select data\n");
                fprintf(stderr, "SQL error: %s\n", err_msg);

                sqlite3_free(err_msg);
                sqlite3_close(db);
                
                return MG_TRUE;   // Tell mongoose to close this connection
            }
            sqlite3_free(err_msg);        
            sqlite3_close(db);

            mg_printf_data(conn, "Delete %d record from Cars table",id);
            return MG_TRUE;   // Tell mongoose to close this connection
        }
    }
    else if (!strcmp(conn->request_method, "PUT") && !strncmp(conn->uri, "/api/v1/cars/",strlen("/api/v1/cars/"))) 
    {
        char sql[100];
        int id;
        sqlite3 *db;
        char *err_msg = 0; 
        char *json_data;
        sscanf(conn->uri,"/api/v1/cars/%d",&id);
        if (id != 0)
        {
            json_data = get_post_data(conn);
            if(json_data == NULL)
            {
                printf("Error json data: NULL\n");
                mg_printf_data(conn, "%s","Error jason data:NULL");
                free(json_data);
                return MG_TRUE;   // Tell mongoose to close this connection
            }
            printf("input json data: %s\n", json_data);
            root = cJSON_Parse(json_data);
            if (root == NULL)
            {
                mg_printf_data(conn, "%s","Error jason data:Wrong format");
                free(json_data);
                return MG_TRUE;
            }
            char *name;
            int price;
            name = cJSON_GetObjectItem(root,"Name")->valuestring;
            price = cJSON_GetObjectItem(root,"Price")->valueint;
            printf("id = %d,Name = %s, Price=%d",id,name,price);
            int rc = sqlite3_open("quanta.db", &db);
            if (rc != SQLITE_OK) 
            {
            
                fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
                sqlite3_close(db);        
                return MG_TRUE;   // Tell mongoose to close this connection
            }
            char sql[100];
            sprintf(sql,"UPDATE Cars SET Name = '%s', Price = %d WHERE ID = %d;",name,price,id);
            rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
            if (rc != SQLITE_OK ) {
        
                fprintf(stderr, "SQL error: %s\n", err_msg);
        
                sqlite3_free(err_msg);        
                sqlite3_close(db);
        
                return MG_TRUE;   // Tell mongoose to close this connection
            } 
            sqlite3_free(err_msg);        
            sqlite3_close(db);

            mg_printf_data(conn, "UPDATE Cars table %d record",id);
            free(json_data);
            return MG_TRUE;   // Tell mongoose to close this connection
        }
    }
    else 
    {
        printf("else %d\n",__LINE__);
        mg_printf_data(conn, "%s","Not Support");
        return MG_TRUE;   // Tell mongoose to close this connection
    }
    return MG_TRUE;   // Tell mongoose to close this connection
}