|
| 1 | +// SiYuan - Refactor your thinking |
| 2 | +// Copyright (c) 2020-present, b3log.org |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package model |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/siyuan-note/logging" |
| 21 | + "github.com/siyuan-note/siyuan/kernel/util" |
| 22 | + "golang.org/x/sys/windows" |
| 23 | + "os" |
| 24 | + "os/exec" |
| 25 | + "path/filepath" |
| 26 | + "runtime" |
| 27 | + "strings" |
| 28 | + "syscall" |
| 29 | + |
| 30 | + "github.com/88250/gulu" |
| 31 | +) |
| 32 | + |
| 33 | +func processMicrosoftDefender() { |
| 34 | + if !gulu.OS.IsWindows() || Conf.System.MicrosoftDefenderExcluded { |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + elevator := filepath.Join(util.WorkingDir, "elevator.exe") |
| 39 | + if "dev" == util.Mode || !gulu.File.IsExist(elevator) { |
| 40 | + elevator = filepath.Join(util.WorkingDir, "elevator", "elevator-"+runtime.GOARCH+".exe") |
| 41 | + } |
| 42 | + |
| 43 | + if !gulu.File.IsExist(elevator) { |
| 44 | + logging.LogWarnf("not found elevator [%s]", elevator) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if !isUsingMicrosoftDefender() { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + installPath := filepath.Dir(util.WorkingDir) |
| 53 | + |
| 54 | + if isAdmin() { |
| 55 | + cmd := exec.Command("powershell", "-Command", "Add-MpPreference", "-ExclusionPath", installPath, ",", util.WorkspaceDir) |
| 56 | + gulu.CmdAttr(cmd) |
| 57 | + output, err := cmd.CombinedOutput() |
| 58 | + if nil != err { |
| 59 | + logging.LogErrorf("add Windows Defender exclusion path [%s] failed: %s, %s", installPath, err, string(output)) |
| 60 | + return |
| 61 | + } |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + cwd, _ := os.Getwd() |
| 66 | + args := strings.Join([]string{"powershell", "-Command", "Add-MpPreference", "-ExclusionPath", installPath, ",", util.WorkspaceDir}, " ") |
| 67 | + verbPtr, _ := syscall.UTF16PtrFromString("runas") |
| 68 | + exePtr, _ := syscall.UTF16PtrFromString(elevator) |
| 69 | + cwdPtr, _ := syscall.UTF16PtrFromString(cwd) |
| 70 | + argPtr, _ := syscall.UTF16PtrFromString(args) |
| 71 | + err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, 1) |
| 72 | + if err != nil { |
| 73 | + logging.LogErrorf("add Windows Defender exclusion path [%s] failed: %s", installPath, err) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // TODO Conf.System.MicrosoftDefenderExcluded = true |
| 78 | + Conf.Save() |
| 79 | +} |
| 80 | + |
| 81 | +func isUsingMicrosoftDefender() bool { |
| 82 | + if !gulu.OS.IsWindows() { |
| 83 | + return false |
| 84 | + } |
| 85 | + |
| 86 | + cmd := exec.Command("powershell", "-Command", "Get-MpPreference") |
| 87 | + gulu.CmdAttr(cmd) |
| 88 | + return cmd.Run() == nil |
| 89 | +} |
| 90 | + |
| 91 | +func isAdmin() bool { |
| 92 | + _, err := os.Open("\\\\.\\PHYSICALDRIVE0") |
| 93 | + return err == nil |
| 94 | +} |
0 commit comments