-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
347 lines (279 loc) · 9.3 KB
/
Program.fs
File metadata and controls
347 lines (279 loc) · 9.3 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
module RemoteDesktopAssistant.Program
open System.Windows.Forms
open System.Data
open System.IO
open RemoteCore
let dataRowToRemoteHost (row: DataRow) =
let port =
match System.UInt16.TryParse(row.Item("Port").ToString()) with
| (true, v) -> v
| _ -> 3389us
let name =
match row.Item("Name").ToString() with
| null -> ""
| s -> s
let _ = null |> Option.ofObj
let host =
match row.Item("Host").ToString() with
| null -> ""
| s -> s
{ name = name
host = host
port = port }
let db = new DataTable("rdp")
db.Columns.AddRange(
[| new DataColumn("Name")
new DataColumn("Host")
new DataColumn("Port") |]
)
if File.Exists("db.xml") then
db.ReadXml("db.xml") |> ignore
else
db.Rows.Add(
[| "Домашний компьютер" :> obj
"home-pc" :> obj
3389us :> obj |]
)
|> ignore
let servers = new ToolStripMenuItem "&Server"
let addServers = servers.DropDownItems.Add "&Add"
let connectServer = servers.DropDownItems.Add "&Connect"
let editServers = servers.DropDownItems.Add "&Edit"
let deleteServers = servers.DropDownItems.Add "&Delete"
let hosts = new ToolStripMenuItem "&Hosts"
let openHosts = hosts.DropDownItems.Add "&Open"
let changeHosts = hosts.DropDownItems.Add "&Change"
let cmdKeys = new ToolStripMenuItem "&CmdKey"
let listCmdKey = cmdKeys.DropDownItems.Add "&List"
let purgeCmdKey = cmdKeys.DropDownItems.Add "&Purge"
let addCmdKey = cmdKeys.DropDownItems.Add "&Add"
openHosts.Click.Add(fun _ -> Utils.viewHosts ())
let mutable hostEntries = Hosts.readHostEntries ()
let chekHost host =
hostEntries
|> Array.exists (fun h -> h.host = host.host)
changeHosts.Click.Add(fun _ -> Utils.changeHosts ())
type MainMenu() as x =
class
inherit MenuStrip()
do
x.Dock <- DockStyle.Top
x.LayoutStyle <- ToolStripLayoutStyle.Flow
x.Items.Add servers |> ignore
x.Items.Add hosts |> ignore
x.Items.Add cmdKeys |> ignore
x.Items.Add VpnUI.vpnActions |> ignore
end
type ConnectionsGrid() as x =
class
inherit DataGridView()
do
x.DataSource <- db
x.AllowUserToAddRows <- false
x.AllowUserToDeleteRows <- false
x.CellFormatting.Add (fun e ->
if e.RowIndex > x.Rows.Count then
()
else
match x.Rows.Item(e.RowIndex).DataBoundItem with
| :? DataRowView as item ->
let host = item.Row |> dataRowToRemoteHost
let is_new = (chekHost >> not) host
if is_new then
e.CellStyle.BackColor <- System.Drawing.Color.LightYellow
| _ -> ())
end
let panel = new FlowLayoutPanel()
let grid = new ConnectionsGrid()
let watch =
FileWatch.watchHosts (fun e ->
System.Diagnostics.Debug.WriteLine $"{e.ChangeType} -> {e.FullPath}"
hostEntries <- Hosts.readHostEntries ()
grid.Invalidate())
type AccountEdit(host) as x =
class
inherit Form()
let buttons = new WinForms.DialogButtons(x)
let host = new TextBox(Text = host, Width = 200)
let user =
new TextBox(Text = System.Environment.UserName, Width = 200)
let pass =
new TextBox(PasswordChar = '*', Width = 200)
do
x.Text <- "Account"
x.StartPosition <- FormStartPosition.CenterParent
x.FormBorderStyle <- FormBorderStyle.FixedDialog
let flow = new FlowLayoutPanel()
flow.AutoSizeMode <- AutoSizeMode.GrowAndShrink
flow.AutoSize <- true
flow.MaximumSize <- System.Drawing.Size(300, 0)
flow.Dock <- DockStyle.Fill
flow.Controls.Add(new Label(Text = "Host:"))
flow.Controls.Add host
flow.Controls.Add(new Label(Text = "User:"))
flow.Controls.Add user
flow.Controls.Add(new Label(Text = "Pass:"))
flow.Controls.Add pass
flow.Controls.Add buttons
x.Controls.Add flow
x.ClientSize <- flow.PreferredSize
member x.Edit() = x.ShowDialog() = DialogResult.OK
member x.User = user.Text
member x.Pass = pass.Text
end
type ConnectionEdit() as x =
class
inherit Form()
let buttons = new WinForms.DialogButtons(x)
let name = new TextBox(Width = 200)
let host = new TextBox(Width = 200)
let port = new TextBox(Width = 200)
do
x.Text <- "Host"
x.StartPosition <- FormStartPosition.CenterParent
x.FormBorderStyle <- FormBorderStyle.FixedDialog
let flow = new FlowLayoutPanel()
flow.AutoSizeMode <- AutoSizeMode.GrowAndShrink
flow.AutoSize <- true
flow.Dock <- DockStyle.Fill
flow.MaximumSize <- System.Drawing.Size(300, 0)
flow.Controls.Add(new Label(Text = "Name:"))
flow.Controls.Add name
flow.Controls.Add(new Label(Text = "Host:"))
flow.Controls.Add host
flow.Controls.Add(new Label(Text = "Port:"))
flow.Controls.Add port
flow.Controls.Add buttons
x.Controls.Add flow
x.ClientSize <- flow.PreferredSize
member x.Name
with get () = name.Text.Trim()
and set v = name.Text <- v
member x.Host
with get () = host.Text.Trim()
and set v = host.Text <- v
member x.Port
with get () =
match System.UInt16.TryParse(port.Text) with
| true, v -> v
| _ -> 0us
and set (value: uint16) = port.Text <- $"{value}"
member x.Edit() = x.ShowDialog() = DialogResult.OK
end
let current () =
match grid.CurrentRow with
| Null -> None
| NonNull row ->
match row.DataBoundItem with
| Null -> None
| NonNull dbi -> Some (dbi :?> DataRowView).Row
let show (text: string) =
MessageBox.Show(text, "Rdp Assistant") |> ignore
let question text =
MessageBox.Show(text, "Rdp Assistant", buttons = MessageBoxButtons.YesNo) = DialogResult.Yes
deleteServers.Click.Add (fun _ ->
match current () with
| None -> ()
| Some r ->
let h = dataRowToRemoteHost r
if question $"Delete {h.name} ({h.host})?" then
db.Rows.Remove r
grid.Refresh())
connectServer.Click.Add (fun _ ->
match current () with
| None -> ()
| Some v ->
v
|> dataRowToRemoteHost
|> WinProcess.startConnect
|> ignore)
listCmdKey.Click.Add (fun _ ->
match current () with
| None -> ()
| Some v ->
v
|> dataRowToRemoteHost
|> (fun x -> x.host)
|> Utils.listCmdKey
|> ignore)
purgeCmdKey.Click.Add (fun _ ->
match current () with
| None -> ()
| Some v ->
v
|> dataRowToRemoteHost
|> fun x ->
if question $"Delete account for this host {x.name}?" then
Utils.deleteCmdKey x.host |> ignore)
grid.Dock <- DockStyle.Fill
panel.Controls.Add grid
panel.BackColor <- System.Drawing.Color.Red
panel.Dock <- DockStyle.Fill
let menu = new MainMenu()
type RdpConnections() as x =
class
inherit Form()
do
x.Icon <- App.icon
x.Text <- "RDP Assistant"
x.StartPosition <- FormStartPosition.CenterScreen
x.Height <- 600
x.Width <- 800
x.Controls.Add grid
x.Controls.Add menu
x.MainMenuStrip <- menu
end
let mainForm = new RdpConnections()
addCmdKey.Click.Add (fun _ ->
match current () with
| None -> ()
| Some v ->
v
|> dataRowToRemoteHost
|> fun x ->
use acc = new AccountEdit(x.host)
if acc.Edit() then
Utils.addCmdKey x.host acc.User acc.Pass |> ignore)
addServers.Click.Add (fun e ->
use connectionEdit = new ConnectionEdit()
if connectionEdit.Edit() then
db.Rows.Add [| connectionEdit.Name :> obj
connectionEdit.Host :> obj
connectionEdit.Port :> obj |]
|> ignore
grid.Refresh())
editServers.Click.Add (fun e ->
match current () with
| None -> ()
| Some dr ->
let rdeskop = dataRowToRemoteHost dr
use edit =
new ConnectionEdit(Name = rdeskop.name, Host = rdeskop.host, Port = rdeskop.port)
if edit.Edit() then
dr.BeginEdit()
dr.Item"Name" <- edit.Name
dr.Item"Host" <- edit.Host
dr.Item"Port" <- edit.Port
dr.EndEdit()
grid.Refresh())
let closeVPN () =
let cfg = Init.read ()
let ps = OpenVpnGUI.disConnect cfg.VpnDisconnect
match ps with
| NonNull x ->
System.Console.Beep(880, 500)
if WinProcess.minimizeAll () then
System.Threading.Thread.Sleep 100
else ()
WinProcess.bringToFront x
|> System.Console.WriteLine
x.WaitForExit()
| Null -> ()
[<System.STAThreadAttribute>]
[<EntryPoint>]
let main args =
Application.Run mainForm
watch.Dispose()
db.WriteXml "db.xml"
closeVPN ()
0