Skip to content

Commit e67378f

Browse files
committed
Add option to specify proxy connection
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 <bjorn.bidar@thaodan.de>
1 parent 248c8a4 commit e67378f

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

librocketchat.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ typedef struct {
263263

264264
gchar *username;
265265
gchar *server;
266+
gchar *websocket_server;
266267
gchar *path;
267268
gboolean tls;
268269
gchar *http_str;
@@ -832,6 +833,9 @@ rc_fetch_url(RocketChatAccount *ya, const gchar *url, const gchar *postdata, Roc
832833
#if PURPLE_VERSION_CHECK(3, 0, 0)
833834

834835
PurpleHttpRequest *request = purple_http_request_new(url);
836+
if (ya->websocket_server != ya->server) {
837+
purple_http_request_header_set(request, "Host", g_strdup(ya->websocket_server));
838+
}
835839
purple_http_request_header_set(request, "Accept", "*/*");
836840
purple_http_request_header_set(request, "User-Agent", ROCKETCHAT_USERAGENT);
837841
purple_http_request_header_set(request, "Cookie", cookies);
@@ -864,9 +868,15 @@ rc_fetch_url(RocketChatAccount *ya, const gchar *url, const gchar *postdata, Roc
864868
gchar *host = NULL, *path = NULL, *user = NULL, *password = NULL;
865869
int port;
866870
purple_url_parse(url, &host, &port, &path, &user, &password);
867-
871+
868872
headers = g_string_new(NULL);
869-
873+
874+
if (ya->websocket_server != ya->server) {
875+
host = g_strdup(ya->websocket_server);
876+
purple_debug_misc("rocketchat" , "Proxy enabled, sending %s instead of %s\n",
877+
host, ya->server);
878+
}
879+
870880
//Use the full 'url' until libpurple can handle path's longer than 256 chars
871881
g_string_append_printf(headers, "%s /%s HTTP/1.0\r\n", (postdata ? "POST" : "GET"), path);
872882
//g_string_append_printf(headers, "%s %s HTTP/1.0\r\n", (postdata ? "POST" : "GET"), url);
@@ -2126,6 +2136,7 @@ rc_login(PurpleAccount *account)
21262136
gchar *url;
21272137
PurpleConnectionFlags pc_flags;
21282138
const char *connection_security;
2139+
char *proxy;
21292140

21302141
pc_flags = purple_connection_get_flags(pc);
21312142
pc_flags |= PURPLE_CONNECTION_FLAG_HTML;
@@ -2182,6 +2193,14 @@ rc_login(PurpleAccount *account)
21822193
ya->http_str = g_strdup("https://");
21832194
}
21842195

2196+
ya->websocket_server = g_strdup(ya->server);
2197+
purple_debug_info("rocketchat", "websocket_server: %s\n", ya->websocket_server);
2198+
2199+
proxy = g_strdup(purple_account_get_string(account, "proxy", NULL));
2200+
if (proxy && !purple_strequal(proxy, "")) {
2201+
ya->server = proxy;
2202+
}
2203+
21852204
ya->session_token = g_strdup(purple_account_get_string(account, "personal_access_token", NULL));
21862205
if (ya->session_token && *ya->session_token) {
21872206
const gchar *user_id = purple_account_get_string(account, "personal_access_token_user_id", NULL);
@@ -2290,6 +2309,7 @@ rc_close(PurpleConnection *pc)
22902309
g_free(ya->username); ya->username = NULL;
22912310
g_free(ya->server); ya->server = NULL;
22922311
g_free(ya->http_str); ya->http_str = NULL;
2312+
g_free(ya->websocket_server); ya->websocket_server = NULL;
22932313
g_free(ya->path); ya->path = NULL;
22942314
g_free(ya->frame); ya->frame = NULL;
22952315
g_free(ya->session_token); ya->session_token = NULL;
@@ -2665,7 +2685,7 @@ rc_socket_upgrade(RocketChatAccount *ya)
26652685

26662686
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));
26672687
cookies = rc_cookies_to_string(ya);
2668-
2688+
26692689
websocket_header = g_strdup_printf("GET %s HTTP/1.1\r\n"
26702690
"Host: %s\r\n"
26712691
"Connection: Upgrade\r\n"
@@ -2677,8 +2697,11 @@ rc_socket_upgrade(RocketChatAccount *ya)
26772697
"User-Agent: " ROCKETCHAT_USERAGENT "\r\n"
26782698
"Cookie: %s\r\n"
26792699
//"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
2680-
"\r\n", url->str, ya->server,
2700+
"\r\n", url->str, ya->websocket_server,
26812701
websocket_key, cookies);
2702+
2703+
purple_debug_misc("rocketchat", "websocket_header: %s",
2704+
websocket_header);
26822705
rc_sock_write(ya, websocket_header, strlen(websocket_header));
26832706

26842707
g_free(websocket_header);
@@ -3790,6 +3813,9 @@ rc_add_account_options(GList *account_options)
37903813
"connection_security", encryption_values);
37913814
account_options = g_list_append(account_options, option);
37923815

3816+
option = purple_account_option_string_new(N_("Proxy"), "proxy", "");
3817+
account_options = g_list_append(account_options, option);
3818+
37933819
return account_options;
37943820
}
37953821

0 commit comments

Comments
 (0)