websocket.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Sapphire backend
  3. *
  4. * Copyright (C) 2018 Alyssa Rosenzweig
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <purple.h>
  23. #include <gio/gio.h>
  24. #include <gio/gunixsocketaddress.h>
  25. #include <glib/gstdio.h>
  26. #include "core.h"
  27. #include "websocket.h"
  28. #include "push.h"
  29. #define WS_PORT 7070
  30. /* This module is responsible for networking via UNIX sockets, translated to
  31. * WebSockets at the proxy level. */
  32. /* List of all authenticated connections. These connections will be broadcasted
  33. * to by broadcast_raw_packet. */
  34. GSList *authenticated_connections = NULL;
  35. /* Returns TRUE if there are connected clients (such that we don't need push
  36. * notifications) */
  37. gboolean
  38. sapphire_any_connected_clients(void){
  39. return authenticated_connections != NULL;
  40. }
  41. void
  42. sapphire_send_raw_packet(Connection *conn, const char *frame)
  43. {
  44. if (!g_socket_connection_is_connected(conn->connection)) {
  45. printf("Tried to send %s to closed connection, ignoring\n", frame);
  46. return;
  47. }
  48. GError *gerror = NULL;
  49. GOutputStream *ostream = g_io_stream_get_output_stream(G_IO_STREAM(conn->connection));
  50. g_output_stream_write_all(ostream, frame, strlen(frame), NULL, NULL, &gerror);
  51. if (gerror) {
  52. printf("Ahh gerror %s!\n", gerror->message);
  53. gerror = NULL;
  54. return;
  55. }
  56. char end = '\n';
  57. g_output_stream_write(ostream, &end, 1, NULL, &gerror);
  58. if (gerror) {
  59. printf("Ahhh gerror v2!\n");
  60. }
  61. }
  62. /* Sends the packet to any connection, or save it for the next connection */
  63. GSList *queued_messages = NULL;
  64. void sapphire_send_any_or_save(char *packet)
  65. {
  66. if (authenticated_connections) {
  67. Connection *first_conn = (Connection *) authenticated_connections->data;
  68. sapphire_send_raw_packet(first_conn, packet);
  69. g_free(packet);
  70. } else {
  71. queued_messages = g_slist_prepend(queued_messages, packet);
  72. }
  73. }
  74. /* Dequeue saved, send and free */
  75. static void sapphire_dequeue_saved_messages(Connection *conn)
  76. {
  77. for (GSList *it = queued_messages; it != NULL; it = it->next) {
  78. gchar *msg = (gchar *) it->data;
  79. sapphire_send_raw_packet(conn, msg);
  80. g_free(msg);
  81. }
  82. g_slist_free(queued_messages);
  83. queued_messages = NULL;
  84. }
  85. /* Broadcast a packet to all currently connected clients. */
  86. void
  87. sapphire_broadcast_raw_packet(const char *packet)
  88. {
  89. /* Iterate the connection list and send message everywhere */
  90. for (GSList *l = authenticated_connections; l != NULL; l = l->next) {
  91. Connection *connection = (Connection *) l->data;
  92. sapphire_send_raw_packet(connection, packet);
  93. }
  94. }
  95. /* XXX copypasted from proxy.c */
  96. static void sapphire_got_line(GObject *source_object, GAsyncResult *res, gpointer user_data);
  97. static void
  98. sapphire_read_line(Connection *conn)
  99. {
  100. g_data_input_stream_read_line_async(conn->distream, G_PRIORITY_DEFAULT, NULL, sapphire_got_line, conn);
  101. }
  102. static void
  103. sapphire_got_line(GObject *source_object,
  104. GAsyncResult *res,
  105. gpointer user_data)
  106. {
  107. Connection *conn = (Connection *) user_data;
  108. GError *err = NULL;
  109. gsize len;
  110. char *data = g_data_input_stream_read_line_finish_utf8(G_DATA_INPUT_STREAM(source_object), res, &len, &err);
  111. if (err || !data) {
  112. /* Borp, error -- disconnect */
  113. /* Free the connection */
  114. g_hash_table_remove_all(conn->subscribed_ids);
  115. /* Splice the socket out of the authenticated list, so we no longer
  116. * attempt to broadcast to it */
  117. authenticated_connections = g_slist_remove(authenticated_connections, conn);
  118. g_free(conn);
  119. /* Disconnect accounts if needed */
  120. sapphire_enable_accounts_by_connections();
  121. return;
  122. }
  123. /* The message should be interpreted as JSON, decode that here */
  124. JsonParser *parser = json_parser_new();
  125. if (!json_parser_load_from_data(parser, data, -1, NULL)) {
  126. fprintf(stderr, "Error parsing response: %s\n", data);
  127. fprintf(stderr, "^ Couldn't do it\n");
  128. goto refresh;
  129. }
  130. JsonNode *root = json_parser_get_root(parser);
  131. if (root == NULL) {
  132. printf("NULL root, ignoring\n");
  133. goto refresh;
  134. }
  135. /* Delegate off */
  136. JsonObject *obj = json_node_get_object(root);
  137. sapphire_process_message(conn, obj);
  138. refresh:
  139. g_free(data);
  140. sapphire_read_line(conn);
  141. }
  142. gboolean
  143. incoming_callback (GSocketService *service,
  144. GSocketConnection *connection,
  145. GObject *source_object,
  146. gpointer user_data)
  147. {
  148. g_print("Received Connection from client!\n");
  149. /* Allocate a connection object for us and fill it in */
  150. Connection *conn = g_new0(Connection, 1);
  151. conn->is_authenticated = FALSE;
  152. /* Save the connection.
  153. * IMPORTANT: Reference counting is necessary to keep the connection
  154. * alive. No idea why this isn't documented anywhere, xxx
  155. */
  156. conn->connection = g_object_ref(connection);
  157. /* Subscribe to incoming data */
  158. GInputStream *istream = g_io_stream_get_input_stream (G_IO_STREAM (conn->connection));
  159. conn->distream = g_data_input_stream_new(istream);
  160. sapphire_read_line(conn);
  161. /* Consider ourselves authenticated */
  162. conn->is_authenticated = TRUE;
  163. authenticated_connections = g_slist_append(authenticated_connections, conn);
  164. /* Dequeue messsages */
  165. sapphire_dequeue_saved_messages(conn);
  166. /* Flag the new connection in the push notification module */
  167. sapphire_push_connected();
  168. /* Setup accounts if needed */
  169. sapphire_enable_accounts_by_connections();
  170. /* Now that we're authenticated, it's necessary to bring the client up-to-date on what's popping */
  171. sapphire_send_world(conn);
  172. return FALSE;
  173. }
  174. void
  175. sapphire_init_websocket(void)
  176. {
  177. const gchar *filename = "./sockpuppet";
  178. g_unlink(filename);
  179. GSocketService *service = g_socket_service_new();
  180. GSocketAddress *addr = g_unix_socket_address_new (filename);
  181. g_socket_listener_add_address(G_SOCKET_LISTENER(service), addr, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL, NULL, NULL);
  182. g_signal_connect (service, "incoming", G_CALLBACK (incoming_callback), NULL);
  183. }