[Golang] 纯文本查看 复制代码
package main
import (
"fmt"
"strings"
"syscall"
"time"
"unsafe"
"github.com/shirou/gopsutil/process"
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
procEnumWindows = user32.NewProc("EnumWindows")
procGetWindowTextW = user32.NewProc("GetWindowTextW")
procPostMessage = user32.NewProc("PostMessageW")
)
const (
WM_CLOSE = 0x0010
)
func EnumWindows(enumFunc uintptr, lParam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lParam), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
r1, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r1)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func PostMessage(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) (err error) {
r1, _, e1 := syscall.Syscall6(procPostMessage.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wParam), uintptr(lParam), 0, 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func enumWindowsProc(hwnd syscall.Handle, lParam uintptr) uintptr {
const maxLen = 256
var buf [maxLen]uint16
_, err := GetWindowText(hwnd, &buf[0], maxLen)
if err == nil {
title := syscall.UTF16ToString(buf[:])
if containsGameKeywords(title) {
fmt.Printf("Closing window: %s\n", title)
PostMessage(hwnd, WM_CLOSE, 0, 0)
}
}
return 1
}
func containsGameKeywords(title string) bool {
keywords := []string{"游戏", "页游", "网游",
"手游",
"电竞",
"魔兽",
"迷你世界",
"精英",
"吃鸡",
"王者荣耀",
"和平精英",
"英雄联盟",
"穿越火线",
"绝地求生",
"堡垒之夜",
"使命召唤",
"守望先锋",
"魔兽世界",
"剑灵",
"梦幻西游",
"传奇",
"征途",
"天龙八部",
"问道",
"神武",
"大话西游",
"龙之谷",
"地下城与勇士",
"逆战",
"火影忍者",
"部落冲突",
"皇室战争",
"炉石传说",
"星际争霸",
"暗黑破坏神",
"魔域",
"传奇"}
for _, keyword := range keywords {
if strings.Contains(strings.ToLower(title), keyword) {
return true
}
}
return false
}
func main() {
go monitorBrowsers()
select {} // Keep the program running
}
func monitorBrowsers() {
for {
processes, err := process.Processes()
if err != nil {
fmt.Println("Error getting processes:", err)
continue
}
for _, p := range processes {
name, err := p.Name()
if err != nil {
continue
}
if isBrowserToBlock(name) {
fmt.Printf("Monitoring browser: %s\n", name)
monitorBrowserActivity(p)
}
}
time.Sleep(5 * time.Second) // Sleep for 5 seconds before next check
}
}
func isBrowserToBlock(name string) bool {
blockedBrowsers := []string{"firefox.exe", "opera.exe", "360se.exe", "chrome.exe", "iexplore.exe", "msedge.exe", "qqbrowser.exe"}
for _, browser := range blockedBrowsers {
if strings.EqualFold(name, browser) {
return true
}
}
return false
}
func monitorBrowserActivity(p *process.Process) {
// This function should monitor the browser activity and close it if it detects the keywords
// You can use Windows API or other tools to monitor the browser activity
// For simplicity, we just close the browser process if it contains the keywords in the window title
cb := syscall.NewCallback(enumWindowsProc)
EnumWindows(cb, 0)
}