-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
144 lines (124 loc) · 2.88 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
package main
import (
_ "embed"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"sync"
)
func main() {
exe, err := os.Executable()
if err != nil {
panic(err)
}
if len(os.Args) >= 2 && os.Args[1] == "cli" {
err = clientMain(exe, os.Args[2:])
} else {
err = serverMain(exe, os.Args[1:])
}
if err != nil {
fmt.Printf("%+v\n", err)
}
}
const (
fileMode = 0o666
flagW = os.O_CREATE | os.O_WRONLY | os.O_TRUNC
flagA = os.O_CREATE | os.O_WRONLY | os.O_APPEND
)
//go:embed fileServer.ico
var icoData []byte // 嵌入图标文件
func createRegFile(exe, addr string) error {
fw, err := os.Create("addRightClickRegistry.reg")
if err != nil {
return err
}
//goland:noinspection GoUnhandledErrorResult
defer fw.Close()
icoFile := filepath.Join(filepath.Dir(exe), "fileServer.ico")
err = os.WriteFile(icoFile, icoData, fileMode)
if err != nil {
return err
}
_, _ = fmt.Fprintf(fw, `Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\fileServer]
@="File Server Here"
"Icon"="%s"
[HKEY_CLASSES_ROOT\Directory\Background\shell\fileServer\command]
@="\"%s\" -s \"%s\" -p \"%%V\""
`, strings.ReplaceAll(icoFile, "\\", "\\\\"),
strings.ReplaceAll(exe, "\\", "\\\\"), addr)
return nil
}
func InternalIp() (ips []net.IP) {
interfaces, err := net.Interfaces()
if err != nil {
return
}
for _, inf := range interfaces {
if inf.Flags&net.FlagUp != net.FlagUp ||
inf.Flags&net.FlagLoopback == net.FlagLoopback {
continue
}
addr, err := inf.Addrs()
if err != nil {
continue
}
for _, a := range addr {
if ipNet, ok := a.(*net.IPNet); ok &&
!ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
ips = append(ips, ipNet.IP)
}
}
}
return
}
type poolByte struct {
buf []byte
}
var bytePool = sync.Pool{New: func() any {
return &poolByte{buf: make([]byte, 32<<10)}
}}
var (
defaultByteUnit = []struct {
unit string
byte float64
}{
{byte: 1}, // 大于TB可能超过float64限制
{byte: 1 << 10, unit: "B"},
{byte: 1 << 20, unit: "KB"},
{byte: 1 << 30, unit: "MB"},
{byte: 1 << 40, unit: "GB"},
{byte: 1 << 50, unit: "TB"},
}
byteUnitFormat = []string{"%.2f%s", "%.3f%s", "%7.2f%s", "%8.3f%s"}
)
func convertByte(size int64, fill bool) string {
// fill=true时,需要保证返回字符串长度为9,主要是为了外部对齐
b := float64(size)
if b <= 0 {
b = 0 // size=0 或 超过float64
index := 1
if fill {
index |= 2
}
return fmt.Sprintf(byteUnitFormat[index], b, defaultByteUnit[1].unit)
}
for i := 1; i < len(defaultByteUnit); i++ {
if b < defaultByteUnit[i].byte {
var (
index int
unit = defaultByteUnit[i].unit
)
if fill {
index |= 2 // 左边填充空白
}
if unit == defaultByteUnit[1].unit {
index |= 1 // 为了保持长度,小数取3位
}
return fmt.Sprintf(byteUnitFormat[index], b/defaultByteUnit[i-1].byte, unit)
}
}
return "OverLimit"
}