mirror of
https://github.com/haiwen/seafile.git
synced 2025-01-09 04:17:30 +08:00
[client] Proxy support for http sync.
New config options in config.db: - use_proxy: true or false - proxy_type: http or socks - proxy_addr: proxy server address - proxy_port: proxy server port
This commit is contained in:
parent
4700c94fe6
commit
de3d819e37
@ -5,6 +5,8 @@
|
||||
#include <jansson.h>
|
||||
#include <event2/buffer.h>
|
||||
|
||||
#include "seafile-config.h"
|
||||
|
||||
#include "seafile-session.h"
|
||||
#include "http-tx-mgr.h"
|
||||
|
||||
@ -261,6 +263,29 @@ http_tx_manager_start (HttpTxManager *mgr)
|
||||
|
||||
/* Common Utility Functions. */
|
||||
|
||||
static void
|
||||
set_proxy (CURL *curl, gboolean is_https)
|
||||
{
|
||||
if (!seaf->use_http_proxy || !seaf->http_proxy_type || !seaf->http_proxy_addr)
|
||||
return;
|
||||
|
||||
if (g_strcmp0(seaf->http_proxy_type, PROXY_TYPE_HTTP) == 0) {
|
||||
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
|
||||
/* Use CONNECT method create a SSL tunnel if https is used. */
|
||||
if (is_https)
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_PROXY, seaf->http_proxy_addr);
|
||||
curl_easy_setopt(curl, CURLOPT_PROXYPORT,
|
||||
seaf->http_proxy_port > 0 ? seaf->http_proxy_port : 80);
|
||||
} else if (g_strcmp0(seaf->http_proxy_type, PROXY_TYPE_SOCKS) == 0) {
|
||||
if (seaf->http_proxy_port < 0)
|
||||
return;
|
||||
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
||||
curl_easy_setopt(curl, CURLOPT_PROXY, seaf->http_proxy_addr);
|
||||
curl_easy_setopt(curl, CURLOPT_PROXYPORT, seaf->http_proxy_port);
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct _HttpResponse {
|
||||
char *content;
|
||||
size_t size;
|
||||
@ -327,6 +352,9 @@ http_get (CURL *curl, const char *url, const char *token,
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, cb_data);
|
||||
}
|
||||
|
||||
gboolean is_https = (strncasecmp(url, "https", strlen("https")) == 0);
|
||||
set_proxy (curl, is_https);
|
||||
|
||||
int rc = curl_easy_perform (curl);
|
||||
if (rc != 0) {
|
||||
seaf_warning ("libcurl failed to GET %s: %s.\n",
|
||||
@ -436,6 +464,9 @@ http_put (CURL *curl, const char *url, const char *token,
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &rsp);
|
||||
}
|
||||
|
||||
gboolean is_https = (strncasecmp(url, "https", strlen("https")) == 0);
|
||||
set_proxy (curl, is_https);
|
||||
|
||||
int rc = curl_easy_perform (curl);
|
||||
if (rc != 0) {
|
||||
seaf_warning ("libcurl failed to PUT %s: %s.\n",
|
||||
@ -513,6 +544,9 @@ http_post (CURL *curl, const char *url, const char *token,
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &rsp);
|
||||
}
|
||||
|
||||
gboolean is_https = (strncasecmp(url, "https", strlen("https")) == 0);
|
||||
set_proxy (curl, is_https);
|
||||
|
||||
int rc = curl_easy_perform (curl);
|
||||
if (rc != 0) {
|
||||
seaf_warning ("libcurl failed to POST %s: %s.\n",
|
||||
|
@ -120,6 +120,21 @@ seafile_session_config_set_string (SeafileSession *session,
|
||||
session->disable_verify_certificate = FALSE;
|
||||
}
|
||||
|
||||
if (g_strcmp0(key, KEY_USE_PROXY) == 0) {
|
||||
if (g_strcmp0(value, "true") == 0)
|
||||
session->use_http_proxy = TRUE;
|
||||
else
|
||||
session->use_http_proxy = FALSE;
|
||||
}
|
||||
|
||||
if (g_strcmp0(key, KEY_PROXY_TYPE) == 0) {
|
||||
session->http_proxy_type = g_strdup(value);
|
||||
}
|
||||
|
||||
if (g_strcmp0(key, KEY_PROXY_ADDR) == 0) {
|
||||
session->http_proxy_addr = g_strdup(value);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -136,6 +151,10 @@ seafile_session_config_set_int (SeafileSession *session,
|
||||
if (sqlite_query_exec (session->config_db, sql) < 0)
|
||||
return -1;
|
||||
|
||||
if (g_strcmp0(key, KEY_PROXY_PORT) == 0) {
|
||||
session->http_proxy_port = value;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -17,9 +17,19 @@
|
||||
#define KEY_ALLOW_INVALID_WORKTREE "allow_invalid_worktree"
|
||||
#define KEY_ALLOW_REPO_NOT_FOUND_ON_SERVER "allow_repo_not_found_on_server"
|
||||
#define KEY_SYNC_EXTRA_TEMP_FILE "sync_extra_temp_file"
|
||||
|
||||
/* Http sync settings. */
|
||||
#define KEY_ENABLE_HTTP_SYNC "enable_http_sync"
|
||||
#define KEY_DISABLE_VERIFY_CERTIFICATE "disable_verify_certificate"
|
||||
|
||||
/* Http sync proxy settings. */
|
||||
#define KEY_USE_PROXY "use_proxy"
|
||||
#define KEY_PROXY_TYPE "proxy_type"
|
||||
#define KEY_PROXY_ADDR "proxy_addr"
|
||||
#define KEY_PROXY_PORT "proxy_port"
|
||||
#define PROXY_TYPE_HTTP "http"
|
||||
#define PROXY_TYPE_SOCKS "socks"
|
||||
|
||||
gboolean
|
||||
seafile_session_config_exists (SeafileSession *session, const char *key);
|
||||
|
||||
|
@ -220,6 +220,15 @@ seafile_session_prepare (SeafileSession *session)
|
||||
session->disable_verify_certificate = seafile_session_config_get_bool
|
||||
(session, KEY_DISABLE_VERIFY_CERTIFICATE);
|
||||
|
||||
session->use_http_proxy = seafile_session_config_get_bool
|
||||
(session, KEY_USE_PROXY);
|
||||
session->http_proxy_type = seafile_session_config_get_string
|
||||
(session, KEY_PROXY_TYPE);
|
||||
session->http_proxy_addr = seafile_session_config_get_string
|
||||
(session, KEY_PROXY_ADDR);
|
||||
session->http_proxy_port = seafile_session_config_get_int
|
||||
(session, KEY_PROXY_PORT, NULL);
|
||||
|
||||
/* Start mq manager earlier, so that we can send notifications
|
||||
* when start repo manager. */
|
||||
seaf_mq_manager_init (session->mq_mgr);
|
||||
|
@ -75,6 +75,11 @@ struct _SeafileSession {
|
||||
gboolean sync_extra_temp_file;
|
||||
gboolean enable_http_sync;
|
||||
gboolean disable_verify_certificate;
|
||||
|
||||
gboolean use_http_proxy;
|
||||
char *http_proxy_type;
|
||||
char *http_proxy_addr;
|
||||
int http_proxy_port;
|
||||
};
|
||||
|
||||
struct _SeafileSessionClass
|
||||
|
Loading…
Reference in New Issue
Block a user