func (t *MIB_TCPTABLE2) String() string { common.Info("================ tcp table ======================= %v", t.dwNumEntries) for i := uint32(0); i < uint32(t.dwNumEntries); i++ { row := t.table[i] common.Info("%v\t%v:%v", row, row.displayIP(row.dwRemoteAddr), row.displayPort(row.dwRemotePort)) } common.Info("================ tcp table end =======================") return "=======================================" }
func doProcess() bool { success := true hostsIPMap := getHostsIpMap() overwriteSystemHosts() processNameMap := GetProcessNameMap() table := getTCPTable() for i := uint32(0); i < uint32(table.dwNumEntries); i++ { row := table.table[i] ip := row.displayIP(row.dwRemoteAddr) port := row.displayPort(row.dwRemotePort) if row.dwOwningPid <= 0 { continue } if port != 80 && port != 443 { continue } process := strings.ToLower(processNameMap[uint32(row.dwOwningPid)]) if hostsIPMap[ip] || common.BrowserMap[process] { if err := CloseTCPEntry(row); err != nil { common.Error("Fail to close TCP connections: Process = %v, Pid = %v, Addr = %v:%v", process, row.dwOwningPid, ip, port) success = false } else { common.Info("Succeed to close TCP connections: Process = %v, Pid = %v, Addr = %v:%v", process, row.dwOwningPid, ip, port) } } } return success }
func FireHostsSwitch() bool { common.Info("============================== Fire hosts switch ==============================") // if batcher != nil { // batcher.Close() // } return doProcess() // batcher = initSystemHostsWatcher() // go startSystemHostsWatcher() }
func ReadCurrentHostConfig() []byte { model := conf.Config.HostConfigModel index := conf.Config.CurrentHostIndex if length := len(model.Roots); index < 0 || length <= 0 || index >= length { return nil } bytes, err := ioutil.ReadFile("conf/hosts/" + model.RootAt(index).Text() + ".hosts") if err != nil { common.Info("Error reading host config: %v", err) return nil } return bytes }
func watchPidFile() { batcher, err := New(time.Millisecond * 50) if err != nil { common.Error("Fail to initialize batcher") return } if err := batcher.Add(pidFilePath); err != nil { common.Error("Fail to add pid file: %s", pidFilePath) return } for events := range batcher.Events { for _, event := range events { if event.Op&fsnotify.Write == fsnotify.Write { common.Info("modified file: %v", event) trigger <- true break } } } }
func readHostConfigMap(path string) map[string]string { hostConfigMap := make(map[string]string) file, err := os.Open(path) if err != nil { common.Info("Fail to open system_hosts: %s", err) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) if line != "" && !strings.HasPrefix(line, "#") { config := strings.Fields(line) if len(config) == 2 { hostConfigMap[config[1]] = config[0] } } } if err := scanner.Err(); err != nil { common.Error("Fail to read system_hosts: %s", err) } return hostConfigMap }
func metrics(funcName string) func(now time.Time) { return func(now time.Time) { common.Info("Processing [%v] costs %v", funcName, time.Since(now)) } }
func newToolBar() ToolBar { tb := ToolBar{ ButtonStyle: ToolBarButtonImageBeforeText, Items: []MenuItem{ Action{ AssignTo: &(context.addButton), Text: "新增", Image: "res/add.png", // Enabled: Bind("isSpecialMode && enabledCB.Checked"), OnTriggered: func() { var dlg *walk.Dialog var hostsNameEdit *walk.LineEdit Dialog{ AssignTo: &dlg, Title: "新增", MinSize: Size{300, 150}, Layout: VBox{}, Children: []Widget{ Composite{ Layout: Grid{Columns: 2}, Children: []Widget{ Label{ ColumnSpan: 2, Text: "Hosts名字:", }, LineEdit{ AssignTo: &hostsNameEdit, ColumnSpan: 2, // Text: Bind("PatienceField"), }, }, }, HSpacer{}, Composite{ Layout: HBox{}, Children: []Widget{ PushButton{ // AssignTo: &acceptPB, Text: "确定", OnClicked: func() { // if err := db.Submit(); err != nil { // log.Print(err) // return // } item := &model.HostConfigItem{Name: hostsNameEdit.Text(), Icon: common.IconMap[common.ICON_NEW]} conf.Config.HostConfigModel.Insert(item) context.treeView.SetCurrentItem(item) dlg.Accept() }, }, PushButton{ // AssignTo: &cancelPB, Text: "取消", OnClicked: func() { dlg.Cancel() }, }, }, }, }, }.Run(context.mw) }, }, //FIXME 去除刷新按钮是因为点击以后, 双击hosts不再生效 // Action{ // Text: "刷新", // Image: "res/refresh.png", // // Enabled: Bind("isSpecialMode && enabledCB.Checked"), // OnTriggered: func() { // conf.Load() // }, // }, // Action{ // Text: "修改", // Image: "res/pencil.png", // // Enabled: Bind("isSpecialMode && enabledCB.Checked"), // // OnTriggered: mw.specialAction_Triggered, // }, Action{ AssignTo: &(context.deleteButton), Text: "删除", Image: "res/delete.png", // Enabled: Bind("isSpecialMode && enabledCB.Checked"), OnTriggered: func() { if context.treeView.CurrentItem() == nil { walk.MsgBox(context.mw, "删除hosts", "请选择左边列表后再删除", walk.MsgBoxIconInformation) context.deleteButton.SetEnabled(false) return } current := context.treeView.CurrentItem().(*model.HostConfigItem) message := fmt.Sprintf("确定要删除hosts '%s'?", current.Text()) ret := walk.MsgBox(context.mw, "删除hosts", message, walk.MsgBoxYesNo) if ret == win.IDYES { if !conf.Config.HostConfigModel.Remove(current) { common.Error("Fail to remove current item: %v", current.Text()) // TODO notify user return } if context.treeView.Model().RootCount() > 0 { context.treeView.SetCurrentItem(context.treeView.Model().RootAt(0)) } file := "conf/hosts/" + current.Text() + ".hosts" if err := os.Remove(file); err != nil { common.Error("Fail to delete file: %s", file) // TODO notify user return } common.Info("Succeed to delete file: %s", file) } }, }, }, } return tb }