Skip to content

Commit

Permalink
Add option to specify proxy connection
Browse files Browse the repository at this point in the history
The option allows the use of a proxy server that is used instead of
the real server. Since the real server is still needed inside `rc_socket_upgrade()`
we still have to keep it for that.

Signed-off-by: Björn Bidar <[email protected]>
  • Loading branch information
Thaodan committed Sep 27, 2022
1 parent 248c8a4 commit e67378f
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions librocketchat.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ typedef struct {

gchar *username;
gchar *server;
gchar *websocket_server;
gchar *path;
gboolean tls;
gchar *http_str;
Expand Down Expand Up @@ -832,6 +833,9 @@ rc_fetch_url(RocketChatAccount *ya, const gchar *url, const gchar *postdata, Roc
#if PURPLE_VERSION_CHECK(3, 0, 0)

PurpleHttpRequest *request = purple_http_request_new(url);
if (ya->websocket_server != ya->server) {
purple_http_request_header_set(request, "Host", g_strdup(ya->websocket_server));
}
purple_http_request_header_set(request, "Accept", "*/*");
purple_http_request_header_set(request, "User-Agent", ROCKETCHAT_USERAGENT);
purple_http_request_header_set(request, "Cookie", cookies);
Expand Down Expand Up @@ -864,9 +868,15 @@ rc_fetch_url(RocketChatAccount *ya, const gchar *url, const gchar *postdata, Roc
gchar *host = NULL, *path = NULL, *user = NULL, *password = NULL;
int port;
purple_url_parse(url, &host, &port, &path, &user, &password);

headers = g_string_new(NULL);


if (ya->websocket_server != ya->server) {
host = g_strdup(ya->websocket_server);
purple_debug_misc("rocketchat" , "Proxy enabled, sending %s instead of %s\n",
host, ya->server);
}

//Use the full 'url' until libpurple can handle path's longer than 256 chars
g_string_append_printf(headers, "%s /%s HTTP/1.0\r\n", (postdata ? "POST" : "GET"), path);
//g_string_append_printf(headers, "%s %s HTTP/1.0\r\n", (postdata ? "POST" : "GET"), url);
Expand Down Expand Up @@ -2126,6 +2136,7 @@ rc_login(PurpleAccount *account)
gchar *url;
PurpleConnectionFlags pc_flags;
const char *connection_security;
char *proxy;

pc_flags = purple_connection_get_flags(pc);
pc_flags |= PURPLE_CONNECTION_FLAG_HTML;
Expand Down Expand Up @@ -2182,6 +2193,14 @@ rc_login(PurpleAccount *account)
ya->http_str = g_strdup("https://");
}

ya->websocket_server = g_strdup(ya->server);
purple_debug_info("rocketchat", "websocket_server: %s\n", ya->websocket_server);

proxy = g_strdup(purple_account_get_string(account, "proxy", NULL));
if (proxy && !purple_strequal(proxy, "")) {
ya->server = proxy;
}

ya->session_token = g_strdup(purple_account_get_string(account, "personal_access_token", NULL));
if (ya->session_token && *ya->session_token) {
const gchar *user_id = purple_account_get_string(account, "personal_access_token_user_id", NULL);
Expand Down Expand Up @@ -2290,6 +2309,7 @@ rc_close(PurpleConnection *pc)
g_free(ya->username); ya->username = NULL;
g_free(ya->server); ya->server = NULL;
g_free(ya->http_str); ya->http_str = NULL;
g_free(ya->websocket_server); ya->websocket_server = NULL;
g_free(ya->path); ya->path = NULL;
g_free(ya->frame); ya->frame = NULL;
g_free(ya->session_token); ya->session_token = NULL;
Expand Down Expand Up @@ -2665,7 +2685,7 @@ rc_socket_upgrade(RocketChatAccount *ya)

g_string_append_printf(url, "%s/sockjs/%d/pidgin%d/websocket", ya->path, g_random_int_range(100, 999), g_random_int_range(1, 100));
cookies = rc_cookies_to_string(ya);

websocket_header = g_strdup_printf("GET %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Connection: Upgrade\r\n"
Expand All @@ -2677,8 +2697,11 @@ rc_socket_upgrade(RocketChatAccount *ya)
"User-Agent: " ROCKETCHAT_USERAGENT "\r\n"
"Cookie: %s\r\n"
//"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
"\r\n", url->str, ya->server,
"\r\n", url->str, ya->websocket_server,
websocket_key, cookies);

purple_debug_misc("rocketchat", "websocket_header: %s",
websocket_header);
rc_sock_write(ya, websocket_header, strlen(websocket_header));

g_free(websocket_header);
Expand Down Expand Up @@ -3790,6 +3813,9 @@ rc_add_account_options(GList *account_options)
"connection_security", encryption_values);
account_options = g_list_append(account_options, option);

option = purple_account_option_string_new(N_("Proxy"), "proxy", "");
account_options = g_list_append(account_options, option);

return account_options;
}

Expand Down

0 comments on commit e67378f

Please sign in to comment.