-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhelper_funcs.go
More file actions
201 lines (170 loc) · 4.8 KB
/
helper_funcs.go
File metadata and controls
201 lines (170 loc) · 4.8 KB
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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/projectdiscovery/gologger"
)
func readFileContent(filePath string) (string, error) {
// Read the entire file content
content, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
// Convert the content to a string and return
return string(content), nil
}
// Write File
func createFile(filePath, content string) error {
// Extract the directory and filename from the provided file path
if strings.Contains(filePath, "/") {
dir := filepath.Dir(filePath)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
}
err := ioutil.WriteFile(filePath, []byte(content), 0644)
if err != nil {
return err
}
return nil
}
// execute bash shell commands
func ExecShell(command string) string {
out, err := exec.Command("bash", "-c", "source ~/.ReconEngine ; "+command).Output()
if err != nil && out == nil {
fmt.Println(err)
gologger.Fatal().Msg("Error in bash ")
}
return strings.TrimSpace(string(out))
}
func ExtractHostAndTld(domain string) (string, string) {
if domain == "" {
return "", ""
}
u, _ := url.Parse("https://" + domain)
parts := strings.Split(u.Hostname(), ".")
if len(parts) <= 1 {
return "", ""
}
domain = parts[len(parts)-2] + "." + parts[len(parts)-1]
return strings.Split(domain, ".")[0], strings.Split(domain, ".")[1]
}
func appendToFile(filePath, content string) error {
// Check if the file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
// If the file doesn't exist, create it with the given content
err := ioutil.WriteFile(filePath, []byte(content), 0644)
if err != nil {
return fmt.Errorf("error creating file: %v", err)
}
} else {
// If the file exists, open it and append the content
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
// Append content to the file
if _, err := file.WriteString(content); err != nil {
return fmt.Errorf("error appending to file: %v", err)
}
}
maxFileSize := int64(100 * 1024 * 1024) // 100 MB
err := splitFileIfLarge(filePath, maxFileSize)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
return nil
}
func splitFileIfLarge(inputFilePath string, maxSize int64) error {
// Open the input file
inputFile, err := os.Open(inputFilePath)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer inputFile.Close()
// Get file information to check its size
fileInfo, err := inputFile.Stat()
if err != nil {
return fmt.Errorf("error getting file information: %v", err)
}
// Check if the file size is larger than the specified maximum size
if fileInfo.Size() <= maxSize {
return nil
}
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(inputFile)
// Create variables for handling the output files
var outputFile *os.File
var currentSize int64
fileCounter := 1
// Iterate through the lines of the input file
for scanner.Scan() {
line := scanner.Text()
// Check if a new file needs to be created
if outputFile == nil || currentSize >= maxSize {
if outputFile != nil {
// Close the previous output file
outputFile.Close()
}
// Create a new output file
outputFilePath := fmt.Sprintf("%s_part%d.txt", strings.TrimSuffix(inputFilePath, filepath.Ext(inputFilePath)), fileCounter)
fileCounter++
outputFile, err = os.Create(outputFilePath)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outputFile.Close()
// Reset the current size for the new file
currentSize = 0
}
// Write the line to the current output file
_, err := outputFile.WriteString(line + "\n")
if err != nil {
return fmt.Errorf("error writing to output file: %v", err)
}
// Update the current size
currentSize += int64(len(line) + 1) // +1 for the newline character
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error scanning file: %v", err)
}
fmt.Printf("File successfully split into %d parts.\n", fileCounter-1)
return nil
}
// formatSize formats file size for display
func formatSize(size int64) string {
const unit = 1024
if size < unit {
return fmt.Sprintf("%d B", size)
}
div, exp := int64(unit), 0
for n := size / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f %cB", float64(size)/float64(div), "KMGTPE"[exp])
}
func makeHTTPRequest(url string) (string, error) {
// Make a GET request to the specified URL
response, err := http.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
// Convert the response body to a string and return
return string(body), nil
}