Skip to content

Commit

Permalink
listen Privoxy host 0.0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Jun 7, 2022
1 parent 27e08f4 commit 8658473
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/run_ssr_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#include "run_ssr_client.h"
#include "proxy_settings.h"
#include "server_connectivity.h"
#include "utf8_to_wchar.h"

#define PRIVOXY_CONFIG_CONTENT_FMT \
"listen-address 127.0.0.1:%d\r\n" \
"listen-address %s:%d\r\n" \
"forward-socks5 / 127.0.0.1:%d .\r\n"

static DWORD WINAPI SsrClientThread(LPVOID lpParam);
Expand All @@ -26,6 +27,7 @@ struct ssr_client_ctx {
struct ssr_client_state* state;
int delay_ms;
uint16_t real_listen_port;
char privoxy_listen_host[MAX_PATH];
uint16_t privoxy_listen_port;
};

Expand All @@ -35,7 +37,7 @@ const char* ssr_client_error_string(void) {
return error_info;
}

struct ssr_client_ctx* ssr_client_begin_run(struct server_config* config, const char* ssr_listen_host, int ssr_listen_port, int proxy_listen_port, int delay_quit_ms, int change_inet_opts)
struct ssr_client_ctx* ssr_client_begin_run(struct server_config* config, const char* ssr_listen_host, int ssr_listen_port, const char* proxy_listen_host, int proxy_listen_port, int delay_quit_ms, int change_inet_opts)
{
struct ssr_client_ctx* ctx = NULL;
DWORD threadId = 0;
Expand Down Expand Up @@ -74,6 +76,7 @@ struct ssr_client_ctx* ssr_client_begin_run(struct server_config* config, const
ctx->config->listen_port = (unsigned short)ssr_listen_port;
string_safe_assign(&ctx->config->listen_host, ssr_listen_host);

strcpy(ctx->privoxy_listen_host, proxy_listen_host);
ctx->privoxy_listen_port = (uint16_t)proxy_listen_port;
ctx->delay_ms = (delay_quit_ms < SSR_DELAY_QUIT_MIN) ? SSR_DELAY_QUIT_MIN : delay_quit_ms;

Expand All @@ -86,7 +89,9 @@ struct ssr_client_ctx* ssr_client_begin_run(struct server_config* config, const
if (error_code == 0) {
ctx->hPrivoxySvr = CreateThread(NULL, 0, PrivoxyThread, ctx, 0, &threadId);
if (change_inet_opts != 0) {
enable_system_proxy(PRIVOXY_LISTEN_ADDR, ctx->privoxy_listen_port);
wchar_t*p = utf8_to_wchar_string(ctx->privoxy_listen_host, &malloc);
enable_system_proxy(p, ctx->privoxy_listen_port);
free(p);
} else {
disable_system_proxy();
}
Expand Down Expand Up @@ -163,7 +168,7 @@ static DWORD WINAPI PrivoxyThread(LPVOID lpParam) {
}

assert(ctx->real_listen_port != 0);
sprintf(content, PRIVOXY_CONFIG_CONTENT_FMT, ctx->privoxy_listen_port, ctx->real_listen_port);
sprintf(content, PRIVOXY_CONFIG_CONTENT_FMT, ctx->privoxy_listen_host, ctx->privoxy_listen_port, ctx->real_listen_port);

hFile = CreateFileA(privoxy_config_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
Expand Down
4 changes: 2 additions & 2 deletions src/run_ssr_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ extern "C" {

#define SSR_DELAY_QUIT_MIN 500

#define PRIVOXY_LISTEN_ADDR L"127.0.0.1"
#define PRIVOXY_LISTEN_ADDR "127.0.0.1"
#define PRIVOXY_LISTEN_PORT 8118

struct ssr_client_ctx;
struct server_config;

struct ssr_client_ctx* ssr_client_begin_run(struct server_config* config, const char* ssr_listen_host, int ssr_listen_port, int proxy_listen_port, int delay_quit_ms, int change_inet_opts);
struct ssr_client_ctx* ssr_client_begin_run(struct server_config* config, const char* ssr_listen_host, int ssr_listen_port, const char* proxy_listen_host, int proxy_listen_port, int delay_quit_ms, int change_inet_opts);
void ssr_client_terminate(struct ssr_client_ctx* ctx);
const char* ssr_client_error_string(void);

Expand Down
13 changes: 11 additions & 2 deletions src/ssrWin/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct main_wnd_data {
BOOL auto_connect;
char ssr_listen_host[MAX_PATH];
int ssr_listen_port;
char privoxy_listen_host[MAX_PATH];
int privoxy_listen_port;
int delay_quit_ms;
int change_inet_opts;
Expand Down Expand Up @@ -347,7 +348,7 @@ LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar
else {
assert(wnd_data->client_ctx == NULL);
config = retrieve_config_from_list_view(wnd_data->hListView, wnd_data->cur_selected);
wnd_data->client_ctx = ssr_client_begin_run(config, wnd_data->ssr_listen_host, wnd_data->ssr_listen_port, wnd_data->privoxy_listen_port, wnd_data->delay_quit_ms, wnd_data->change_inet_opts);
wnd_data->client_ctx = ssr_client_begin_run(config, wnd_data->ssr_listen_host, wnd_data->ssr_listen_port, wnd_data->privoxy_listen_host, wnd_data->privoxy_listen_port, wnd_data->delay_quit_ms, wnd_data->change_inet_opts);
if (wnd_data->client_ctx == NULL) {
const char*info = ssr_client_error_string();
put_string_to_rich_edit_control(wnd_data->hWndLogBox, TRUE, info, 2);
Expand Down Expand Up @@ -444,6 +445,7 @@ static void on_wm_create(HWND hWnd, LPCREATESTRUCTW pcs)
wnd_data->auto_run = FALSE;
lstrcpyA(wnd_data->ssr_listen_host, "127.0.0.1");
wnd_data->ssr_listen_port = 0;
lstrcpyA(wnd_data->privoxy_listen_host, PRIVOXY_LISTEN_ADDR);
wnd_data->privoxy_listen_port = PRIVOXY_LISTEN_PORT;
wnd_data->delay_quit_ms = SSR_DELAY_QUIT_MIN;
wnd_data->change_inet_opts = TRUE;
Expand Down Expand Up @@ -482,6 +484,9 @@ static void on_wm_create(HWND hWnd, LPCREATESTRUCTW pcs)
sizeBuff = sizeof(wnd_data->ssr_listen_port);
lRet = RegQueryValueExW(hKey, L"ssr_listen_port", 0, &dwtype, (BYTE*)&wnd_data->ssr_listen_port, &sizeBuff);

sizeBuff = sizeof(wnd_data->privoxy_listen_host);
lRet = RegQueryValueExW(hKey, L"privoxy_listen_host", 0, &dwtype, (BYTE*)&wnd_data->privoxy_listen_host[0], &sizeBuff);

sizeBuff = sizeof(wnd_data->privoxy_listen_port);
lRet = RegQueryValueExW(hKey, L"privoxy_listen_port", 0, &dwtype, (BYTE*)&wnd_data->privoxy_listen_port, &sizeBuff);

Expand Down Expand Up @@ -542,7 +547,7 @@ static void on_wm_create(HWND hWnd, LPCREATESTRUCTW pcs)
assert(wnd_data->client_ctx == NULL);
config = retrieve_config_from_list_view(wnd_data->hListView, index);
assert(config);
wnd_data->client_ctx = ssr_client_begin_run(config, wnd_data->ssr_listen_host, wnd_data->ssr_listen_port, wnd_data->privoxy_listen_port, wnd_data->delay_quit_ms, wnd_data->change_inet_opts);
wnd_data->client_ctx = ssr_client_begin_run(config, wnd_data->ssr_listen_host, wnd_data->ssr_listen_port, wnd_data->privoxy_listen_host, wnd_data->privoxy_listen_port, wnd_data->delay_quit_ms, wnd_data->change_inet_opts);
if (wnd_data->client_ctx == NULL) {
const char*info = ssr_client_error_string();
put_string_to_rich_edit_control(wnd_data->hWndLogBox, TRUE, info, 2);
Expand Down Expand Up @@ -576,6 +581,7 @@ static void on_wm_destroy(HWND hWnd) {
RegSetValueExW(hKey, L"auto_connect", 0, REG_BINARY, (BYTE*)&wnd_data->auto_connect, sizeof(wnd_data->auto_connect));
RegSetValueExW(hKey, L"ssr_listen_host", 0, REG_BINARY, (BYTE*)&wnd_data->ssr_listen_host[0], sizeof(wnd_data->ssr_listen_host));
RegSetValueExW(hKey, L"ssr_listen_port", 0, REG_BINARY, (BYTE*)&wnd_data->ssr_listen_port, sizeof(wnd_data->ssr_listen_port));
RegSetValueExW(hKey, L"privoxy_listen_host", 0, REG_BINARY, (BYTE*)&wnd_data->privoxy_listen_host[0], sizeof(wnd_data->privoxy_listen_host));
RegSetValueExW(hKey, L"privoxy_listen_port", 0, REG_BINARY, (BYTE*)&wnd_data->privoxy_listen_port, sizeof(wnd_data->privoxy_listen_port));
RegSetValueExW(hKey, L"delay_quit_ms", 0, REG_BINARY, (BYTE*)&wnd_data->delay_quit_ms, sizeof(wnd_data->delay_quit_ms));
RegSetValueExW(hKey, L"change_inet_opts", 0, REG_BINARY, (BYTE*)&wnd_data->change_inet_opts, sizeof(wnd_data->change_inet_opts));
Expand Down Expand Up @@ -1323,6 +1329,7 @@ static INT_PTR CALLBACK OptionsDlgProc(HWND hDlg, UINT uMessage, WPARAM wParam,
CheckDlgButton(hDlg, IDC_CHK_AUTO_CONN, wnd_data->auto_connect);
SetDlgItemTextA(hDlg, IDC_EDT_SSR_HOST, wnd_data->ssr_listen_host);
SetDlgItemInt(hDlg, IDC_EDT_SSR_PORT, wnd_data->ssr_listen_port, FALSE);
SetDlgItemTextA(hDlg, IDC_EDT_PRIVOXY_HOST, wnd_data->privoxy_listen_host);
SetDlgItemInt(hDlg, IDC_EDT_PRIVOXY_PORT, wnd_data->privoxy_listen_port, FALSE);
SetDlgItemInt(hDlg, IDC_EDT_DELAY_MS, wnd_data->delay_quit_ms, FALSE);
CheckDlgButton(hDlg, IDC_CHK_CHANGE_INET_OPTS, wnd_data->change_inet_opts);
Expand All @@ -1336,6 +1343,7 @@ static INT_PTR CALLBACK OptionsDlgProc(HWND hDlg, UINT uMessage, WPARAM wParam,
CheckDlgButton(hDlg, IDC_CHK_AUTO_CONN, FALSE);
SetDlgItemTextW(hDlg, IDC_EDT_SSR_HOST, L"127.0.0.1");
SetDlgItemTextW(hDlg, IDC_EDT_SSR_PORT, L"0");
SetDlgItemTextA(hDlg, IDC_EDT_PRIVOXY_HOST, PRIVOXY_LISTEN_ADDR);
SetDlgItemInt(hDlg, IDC_EDT_PRIVOXY_PORT, PRIVOXY_LISTEN_PORT, FALSE);
SetDlgItemInt(hDlg, IDC_EDT_DELAY_MS, SSR_DELAY_QUIT_MIN, FALSE);
CheckDlgButton(hDlg, IDC_CHK_CHANGE_INET_OPTS, TRUE);
Expand All @@ -1346,6 +1354,7 @@ static INT_PTR CALLBACK OptionsDlgProc(HWND hDlg, UINT uMessage, WPARAM wParam,
wnd_data->auto_connect = IsDlgButtonChecked(hDlg, IDC_CHK_AUTO_CONN);
GetDlgItemTextA(hDlg, IDC_EDT_SSR_HOST, wnd_data->ssr_listen_host, sizeof(wnd_data->ssr_listen_host));
wnd_data->ssr_listen_port = GetDlgItemInt(hDlg, IDC_EDT_SSR_PORT, NULL, FALSE);
GetDlgItemTextA(hDlg, IDC_EDT_PRIVOXY_HOST, wnd_data->privoxy_listen_host, sizeof(wnd_data->privoxy_listen_host));
wnd_data->privoxy_listen_port = GetDlgItemInt(hDlg, IDC_EDT_PRIVOXY_PORT, NULL, FALSE);
wnd_data->delay_quit_ms = GetDlgItemInt(hDlg, IDC_EDT_DELAY_MS, NULL, FALSE);
wnd_data->change_inet_opts = IsDlgButtonChecked(hDlg, IDC_CHK_CHANGE_INET_OPTS);
Expand Down
Binary file modified src/ssrWin/resource.h
Binary file not shown.
Binary file modified src/ssrWin/ssrwin.rc
Binary file not shown.

0 comments on commit 8658473

Please sign in to comment.