mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🎨 Support one-click addition of Microsoft Defender exclusions https://github.com/siyuan-note/siyuan/issues/13650
This commit is contained in:
parent
15d37ce036
commit
6145ffe821
2 changed files with 97 additions and 76 deletions
|
|
@ -14,84 +14,10 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build windows
|
||||
//go:build !windows
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
|
||||
"github.com/88250/gulu"
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
func processMicrosoftDefender() {
|
||||
if !gulu.OS.IsWindows() || Conf.System.MicrosoftDefenderExcluded {
|
||||
return
|
||||
}
|
||||
|
||||
elevator := filepath.Join(util.WorkingDir, "elevator.exe")
|
||||
if "dev" == util.Mode || !gulu.File.IsExist(elevator) {
|
||||
elevator = filepath.Join(util.WorkingDir, "elevator", "elevator-"+runtime.GOARCH+".exe")
|
||||
}
|
||||
|
||||
if !gulu.File.IsExist(elevator) {
|
||||
logging.LogWarnf("not found elevator [%s]", elevator)
|
||||
return
|
||||
}
|
||||
|
||||
if !isUsingMicrosoftDefender() {
|
||||
return
|
||||
}
|
||||
|
||||
installPath := filepath.Dir(util.WorkingDir)
|
||||
|
||||
if isAdmin() {
|
||||
cmd := exec.Command("powershell", "-Command", "Add-MpPreference", "-ExclusionPath", installPath, ",", util.WorkspaceDir)
|
||||
gulu.CmdAttr(cmd)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if nil != err {
|
||||
logging.LogErrorf("add Windows Defender exclusion path [%s] failed: %s, %s", installPath, err, string(output))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
cwd, _ := os.Getwd()
|
||||
args := strings.Join([]string{"powershell", "-Command", "Add-MpPreference", "-ExclusionPath", installPath, ",", util.WorkspaceDir}, " ")
|
||||
verbPtr, _ := syscall.UTF16PtrFromString("runas")
|
||||
exePtr, _ := syscall.UTF16PtrFromString(elevator)
|
||||
cwdPtr, _ := syscall.UTF16PtrFromString(cwd)
|
||||
argPtr, _ := syscall.UTF16PtrFromString(args)
|
||||
err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, 1)
|
||||
if err != nil {
|
||||
logging.LogErrorf("add Windows Defender exclusion path [%s] failed: %s", installPath, err)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO Conf.System.MicrosoftDefenderExcluded = true
|
||||
Conf.Save()
|
||||
}
|
||||
|
||||
func isUsingMicrosoftDefender() bool {
|
||||
if !gulu.OS.IsWindows() {
|
||||
return false
|
||||
}
|
||||
|
||||
cmd := exec.Command("powershell", "-Command", "Get-MpPreference")
|
||||
gulu.CmdAttr(cmd)
|
||||
return cmd.Run() == nil
|
||||
}
|
||||
|
||||
func isAdmin() bool {
|
||||
_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
|
||||
return err == nil
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue