-
Notifications
You must be signed in to change notification settings - Fork 76
/
main.go
193 lines (175 loc) · 4.71 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
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
/*
Copyright 2023 AmidaWare Inc.
Licensed under the Tactical RMM License Version 1.0 (the “License”).
You may only use the Licensed Software in accordance with the License.
A copy of the License is available at:
https://license.tacticalrmm.com
*/
package main
import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"github.com/amidaware/rmmagent/agent"
"github.com/kardianos/service"
"github.com/sirupsen/logrus"
)
var (
version = "2.8.0"
log = logrus.New()
logFile *os.File
)
func main() {
ver := flag.Bool("version", false, "Prints version")
mode := flag.String("m", "", "The mode to run")
taskPK := flag.Int("p", 0, "Task PK")
logLevel := flag.String("log", "INFO", "The log level")
logTo := flag.String("logto", "file", "Where to log to")
api := flag.String("api", "", "API URL")
clientID := flag.Int("client-id", 0, "Client ID")
siteID := flag.Int("site-id", 0, "Site ID")
timeout := flag.Duration("timeout", 900, "Installer timeout (seconds)")
desc := flag.String("desc", "", "Agent's Description")
atype := flag.String("agent-type", "server", "server or workstation")
token := flag.String("auth", "", "Token")
power := flag.Bool("power", false, "Disable sleep/hibernate")
rdp := flag.Bool("rdp", false, "Enable RDP")
ping := flag.Bool("ping", false, "Enable ping")
localMesh := flag.String("local-mesh", "", "Path to mesh executable")
noMesh := flag.Bool("nomesh", false, "Do not install mesh agent")
meshDir := flag.String("meshdir", "", "Path to custom meshcentral dir")
meshNodeID := flag.String("meshnodeid", "", "Mesh Node ID")
cert := flag.String("cert", "", "Path to domain CA .pem")
silent := flag.Bool("silent", false, "Do not popup any message boxes during installation")
proxy := flag.String("proxy", "", "Use a http proxy")
insecure := flag.Bool("insecure", false, "Insecure for testing only")
natsport := flag.String("natsport", "", "nats standard port")
flag.Parse()
if *ver {
agent.ShowVersionInfo(version)
return
}
if len(os.Args) == 1 {
switch runtime.GOOS {
case "windows":
agent.ShowStatus(version)
default:
agent.ShowVersionInfo(version)
}
return
}
setupLogging(logLevel, logTo)
defer logFile.Close()
a := *agent.New(log, version)
if *mode == "install" {
a.Logger.SetOutput(os.Stdout)
}
a.Logger.Debugf("%+v\n", a)
switch *mode {
case "getenv":
fmt.Println(os.Getenv(flag.Arg(0)))
case "nixmeshnodeid":
fmt.Print(a.NixMeshNodeID())
case "installsvc":
a.InstallService()
case "checkin":
a.DoNatsCheckIn()
case "rpc":
a.RunRPC()
case "svc":
if runtime.GOOS == "windows" {
s, _ := service.New(&a, a.ServiceConfig)
s.Run()
} else {
a.RunRPC()
}
case "pk":
fmt.Println(a.AgentPK)
case "winagentsvc":
fmt.Println("deprecated. use 'svc'")
case "runchecks":
a.RunChecks(true)
case "checkrunner":
a.RunChecks(false)
case "software":
a.SendSoftware()
case "cleanup":
a.UninstallCleanup()
case "publicip":
fmt.Println(a.PublicIP())
case "getpython":
a.GetPython(true)
case "installdeno":
a.InstallDeno(true)
case "installnushell":
a.InstallNushell(true)
case "runmigrations":
a.RunMigrations()
case "recovermesh":
a.RecoverMesh()
case "macventurafix":
a.FixVenturaMesh()
case "taskrunner":
if len(os.Args) < 5 || *taskPK == 0 {
return
}
a.RunTask(*taskPK)
case "install":
if runtime.GOOS != "windows" {
u, err := user.Current()
if err != nil {
log.Fatalln(err)
}
if u.Uid != "0" {
log.Fatalln("must run as root")
}
}
if *api == "" || *clientID == 0 || *siteID == 0 || *token == "" {
return
}
a.Install(&agent.Installer{
RMM: *api,
ClientID: *clientID,
SiteID: *siteID,
Description: *desc,
AgentType: *atype,
Power: *power,
RDP: *rdp,
Ping: *ping,
Token: *token,
LocalMesh: *localMesh,
Cert: *cert,
Proxy: *proxy,
Timeout: *timeout,
Silent: *silent,
NoMesh: *noMesh,
MeshDir: *meshDir,
MeshNodeID: *meshNodeID,
Insecure: *insecure,
NatsStandardPort: *natsport,
})
default:
agent.ShowStatus(version)
}
}
func setupLogging(level, to *string) {
ll, err := logrus.ParseLevel(*level)
if err != nil {
ll = logrus.InfoLevel
}
log.SetLevel(ll)
if *to == "stdout" {
log.SetOutput(os.Stdout)
} else {
switch runtime.GOOS {
case "windows":
logFile, _ = os.OpenFile(filepath.Join(os.Getenv("ProgramFiles"), "TacticalAgent", "agent.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
default:
logFile, _ = os.OpenFile(filepath.Join("/var/log/", "tacticalagent.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
}
log.SetOutput(logFile)
}
}