forked from cyd01/KiTTY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitty_proxy.c
177 lines (171 loc) · 7.87 KB
/
kitty_proxy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "putty.h"
#include "kitty.h"
#include "kitty_tools.h"
#include "kitty_registry.h"
#include "kitty_proxy.h"
#include "kitty_store.h"
#include "dialog.h"
#include "storage.h"
#include <sys/types.h>
#include <dirent.h>
#include <windows.h>
// Flag pour ajouter la fonction Proxy Selector
static int ProxySelectionFlag = 0 ;
int GetProxySelectionFlag() { return ProxySelectionFlag ; }
void SetProxySelectionFlag( const int flag ) { ProxySelectionFlag = flag ; }
void debug_logevent( const char *fmt, ... ) ;
struct Proxies proxies[MAX_PROXY] ;
void InitProxyList(void) {
HKEY hKey ;
int i,j;
char buffer[MAX_VALUE_NAME] ;
for( i=0; i<lenof(proxies); i++) {
proxies[i].name = NULL;
proxies[i].val = i;
}
proxies[0].name=(char*)malloc(26); strcpy(proxies[0].name,"- Session defined proxy -");
proxies[1].name=(char*)malloc(13); strcpy(proxies[1].name,"- No proxy -");
j=2;
if( (IniFileFlag == SAVEMODE_REG)||(IniFileFlag == SAVEMODE_FILE) ) {
TCHAR achClass[MAX_PATH] = TEXT("");
DWORD cchClassName=MAX_PATH,cSubKeys=0,cbMaxSubKey,cchMaxClass;
DWORD cValues,cchMaxValue,cbMaxValueData,cbSecurityDescriptor;
FILETIME ftLastWriteTime;
sprintf( buffer, "%s\\Proxies", PUTTY_REG_POS ) ;
RegTestOrCreate( HKEY_CURRENT_USER, buffer, NULL, NULL ) ;
if( RegOpenKeyEx( HKEY_CURRENT_USER, buffer, 0, KEY_READ, &hKey) != ERROR_SUCCESS ) return ;
RegQueryInfoKey(hKey,achClass,&cchClassName,NULL,&cSubKeys,&cbMaxSubKey,&cchMaxClass,&cValues,&cchMaxValue,&cbMaxValueData,&cbSecurityDescriptor,&ftLastWriteTime);
if( cSubKeys>0 )
for (i=0; i<cSubKeys; i++) {
DWORD cchValue = MAX_VALUE_NAME;
char lpData[4096] ;
if( RegEnumKeyEx(hKey, i, lpData, &cchValue, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS ) {
if( strcmp(lpData,"None") && strcmp(lpData,"Default") ) {
proxies[j].name=(char*)malloc(strlen(lpData)+1);
unmungestr( lpData, proxies[j].name, MAX_VALUE_NAME ) ;
j++;
}
}
}
RegCloseKey( hKey ) ;
} else if( IniFileFlag == SAVEMODE_DIR ) {
char fullpath[MAX_VALUE_NAME];
DIR * dir ;
struct dirent * de ;
sprintf( fullpath, "%s\\Proxies", ConfigDirectory ) ;
if(!MakeDir( fullpath ) ) { MessageBox(NULL,"Unable to create the proxy definitions directory","Error",MB_OK|MB_ICONERROR); }
if( (dir=opendir(fullpath)) != NULL ) {
while( (de=readdir(dir)) != NULL )
if( strcmp(de->d_name,".") && strcmp(de->d_name,"..") ) {
sprintf( fullpath, "%s\\Proxies\\%s", ConfigDirectory, de->d_name ) ;
if( !(GetFileAttributes( fullpath ) & FILE_ATTRIBUTE_NORMAL) ) {
if( strcmp(de->d_name,"None") && strcmp(de->d_name,"Default") ) {
proxies[j].name=(char*)malloc(strlen(de->d_name)+1);
unmungestr( de->d_name, proxies[j].name, MAX_VALUE_NAME ) ;
j++;
}
}
}
closedir(dir) ;
}
}
}
int LoadProxyInfo( Conf * conf, const char * name ) {
char buffer[MAX_VALUE_NAME] ;
if( !strcmp(name,"- Session defined proxy -") ) { return 0 ; }
if( !strcmp(name,"- No proxy -") ) {
debug_logevent( "Remove proxy definition" ) ;
conf_set_int(conf, CONF_proxy_type, PROXY_NONE) ;
return 1 ;
}
debug_logevent( "Load proxy \"%s\" definition", name ) ;
if( (IniFileFlag == SAVEMODE_REG)||(IniFileFlag == SAVEMODE_FILE) ) {
HKEY hKey ;
sprintf( buffer, "%s\\Proxies\\", PUTTY_REG_POS ) ;
char *b = (char*)malloc(4*strlen(name)+1);
mungestr(name,b);
strcat(buffer,b);
free(b);
if( RegOpenKeyEx( HKEY_CURRENT_USER, buffer, 0, KEY_READ, &hKey) != ERROR_SUCCESS ) {
debug_logevent( "Unable to load proxy definition" ) ;
return 0;
}
char lpData[4096] ;
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyExcludeList", lpData ) ) {
conf_set_str( conf, CONF_proxy_exclude_list, lpData ) ;
}
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyDNS", lpData ) ) {
int i=atoi(lpData);
conf_set_int(conf, CONF_proxy_dns, (i+1)%3);
}
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyLocalhost", lpData ) ) {
if( atoi(lpData) == 0 ) { conf_set_bool( conf, CONF_even_proxy_localhost, false ) ;
} else { conf_set_bool( conf, CONF_even_proxy_localhost, true ) ;
}
}
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyMethod", lpData ) ) {
int i = atoi(lpData) ;
if (i == 0) conf_set_int(conf, CONF_proxy_type, PROXY_NONE);
else if (i == 1) conf_set_int(conf, CONF_proxy_type, PROXY_SOCKS4) ;
else if (i == 2) conf_set_int(conf, CONF_proxy_type, PROXY_SOCKS5) ;
else if (i == 3) conf_set_int(conf, CONF_proxy_type, PROXY_HTTP) ;
else if (i == 4) conf_set_int(conf, CONF_proxy_type, PROXY_TELNET) ;
else if (i == 5) conf_set_int(conf, CONF_proxy_type, PROXY_CMD) ;
else conf_set_int(conf, CONF_proxy_type, PROXY_NONE) ;
}
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyHost", lpData ) ) { conf_set_str( conf, CONF_proxy_host, lpData ) ; }
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyPort", lpData ) ) { conf_set_int( conf, CONF_proxy_port, atoi(lpData) ) ; }
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyUsername", lpData ) ) { conf_set_str( conf, CONF_proxy_username, lpData ) ; }
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyPassword", lpData ) ) { conf_set_str( conf, CONF_proxy_password, lpData ) ; }
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyTelnetCommand", lpData ) ) { conf_set_str( conf, CONF_proxy_telnet_command, lpData ) ; }
if( GetValueData(HKEY_CURRENT_USER, buffer, "ProxyLogToTerm", lpData ) ) { conf_set_int( conf, CONF_proxy_log_to_term, atoi(lpData) ) ; }
RegCloseKey( hKey ) ;
} else if( IniFileFlag == SAVEMODE_DIR ) {
char fullpath[MAX_VALUE_NAME] ;
char *filename = (char*) malloc( 4*strlen(name)+1 ) ;
mungestr( name, filename ) ;
sprintf( fullpath, "%s\\Proxies\\%s", ConfigDirectory, filename ) ;
if( existfile(fullpath) ) {
FILE *fp;
if( (fp=fopen(fullpath,"r")) != NULL ) {
char buf2[MAX_VALUE_NAME]="";
while( fgets(buffer,MAX_VALUE_NAME,fp)!=NULL ) {
if( ReadPortableValue(buffer, "ProxyExcludeList", buf2, MAX_VALUE_NAME) ) {
conf_set_str( conf, CONF_proxy_exclude_list, buf2 ) ;
} else if( ReadPortableValue(buffer, "ProxyDNS", buf2, MAX_VALUE_NAME) ) {
int i=atoi(buf2);
conf_set_int(conf, CONF_proxy_dns, (i+1)%3);
} else if( ReadPortableValue(buffer, "ProxyLocalhost", buf2, MAX_VALUE_NAME) ) {
if( atoi(buf2) == 0 ) { conf_set_bool( conf, CONF_even_proxy_localhost, false ) ;
} else { conf_set_bool( conf, CONF_even_proxy_localhost, true ) ;
}
} else if( ReadPortableValue(buffer, "ProxyMethod", buf2, MAX_VALUE_NAME) ) {
int i = atoi(buf2) ;
if (i == 0) conf_set_int(conf, CONF_proxy_type, PROXY_NONE);
else if (i == 1) conf_set_int(conf, CONF_proxy_type, PROXY_SOCKS4) ;
else if (i == 2) conf_set_int(conf, CONF_proxy_type, PROXY_SOCKS5) ;
else if (i == 3) conf_set_int(conf, CONF_proxy_type, PROXY_HTTP) ;
else if (i == 4) conf_set_int(conf, CONF_proxy_type, PROXY_TELNET) ;
else if (i == 5) conf_set_int(conf, CONF_proxy_type, PROXY_CMD) ;
else conf_set_int(conf, CONF_proxy_type, PROXY_NONE) ;
} else if( ReadPortableValue(buffer, "ProxyHost", buf2, MAX_VALUE_NAME) ) {
conf_set_str( conf, CONF_proxy_host, buf2 ) ;
} else if( ReadPortableValue(buffer, "ProxyPort", buf2, MAX_VALUE_NAME) ) {
conf_set_int( conf, CONF_proxy_port, atoi(buf2) ) ;
} else if( ReadPortableValue(buffer, "ProxyUsername", buf2, MAX_VALUE_NAME) ) {
conf_set_str( conf, CONF_proxy_username, buf2 ) ;
} else if( ReadPortableValue(buffer, "ProxyPassword", buf2, MAX_VALUE_NAME) ) {
conf_set_str( conf, CONF_proxy_password, buf2 ) ;
} else if( ReadPortableValue(buffer, "ProxyTelnetCommand", buf2, MAX_VALUE_NAME) ) {
conf_set_str( conf, CONF_proxy_telnet_command, buf2 ) ;
} else if( ReadPortableValue(buffer, "ProxyLogToTerm", buf2, MAX_VALUE_NAME) ) {
conf_set_int( conf, CONF_proxy_log_to_term, atoi(buf2) ) ;
}
}
fclose(fp);
}
}
free( filename ) ;
}
return 1;
}