11/***************************************************************************
22 libproxybound.c
33 ---------------
4- begin : Tue May 14 2002
5- copyright : netcreature (C) 2002
6- email : netcreature@users.sourceforge.net
4+
5+ copyright: intika (C) 2019 intika@librefox.org
6+ copyright: rofl0r (C) 2012 https://github.com/rofl0r
7+ copyright: haad (C) 2012 https://github.com/haad
8+ copyright: netcreature (C) 2002 netcreature@users.sourceforge.net
9+
710 ***************************************************************************
8- * GPL *
11+ * GPL *
912 ***************************************************************************
1013 * *
1114 * This program is free software; you can redistribute it and/or modify *
3235#include <fcntl.h>
3336#include <dlfcn.h>
3437
35-
3638#include "core.h"
3739#include "common.h"
3840
@@ -62,14 +64,18 @@ int proxybound_resolver = 0;
6264localaddr_arg localnet_addr [MAX_LOCALNET ];
6365size_t num_localnet_addr = 0 ;
6466unsigned int remote_dns_subnet = 224 ;
65-
6667#ifdef THREAD_SAFE
6768pthread_once_t init_once = PTHREAD_ONCE_INIT ;
6869#endif
70+
6971static int init_l = 0 ;
7072
73+ static void init_additional_settings (chain_type * ct );
74+
7175static inline void get_chain_data (proxy_data * pd , unsigned int * proxy_count , chain_type * ct );
7276
77+ static void manual_socks5_env (proxy_data * pd , unsigned int * proxy_count , chain_type * ct );
78+
7379static void * load_sym (char * symname , void * proxyfunc ) {
7480
7581 void * funcptr = dlsym (RTLD_NEXT , symname );
@@ -94,6 +100,10 @@ static void* load_sym(char* symname, void* proxyfunc) {
94100static void do_init (void ) {
95101 MUTEX_INIT (& internal_ips_lock , NULL );
96102 MUTEX_INIT (& hostdb_lock , NULL );
103+
104+ /* check for simple SOCKS5 proxy setup */
105+ manual_socks5_env (proxybound_pd , & proxybound_proxy_count , & proxybound_ct );
106+
97107 /* read the config file */
98108 get_chain_data (proxybound_pd , & proxybound_proxy_count , & proxybound_ct );
99109
@@ -135,6 +145,18 @@ static void gcc_init(void) {
135145}
136146#endif
137147
148+ static void init_additional_settings (chain_type * ct ) {
149+ char * env ;
150+
151+ tcp_read_time_out = 4 * 1000 ;
152+ tcp_connect_time_out = 10 * 1000 ;
153+ * ct = DYNAMIC_TYPE ;
154+
155+ env = getenv (PROXYBOUND_QUIET_MODE_ENV_VAR );
156+ if (env && * env == '1' )
157+ proxybound_quiet_mode = 1 ;
158+ }
159+
138160/* get configuration from config file */
139161static void get_chain_data (proxy_data * pd , unsigned int * proxy_count , chain_type * ct ) {
140162 int count = 0 , port_n = 0 , list = 0 ;
@@ -148,17 +170,11 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ
148170 return ;
149171
150172 //Some defaults
151- tcp_read_time_out = 4 * 1000 ;
152- tcp_connect_time_out = 10 * 1000 ;
153- * ct = DYNAMIC_TYPE ;
173+ init_additional_settings (ct );
154174
155175 env = get_config_path (getenv (PROXYBOUND_CONF_FILE_ENV_VAR ), buff , sizeof (buff ));
156176 file = fopen (env , "r" );
157177
158- env = getenv (PROXYBOUND_QUIET_MODE_ENV_VAR );
159- if (env && * env == '1' )
160- proxybound_quiet_mode = 1 ;
161-
162178 while (fgets (buff , sizeof (buff ), file )) {
163179 if (buff [0 ] != '\n' && buff [strspn (buff , " " )] != '#' ) {
164180 /* proxylist has to come last */
@@ -267,6 +283,38 @@ static void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_typ
267283 proxybound_got_chain_data = 1 ;
268284}
269285
286+ static void manual_socks5_env (proxy_data * pd , unsigned int * proxy_count , chain_type * ct ) {
287+ char * port_string ;
288+ char * host_string ;
289+
290+ if (proxybound_got_chain_data )
291+ return ;
292+
293+ init_additional_settings (ct );
294+
295+ port_string = getenv (PROXYBOUND_SOCKS5_PORT_ENV_VAR );
296+ if (!port_string )
297+ return ;
298+
299+ host_string = getenv (PROXYBOUND_SOCKS5_HOST_ENV_VAR );
300+ if (!host_string )
301+ host_string = "127.0.0.1" ;
302+
303+ memset (pd , 0 , sizeof (proxy_data ));
304+
305+ pd [0 ].ps = PLAY_STATE ;
306+ pd [0 ].ip .as_int = (uint32_t ) inet_addr (host_string );
307+ pd [0 ].port = htons ((unsigned short ) strtol (port_string , NULL , 0 ));
308+ pd [0 ].pt = SOCKS5_TYPE ;
309+ proxybound_max_chain = 1 ;
310+
311+ if (getenv (PROXYBOUND_FORCE_DNS_ENV_VAR ) && (* getenv (PROXYBOUND_FORCE_DNS_ENV_VAR ) == '1' ))
312+ proxybound_resolver = 1 ;
313+
314+ * proxy_count = 1 ;
315+ proxybound_got_chain_data = 1 ;
316+ }
317+
270318/******* HOOK FUNCTIONS *******/
271319
272320int connect (int sock , const struct sockaddr * addr , socklen_t len ) {
0 commit comments