-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.go
More file actions
178 lines (156 loc) · 4.15 KB
/
util.go
File metadata and controls
178 lines (156 loc) · 4.15 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
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/astaxie/beego/logs"
"github.com/lxn/walk"
mrand "math/rand"
)
func VersionGet() string {
return "v0.2.2"
}
func SaveToFile(name string, body []byte) error {
return os.WriteFile(name, body, 0664)
}
func CapSignal(proc func()) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signalChan
proc()
logs.Error("recv signcal %s, ready to exit", sig.String())
os.Exit(-1)
}()
}
func ByteView(size int64) string {
if size < 1024 {
return fmt.Sprintf("%dB", size)
} else if size < (1024 * 1024) {
return fmt.Sprintf("%.1fKB", float64(size)/float64(1024))
} else if size < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.1fMB", float64(size)/float64(1024*1024))
} else if size < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.1fGB", float64(size)/float64(1024*1024*1024))
} else {
return fmt.Sprintf("%.1fTB", float64(size)/float64(1024*1024*1024*1024))
}
}
func InterfaceGet(iface *net.Interface) ([]net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
ips := make([]net.IP, 0)
for _, v := range addrs {
ipone, _, err := net.ParseCIDR(v.String())
if err != nil {
continue
}
if len(ipone) > 0 {
ips = append(ips, ipone)
}
}
if len(ips) == 0 {
return nil, fmt.Errorf("interface not any address")
}
return ips, nil
}
func InterfaceOptions() []string {
output := []string{"0.0.0.0", "::"}
ifaces, err := net.Interfaces()
if err != nil {
logs.Error(err.Error())
return output
}
for _, v := range ifaces {
if v.Flags&net.FlagUp == 0 {
continue
}
address, err := InterfaceGet(&v)
if err != nil {
continue
}
for _, addr := range address {
output = append(output, addr.String())
}
}
return output
}
func GenerateUsername(length int) string {
charSet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*_+-="
username := make([]byte, length)
for i := range username {
index := mrand.Intn(len(charSet))
username[i] = charSet[index]
}
return string(username)
}
func CopyClipboard() (string, error) {
text, err := walk.Clipboard().Text()
if err != nil {
logs.Error(err.Error())
return "", fmt.Errorf("can not find the any clipboard")
}
return text, nil
}
func PasteClipboard(input string) error {
err := walk.Clipboard().SetText(input)
if err != nil {
logs.Error(err.Error())
}
return err
}
func CreateTlsConfig(cert, key string) (*tls.Config, error) {
certs, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
return nil, err
}
return &tls.Config{
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS13,
Certificates: []tls.Certificate{certs},
ClientAuth: tls.RequestClientCert,
}, nil
}
func GenerateKeyCert(addr string) (string, string) {
max := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, _ := rand.Int(rand.Reader, max)
subject := pkix.Name{
Organization: []string{"simple http server windows app"},
OrganizationalUnit: []string{"simple http server windows app"},
CommonName: "simple http server windows app",
}
if addr == "0.0.0.0" || addr == "::" {
addr = "127.0.0.1"
}
ipAddress := make([]net.IP, 0)
ipAddress = append(ipAddress, net.ParseIP(addr))
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: subject,
NotBefore: time.Now(),
NotAfter: time.Now().Add(30 * 24 * time.Hour),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
IPAddresses: ipAddress,
}
pk, _ := rsa.GenerateKey(rand.Reader, 2048)
derBytes, _ := x509.CreateCertificate(rand.Reader, &template, &template, &pk.PublicKey, pk)
certOut := bytes.NewBuffer(make([]byte, 0))
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
keyOut := bytes.NewBuffer(make([]byte, 0))
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(pk)})
return certOut.String(), keyOut.String()
}