Skip to content

Commit d6225c9

Browse files
committed
misc: Add notification on switch
1 parent 3cc19fb commit d6225c9

File tree

6 files changed

+62
-61
lines changed

6 files changed

+62
-61
lines changed

common/constants.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,21 @@ const (
1414
)
1515

1616
var IconMap = make(map[int]*walk.Bitmap)
17-
var BrowserMap = make(map[string]bool)
17+
var BrowserMap = map[string]bool{
18+
"chrome.exe": true,
19+
"firefox.exe": true,
20+
"opera.exe": true,
21+
"iexplore.exe": true,
22+
"microsoftedge.exe": true,
23+
"microsoftedgecp.exe": true,
24+
"sogouexplorer.exe": true,
25+
"qqbrowser.exe": true,
26+
"360se.exe": true,
27+
"360chrome.exe": true,
28+
"liebao.exe": true,
29+
"maxthon.exe": true,
30+
"ucbrowser.exe": true,
31+
}
1832

1933
func Init() {
2034
IconMap[ICON_ADD], _ = walk.NewBitmapFromFile("res/add.png")
@@ -25,18 +39,4 @@ func Init() {
2539
IconMap[ICON_PLUS], _ = walk.NewBitmapFromFile("res/plus.png")
2640
IconMap[ICON_REFRESH], _ = walk.NewBitmapFromFile("res/refresh.png")
2741
IconMap[ICON_SETTING], _ = walk.NewBitmapFromFile("res/setting.png")
28-
29-
BrowserMap["chrome.exe"] = true
30-
BrowserMap["firefox.exe"] = true
31-
BrowserMap["opera.exe"] = true
32-
BrowserMap["iexplore.exe"] = true
33-
BrowserMap["microsoftedge.exe"] = true
34-
BrowserMap["microsoftedgecp.exe"] = true
35-
BrowserMap["sogouexplorer.exe"] = true
36-
BrowserMap["qqbrowser.exe"] = true
37-
BrowserMap["360se.exe"] = true
38-
BrowserMap["360chrome.exe"] = true
39-
BrowserMap["liebao.exe"] = true
40-
BrowserMap["maxthon.exe"] = true
41-
BrowserMap["ucbrowser.exe"] = true
4242
}

core/core.go

+18-29
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,58 @@ const (
1616

1717
// var batcher *Batcher
1818

19-
func FireHostsSwitch() {
19+
func FireHostsSwitch() bool {
2020
common.Info("============================== Fire hosts switch ==============================")
2121
// if batcher != nil {
2222
// batcher.Close()
2323
// }
24-
doProcess()
24+
return doProcess()
2525
// batcher = initSystemHostsWatcher()
2626
// go startSystemHostsWatcher()
2727
}
2828

2929
// 1. Find collection of same domain names between system hosts and currentHostIndex
3030
// 2. Disconnect the TCP connections(http:80 & https:443) of collection found above
31-
func doProcess() {
32-
overlapHostConfigMap := getHostsIpMap()
31+
func doProcess() bool {
32+
success := true
33+
hostsIpMap := getHostsIpMap()
3334
overwriteSystemHosts()
34-
common.Info("overlapHostConfigMap: %v", overlapHostConfigMap)
35-
if len(overlapHostConfigMap) == 0 {
36-
return
37-
}
3835
table := getTCPTable()
3936
for i := uint32(0); i < uint32(table.dwNumEntries); i++ {
4037
row := table.table[i]
38+
ip := row.displayIP(row.dwRemoteAddr)
39+
port := row.displayPort(row.dwRemotePort)
4140
if row.dwOwningPid <= 0 {
4241
continue
4342
}
44-
ip := row.displayIP(row.dwRemoteAddr)
45-
port := row.displayPort(row.dwRemotePort)
46-
if _, ok := overlapHostConfigMap[ip]; !ok {
43+
if port != 80 && port != 443 {
4744
continue
4845
}
49-
if common.BrowserMap[OpenProcess(uint32(row.dwOwningPid))] || port == 80 || port == 443 {
46+
supported := common.BrowserMap[strings.ToLower(OpenProcess(uint32(row.dwOwningPid)))]
47+
if hostsIpMap[ip] || supported {
5048
if err := CloseTCPEntry(row); err != nil {
5149
common.Error("Fail to close TCP connections: Pid = %v, Addr = %v:%v\n", row.dwOwningPid, ip, port)
50+
success = false
5251
} else {
5352
common.Info("Succeed to close TCP connections: Pid = %v, Addr = %v:%v", row.dwOwningPid, ip, port)
5453
}
5554
}
5655
}
56+
return success
5757
}
5858

5959
// find all the ip of system and current hosts
6060
func getHostsIpMap() map[string]bool {
6161
ipMap := make(map[string]bool)
62-
for k := range readHostConfigMap(systemHosts) {
63-
ipMap[k] = true
62+
for _, v := range readHostConfigMap(systemHosts) {
63+
ipMap[v] = true
6464
}
6565
model := conf.Config.HostConfigModel
6666
index := conf.Config.CurrentHostIndex
6767
if length := len(model.Roots); index >= 0 && length > 0 && index < length {
6868
path := "conf/hosts/" + model.RootAt(index).Text() + ".hosts"
69-
for k := range readHostConfigMap(path) {
70-
ipMap[k] = true
69+
for _, v := range readHostConfigMap(path) {
70+
ipMap[v] = true
7171
}
7272
}
7373
return ipMap
@@ -152,29 +152,18 @@ func readHostConfigMap(path string) map[string]string {
152152
for scanner.Scan() {
153153
line := strings.TrimSpace(scanner.Text())
154154
if line != "" && !strings.HasPrefix(line, "#") {
155-
config := trimDuplicateSpaces(line)
155+
config := strings.Fields(line)
156156
if len(config) == 2 {
157157
hostConfigMap[config[1]] = config[0]
158158
}
159159
}
160160
}
161161
if err := scanner.Err(); err != nil {
162-
common.Info("Fail to read system_hosts: %s", err)
162+
common.Error("Fail to read system_hosts: %s", err)
163163
}
164164
return hostConfigMap
165165
}
166166

167-
func trimDuplicateSpaces(line string) []string {
168-
temp := []string{}
169-
line = strings.TrimSpace(line)
170-
for _, v := range strings.SplitN(line, " ", 2) {
171-
if trimed := strings.TrimSpace(v); trimed != "" {
172-
temp = append(temp, trimed)
173-
}
174-
}
175-
return temp
176-
}
177-
178167
// func initSystemHostsWatcher() *Batcher {
179168
// batcher, err := New(time.Millisecond * 300)
180169
// if err != nil {

core/sys_process.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func OpenProcess(pid uint32) string {
5050
var dwDesiredAccess uint32 = 0x0400 | 0x0010
5151
openPid, _, _ := procOpenProcess.Call(uintptr(unsafe.Pointer(&dwDesiredAccess)), 0, uintptr(pid))
5252
if openPid <= 0 {
53-
common.Info("Fail to open process: Pid = %v", pid)
53+
common.Error("Fail to open process: Pid = %v", pid)
5454
return ""
5555
}
5656
defer closeHandle(openPid)
@@ -71,7 +71,13 @@ func getProcessName(pid uintptr) string {
7171
var cbNeeded uint32 = 1024
7272
var processName = make([]byte, cbNeeded)
7373
procQueryFullProcessImageName.Call(uintptr(pid), 0, uintptr(unsafe.Pointer(&processName[0])), uintptr(unsafe.Pointer(&cbNeeded)))
74-
return string(processName[0:cbNeeded])
74+
processPath := string(processName[0:cbNeeded])
75+
for i := len(processPath) - 2; i >= 0; i-- {
76+
if processPath[i] == '\\' {
77+
return processPath[i+1:]
78+
}
79+
}
80+
return processPath
7581
}
7682

7783
func getProcessName2(pid uintptr) string {

gui/mainwindow.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type widgetContext struct {
1818
hostConfigText *walk.TextEdit
1919
addButton *walk.Action
2020
deleteButton *walk.Action
21+
notifyIcon *walk.NotifyIcon
2122
}
2223

2324
// InitMainWindow initialize main window
@@ -43,12 +44,13 @@ func InitMainWindow() {
4344
}
4445
setWindowIcon(context.mw)
4546
setXY(context.mw)
46-
newNotify(context.mw)
47+
newNotify()
4748
setTreeViewBackground(context.treeView)
4849
showCurrentItem(context.hostConfigText)
4950
context.mw.Closing().Attach(func(canceled *bool, reason walk.CloseReason) {
5051
*canceled = true
5152
context.mw.Hide()
53+
context.notifyIcon.ShowCustom("当前程序最小化到托盘", "Best hosts manager ever")
5254
})
5355
(*context.deleteButton).SetEnabled(false)
5456
context.mw.Run()

gui/notify.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@ import (
55
"github.com/tinycedar/lily/common"
66
)
77

8-
func newNotify(mw *walk.MainWindow) {
9-
ni, err := walk.NewNotifyIcon()
8+
func newNotify() {
9+
var err error
10+
context.notifyIcon, err = walk.NewNotifyIcon()
1011
if err != nil {
1112
common.Error("Error invoking NewNotifyIcon: %v", err)
1213
}
1314
icon, _ := walk.NewIconFromFile("res/lily.ico")
14-
if err := ni.SetIcon(icon); err != nil {
15+
if err := context.notifyIcon.SetIcon(icon); err != nil {
1516
common.Error("Error setting notify icon: %v", err)
1617
}
17-
if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
18+
if err := context.notifyIcon.SetToolTip("Click for info or use the context menu to exit."); err != nil {
1819
common.Error("Fail to set tooltip: %v", err)
1920
}
20-
ni.MouseUp().Attach(func(x, y int, button walk.MouseButton) {
21+
context.notifyIcon.MouseUp().Attach(func(x, y int, button walk.MouseButton) {
2122
if button == walk.LeftButton {
22-
if !mw.Visible() {
23-
mw.Show()
23+
if !context.mw.Visible() {
24+
context.mw.Show()
2425
} else {
2526
//TODO
2627
}
2728
}
28-
// if err := ni.ShowCustom(
29+
// if err := context.notifyIcon.ShowCustom(
2930
// "Walk NotifyIcon Example",
3031
// "There are multiple ShowX methods sporting different icons."); err != nil {
3132
// common.Error("Fail to show custom notify: %v", err)
@@ -36,16 +37,16 @@ func newNotify(mw *walk.MainWindow) {
3637
common.Error("Error setting exitAction text: %v", err)
3738
}
3839
exitAction.Triggered().Attach(func() {
39-
defer ni.Dispose()
40+
defer context.notifyIcon.Dispose()
4041
walk.App().Exit(0)
4142
})
42-
if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
43+
if err := context.notifyIcon.ContextMenu().Actions().Add(exitAction); err != nil {
4344
common.Error("Error Adding exitAction: %v", err)
4445
}
45-
if err := ni.SetVisible(true); err != nil {
46+
if err := context.notifyIcon.SetVisible(true); err != nil {
4647
common.Error("Error setting notify visible: %v", err)
4748
}
48-
// if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
49+
// if err := context.notifyIcon.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
4950
// common.Error("Error showing info: %v", err)
5051
// }
5152
}

gui/treeview.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ func newTreeView() TreeView {
4848
} else {
4949
ioutil.WriteFile("conf/config.json", configJSON, os.ModeExclusive)
5050
}
51-
// fire hosts switch
52-
core.FireHostsSwitch()
51+
if core.FireHostsSwitch() {
52+
context.notifyIcon.ShowCustom("切换"+current.Text()+"成功!", "Best hosts manager ever")
53+
} else {
54+
context.notifyIcon.ShowError("切换"+current.Text()+"失败!", "Best hosts manager ever")
55+
}
5356
},
5457
}
5558
}

0 commit comments

Comments
 (0)