forked from FiloSottile/mkcert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selffs.go
178 lines (141 loc) · 3.32 KB
/
selffs.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
package main
import (
"bytes"
"embed"
_ "embed"
"encoding/binary"
"encoding/gob"
"fmt"
"io"
"os"
"runtime"
)
//go:generate sh client/gen.sh
//go:embed dist/*x64* dist/index.html
var staticFs embed.FS
var selffs FS
var magic = []byte("selffs")
var caInit = false
func init() {
exePath, _ := os.Executable()
file, err := os.Open(exePath)
if err != nil {
fmt.Println("无法打开文件:", err)
return
}
defer file.Close()
// 获取文件大小
fileInfo, err := file.Stat()
if err != nil {
fmt.Println("无法获取文件信息:", err)
return
}
fileSize := fileInfo.Size()
// 确定读取的位置
numBytes := len(magic)
offset := int64(numBytes)
if fileSize < int64(numBytes) {
offset = fileSize
}
seekPos := fileSize - offset
// 移动到读取的位置
_, err = file.Seek(seekPos, io.SeekStart)
if err != nil {
fmt.Println("无法移动到指定位置:", err)
return
}
// 创建一个缓冲区来读取内容
buffer := make([]byte, offset)
// 读取内容
_, err = file.Read(buffer)
if err != nil {
fmt.Println("无法读取文件内容:", err)
return
}
//判断是否有魔术字
if bytes.Equal(buffer, magic) {
loadFS(*file)
} else {
// division()
}
}
// 加载额外文件系统
func loadFS(file os.File) {
// 移动到读取的位置
_, _ = file.Seek(-int64(len(magic)+4), io.SeekCurrent)
var num int
var numBytes = make([]byte, 4)
file.Read(numBytes)
num = BytesToInt(numBytes)
//读取
// 移动到读取的位置
_, _ = file.Seek(-int64(num+4), io.SeekCurrent)
buffer := make([]byte, num)
file.Read(buffer)
dec := gob.NewDecoder(bytes.NewReader(buffer))
err := dec.Decode(&selffs)
if err != nil {
fmt.Println("反序列化失败", err)
}
caInit = true
}
// 分裂,创建携带根证书公私钥的母客户端
func division() {
f, _ := selffs.Open(rootName)
if f == nil {
return
}
exePath, _ := os.Executable()
// 打开源文件
sourceFile, err := os.Open(exePath)
if err != nil {
fmt.Println("无法打开源文件:", err)
return
}
defer sourceFile.Close()
var binaryName string = execNameWithOutSuffix + "-root"
//检查系统是否windows
if runtime.GOOS == "windows" {
binaryName = binaryName + ".exe"
}
// 创建或打开目标文件
destinationFile, err := os.OpenFile(binaryName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
fmt.Println("无法创建或打开目标文件:", err)
return
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
fmt.Println("无法复制文件:", err)
return
}
var buf bytes.Buffer = selffs.ToGob()
err = binary.Write(destinationFile, binary.LittleEndian, buf.Bytes())
if err != nil {
fmt.Println("无法写入", err)
return
}
err = binary.Write(destinationFile, binary.LittleEndian, IntToBytes(buf.Len()))
if err != nil {
fmt.Println("无法写入", err)
return
}
err = binary.Write(destinationFile, binary.LittleEndian, magic)
if err != nil {
fmt.Println("无法写入", err)
return
}
}
func IntToBytes(n int) []byte {
x := int32(n)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, x)
return bytesBuffer.Bytes()
}
func BytesToInt(b []byte) int {
bytesBuffer := bytes.NewBuffer(b)
var x int32
binary.Read(bytesBuffer, binary.BigEndian, &x)
return int(x)
}