Skip to content

Commit e6ecfc5

Browse files
committed
🎨 Support one-click addition of Microsoft Defender exclusions #13650
1 parent 10d5d4f commit e6ecfc5

File tree

8 files changed

+103
-1
lines changed

8 files changed

+103
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Binaries for programs and plugins
22
*.exe
3+
!app/elevator/elevator-*.exe
34
*.dll
45
*.so
56
*.dylib

app/electron-builder-arm64.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@ extraResources:
6969
filter: "!**/{.DS_Store,custom.css}"
7070
- from: "src/assets/fonts"
7171
to: "appearance/fonts"
72-
filter: "!**/{.DS_Store}"
72+
filter: "!**/{.DS_Store}"
73+
- from: "elevator/elevator-arm64.exe"
74+
to: "elevator.exe"

app/electron-builder.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,5 @@ extraResources:
7171
filter: "!**/{.DS_Store}"
7272
- from: "pandoc/pandoc-windows-amd64.zip"
7373
to: "pandoc.zip"
74+
- from: "elevator/elevator-amd64.exe"
75+
to: "elevator.exe"

app/elevator/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/siyuan-note/elevator

app/elevator/elevator-amd64.exe

3.02 MB
Binary file not shown.

app/elevator/elevator-arm64.exe

2.87 MB
Binary file not shown.

kernel/conf/system.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type System struct {
4545
LockScreenMode int `json:"lockScreenMode"` // 0:手动,1:手动+跟随系统 https://github.com/siyuan-note/siyuan/issues/9087
4646

4747
DisabledFeatures []string `json:"disabledFeatures"`
48+
49+
MicrosoftDefenderExcluded bool `json:"microsoftDefenderExcluded"` // 是否已加入 Microsoft Defender 排除项 https://github.com/siyuan-note/siyuan/issues/13650
4850
}
4951

5052
func NewSystem() *System {

kernel/model/elevator.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)