Repository navigation
Expand file tree
/
Copy pathportfoward.cpp
More file actions
1148 lines (979 loc) · 28 KB
/
Copy pathportfoward.cpp
File metadata and controls
1148 lines (979 loc) · 28 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// portfoward.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "portfoward.h"
#include <iostream>
#include <set>
#include <map>
#include <iterator>
using namespace std;
using std::set;
using std::map;
using std::iterator;
using std::reverse_iterator;
using std::reverse_bidirectional_iterator;
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HWND topWindow, status, redirs;
unsigned int TaskbarResetMsg = 0;
enum {
WM_APP_1 = WM_APP,
WM_APP_2, // tray menu
WM_APP_3, // handle command line
};
// *** neal@wanlink.com - 2010/06/04
// Shell Tray API
#include <shellapi.h>
typedef struct _MYNOTIFYICONDATA
{
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
TCHAR szTip[128]; // Version 5.0 = 128 < 5.0 = 64
DWORD dwState; //Version 5.0
DWORD dwStateMask; //Version 5.0
TCHAR szInfo[256]; //Version 5.0
union
{
UINT uTimeout; //Version 5.0
UINT uVersion; //Version 5.0
};
TCHAR szInfoTitle[64]; //Version 5.0
DWORD dwInfoFlags; //Version 5.0
} MYNOTIFYICONDATA, *PMYNOTIFYICONDATA;
#define NIM_SETFOCUS 0x00000003
#define NIM_SETVERSION 0x00000004
#define NOTIFYICON_VERSION 3
#define NIF_STATE 0x00000008
#define NIF_INFO 0x00000010
#define NIS_HIDDEN 0x00000001
#define NIS_SHAREDICON 0x00000002
#define NIIF_INFO 0x00000001
#define NIIF_WARNING 0x00000002
#define NIIF_ERROR 0x00000003
#ifndef NIN_SELECT
#define NIN_SELECT (WM_USER+0)
#define NINF_KEY 0x1
#define NIN_KEYSELECT (NIN_SELECT|NINF_KEY)
#define NIN_BALLOONSHOW (WM_USER+2)
#define NIN_BALLOONHIDE (WM_USER+3)
#define NIN_BALLOONTIMEOUT (WM_USER+4)
#define NIN_BALLOONUSERCLICK (WM_USER+5)
#endif
BOOL SetTrayTip(MYNOTIFYICONDATA *ptnd, char *str, DWORD msg)
{ BOOL bOk = FALSE;
if(str != NULL && strlen(str))
{
ptnd->szInfo[0] = 0;
ptnd->szInfoTitle[0] = 0;
ptnd->uFlags &= ~NIF_INFO;
ptnd->uFlags |= NIF_TIP;
//sprintf(ptnd->szTip,"%s - %s",m_szAppName,str);
strcpy(ptnd->szTip,str);
bOk = Shell_NotifyIcon(msg, (NOTIFYICONDATA *)ptnd);
}
return(bOk);
}
BOOL SetTrayIcon(MYNOTIFYICONDATA *ptnd, HICON icon, DWORD msg)
{
ptnd->hIcon = icon;
ptnd->uFlags |= NIF_ICON;
return(Shell_NotifyIcon (msg, (NOTIFYICONDATA *)ptnd));
}
BOOL SetTrayBalloon(MYNOTIFYICONDATA *ptnd, char *str, char *title, DWORD flags, UINT maxtime, DWORD msg)
{ BOOL bOk = FALSE;
if(str != NULL && strlen(str))
{
ptnd->dwInfoFlags = flags;
ptnd->uTimeout = maxtime * 1000;
ptnd->uFlags |= NIF_INFO|NIF_TIP;
strcpy(ptnd->szInfoTitle,title);
strcpy(ptnd->szInfo,str);
sprintf(ptnd->szTip,"%s - %s",title,str);
bOk = Shell_NotifyIcon(msg, (NOTIFYICONDATA *)ptnd);
}
return(bOk);
}
// Shell Tray API
// *** neal@wanlink.com - 2010/06/04
MYNOTIFYICONDATA TaskbarNotifyIconData;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI DlgProc_About(HWND, UINT, WPARAM, LPARAM);
int WINAPI DlgProc_Add(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
HACCEL hAccelTable;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 1;
}
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_PORTFOWARD, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
INITCOMMONCONTROLSEX init;
init.dwSize = sizeof(init);
init.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&init);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PORTFOWARD));
PostMessage(topWindow,WM_APP_3,0,0);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PORTFOWARD));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_PORTFOWARD);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 400, 400, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
topWindow = hWnd;
status = CreateWindowEx(0, STATUSCLASSNAME, "Stopped.", WS_CHILD, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWnd, NULL, hInstance, NULL);
ShowWindow(status, nCmdShow);
RECT rs;
GetWindowRect(status, &rs);
RECT r;
GetClientRect(hWnd, &r);
redirs = CreateWindowEx(0, WC_LISTVIEW, "Port Redirections", WS_CHILD | LVS_REPORT, 0, 0, r.right - r.left, r.bottom - (rs.bottom - rs.top), hWnd, NULL, hInstance, NULL);
ListView_SetExtendedListViewStyle(redirs, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
LVCOLUMN col;
col.mask = LVCF_TEXT | LVCF_WIDTH;
col.pszText = "Local Port";
col.cx = 100;
ListView_InsertColumn(redirs, 0, &col);
col.pszText = "Destination";
col.cx = r.right - r.left - 300;
ListView_InsertColumn(redirs, 1, &col);
col.mask |= LVCF_FMT;
col.fmt = LVCFMT_CENTER;
col.pszText = "Destination Port";
col.cx = 100;
ListView_InsertColumn(redirs, 2, &col);
col.pszText = "# Connections";
col.cx = 100;
ListView_InsertColumn(redirs, 3, &col);
ShowWindow(redirs, nCmdShow);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
int edit = -1;
bool delete_redir(int n);
void close_connections_for_redir(int idx);
int get_total_connections();
void load_redirFile(char *filename);
void load_redirs();
void save_redirs();
void ShowTrayContextMenu()
{ HMENU menu = CreatePopupMenu();
POINT pt;
AppendMenu(menu,MF_STRING,IDM_EXIT,"E&xit");
GetCursorPos(&pt);
SetForegroundWindow(topWindow);
TrackPopupMenu(menu,TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, 0,topWindow,NULL);
DestroyMenu(menu);
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
TaskbarResetMsg = RegisterWindowMessage(TEXT("TaskbarCreated"));
memset(&TaskbarNotifyIconData,0,sizeof(TaskbarNotifyIconData));
TaskbarNotifyIconData.cbSize = sizeof(TaskbarNotifyIconData);
TaskbarNotifyIconData.hWnd = hWnd;
TaskbarNotifyIconData.uFlags = NIF_MESSAGE;
TaskbarNotifyIconData.uCallbackMessage = WM_APP_2;
TaskbarNotifyIconData.uFlags = NIF_MESSAGE;
PostMessage(hWnd,TaskbarResetMsg,0,0);
break;
case WM_CLOSE:
Shell_NotifyIcon(NIM_DELETE, (NOTIFYICONDATA *)&TaskbarNotifyIconData); // kill tray
wParam = IDM_EXIT;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)DlgProc_About);
break;
case ID_FILE_LOAD:
load_redirs();
break;
case ID_FILE_SAVE:
save_redirs();
break;
case ID_REDIR_ADD:
edit = -1;
DialogBox(hInst, MAKEINTRESOURCE(IDD_ADDBOX), hWnd, (DLGPROC)DlgProc_Add);
break;
case ID_REDIR_CLOSECONNECTIONS:
{
for (int i = 0; i < ListView_GetItemCount(redirs); i++)
if (ListView_GetItemState(redirs, i, LVIS_SELECTED))
close_connections_for_redir(i);
}
break;
case ID_REDIR_DELETE:
{
for (int i = 0; i < ListView_GetItemCount(redirs); )
if (ListView_GetItemState(redirs, i, LVIS_SELECTED))
{
delete_redir(i);
}
else
i++;
}
break;
case ID_REDIR_EDIT:
{
edit = -1;
for (int i = 0; i < ListView_GetItemCount(redirs); i++)
if (ListView_GetItemState(redirs, i, LVIS_SELECTED)) {
edit = i;
break;
}
if (edit != -1)
DialogBox(hInst, MAKEINTRESOURCE(IDD_ADDBOX), hWnd, (DLGPROC)DlgProc_Add);
}
break;
case IDM_EXIT:
{
int nconnections = get_total_connections();
if (nconnections > 0) {
char buf[1024];
sprintf(buf,"There are %i active connections being redirected, they will all be disconnected. Do you really want to quit?", nconnections);
if (MessageBox(hWnd, buf, "Warning", MB_ICONHAND|MB_YESNO) == IDNO)
break;
}
DestroyWindow(hWnd);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_NOTIFY:
{
LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)lParam;
if (lpnmlv->hdr.code == LVN_ITEMACTIVATE) {
edit = lpnmlv->iItem;
DialogBox(hInst, MAKEINTRESOURCE(IDD_ADDBOX), hWnd, (DLGPROC)DlgProc_Add);
} else if (lpnmlv->hdr.code == LVN_ITEMCHANGED) {
EnableMenuItem(GetMenu(hWnd), ID_REDIR_DELETE, ListView_GetSelectedCount(redirs) != 0 ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(GetMenu(hWnd), ID_REDIR_CLOSECONNECTIONS, ListView_GetSelectedCount(redirs) != 0 ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(GetMenu(hWnd), ID_REDIR_EDIT, ListView_GetSelectedCount(redirs) == 1 ? MF_ENABLED : MF_GRAYED);
}
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_SIZE:
{
RECT r;
GetClientRect(status, &r);
SetWindowPos(status, NULL, 0, HIWORD(lParam)-(r.bottom - r.top), LOWORD(lParam), r.bottom - r.top, SWP_NOZORDER);
SetWindowPos(redirs, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam)-1 - (r.bottom - r.top), SWP_NOMOVE|SWP_NOZORDER);
ListView_SetColumnWidth(redirs, 1, r.right - r.left - 300);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SYSCOMMAND:
switch(wParam)
{
case SC_MINIMIZE:
ShowWindow(topWindow,SW_HIDE);
SetWindowLong(topWindow,GWL_EXSTYLE,GetWindowLong(topWindow,GWL_EXSTYLE)|WS_EX_TOOLWINDOW);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
break;
case WM_APP_2: // tray popup menu
switch(LOWORD(lParam))
{
case WM_LBUTTONDOWN: // version < 5.0
if(!IsWindowVisible(topWindow))
{
SetWindowLong(topWindow,GWL_EXSTYLE,GetWindowLong(topWindow,GWL_EXSTYLE)&(~WS_EX_TOOLWINDOW));
ShowWindow(topWindow,SW_SHOWNORMAL);
}
else
{
ShowWindow(topWindow,SW_HIDE);
SetWindowLong(topWindow,GWL_EXSTYLE,GetWindowLong(topWindow,GWL_EXSTYLE)|WS_EX_TOOLWINDOW);
}
break;
case WM_RBUTTONDOWN: // version < 5.0
ShowTrayContextMenu();
return FALSE;
break;
}
break;
case WM_APP_3:
{ LPSTR pGCL = GetCommandLine();
int count = 0;
while(*pGCL && (count < 2 || *pGCL == ' ' || *pGCL == '\t'))
{
count += (*pGCL == '"');
pGCL++;
}
if(*pGCL)
load_redirFile(pGCL); //MessageBox(topWindow,pGCL,"The Command Line",MB_OK);
}
break;
default:
if(message == TaskbarResetMsg)
{
// add an icon to the tray
SetTrayIcon(&TaskbarNotifyIconData,LoadIcon(hInst,MAKEINTRESOURCE(IDI_PORTFOWARD)),NIM_ADD);
SetTrayTip(&TaskbarNotifyIconData,"PortForward",NIM_MODIFY);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
bool setup_redir(int idx, int lport, const char *dest, int dport);
// Message handler for about box.
// *** neal@wanlink.com - 2010/06/04
int GetTextExtent(HWND hWnd, LPCTSTR str)
{ HDC dc = GetDC(hWnd);
HFONT font = (HFONT)SendMessage(hWnd,WM_GETFONT,0,0);
HFONT oldfont = NULL;
SIZE size;
size.cx = 0;
size.cy = 0;
if(dc != NULL)
{
oldfont = (HFONT)SelectObject(dc,font);
GetTextExtentPoint32(dc,str,strlen(str),&size);
if (oldfont != NULL)
SelectObject(dc,oldfont);
ReleaseDC(hWnd,dc);
}
return(size.cx);
}
static BOOL bHovering = FALSE;
static BOOL bVisited = FALSE;
static HCURSOR cursor = NULL;
int WINAPI DlgProc_About(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{// RECT rect;
switch (message)
{
case WM_INITDIALOG:
{ char buf[255];
HWND hCtrl = GetDlgItem(hWnd,IDC_HYPERLINK);
/*
// Set dialog title
sprintf(buf,"%s - About",m_szAppTitle);
SetWindowText(hWnd,buf);
*/
/*
// Center the dialog
GetWindowRect( hWnd, &rect );
MoveWindow( hWnd,
(GetSystemMetrics(SM_CXSCREEN) - (rect.right-rect.left)) / 2,
(GetSystemMetrics(SM_CYSCREEN) - (rect.bottom-rect.top)) / 2,
rect.right-rect.left,
rect.bottom-rect.top,
FALSE );
*/
// Make sure the hyperlink notify's parent
SetClassLong(hCtrl,GCL_STYLE,GetClassLong(hCtrl,GCL_STYLE)|SS_NOTIFY);
// Make sure the hyperlink is underlined
LOGFONT lf;
if(GetObject((HGDIOBJ)SendMessage(hCtrl,WM_GETFONT,0,0),sizeof(LOGFONT),&lf))
{
lf.lfUnderline = TRUE;
SendMessage(hCtrl,WM_SETFONT,(WPARAM)CreateFontIndirect(&lf),1);
}
// Setup the cursor for the hyperlink
if(cursor == NULL)
cursor = LoadCursor(hInst,MAKEINTRESOURCE(IDC_CURSOR1));
SetClassLong(hCtrl,GCL_HCURSOR,(LONG)cursor);
// Size the hyperlink window to the extent of the text
RECT rect;
GetWindowText(hCtrl,buf,sizeof(buf));
GetWindowRect(hCtrl,&rect);
ScreenToClient(hWnd,(LPPOINT)&rect);
ScreenToClient(hWnd,((LPPOINT)&rect)+1);
rect.right = rect.left + GetTextExtent(hCtrl,buf);
SetWindowPos(hCtrl, hWnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top, SWP_NOZORDER);
bHovering = FALSE;
}
return FALSE;
break; // case WM_INITDIALOG
case WM_COMMAND:
switch ( LOWORD(wParam) )
{
case IDCANCEL:
case IDOK:
EndDialog( hWnd, 0 );
break;
case IDC_HYPERLINK:
case IDC_HYPERLINK2:
{
switch(HIWORD(wParam))
{
case STN_CLICKED:
{ char buf[256];
GetWindowText((HWND)lParam,buf,sizeof(buf));
bVisited = ((int)ShellExecute(NULL, "open", buf, NULL,NULL, SW_SHOW) > HINSTANCE_ERROR);
}
break;
}
}
break;
default:
return FALSE;
break;
}
return TRUE;
break; // case WM_COMAND
case WM_MOUSEMOVE:
{ HWND hCtl = GetDlgItem(hWnd,IDC_HYPERLINK);
POINT pt;
RECT rect;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
if(hWnd != NULL && hCtl != NULL)
{
GetWindowRect(hCtl,&rect);
ScreenToClient(hWnd,(LPPOINT)&rect);
ScreenToClient(hWnd,((LPPOINT)&rect)+1);
if(pt.x >= rect.left &&
pt.x <= rect.right &&
pt.y >= rect.top &&
pt.y <= rect.bottom)
{
if(!bHovering)
{
bHovering = TRUE;
RedrawWindow(hCtl,NULL,NULL,RDW_INVALIDATE|RDW_UPDATENOW|RDW_ERASE);
}
}
else if(bHovering)
{
bHovering = FALSE;
RedrawWindow(hCtl,NULL,NULL,RDW_INVALIDATE|RDW_UPDATENOW|RDW_ERASE);
}
}
return(FALSE);
}
break;
case WM_CTLCOLORSTATIC:
{ int idc = GetDlgCtrlID((HWND)lParam);
if(idc == IDC_HYPERLINK || idc == IDC_HYPERLINK2)
{
if(bHovering)
SetTextColor((HDC)wParam,RGB(238,0,0));
else if(bVisited)
SetTextColor((HDC)wParam,RGB(85,26,139));
else
SetTextColor((HDC)wParam,RGB(0,0,238));
SetBkMode((HDC)wParam,TRANSPARENT);
return((LRESULT)GetStockObject(NULL_BRUSH));
}
else
return(FALSE);
}
break;
default:
return FALSE;
break;
} // switch message
return( TRUE );
}
// *** neal@wanlink.com - 2010/06/04
bool update_redir(int idx, const char *dest, int dport);
// Message handler for add box.
int WINAPI DlgProc_Add(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
if (edit != -1) {
SetWindowText(hDlg, "Edit Port Redirection");
char buf[1024];
ListView_GetItemText(redirs, edit, 0, buf, 1024);
SetDlgItemText(hDlg, IDC_LPORT, buf);
EnableWindow(GetDlgItem(hDlg, IDC_LPORT), FALSE);
ListView_GetItemText(redirs, edit, 1, buf, 1024);
SetDlgItemText(hDlg, IDC_DEST, buf);
ListView_GetItemText(redirs, edit, 2, buf, 1024);
SetDlgItemText(hDlg, IDC_DPORT, buf);
}
return (int)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
char buf[1024];
int lport, dport;
char dest[1024];
GetDlgItemText(hDlg, IDC_LPORT, buf, 1024);
lport = atoi(buf);
int n;
if (edit == -1) {
LVITEM item;
item.iItem = ListView_GetItemCount(redirs) + 10;
item.iSubItem = 0;
item.mask = LVIF_TEXT;
item.pszText = buf;
n = ListView_InsertItem(redirs, &item);
} else {
n = edit;
GetDlgItemTextA(hDlg, IDC_DEST, dest, 1024);
GetDlgItemText(hDlg, IDC_DPORT, buf, 1024);
dport = atoi(buf);
if (!update_redir(n, dest, dport))
return (int)TRUE;
}
GetDlgItemText(hDlg, IDC_DEST, buf, 1024);
GetDlgItemTextA(hDlg, IDC_DEST, dest, 1024);
ListView_SetItemText(redirs, n, 1, buf);
GetDlgItemText(hDlg, IDC_DPORT, buf, 1024);
ListView_SetItemText(redirs, n, 2, buf);
dport = atoi(buf);
if (edit != -1) {
EndDialog(hDlg, LOWORD(wParam));
return (int)TRUE;
}
ListView_SetItemText(redirs, n, 3, "0");
if (!setup_redir(n, lport, dest, dport)) {
ListView_DeleteItem(redirs, n);
return (int)TRUE;
}
EndDialog(hDlg, LOWORD(wParam));
return (int)TRUE;
}
else if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (int)TRUE;
}
break;
}
return (int)FALSE;
}
struct redir_info_t;
typedef struct conn_info_t {
redir_info_t *info;
const char *dest_host;
int dport;
SOCKET n, d;
bool started;
bool stop, stopped;
bool writer_exited;
bool reader_stopping;
} conn_info;
typedef struct redir_info_t {
int idx;
int lport;
const char *dest_host;
unsigned int dest; // yeah, ipv4 only, sue me
int dport;
SOCKET s;
std::set<conn_info*> connections;
} redir_info;
std::map<int, redir_info*> redir_infos;
DWORD WINAPI accepter(LPVOID lpThreadParameter);
unsigned int dnslookup(const char *dest);
bool bindToPort(SOCKET s, int port)
{
sockaddr_in sin;
sin.sin_addr.S_un.S_addr = INADDR_ANY;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
if (bind(s, (sockaddr*)&sin, sizeof(sin)) != 0) {
MessageBox(NULL, "Cannot bind to the specified local port. Do you already have a redirection on this port? If not, another process is already bound to this port.", "Error", MB_OK);
return false;
}
return true;
}
bool setup_redir(int idx, int lport, const char *dest, int dport)
{
unsigned int dest_ip = dnslookup(dest);
if (dest_ip == 0)
return false;
redir_info *info = new redir_info;
info->idx = idx;
info->lport = lport;
info->dest_host = _strdup(dest);
info->dest = dest_ip;
info->dport = dport;
info->s = socket(AF_INET, SOCK_STREAM, 0);
if (!bindToPort(info->s, info->lport)) {
delete info;
return false;
}
listen(info->s, 5);
redir_infos[idx] = info;
DWORD id;
CreateThread(NULL, 0, accepter, (LPVOID)idx, 0, &id);
return true;
}
void updateNumConnections(int idx);
int get_total_connections()
{
int nconnections = 0;
for (std::map<int, redir_info*>::iterator it = redir_infos.begin(); it != redir_infos.end(); it++)
nconnections += it->second->connections.size();
return nconnections;
}
void load_redirFile(char *filename)
{
FILE *f = fopen(filename, "r");
if (f == NULL) {
MessageBox(topWindow, filename, "Error - Unable to open file", MB_OK);
return;
}
char buf[1024];
while (fgets(buf, 1024, f))
{
int lport, dport;
char dest[1024];
memset(dest,0,sizeof(dest));
if (sscanf(buf, "%i %s %i", &lport, dest, &dport) == 3)
{
char buf[1024];
sprintf(buf, "%i", lport);
LVITEM item;
item.iItem = ListView_GetItemCount(redirs) + 10;
item.iSubItem = 0;
item.mask = LVIF_TEXT;
item.pszText = buf;
int n = ListView_InsertItem(redirs, &item);
//MultiByteToWideChar(CP_ACP, 0, dest, -1, buf, 1024);
ListView_SetItemText(redirs, n, 1, dest);
sprintf(buf, "%i", dport);
ListView_SetItemText(redirs, n, 2, buf);
ListView_SetItemText(redirs, n, 3, "0");
if (!setup_redir(n, lport, dest, dport))
ListView_DeleteItem(redirs, n);
}
}
fclose(f);}
void load_redirs()
{
char filename[MAX_PATH];
OPENFILENAME of;
memset(&filename,0,sizeof(filename));
memset(&of,0,sizeof(of));
of.lStructSize = sizeof(of);
of.hwndOwner = topWindow;
of.hInstance = hInst;
of.lpstrFilter = "PortForward List - *.pfl\0*.pfl\0Text File - *.txt\0*.txt\0All Files - *.*\0*.*\0";
of.lpstrCustomFilter = NULL;
of.nFilterIndex = 1;
of.lpstrFile = filename;
of.nMaxFile = sizeof(filename);
of.lpstrTitle = "Load redirections from..";
of.lpstrDefExt = "pfl";
if(GetOpenFileName(&of))
load_redirFile(filename);
}
void save_redirs()
{
char filename[MAX_PATH];
OPENFILENAME of;
memset(&filename,0,sizeof(filename));
memset(&of,0,sizeof(of));
of.lStructSize = sizeof(of);
of.hwndOwner = topWindow;
of.hInstance = hInst;
of.lpstrFilter = "PortForward List - *.pfl\0*.pfl\0Text File - *.txt\0*.txt\0";
of.nFilterIndex = 1;
of.lpstrFile = filename;
of.nMaxFile = sizeof(filename);
of.lpstrTitle = "Save redirections to..";
of.lpstrDefExt = "pfl";
//of.Flags = OFN_OVERWRITEPROMPT;
if (!GetSaveFileName(&of))
return;
FILE *f = fopen(filename, "w");
if (f == NULL) {
MessageBox(topWindow, "Cannot create save file", "Error", MB_OK);
return;
}
for (std::map<int, redir_info*>::iterator it = redir_infos.begin(); it != redir_infos.end(); it++)
{
redir_info *info = it->second;
fprintf(f,"%i %s %i\n", info->lport, info->dest_host, info->dport);
}
fclose(f);
}
void close_connections_for_redir(int idx)
{
redir_info *info = redir_infos[idx];
// close the ones that are open
for (std::set<conn_info*>::iterator it = info->connections.begin(); it != info->connections.end(); it++)
{
conn_info *ci = *it;
ci->stop = true;
while (ci->started && !ci->stopped)
{
Sleep(10);
// pay attention!
closesocket(ci->d);
closesocket(ci->n);
}
delete ci;
}
info->connections.clear();
updateNumConnections(idx);
}
bool delete_redir(int n)
{
redir_info *info = redir_infos[n];
if (info->connections.size()) {
char buf[1024];
sprintf(buf, "There are %d connections currently being redirected from %d to %s:%d.\nThese connections will NOT be disconnected (at least not now).\nAre you sure you want to delete this redirection?",
info->connections.size(), info->lport, info->dest_host, info->dport);
if (MessageBoxA(topWindow, buf, "Warning", MB_YESNO) == IDNO)
return false;
// dangle these connections
for (std::set<conn_info*>::iterator it = info->connections.begin(); it != info->connections.end(); it++)
(*it)->info = NULL;
}
// don't accept any more connections
closesocket(info->s);
redir_infos.erase(n);
delete info;
int count = ListView_GetItemCount(redirs);
ListView_DeleteItem(redirs, n);
if (count - 1 > n) {
for (int i = n + 1; i < count; i++) {
redir_infos[i - 1] = redir_infos[i];
redir_infos[i - 1]->idx = i - 1;
}
redir_infos.erase(count - 1);
}
return true;
}
bool update_redir(int idx, const char *dest, int dport)
{
redir_info *info = redir_infos[idx];
info->dport = dport;
if (strcmp(info->dest_host, dest)) {
unsigned int dest_ip = dnslookup(dest);
if (dest_ip == 0)
return false;