-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
202 lines (181 loc) · 5.76 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
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"io/ioutil"
"os/exec"
"runtime"
"strings"
"sync"
)
// --> RunCommand executes a shell command
func RunCommand(cmd string) (string, error) {
fmt.Printf("Running command: %s\n", cmd)
output, err := exec.Command("bash", "-c", cmd).CombinedOutput()
if err != nil {
return "", fmt.Errorf("error running command: %v, output: %s", err, string(output))
}
return string(output), nil
}
// --> Detects the package manager based on the system
func DetectPackageManager() string {
packageManagers := []string{"apt", "yum", "zypper", "dnf", "apk", "pacman"}
for _, pm := range packageManagers {
if _, err := exec.LookPath(pm); err == nil {
return pm
}
}
return ""
}
// --> Installs a package using the detected package manager
func InstallPackage(packageName, manager string) error {
var cmd string
switch manager {
case "apt":
cmd = fmt.Sprintf("sudo apt install -y %s", packageName)
case "dnf", "yum":
cmd = fmt.Sprintf("sudo %s install -y %s", manager, packageName)
case "pacman":
cmd = fmt.Sprintf("sudo pacman -S --noconfirm %s", packageName)
case "zypper":
cmd = fmt.Sprintf("sudo zypper install -y %s", packageName)
case "apk":
cmd = fmt.Sprintf("sudo apk add %s", packageName)
}
_, err := RunCommand(cmd)
return err
}
// --> Installs a Go tool using `go install`
func InstallGoTool(tool, packageName string) error {
fmt.Printf("Installing Go tool: %s\n", tool)
_, err := RunCommand(fmt.Sprintf("go install %s", packageName))
if err != nil {
return fmt.Errorf("failed to install Go tool %s: %w", tool, err)
}
return nil
}
// --> Installs a tool either through a package manager or directly from source
func InstallTool(name, installCmd, checkCmd string) {
if _, err := exec.LookPath(checkCmd); err != nil {
fmt.Printf("Installing %s...\n", name)
if _, err := RunCommand(installCmd); err != nil {
fmt.Printf("Failed to install %s: %v\n", name, err)
} else {
fmt.Printf("%s installed successfully\n", name)
}
} else {
fmt.Printf("Found %s\n", name)
}
}
// --> Checks if the system is WSL (Windows Subsystem for Linux)
func CheckWSL() bool {
if runtime.GOOS == "linux" {
data, err := ioutil.ReadFile("/proc/version")
if err != nil {
return false
}
return strings.Contains(string(data), "Microsoft")
}
return false
}
// --> Updates and upgrades the system using the detected package manager
func UpdateUpgradeSystem(packageManager string) error {
fmt.Println("Updating and upgrading the system...")
switch packageManager {
case "apt":
_, _ = RunCommand("sudo apt update && sudo apt upgrade -y")
case "dnf", "yum":
_, _ = RunCommand(fmt.Sprintf("sudo %s update -y", packageManager))
case "pacman":
_, _ = RunCommand("sudo pacman -Syu --noconfirm")
case "zypper":
_, _ = RunCommand("sudo zypper update -y")
case "apk":
_, _ = RunCommand("sudo apk update && sudo apk upgrade")
default:
return fmt.Errorf("unsupported package manager: %s", packageManager)
}
fmt.Println("System updated and upgraded successfully.")
return nil
}
// --> pip is installed on the system
func EnsurePipInstalled(packageManager string) error {
if _, err := exec.LookPath("pip3"); err != nil {
fmt.Println("pip is not installed. Installing pip...")
switch packageManager {
case "apt":
_, _ = RunCommand("sudo apt install -y python3-pip")
case "dnf", "yum":
_, _ = RunCommand(fmt.Sprintf("sudo %s install -y python3-pip", packageManager))
case "pacman":
_, _ = RunCommand("sudo pacman -S --noconfirm python-pip")
case "zypper":
_, _ = RunCommand("sudo zypper install -y python3-pip")
case "apk":
_, _ = RunCommand("sudo apk add py3-pip")
}
fmt.Println("pip installed successfully.")
return nil
}
fmt.Println("pip is already installed.")
return nil
}
func main() {
isWSL := CheckWSL()
if isWSL {
fmt.Println("Detected Windows Subsystem for Linux (WSL)")
}
// --> Detect the package manager
packageManager := DetectPackageManager()
if packageManager == "" {
fmt.Println("Unable to detect package manager. Please install packages manually.")
return
}
fmt.Printf("Detected package manager: %s\n", packageManager)
if isWSL {
if err := UpdateUpgradeSystem(packageManager); err != nil {
fmt.Println("Error updating system:", err)
return
}
}
if err := EnsurePipInstalled(packageManager); err != nil {
fmt.Println("Error ensuring pip installation:", err)
return
}
// --> Install tools (using InstallTool function for each)
tools := map[string][2]string{
"go": {"sudo apt install -y golang", "go"},
"node": {"sudo apt install -y nodejs", "node"},
"npm": {"sudo apt install -y npm", "npm"},
"jq": {"sudo apt install -y jq", "jq"},
"shodan": {"pip3 install shodan", "shodan"},
"paramspider": {"sudo apt install -y paramspider"},
}
for name, cmd := range tools {
InstallTool(name, cmd[0], cmd[1])
}
// --> Go tools
var wg sync.WaitGroup
goTools := map[string]string{
"nuclei": "github.com/projectdiscovery/nuclei/v2/cmd/nuclei@latest",
"dnsx": "github.com/projectdiscovery/dnsx/cmd/dnsx@latest",
"subfinder": "github.com/projectdiscovery/subfinder/v2/cmd/subfinder@latest",
"waybackurls": "github.com/tomnomnom/waybackurls@latest",
}
for tool, pkg := range goTools {
wg.Add(1)
go func(tool, pkg string) {
defer wg.Done()
if err := InstallGoTool(tool, pkg); err != nil {
fmt.Printf("Failed to install %s: %v\n", tool, err)
} else {
fmt.Printf("%s installed successfully.\n", tool)
}
}(tool, pkg)
}
// --> Wait for tools to finish
wg.Wait()
// --> paramspider
InstallTool("paramspider", "git clone https://github.com/devanshbatham/paramspider && cd paramspider && python3 setup.py install", "paramspider")
// --> httpx
InstallTool("httpx", "go get -u github.com/projectdiscovery/httpx/cmd/httpx", "httpx")
}