1 |
Attribute VB_Name = "Module_Main" |
2 |
Option Explicit |
3 |
|
4 |
' see: How To Obtain the IP Address Assigned to a RAS Client |
5 |
' http://support.microsoft.com/default.aspx?scid=kb;en-us;160622 |
6 |
|
7 |
' see: RasGetProjectionInfo |
8 |
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50lrfRasGetProjectionInfo.asp |
9 |
|
10 |
' http://home.iprimus.com.au/billmcc/PlatformVB/dun/rasenumentries.htm |
11 |
' http://home.iprimus.com.au/billmcc/PlatformVB/dun/raserrorhandler.htm |
12 |
' http://www.mentalis.org/apilist/RasEnumEntries.shtml |
13 |
|
14 |
' http://www.activevb.de/tipps/tipkat/kat1.html |
15 |
' http://www.activevb.de/rubriken/apikatalog/deklarationen/rasenumentries.html |
16 |
' http://www.dotnet247.com/247reference/msgs/18/93960.aspx |
17 |
|
18 |
' contains all ras entry objects |
19 |
Public RasEntries As New Collection |
20 |
|
21 |
' globals to store connection name and state |
22 |
Public ConnectionName As String |
23 |
Public ConnectionOnline As Boolean |
24 |
|
25 |
' globals to store information about action to do on up|down |
26 |
Enum ActionTypes |
27 |
RUN_SCRIPT |
28 |
ADD_ROUTE |
29 |
End Enum |
30 |
Public ActionType As ActionTypes |
31 |
|
32 |
Public ScriptName As String |
33 |
Public RouteNet As String, RouteMask As String |
34 |
|
35 |
Const RouteMaskDefault As String = "255.255.255.0" |
36 |
|
37 |
|
38 |
Sub Main() |
39 |
|
40 |
Dim cmdline As New CommandLine |
41 |
Dim conName As String |
42 |
Dim rasItem As RasEntryData |
43 |
Dim success As Boolean |
44 |
|
45 |
Dim script_name As String, script_args As String |
46 |
Dim setup_user As String, setup_pass As String |
47 |
|
48 |
ReadRasEntries |
49 |
cmdline.parse |
50 |
|
51 |
'If cmdline.hasSwitch("gui") Then |
52 |
' Form_Main.Show |
53 |
'Else |
54 |
|
55 |
' dial command |
56 |
If cmdline.hasSwitch("dial") Then |
57 |
|
58 |
conName = cmdline.getArgument("dial") |
59 |
On Error Resume Next |
60 |
Set rasItem = RasEntries(conName) |
61 |
If Err.Number = 0 Then |
62 |
success = RasConnect(rasItem.entryname, rasItem.PhonebookPath) |
63 |
Else |
64 |
MsgBox "Unkown RAS-Connection """ & conName & """." |
65 |
End If |
66 |
On Error GoTo 0 |
67 |
|
68 |
' hangup command |
69 |
ElseIf cmdline.hasSwitch("hangup") Then |
70 |
conName = cmdline.getArgument("hangup") |
71 |
success = RasDisconnect(conName) |
72 |
'MsgBox success |
73 |
|
74 |
'End If |
75 |
|
76 |
' run script |
77 |
ElseIf cmdline.hasSwitch("script") And success = True Then |
78 |
script_name = cmdline.getArgument("script") |
79 |
If script_name <> "" Then |
80 |
script_args = Chr(34) & DetermineClientIP(conName) & Chr(34) & " " & Chr(34) & DetermineServerIP(conName) & Chr(34) |
81 |
Shell App.Path & "\" & script_name & " " & script_args, vbHide |
82 |
End If |
83 |
'End If |
84 |
|
85 |
' setup |
86 |
ElseIf cmdline.hasSwitch("setup") Then |
87 |
conName = cmdline.getArgument("setup") |
88 |
If conName <> "" Then |
89 |
|
90 |
On Error Resume Next |
91 |
Set rasItem = RasEntries(conName) |
92 |
If Err.Number <> 0 Then |
93 |
MsgBox "Error while accessing RAS entry """ & conName & """." & vbCrLf & "Probably it does not exist?" |
94 |
End |
95 |
End If |
96 |
On Error GoTo 0 |
97 |
|
98 |
If cmdline.hasSwitch("gui") Then |
99 |
With Form_Credentials |
100 |
.ras_connectionName = rasItem.entryname |
101 |
.ras_phoneBook = rasItem.PhonebookPath |
102 |
.Show |
103 |
End With |
104 |
|
105 |
ElseIf cmdline.hasSwitch("user") And cmdline.hasSwitch("pass") Then |
106 |
setup_user = cmdline.getArgument("user") |
107 |
setup_pass = cmdline.getArgument("pass") |
108 |
SetupRasEntry rasItem.entryname, rasItem.PhonebookPath, setup_user, setup_pass |
109 |
End If |
110 |
|
111 |
End If |
112 |
|
113 |
' monitor |
114 |
ElseIf cmdline.hasSwitch("monitor") Then |
115 |
conName = cmdline.getArgument("monitor") |
116 |
If conName <> "" Then |
117 |
|
118 |
' run script |
119 |
If cmdline.hasSwitch("script") Then |
120 |
ActionType = RUN_SCRIPT |
121 |
ScriptName = cmdline.getArgument("script") |
122 |
End If |
123 |
|
124 |
' add a route with target network via gateway |
125 |
If cmdline.hasSwitch("net") Then |
126 |
ActionType = ADD_ROUTE |
127 |
RouteNet = cmdline.getArgument("net") |
128 |
If cmdline.hasSwitch("mask") Then |
129 |
RouteMask = cmdline.getArgument("mask") |
130 |
Else |
131 |
RouteMask = RouteMaskDefault |
132 |
End If |
133 |
End If |
134 |
|
135 |
'Set rasItem = RasEntries(conName) |
136 |
'RasRetrieveConnectionHandler conName |
137 |
ConnectionName = conName |
138 |
ConnectionOnline = RasIsOnline(conName) |
139 |
|
140 |
If cmdline.hasSwitch("gui") Then |
141 |
ShowTrayIcon Form_Main, getTrayIconTipText(ConnectionName, ConnectionOnline) |
142 |
End If |
143 |
|
144 |
MonitorRASStatusAsync |
145 |
End If |
146 |
|
147 |
End If |
148 |
|
149 |
'End If |
150 |
|
151 |
End Sub |
152 |
|
153 |
Private Sub ReadRasEntries() |
154 |
|
155 |
Dim myEntries() As VBRasEntryName |
156 |
Dim lngCount As Long |
157 |
Dim rasItem As RasEntryData |
158 |
|
159 |
lngCount = VBRasGetAllEntries(myEntries) |
160 |
|
161 |
'MsgBox lngCount |
162 |
Dim i As Integer |
163 |
Dim curEntry As VBRasEntryName |
164 |
For i = 0 To lngCount - 1 |
165 |
curEntry = myEntries(i) |
166 |
|
167 |
Set rasItem = New RasEntryData |
168 |
rasItem.entryname = curEntry.entryname |
169 |
rasItem.PhonebookPath = curEntry.PhonebookPath |
170 |
rasItem.Win2000_SystemPhonebook = curEntry.Win2000_SystemPhonebook |
171 |
|
172 |
'MsgBox rasItem.entryname |
173 |
On Error Resume Next |
174 |
RasEntries.add rasItem, rasItem.entryname |
175 |
If Err.Number = 457 Then |
176 |
MsgBox "Error: Duplicate RAS entry. Don't know what to dial. This error should not occour." |
177 |
End If |
178 |
On Error GoTo 0 |
179 |
Next i |
180 |
|
181 |
End Sub |
182 |
|
183 |
' callback from MonitorRASStatusAsync |
184 |
Public Sub detectOnlineOfflineChange() |
185 |
Dim isOnline As Boolean |
186 |
Dim script_name As String, script_args As String |
187 |
Dim cmd As String |
188 |
|
189 |
isOnline = RasIsOnline(ConnectionName) |
190 |
|
191 |
If ConnectionOnline <> isOnline Then |
192 |
'MsgBox isOnline |
193 |
|
194 |
Select Case ActionType |
195 |
|
196 |
Case RUN_SCRIPT: |
197 |
script_name = ScriptName |
198 |
If script_name <> "" Then |
199 |
script_args = Chr(34) & DetermineClientIP(ConnectionName) & Chr(34) & " " & Chr(34) & DetermineServerIP(ConnectionName) & Chr(34) |
200 |
cmd = App.Path & "\" & script_name & " " & script_args |
201 |
End If |
202 |
|
203 |
Case ADD_ROUTE: |
204 |
' connection goes online |
205 |
If isOnline = True Then |
206 |
script_name = "route" |
207 |
script_args = "add " & RouteNet & " mask " & RouteMask & " " & DetermineClientIP(ConnectionName) |
208 |
cmd = script_name & " " & script_args |
209 |
|
210 |
' connection goes offline |
211 |
Else |
212 |
' Nothing to do in this case |
213 |
|
214 |
End If |
215 |
|
216 |
End Select |
217 |
|
218 |
If cmd <> "" Then |
219 |
'MsgBox cmd |
220 |
On Error Resume Next |
221 |
Shell cmd, vbHide |
222 |
'Shell cmd, vbNormalFocus |
223 |
'If Err.Number <> 0 Then |
224 |
' MsgBox "Error while calling cmd: " & cmd & vbCrLf & "Error-Number: " & Err.Number |
225 |
'End If |
226 |
On Error GoTo 0 |
227 |
End If |
228 |
|
229 |
ConnectionOnline = isOnline |
230 |
|
231 |
UpdateTrayIcon getTrayIconTipText(ConnectionName, ConnectionOnline) |
232 |
|
233 |
End If |
234 |
End Sub |
235 |
|
236 |
Private Function getTrayIconTipText(conName As String, isOnline As Boolean) As String |
237 |
Dim TipText As String |
238 |
TipText = "VpnDial monitoring """ & conName & """: " |
239 |
If isOnline Then |
240 |
TipText = TipText & "online" |
241 |
Else |
242 |
TipText = TipText & "offline" |
243 |
End If |
244 |
getTrayIconTipText = TipText |
245 |
End Function |