-
Notifications
You must be signed in to change notification settings - Fork 0
/
swhosts2.cpp
235 lines (190 loc) · 6.12 KB
/
swhosts2.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// swhosts2.cpp : Defines the entry point for the application.
//
//#include "stdafx.h"
//#include "swhosts2.h"
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ShellAPI.h>
struct hosts_s{
char mac[ 128 ];
char hosts_fn[ 512 ];
};
typedef struct hosts_s hosts_t, *hosts_p_t;
HANDLE SpawnAndRedirect(LPSTR commandLine, HANDLE *hStdOutputReadPipe, LPCTSTR lpCurrentDirectory)
// copy from http://packcab.googlecode.com/svn/trunk/PipeTest/PipeTestDlg.cpp
{
HANDLE hStdOutputWritePipe, hStdOutput, hStdError;
CreatePipe(hStdOutputReadPipe, &hStdOutputWritePipe, NULL, 0); // create a non-inheritable pipe
DuplicateHandle(GetCurrentProcess(), hStdOutputWritePipe,
GetCurrentProcess(), &hStdOutput, // duplicate the "write" end as inheritable stdout
0, TRUE, DUPLICATE_SAME_ACCESS);
DuplicateHandle(GetCurrentProcess(), hStdOutput,
GetCurrentProcess(), &hStdError, // duplicate stdout as inheritable stderr
0, TRUE, DUPLICATE_SAME_ACCESS);
CloseHandle(hStdOutputWritePipe); // no longer need the non-inheritable "write" end of the pipe
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); // (this is bad on a GUI app)
si.hStdOutput = hStdOutput;
si.hStdError = hStdError;
si.wShowWindow = SW_HIDE; // IMPORTANT: hide subprocess console window
//TCHAR commandLineCopy[1024]; // CreateProcess requires a modifiable buffer
//strcpy(commandLineCopy, commandLine);
if (!CreateProcess( NULL, commandLine, NULL, NULL, TRUE,
CREATE_NEW_CONSOLE, NULL, lpCurrentDirectory, &si, &pi))
{
CloseHandle(hStdOutput);
CloseHandle(hStdError);
CloseHandle(*hStdOutputReadPipe);
*hStdOutputReadPipe = INVALID_HANDLE_VALUE;
return NULL;
}
CloseHandle(pi.hThread);
CloseHandle(hStdOutput);
CloseHandle(hStdError);
return pi.hProcess;
}
int getcmdexebuffer( char *cmdline, char *buffer, int bsz )
{
DWORD read;
int err, rt = FALSE;
HANDLE hOutput, hProcess;
hProcess = SpawnAndRedirect( cmdline, &hOutput, NULL );
if (!hProcess ) return rt;
err = ReadFile( hOutput, buffer, bsz, &read, NULL );
if( err == TRUE && read != 0 ){
buffer[read] = '\0';
rt = TRUE;
}
CloseHandle(hOutput);
CloseHandle(hProcess);
return rt;
}
int getcmdpath( char *cmdpath )
{
char *cmdline;
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
char cmdline2[ 512 ];
int len;
cmdline = GetCommandLine( );
if( cmdline[ 0 ] == '"' ) strcpy( cmdline2, &( cmdline[ 1 ] ));
else strcpy( cmdline2, cmdline );
len = strlen( cmdline2 );
if( cmdline2[ len - 1 ] == '"' || cmdline2[ len - 1 ] == ' ' ) cmdline2[ len - 1 ] = 0;
len = strlen( cmdline2 );
if( cmdline2[ len - 1 ] == '"' || cmdline2[ len - 1 ] == ' ' ) cmdline2[ len - 1 ] = 0;
_splitpath( cmdline2, drive, dir, fname, ext );
_makepath( cmdpath, drive, dir, NULL, NULL );
if(cmdpath[ strlen( cmdpath ) - 1 ] == '\\' ) cmdpath[ strlen( cmdpath ) - 1 ] = 0;
return 0;
}
int fileisexist( char *fn )
{
FILE *fp;
fp = fopen( fn, "rb" );
if( fp == NULL ) return FALSE;
fclose( fp );
return TRUE;
}
void TrimString( char *line )
//过滤注释行(假定以//或#开头),去除回车换行符,以及头尾空格
{
int i, len;
if( line == NULL ) return;
//清除回车换行符号
if( line[ strlen( line ) - 1 ] == 0x0A || line[ strlen( line ) - 1 ] == 0x0D )
line[ strlen( line ) - 1 ] = '\0';
if( line[ strlen( line ) - 1 ] == 0x0A || line[ strlen( line ) - 1 ] == 0x0D )
line[ strlen( line ) - 1 ] = '\0';
//裁剪尾部字空格
len = strlen( line );
for( i = len - 1; i >= 0; i-- ){
if( line[ i ] == ' ' ) line[ i ] = '\0';
else break;
}
//裁剪头部空格
strrev( line );
len = strlen( line );
for( i = len - 1; i >= 0; i-- ){
if( line[ i ] == ' ' ) line[ i ] = '\0';
else break;
}
strrev( line );
if( line[ 0 ] == '/' && line[ 1 ] == '/' )
line[ 0 ] = 0; //过滤注释
if( line[ 0 ] == '#' )
line[ 0 ] = 0; //过滤注释
return;
}
int gethostinfo( char *cmdpath, hosts_t *myhosts, int *num )
{
FILE *fp;
int i = 0;
char inifile[ 512 ], line[ 512 ], *strp;
*num = 0;
sprintf( inifile, "%s\\%s", cmdpath, "swhosts.ini" );
fp = fopen( inifile, "rb" );
if( fp == NULL ) return FALSE;
while( TRUE ){
if( fgets( line, 511, fp ) == NULL ) break;
TrimString( line );
if( line[ 0 ] == 0 ) continue;
strp = strtok( line, " " ); //mac string
if( strp != NULL ) strcpy( myhosts[ i ].mac, strp );
else continue;
strp = strtok( NULL, " " ); //hosts file string
if( strp != NULL ) sprintf( myhosts[ i ].hosts_fn, "%s\\%s", cmdpath, strp );
else continue;
i++;
}
fclose( fp );
*num = i;
return TRUE;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
char buffer[ 2048 ];
char cmdpath[ 512 ], winpath[ 512 ], cmdfile[ 512 ];
hosts_t myhosts[ 32 ];
int num, err, i, find;
FILE *fp;
getcmdpath( cmdpath );
GetWindowsDirectory( winpath, 511 );
strcat( winpath, "\\system32" ); // c:\windows\system32
num = 0;
err = gethostinfo( cmdpath, myhosts, &num );
if( err == FALSE ) return FALSE;
err = getcmdexebuffer( "arp -a", buffer, 2048 );
if( err == FALSE ) return FALSE;
sprintf( cmdfile, "%s%s", cmdpath, "\\swh.bat" );
fp = fopen( cmdfile, "wb" );
if( fp == NULL ) return FALSE;
find = 0;
for( i = 0; i < num; i++ ){
if( strstr( buffer, myhosts[ i ].mac ) == NULL ) continue;
if( fileisexist( myhosts[ i ].hosts_fn ) ){ //switch router hosts
fprintf( fp, "copy %s %s\\drivers\\etc\\hosts\n", myhosts[ i ].hosts_fn, winpath );
find = 1;
break;
}
}
if( find == 0 ){
//switch default hosts
fprintf( fp, "copy %s\\hosts.default %s\\drivers\\etc\\hosts\n", cmdpath, winpath );
}
fclose( fp );
ShellExecute( NULL, "open", cmdfile, NULL, NULL, SW_HIDE );
//DeleteFile( cmdfile ); //如要删除此文件,则要等此批处理命令执行完毕后。
return TRUE;
}