-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUnitMap.cpp
More file actions
164 lines (143 loc) · 4.39 KB
/
UnitMap.cpp
File metadata and controls
164 lines (143 loc) · 4.39 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
// ---------------------------------------------------------------------------
#include <vcl.h>
#include <System.RegularExpressionsCore.hpp>
#include <IdStackWindows.hpp>
#pragma hdrstop
#include "UnitMap.h"
#include "Unit1.h"
#include "tools.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMap *FormMap;
// ---------------------------------------------------------------------------
__fastcall TFormMap::TFormMap(TComponent* Owner) : TForm(Owner)
{
}
// ---------------------------------------------------------------------------
void __fastcall TFormMap::CreateParams(TCreateParams &Params)
{
TForm::CreateParams(Params);
Params.ExStyle = Params.ExStyle | WS_EX_APPWINDOW;
Params.WndParent = 0;
}
// ---------------------------------------------------------------------------
void __fastcall TFormMap::FormClose(TObject *Sender, TCloseAction &Action)
{
Action = caHide;
}
// ---------------------------------------------------------------------------
void __fastcall TFormMap::ActionHideExecute(TObject *Sender)
{
Hide();
}
// ---------------------------------------------------------------------------
void TFormMap::Clear()
{
memoLocalHost->Clear();
lbLocalGate->Clear();
memoPublicGate->Clear();
edtLocalPort->Clear();
edtPublicPort->Clear();
}
// ---------------------------------------------------------------------------
void __fastcall TFormMap::btnSysInfoClick(TObject *Sender)
{
String cmd;
cmd.printf(L"CMD.EXE /K \"ECHO %s && %s\"", btnSysInfo->Hint.w_str(),
btnSysInfo->Hint.w_str());
t_RunProcess(cmd);
}
// ---------------------------------------------------------------------------
void __fastcall TFormMap::btnPortForwardClick(TObject *Sender)
{
ShellExecute(NULL, L"open", btnPortForward->Hint.w_str(), NULL, NULL, SW_NORMAL);
}
// ---------------------------------------------------------------------------
void __fastcall TFormMap::lbLocalGateClick(TObject *Sender)
{
SelectGateway();
}
// ---------------------------------------------------------------------------
bool TFormMap::SelectGateway()
{
String ip;
int index = lbLocalGate->ItemIndex;
bool valid = false;
if (index > -1)
{
ip = lbLocalGate->Items->Strings[index];
if (!ip.IsEmpty())
{
TPerlRegEx *regex = new TPerlRegEx();
// https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp
regex->RegEx = L"^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$";
regex->Subject = ip;
if (regex->Match() && regex->MatchedText == ip)
valid = true;
delete regex;
}
}
btnPortForward->Enabled = valid;
if (valid)
{
String url;
url.printf(L"http://%s", ip.w_str());
btnPortForward->Hint = url;
}
return valid;
}
// ---------------------------------------------------------------------------
void TFormMap::AutoSelectGateway()
{
bool gateFound = false;
for (int i = 0; i < lbLocalGate->Items->Count; i++)
{
lbLocalGate->ItemIndex = i;
if ((gateFound = SelectGateway()))
break;
}
if (!gateFound)
{
lbLocalGate->ItemIndex = -1;
}
}
// ---------------------------------------------------------------------------
#define SETUIVALUE(var, value) if ((var) != (value)) (var) = (value);
#define SETUIVALUE2(var, value, cmd1, cmd2) if ((var) != (value)) { (var) = (value); (cmd1); (cmd2); }
void TFormMap::UpdateMap()
{
// localhost info
String str;
TStringList *list = new TStringList();
TIdStackWindows *stack = new TIdStackWindows();
str = stack->HostName + L"\r\n";
stack->AddLocalAddressesToList(list);
str += list->Text;
SETUIVALUE(memoLocalHost->Text, str);
SETUIVALUE(edtLocalPort->Text, Form1->edtPortLocal->Text);
// gateway info
try
{
String gate(t_GetGateways());
SETUIVALUE2(lbLocalGate->Items->Text, gate, t_SetScrollWidth(lbLocalGate),
AutoSelectGateway());
}
catch (Exception &ex)
{
SETUIVALUE(lbLocalGate->Items->Text, ex.Message);
}
if (Form1->edtPublicIP->Text.IsEmpty())
{
SETUIVALUE(memoPublicGate->Text,
L"Press button (2) to determine your public IP address.");
}
else
{
SETUIVALUE(memoPublicGate->Text, Form1->edtPublicIP->Text);
}
SETUIVALUE(edtPublicPort->Text, Form1->edtPort->Text);
delete stack;
delete list;
}
// ---------------------------------------------------------------------------