-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (62 loc) · 1.45 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
)
func tutorial() {
fmt.Println("[~] First argument should be range in 41 and 100")
fmt.Println("[~] Example:")
fmt.Println(os.Args[0], "50")
fmt.Println(os.Args[0], "80")
fmt.Println(os.Args[0], "100")
}
func currentLimit() string {
defer rec()
currentLimitByte, err := ioutil.ReadFile("/sys/devices/platform/huawei-wmi/charge_control_thresholds")
if err != nil {
fmt.Println("[!] Read error :", err)
os.Exit(127)
}
currentLimit := fmt.Sprintf("%s", currentLimitByte)
currentLimit = strings.Trim(currentLimit, "\n")
currentLimit = strings.Split(currentLimit, " ")[1]
return currentLimit
}
func rec() {
if r := recover(); r != nil {
tutorial()
}
}
func main() {
defer rec()
param := os.Args[1]
match, err := regexp.MatchString("^([0-9]{2,3})$", param)
if err != nil {
fmt.Println("[!] Limit should be between 41-100")
os.Exit(1)
}
if match {
control, _ := strconv.Atoi(param)
if control > 40 && control <= 100 {
arg := []byte("40 " + param)
current := currentLimit()
err := os.WriteFile("/sys/devices/platform/huawei-wmi/charge_control_thresholds", arg, 0644)
if err != nil {
fmt.Println("[!] Write error :", err)
os.Exit(127)
} else {
fmt.Printf("[+] Limit changed from %s to %s\n", current, param)
}
} else {
fmt.Println("[!] Limit should be between 41-100")
os.Exit(1)
}
} else {
tutorial()
os.Exit(1)
}
}