aboutsummaryrefslogtreecommitdiff
path: root/wakedlg.go
blob: 53fd3e56ca09522e3f88334470a8c0deccf55a1a (plain) (blame)
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
package main

import (
	"fmt"
	"net"
	"regexp"

	"github.com/lxn/walk"
	"github.com/lxn/walk/declarative"
	"github.com/mdlayher/wol"
)

// WakeOnLAN struct type for wake on lan
type WakeOnLAN struct {
	SleepHost, SleepPass, SleepMac string
	SleepPort                      int
}

func runWakeOnLanDg(owner walk.Form, dfgAddr, mactgadd string) (int, error) {
	const regexSingle = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"

	var wakeClientguiwindow struct {
		dlgWakeOnLan       *walk.Dialog
		dbindersshx        *walk.DataBinder
		acceptPB, cancelPB *walk.PushButton
		macLine, addrLine  *walk.LineEdit
	}

	wakestatset := &WakeOnLAN{
		SleepHost: dfgAddr,
		SleepMac:  mactgadd,
		SleepPort: 7,
	}
	return declarative.Dialog{
		AssignTo:      &wakeClientguiwindow.dlgWakeOnLan,
		FixedSize:     true,
		Title:         "Wake On LAN Client",
		DefaultButton: &wakeClientguiwindow.acceptPB,
		CancelButton:  &wakeClientguiwindow.cancelPB,
		DataBinder: declarative.DataBinder{
			AssignTo:       &wakeClientguiwindow.dbindersshx,
			Name:           "wakeClient",
			DataSource:     wakestatset,
			ErrorPresenter: declarative.ToolTipErrorPresenter{},
		},
		MinSize: declarative.Size{Width: 320, Height: 300},
		Layout:  declarative.VBox{},
		Children: []declarative.Widget{
			declarative.GroupBox{
				Title:  "Wake On Lan Options",
				Layout: declarative.HBox{},
				Children: []declarative.Widget{
					declarative.Composite{
						Layout: declarative.Grid{Columns: 1},
						Children: []declarative.Widget{
							declarative.GroupBox{
								Title:  "Host Address To Wake",
								Layout: declarative.Grid{Columns: 3},
								Children: []declarative.Widget{
									declarative.LineEdit{
										AssignTo:    &wakeClientguiwindow.addrLine,
										ToolTipText: "format: IP Address",
										CueBanner:   "IPv4 Address...",
										Text:        declarative.Bind("SleepHost"),
									},

									declarative.NumberEdit{
										ToolTipText: "port number",
										MaxSize:     declarative.Size{Width: 40},
										MinSize:     declarative.Size{Width: 40},
										Value:       declarative.Bind("SleepPort"),
										MinValue:    1,
										MaxValue:    65535,
									},
								},
							},
							declarative.GroupBox{
								Title:  "Host MAC Address",
								Layout: declarative.Grid{Columns: 1},
								Children: []declarative.Widget{
									declarative.LineEdit{
										AssignTo:  &wakeClientguiwindow.macLine,
										CueBanner: "Mac Address...",
										Text:      declarative.Bind("SleepMac"),
									},
								},
							},
							declarative.GroupBox{
								Title:  "Optional Wake Up Password",
								Layout: declarative.VBox{},
								Children: []declarative.Widget{
									declarative.LineEdit{
										CueBanner:    "Optional Password...",
										PasswordMode: true,
										Text:         declarative.Bind("SleepPass"),
									},
								},
							},
						},
					},
				},
			},
			declarative.Composite{
				Layout: declarative.HBox{},
				Children: []declarative.Widget{
					declarative.HSpacer{},
					declarative.PushButton{
						AssignTo: &wakeClientguiwindow.acceptPB,
						Text:     "Wake Up",
						OnClicked: func() {
							if err := wakeClientguiwindow.dbindersshx.Submit(); err != nil {
								return
							}
							if wakestatset.SleepHost == "" {
								wakeClientguiwindow.addrLine.Focused()
								return
							}
							if wakestatset.SleepMac == "" {
								wakeClientguiwindow.macLine.Focused()
								return
							}
							if match, _ := regexp.MatchString(regexSingle, wakestatset.SleepHost); !match {
								walk.MsgBox(wakeClientguiwindow.dlgWakeOnLan, "Host syntax error", "Invalid IPv4 address, Example: 192.168.1.1", walk.MsgBoxIconError)
								return
							}
							digMacAddr, err := net.ParseMAC(wakestatset.SleepMac)
							if err != nil {
								walk.MsgBox(wakeClientguiwindow.dlgWakeOnLan, "Mac syntax error", fmt.Sprint(err), walk.MsgBoxIconError)
								return
							}
							if err := wakeMachineUp(fmt.Sprintf("%v:%v", wakestatset.SleepHost, wakestatset.SleepPort), digMacAddr, []byte(wakestatset.SleepPass)); err != nil {
								walk.MsgBox(wakeClientguiwindow.dlgWakeOnLan, "Wake on error", fmt.Sprint(err), walk.MsgBoxIconError)
								return
							}
							walk.MsgBox(wakeClientguiwindow.dlgWakeOnLan, "Wake on lan", fmt.Sprintf("sent UDP Wake-on-LAN magic packet using \n%s to %s", wakestatset.SleepHost, wakestatset.SleepMac), walk.MsgBoxIconInformation)
						},
					},
					declarative.PushButton{
						AssignTo:  &wakeClientguiwindow.cancelPB,
						Text:      "Cancel",
						OnClicked: func() { wakeClientguiwindow.dlgWakeOnLan.Cancel() },
					},
				},
			},
		},
	}.Run(owner)
}

func wakeMachineUp(ipaddr string, macaddr net.HardwareAddr, password []byte) error {
	machine, err := wol.NewClient()
	if err != nil {
		return err
	}
	defer machine.Close()
	return machine.WakePassword(ipaddr, macaddr, password)
}

Snix LLC Git Repository Holder Copyright(C) 2022 All Rights Reserved Email To Snix.IR