-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·269 lines (242 loc) · 7.71 KB
/
build
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
# Project name to define file name
projectName=cyberghostvpn-gui
# Style variables
fontBold=$'\e[1m'
fontUnderline=$'\e[4m'
fontReset=$'\e[0m'
fontYellow=$'\e[93m'
fontWhite=$'\e[97m'
fontBlink=$'\e[5m'
fontRed=$'\e[31m'
fontGreen=$'\e[32m'
fontBlue=$'\e[34m'
fontOrange=$'\e[38;5;214m'
# Regular build tool does not work for Windows
# GOOS=windows GOARCH=amd64 go build
#fyne-cross windows -arch=amd64 -app-id jdas-ui.jtekt.com -icon Icon.png
showHelp() {
echo -e "\n\n${fontBold}${fontUnderline}${fontYellow}You must choose for which operating system you want to build.${fontReset}"
echo -e "\n\t${fontWhite}-l,--linux${fontReset} \tLinux X86_64/AMD64"
echo -e "\t${fontWhite}-m,--macos${fontReset} \tMacOS (darwin) X86_64/AMD64"
echo -e "\t${fontWhite}-w,--windows${fontReset} \tMicrosoft Windows X86_64/AMD64"
echo -e "\n${fontBold}${fontOrange}Extra:${fontReset}\n\t${fontWhite}-32,--i386${fontReset} \tBuild also 32-bits binaries"
echo -e "\t${fontWhite}-arm,--arm${fontReset} \tBuild also for ARM architecture"
echo -e "\t${fontWhite}-g,--use-garble${fontReset} \tBuild using garble to obfuscate code and reduce size"
echo -e "\t${fontWhite}-nc,--no-compression${fontReset}\tDo not create compressed archives"
echo -e "\n${fontBold}${fontOrange}Security:${fontReset}"
echo -e "\t${fontWhite}-nk,--new-key${fontReset} \tGenerate a new encryption key (security/encryption.go => var _keyString = \"...\""
echo -e "\n"
exit 1
}
# Check arguments
buildLinux=0
buildMacOS=0
buildWindows=0
build32bits=0
buildARM=0
buildError=0
built=0
newKey=0
noArgs=1
useGarble=0
useCompression=1
for var in "$@"
do
if [ "$var" == "-l" ] || [ "$var" == "--linux" ] || [ "$var" == "-a" ] || [ "$var" == "--all" ]; then
buildLinux=1
noArgs=0
fi
if [ "$var" == "-m" ] || [ "$var" == "--macos" ] || [ "$var" == "-a" ] || [ "$var" == "--all" ]; then
buildMacOS=1
noArgs=0
fi
if [ "$var" == "-w" ] || [ "$var" == "--windows" ] || [ "$var" == "-a" ] || [ "$var" == "--all" ]; then
buildWindows=1
noArgs=0
fi
if [ "$var" == "-32" ] || [ "$var" == "--i386" ]; then
build32bits=1
fi
if [ "$var" == "-arm" ] || [ "$var" == "--arm" ]; then
buildARM=1
fi
if [ "$var" == "-nc" ] || [ "$var" == "--no-compression" ]; then
useCompression=0
fi
if [ "$var" == "-nk" ] || [ "$var" == "--new-key" ]; then
newKey=1
noArgs=0
fi
if [ "$var" == "-g" ] || [ "$var" == "--use-garble" ]; then
useGarble=1
fi
if [ "$var" == "-h" ] || [ "$var" == "--help" ]; then
showHelp
fi
done
if [ $noArgs -eq 1 ]; then
showHelp
fi
# Check if garble exists
if [ $useGarble -eq 1 ]; then
if command -v garble >/dev/null 2>&1; then
garbleCache=./bin/.garble_cache
if ! [ -d $garbleCache ]; then
mkdir $garbleCache
fi
else
echo -e "${fontBold}${fontOrange}[WARN]${fontReset} Cannot find command 'garble'. Regular build will be used instead."
useGarble=0
fi
fi
# Clean bin folder
if ! [ -d bin ]; then
mkdir bin
fi
rm bin/* &> /dev/null
# Get current version
version=$(grep "const AppVersion =" about/app.go)
version=${version//const/}
version=${version//AppVersion/}
version=${version//\"/}
version=${version//\=/}
version=${version// /}
if ! [ "$version" == "" ]; then
version="_${version}"
else
version="_latest"
fi
# Build function
# Build the binary using go or garble, and compress it with lrzip if possible.
#
# Parameters:
#
# $1: The OS to build for.
# $2: The architecture to build for.
#
# Returns:
#
# 0 if the build and compression were successful.
# 1 otherwise.
buildCmd() {
# Build using go or garble
GOCMD=go
if [ $useGarble -eq 1 ]; then
GOCMD=garble
GARBLE_CACHE=$garbleCache
echo -e "${fontBold}${fontGreen}[INFO]${fontReset} Building using garble..."
fi
# Build
built=1
BINFILE=${projectName}_${1}_${2}${version}
echo "Command => GOOS=${1} GOARCH=${2} ${GOCMD} build -o ./bin/${BINFILE}"
GOOS=${1} GOARCH=${2} ${GOCMD} build -o ./bin/${BINFILE} .
if [ "$?" -eq 0 ]; then
if [ $useCompression -eq 1 ]; then
echo -e "${fontBold}${fontGreen}[INFO]${fontReset} Compressing using lrzip..."
lrzip -f -z bin/${BINFILE}
echo -e "${fontBold}${fontGreen}[INFO]${fontReset} Compressing using tar.gz..."
tar -czf bin/${BINFILE}.tar.gz bin/${BINFILE}
if [ "$?" -gt 0 ]; then
echo -e "${fontBold}${fontRed}[ERROR]${fontReset} Failed to compress bin/${BINFILE}."
return 1
fi
fi
return 0
else
echo -e "${fontBold}${fontRed}[ERROR]${fontReset} Failed to build bin/${BINFILE}."
buildError=1
fi
return 1
}
# Build an executable file for a given OS using go or garble.
#
# Parameters:
# $1: string, the OS to build for. Can be "linux", "macos" or "windows".
#
# Returns:
# 0 if the build was successful, 1 otherwise.
#
# Example:
# build linux
build() {
argOS=""
argArch=amd64
if [ "$1" == "" ]; then
echo -e "\n${fontBold}${fontRed}${fontBlink}[ERROR]${fontReset} Script error! Missing Go OS value.\n"
buildError=1
else
if [ "$1" == "linux" ]; then
argOS=linux
elif [ "$1" == "macos" ]; then
argOS=darwin
elif [ "$1" == "windows" ]; then
argOS=windows
fi
if ! [ "$argOS" == "" ]; then
echo -e "\n${fontBold}${fontGreen}[INFO]${fontReset} Building $1 64-bits executable file..."
buildCmd $argOS $argArch
if [ "$?" -eq 0 ]; then
if [ ${build32bits} -eq 1 ]; then
argArch=386
echo -e "\n${fontBold}${fontGreen}[INFO]${fontReset} Building $1 32-bits executable file..."
buildCmd $argOS $argArch
fi
if [ ${buildARM} -eq 1 ]; then
argArch=arm64
echo -e "\n${fontBold}${fontGreen}[INFO]${fontReset} Building $1 ARM 64-bits executable file..."
buildCmd $argOS $argArch
if [ ${build32bits} -eq 1 ]; then
argArch=arm
echo -e "\n${fontBold}${fontGreen}[INFO]${fontReset} Building $1 ARM 32-bits executable file..."
buildCmd $argOS $argArch
fi
fi
fi
fi
fi
}
generateSecurityKey() {
# Generate a random 64-character hexadecimal string and convert it to uppercase
new_key=$(openssl rand -hex 32 | tr '[:lower:]' '[:upper:]')
# Define the file path
file="security/encryption.go"
# Use sed to replace the value of _keyString with the new random key
sed -i "s/var _keyString = \"[^\"]*\"/var _keyString = \"$new_key\"/" "$file"
if [ "$?" -eq 0 ]; then
echo -e "${fontBold}${fontOrange}[WARN] Replaced _keyString with new random value:${fontReset} ${fontYellow}$new_key${fontReset}"
else
echo -e "${fontBold}${fontRed}[ERROR] Failed to replace _keyString with new random value:${fontReset} ${fontYellow}$new_key${fontReset}"
fi
}
# Generate new encryption key
if [ $newKey -eq 1 ]; then
echo -e "${fontBold}${fontGreen}[INFO]${fontReset} Generating new encryption key..."
generateSecurityKey
fi
# Check if compression tool exists
if ! command -v lrzip >/dev/null 2>&1; then
useCompression=0
echo -e "${fontBold}${fontOrange}[WARN]${fontReset} Compression tool lrzip not found. Compression disabled."
fi
# Linux build
if [ $buildLinux -eq 1 ]; then
build linux
fi
# MacOs build
if [ $buildMacOS -eq 1 ]; then
build macos
fi
# Windows build
if [ $buildWindows -eq 1 ]; then
build windows
fi
if [ $built -eq 1 ]; then
# Show compiled files
if [ $buildError -eq 0 ]; then
echo -e "\n${fontBold}${fontGreen}[INFO]${fontReset} Successfully compiled ${fontBold}${fontBlue}${projectName}${fontReset}!"
else
echo -e "\n${fontBold}${fontRed}[ERROR]${fontReset} Failed to compile ${fontBold}${fontRed}${projectName}${fontReset}!"
fi
fi