teleport-server.c 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* teleport-server.c
 *
 * Copyright 2017 Julian Sparber <julian@sparber.com>
 *
 * Teleport is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

19 20 21
#include <errno.h>

#include <libsoup/soup.h>
22
#include <glib.h>
23 24
#include <glib/gstdio.h>

25
#include "teleport-server.h"
26
#include "teleport-get.h"
27
#include "teleport-app.h"
28 29

static int port;
30 31
static SoupServer *glob_server;
//static const char *tls_cert_file, *tls_key_file;
32

33
static void
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
do_get (SoupServer *server, SoupMessage *msg, const char *path)
{
  GStatBuf st;

  if (g_stat (path, &st) == -1) {
    if (errno == EPERM)
      soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
    else if (errno == ENOENT)
      soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
    else
      soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }

  if (msg->method == SOUP_METHOD_GET) {
    GMappedFile *mapping;
    SoupBuffer *buffer;

    mapping = g_mapped_file_new (path, FALSE, NULL);
    if (!mapping) {
      soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
      return;
    }

    buffer = soup_buffer_new_with_owner (g_mapped_file_get_contents (mapping),
59 60
                                         g_mapped_file_get_length (mapping),
                                         mapping, (GDestroyNotify)g_mapped_file_unref);
61 62 63 64 65 66 67 68 69 70 71
    soup_message_body_append_buffer (msg->response_body, buffer);
    soup_buffer_free (buffer);
  } else /* msg->method == SOUP_METHOD_HEAD */ {
    char *length;

    /* We could just use the same code for both GET and
     * HEAD (soup-message-server-io.c will fix things up).
     * But we'll optimize and avoid the extra I/O.
     */
    length = g_strdup_printf ("%lu", (gulong)st.st_size);
    soup_message_headers_append (msg->response_headers,
72
                                 "Content-Length", length);
73 74 75 76 77 78
    g_free (length);
  }

  soup_message_set_status (msg, SOUP_STATUS_OK);
}

79 80 81 82 83
static void 
handle_incoming_file(const char *hash,
                     const char *filename,
                     const int size,
                     const char *origin) {
84 85 86
  GVariantBuilder *builder;
  GVariant *value;

87
  g_print("Got a new file form %s with size:%d with title: %s\n", origin, size, filename);
88 89 90 91 92 93 94 95
  builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
  g_variant_builder_add (builder, "s", origin);
  g_variant_builder_add (builder,
                         "s",
                         g_strdup_printf("http://%s:%d/transfer/%s",
                                         origin,
                                         port,
                                         hash)),
96
                        g_variant_builder_add (builder, "s", filename);
97 98 99 100 101 102
  value = g_variant_new ("as", builder);
  g_variant_builder_unref (builder);

  // create_user_notification startes the download
  // if the user wants to save the file
  create_user_notification(filename, size, origin, value);
103
}
104

105
static void
106
server_callback (SoupServer *server, SoupMessage *msg,
107 108
                 const char *path, GHashTable *query,
                 SoupClientContext *context, gpointer data)
109 110 111 112
{
  char *file_path;
  SoupMessageHeadersIter iter;
  GString * response;
113 114 115
  const char *name, *value, *token, *size, *file_name, *origin_addr;

  origin_addr = soup_client_context_get_host (context);
116 117

  g_print ("%s %s HTTP/1.%d\n", msg->method, path,
118
           soup_message_get_http_version (msg));
119 120 121 122 123 124 125
  soup_message_headers_iter_init (&iter, msg->request_headers);
  while (soup_message_headers_iter_next (&iter, &name, &value))
    g_print ("%s: %s\n", name, value);
  if (msg->request_body->length)
    g_print ("%s\n", msg->request_body->data);

  if (data != NULL) {
126
    g_print("File to share %s\n", (char *)data);
127 128 129 130 131 132 133 134 135 136 137 138 139
    file_path = g_strdup(data);
    response = g_string_new("{\"error\": false, \"message\": \"Success\"}");
  }
  else {
    file_path = NULL;
    if (query != NULL) {
      token = g_hash_table_lookup (query, "token");
      size = g_hash_table_lookup (query, "size");
      file_name = g_hash_table_lookup (query, "name");

      if (token != NULL && size != NULL && file_name != NULL) {
        g_print("Token: %s, Size: %s, Name: %s\n", token, size, file_name);
        response = g_string_new("{\"error\": false, \"message\": \"Success\"}");
140
        handle_incoming_file(token, file_name, g_ascii_strtoull (size, NULL, 0), origin_addr);
141 142 143 144 145
      }
      else 
        response = g_string_new("{\"error\": true, \"message\": \"query malformed\"}");
    }
    else {
146
      g_print("No query passed");
147 148 149 150 151 152 153 154 155 156 157 158
      response = g_string_new("{\"error\": true, \"message\": \"No query passed\"}");
    }
  }

  if (g_strcmp0(path, "/") == 0) {
  }

  if (msg->method == SOUP_METHOD_GET || msg->method == SOUP_METHOD_HEAD) {
    if (file_path != NULL)
      do_get (server, msg, file_path);
    else {
      soup_message_set_response (msg, "application/json",
159 160
                                 SOUP_MEMORY_TAKE,
                                 response->str, response->len);
161
      soup_message_set_status (msg, SOUP_STATUS_OK);
162
      g_print("Handle response\n");
163 164 165 166 167 168 169 170 171 172 173 174
    }
  }
  else
    soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);

  if (file_path != NULL)
    g_free (file_path);
  if (response != NULL)
    g_string_free (response, FALSE);
  g_print ("  -> %d %s\n\n", msg->status_code, msg->reason_phrase);
}

175 176 177 178
static void
remove_server_route (SoupServer *server, const gchar *path)
{
  soup_server_remove_handler (server, path);
179
  g_print ("Route %s has expired, removing it\n", path);
180 181 182 183 184 185 186 187 188 189 190
}

static gboolean
do_server_timeout (gpointer user_data)
{
  gchar *path = user_data;
  remove_server_route(glob_server, path);
  g_free(path);
  return FALSE;
}

191 192 193 194
int
teleport_server_add_route (gchar *name,
                           gchar *file_to_send,
                           gchar *destination) {
195 196
  GFile *file;
  GFileInfo *fileInfo;
197 198 199 200
  gchar *path;

  path = g_strdup_printf("/transfer/%s", name);
  soup_server_add_handler (glob_server, path,
201
                           server_callback, g_strdup(file_to_send), NULL);
202 203 204

  /* send notification of available file to the client */
  /* getting file size */
205
  file = g_file_new_for_path(file_to_send);
206 207 208 209 210 211
  fileInfo = g_file_query_info(file,
                               "standard::display-name,standard::size",
                               G_FILE_QUERY_INFO_NONE,
                               NULL,
                               NULL);

212 213 214 215 216 217
  teleport_get_do_client_notify(g_strdup_printf("http://%s:%d/?token=%s&size=%jd&name=%s\n",
                                                destination,
                                                port,
                                                name,
                                                g_file_info_get_size(fileInfo),
                                                g_file_info_get_display_name(fileInfo)));
218 219

  /* Add timeout of 2 min which removes the route again */
220
  g_timeout_add_seconds (2 * 60, do_server_timeout, path);
221

222 223
  g_object_unref(fileInfo);
  g_object_unref(file);
224 225
  return 0;
}
226

227 228
int
teleport_server_run (void) {
229 230
  GSList *uris, *u;
  char *str;
231
  //GTlsCertificate *cert;
232 233 234
  GError *error = NULL;

  port = 3000;
235
  glob_server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "teleport-httpd ",
236
                                 NULL);
237
  soup_server_listen_all (glob_server, port, 0, &error);
238

239
  soup_server_add_handler (glob_server, NULL,
240
                           server_callback, NULL, NULL);
241

242
  uris = soup_server_get_uris (glob_server);
243 244 245 246 247 248 249 250 251 252
  for (u = uris; u; u = u->next) {
    str = soup_uri_to_string (u->data, FALSE);
    g_print ("Listening on %s\n", str);
    g_free (str);
    soup_uri_free (u->data);
  }
  g_slist_free (uris);

  return 0;
}