forked from hanc00l/nemo_go
/
fileutil.go
227 lines (204 loc) · 5.23 KB
/
fileutil.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package utils
import (
"archive/zip"
"fmt"
"github.com/disintegration/imaging"
"github.com/hanc00l/nemo_go/pkg/logging"
"github.com/pkg/errors"
"image"
"io"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
)
func init() {
//rand.Seed(time.Now().UnixNano())
}
// GetRandomString2 生成指定长度的随机字符串
func GetRandomString2(n int) string {
randBytes := make([]byte, n/2)
rand.Read(randBytes)
return fmt.Sprintf("%x", randBytes)
}
// GetTempPathFileName 获取一个临时文件名
func GetTempPathFileName() (pathFileName string) {
return filepath.Join(os.TempDir(), fmt.Sprintf("%s.tmp", GetRandomString2(16)))
}
// GetTempPNGPathFileName 获取一个临时文件名,后缀为PNG
func GetTempPNGPathFileName() (pathFileName string) {
return filepath.Join(os.TempDir(), fmt.Sprintf("%s.png", GetRandomString2(16)))
}
// GetTempPathDirName 获取一个临时目录
func GetTempPathDirName() (pathDirName string) {
return filepath.Join(os.TempDir(), fmt.Sprintf("%s.dir", GetRandomString2(16)))
}
// Unzip 解压zip文件
func Unzip(archiveFile, dstPath string) error {
reader, err := zip.OpenReader(archiveFile)
if err != nil {
return errors.WithStack(err)
}
if err := os.MkdirAll(dstPath, 0755); err != nil {
return errors.WithStack(err)
}
for _, file := range reader.File {
unzipped := filepath.Join(dstPath, file.Name)
if file.FileInfo().IsDir() {
err := os.MkdirAll(unzipped, file.Mode())
if err != nil {
return errors.WithStack(err)
}
continue
}
fileReader, err := file.Open()
if err != nil {
return errors.WithStack(err)
}
targetFile, err := os.OpenFile(unzipped, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
fileReader.Close()
return errors.WithStack(err)
}
if _, err := io.Copy(targetFile, fileReader); err != nil {
fileReader.Close()
targetFile.Close()
return errors.WithStack(err)
}
fileReader.Close()
targetFile.Close()
}
return nil
}
// DownloadFile 下载文件
func DownloadFile(url, dstPathFile string) (bool, error) {
// Get the data
resp, err := http.Get(url)
if err != nil {
return false, err
}
defer resp.Body.Close()
// 创建一个文件用于保存
out, err := os.Create(dstPathFile)
if err != nil {
return false, err
}
defer out.Close()
// 然后将响应流和文件流对接起来
_, err = io.Copy(out, resp.Body)
if err != nil {
return false, err
}
return true, nil
}
// CheckFileExist 检测文件或目录是否存在
func CheckFileExist(filepath string) bool {
_, err := os.Stat(filepath)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
// MakePath 创建目录,如果目录存在则直接返回
func MakePath(filepath string) bool {
_, err := os.Stat(filepath)
if err == nil {
return true
}
if os.IsNotExist(err) {
if err = os.MkdirAll(filepath, 0777); err == nil {
return true
}
}
return false
}
// ReSizePicture 对图片文件尺寸缩放
func ReSizePicture(srcFile, dstFile string, width, height int) bool {
src, err := imaging.Open(srcFile)
if err != nil {
logging.RuntimeLog.Errorf("failed to open image: %v", err)
return false
}
dst := imaging.Resize(src, width, height, imaging.CatmullRom)
err = imaging.Save(dst, dstFile)
if err != nil {
logging.RuntimeLog.Errorf("failed to save image: %v", err)
return false
}
return true
}
// ReSizeAndCropPicture 对图片文件尺寸缩放、剪裁
func ReSizeAndCropPicture(srcFile, dstFile string, width, height int) bool {
src, err := imaging.Open(srcFile)
if err != nil {
logging.RuntimeLog.Errorf("failed to open image: %v", err)
return false
}
dstResize := imaging.Resize(src, width, 0, imaging.CatmullRom)
// 按指定的边界进行裁剪
dst := imaging.Crop(dstResize, image.Rect(0, 0, width, height))
err = imaging.Save(dst, dstFile)
if err != nil {
logging.RuntimeLog.Errorf("failed to save image: %v", err)
return false
}
return true
}
type BinShortName string
const (
ObserverWard BinShortName = "observer_ward"
MassDns BinShortName = "massdns"
Xray BinShortName = "xray"
Nuclei BinShortName = "nuclei"
Worker BinShortName = "worker"
Httpx BinShortName = "httpx"
Subfinder BinShortName = "subfinder"
Fingerprintx BinShortName = "fingerprintx"
Gogo BinShortName = "gogo"
)
// GetThirdpartyBinNameByPlatform 根据当前运行平台及架构,生成指定的文件名称
func GetThirdpartyBinNameByPlatform(binShortName BinShortName) (binPlatformName string) {
binPlatformName = fmt.Sprintf("%s_%s_%s", binShortName, runtime.GOOS, runtime.GOARCH)
if runtime.GOOS == "windows" {
binPlatformName += ".exe"
}
/*
https://go.dev/doc/install/source#environment
$GOOS $GOARCH
android arm
darwin 386
darwin amd64
darwin arm
darwin arm64
dragonfly amd64
freebsd 386
freebsd amd64
freebsd arm
linux 386
linux amd64
linux arm
linux arm64
linux ppc64
linux ppc64le
linux mips
linux mipsle
linux mips64
linux mips64le
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
openbsd arm
plan9 386
plan9 amd64
solaris amd64
windows 386
windows amd64
*/
return
}