-
Notifications
You must be signed in to change notification settings - Fork 54
/
windows.go
42 lines (34 loc) · 1.15 KB
/
windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package helper
import (
"fmt"
"os/exec"
)
func SetCoreAffinity(pid int, value uint) error {
args := []string{fmt.Sprintf("$Process = Get-Process -Id %d; $Process.ProcessorAffinity=%d", pid, value)}
cmd := exec.Command("PowerShell", args...)
return cmd.Start()
}
func SetCpuPriority(pid int, p uint) error {
args := []string{"process", "where", fmt.Sprintf("ProcessId=%d", pid), "call", "setpriority", fmt.Sprintf("%d", p)}
cmd := exec.Command("wmic", args...)
return cmd.Start()
}
func AddFirewallRules(pid, tcp, udp int) error {
if err := addFWRule(pid, tcp, "TCP"); err != nil {
return err
}
return addFWRule(pid, udp, "UDP")
}
func addFWRule(pid, port int, t string) error {
args := []string{
"advfirewall", "firewall", "add", "rule", fmt.Sprintf("name=\"ACCSERVER_%d\"", pid),
"dir=in", "action=allow", fmt.Sprintf("protocol=%s", t), fmt.Sprintf("localport=%d", port),
}
cmd := exec.Command("netsh.exe", args...)
return cmd.Start()
}
func DelFirewallRules(pid int) error {
args := []string{"advfirewall", "firewall", "del", "rule", fmt.Sprintf("name=\"ACCSERVER_%d\"", pid)}
cmd := exec.Command("netsh.exe", args...)
return cmd.Start()
}