Skip to content

Commit 20c9230

Browse files
authored
feat(init): Add support for geo ip pack initialization (#7333)
1 parent 02c3a34 commit 20c9230

File tree

5 files changed

+147
-76
lines changed

5 files changed

+147
-76
lines changed

backend/app/service/upgrade.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error {
144144
return
145145
}
146146
_, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(tmpDir, "lang"))
147+
geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo")
148+
_, _ = cmd.Execf("mkdir %s && cp %s %s/", geoPath, path.Join(tmpDir, "GeoIP.mmdb"), geoPath)
149+
147150
if _, err := cmd.Execf("sed -i -e 's#BASE_DIR=.*#BASE_DIR=%s#g' /usr/local/bin/1pctl", global.CONF.System.BaseDir); err != nil {
148151
global.LOG.Errorf("upgrade basedir in 1pctl failed, err: %v", err)
149152
u.handleRollback(originalDir, 2)
@@ -177,6 +180,7 @@ func (u *UpgradeService) handleBackup(fileOp files.FileOp, originalDir string) e
177180
return err
178181
}
179182
_, _ = cmd.Execf("cp -r /usr/local/bin/lang %s", originalDir)
183+
_, _ = cmd.Execf("cp %s %s", path.Join(global.CONF.System.BaseDir, "1panel/geo/GeoIP.mmdb"), originalDir)
180184
checkPointOfWal()
181185
if err := handleTar(path.Join(global.CONF.System.BaseDir, "1panel/db"), originalDir, "db.tar.gz", "db/1Panel.db-*", ""); err != nil {
182186
return err
@@ -208,6 +212,7 @@ func (u *UpgradeService) handleRollback(originalDir string, errStep int) {
208212
global.LOG.Errorf("rollback 1panel failed, err: %v", err)
209213
}
210214
_, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(originalDir, "lang"))
215+
_, _ = cmd.Execf("cp %s %s", path.Join(originalDir, "GeoIP.mmdb"), path.Join(global.CONF.System.BaseDir, "1panel/geo/"))
211216

212217
if errStep == 2 {
213218
return

backend/init/hook/hook.go

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package hook
33
import (
44
"encoding/base64"
55
"encoding/json"
6-
"fmt"
76
"os"
87
"path"
9-
"sort"
108
"strings"
119

1210
"github.com/1Panel-dev/1Panel/backend/app/dto"
@@ -17,7 +15,6 @@ import (
1715
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
1816
"github.com/1Panel-dev/1Panel/backend/utils/common"
1917
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
20-
"github.com/1Panel-dev/1Panel/backend/utils/files"
2118
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
2219
)
2320

@@ -88,8 +85,6 @@ func Init() {
8885
handleSnapStatus()
8986
loadLocalDir()
9087
initDir()
91-
92-
go initLang()
9388
}
9489

9590
func handleSnapStatus() {
@@ -265,72 +260,3 @@ func handleCronJobAlert(cronjob *model.Cronjob) {
265260
return
266261
}
267262
}
268-
269-
func initLang() {
270-
fileOp := files.NewFileOp()
271-
upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade")
272-
if fileOp.Stat("/usr/local/bin/lang/zh.sh") {
273-
return
274-
}
275-
tmpPath, err := loadRestorePath(upgradePath)
276-
files, _ := os.ReadDir(path.Join(upgradePath, tmpPath, "downloads"))
277-
if len(files) == 0 {
278-
tmpPath = "no such file"
279-
} else {
280-
for _, item := range files {
281-
if item.IsDir() && strings.HasPrefix(item.Name(), "1panel-") {
282-
tmpPath = path.Join(upgradePath, tmpPath, "downloads", item.Name(), "lang")
283-
break
284-
}
285-
}
286-
}
287-
288-
if err != nil || tmpPath == "no such file" || !fileOp.Stat(tmpPath) {
289-
path := fmt.Sprintf("%s/language/lang.tar.gz", global.CONF.System.RepoUrl)
290-
if err := fileOp.DownloadFileWithProxy(path, "/usr/local/bin/lang.tar.gz"); err != nil {
291-
global.LOG.Errorf("download lang.tar.gz failed, err: %v", err)
292-
return
293-
}
294-
if !fileOp.Stat("/usr/local/bin/lang.tar.gz") {
295-
global.LOG.Errorf("download lang.tar.gz failed, no such file, err: %v", err)
296-
return
297-
}
298-
std, err := cmd.Execf("tar zxvfC %s %s", "/usr/local/bin/lang.tar.gz", "/usr/local/bin/")
299-
if err != nil {
300-
fmt.Printf("decompress lang.tar.gz failed, std: %s, err: %v", std, err)
301-
return
302-
}
303-
_ = os.Remove("/usr/local/bin/lang.tar.gz")
304-
global.LOG.Info("init lang for 1pctl successful")
305-
return
306-
}
307-
std, err := cmd.Execf("cp -r %s %s", tmpPath, "/usr/local/bin/")
308-
if err != nil {
309-
fmt.Printf("load lang from package failed, std: %s, err: %v", std, err)
310-
return
311-
}
312-
global.LOG.Info("init lang for 1pctl successful")
313-
}
314-
315-
func loadRestorePath(upgradeDir string) (string, error) {
316-
if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) {
317-
return "no such file", nil
318-
}
319-
files, err := os.ReadDir(upgradeDir)
320-
if err != nil {
321-
return "", err
322-
}
323-
var folders []string
324-
for _, file := range files {
325-
if file.IsDir() {
326-
folders = append(folders, file.Name())
327-
}
328-
}
329-
if len(folders) == 0 {
330-
return "no such file", nil
331-
}
332-
sort.Slice(folders, func(i, j int) bool {
333-
return folders[i] > folders[j]
334-
})
335-
return folders[0], nil
336-
}

backend/init/lang/lang.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package lang
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
"sort"
8+
"strings"
9+
10+
"github.com/1Panel-dev/1Panel/backend/global"
11+
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
12+
"github.com/1Panel-dev/1Panel/backend/utils/files"
13+
)
14+
15+
func Init() {
16+
go initLang()
17+
}
18+
19+
func initLang() {
20+
fileOp := files.NewFileOp()
21+
geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo/GeoIP.mmdb")
22+
isLangExist := fileOp.Stat("/usr/local/bin/lang/zh.sh")
23+
isGeoExist := fileOp.Stat(geoPath)
24+
if isLangExist && isGeoExist {
25+
return
26+
}
27+
upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade")
28+
tmpPath, err := loadRestorePath(upgradePath)
29+
upgradeDir := path.Join(upgradePath, tmpPath, "downloads")
30+
if err != nil || len(tmpPath) == 0 || !fileOp.Stat(upgradeDir) {
31+
if !isLangExist {
32+
downloadLangFromRemote(fileOp)
33+
}
34+
if !isGeoExist {
35+
downloadGeoFromRemote(fileOp, geoPath)
36+
}
37+
return
38+
}
39+
40+
files, _ := os.ReadDir(upgradeDir)
41+
if len(files) == 0 {
42+
tmpPath = "no such file"
43+
} else {
44+
for _, item := range files {
45+
if item.IsDir() && strings.HasPrefix(item.Name(), "1panel-") {
46+
tmpPath = path.Join(upgradePath, tmpPath, "downloads", item.Name())
47+
break
48+
}
49+
}
50+
}
51+
if tmpPath == "no such file" || !fileOp.Stat(tmpPath) {
52+
if !isLangExist {
53+
downloadLangFromRemote(fileOp)
54+
}
55+
if !isGeoExist {
56+
downloadGeoFromRemote(fileOp, geoPath)
57+
}
58+
return
59+
}
60+
if !isLangExist {
61+
if !fileOp.Stat(path.Join(tmpPath, "lang")) {
62+
downloadLangFromRemote(fileOp)
63+
return
64+
}
65+
std, err := cmd.Execf("cp -r %s %s", path.Join(tmpPath, "lang"), "/usr/local/bin/")
66+
if err != nil {
67+
global.LOG.Errorf("load lang from package failed, std: %s, err: %v", std, err)
68+
return
69+
}
70+
global.LOG.Info("init lang successful")
71+
}
72+
if !isGeoExist {
73+
if !fileOp.Stat(path.Join(tmpPath, "GeoIP.mmdb")) {
74+
downloadGeoFromRemote(fileOp, geoPath)
75+
return
76+
}
77+
std, err := cmd.Execf("cp %s %s", path.Join(tmpPath, "GeoIP.mmdb"), path.Dir(geoPath))
78+
if err != nil {
79+
global.LOG.Errorf("load geo ip from package failed, std: %s, err: %v", std, err)
80+
return
81+
}
82+
global.LOG.Info("init geo ip successful")
83+
}
84+
}
85+
86+
func loadRestorePath(upgradeDir string) (string, error) {
87+
if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) {
88+
return "no such file", nil
89+
}
90+
files, err := os.ReadDir(upgradeDir)
91+
if err != nil {
92+
return "", err
93+
}
94+
var folders []string
95+
for _, file := range files {
96+
if file.IsDir() {
97+
folders = append(folders, file.Name())
98+
}
99+
}
100+
if len(folders) == 0 {
101+
return "no such file", nil
102+
}
103+
sort.Slice(folders, func(i, j int) bool {
104+
return folders[i] > folders[j]
105+
})
106+
return folders[0], nil
107+
}
108+
109+
func downloadLangFromRemote(fileOp files.FileOp) {
110+
path := fmt.Sprintf("%s/language/lang.tar.gz", global.CONF.System.RepoUrl)
111+
if err := fileOp.DownloadFileWithProxy(path, "/usr/local/bin/lang.tar.gz"); err != nil {
112+
global.LOG.Errorf("download lang.tar.gz failed, err: %v", err)
113+
return
114+
}
115+
if !fileOp.Stat("/usr/local/bin/lang.tar.gz") {
116+
global.LOG.Error("download lang.tar.gz failed, no such file")
117+
return
118+
}
119+
std, err := cmd.Execf("tar zxvfC %s %s", "/usr/local/bin/lang.tar.gz", "/usr/local/bin/")
120+
if err != nil {
121+
fmt.Printf("decompress lang.tar.gz failed, std: %s, err: %v", std, err)
122+
return
123+
}
124+
_ = os.Remove("/usr/local/bin/lang.tar.gz")
125+
global.LOG.Info("download lang successful")
126+
}
127+
func downloadGeoFromRemote(fileOp files.FileOp, targetPath string) {
128+
_ = os.MkdirAll(path.Dir(targetPath), os.ModePerm)
129+
pathItem := fmt.Sprintf("%s/geo/GeoIP.mmdb", global.CONF.System.RepoUrl)
130+
if err := fileOp.DownloadFileWithProxy(pathItem, targetPath); err != nil {
131+
global.LOG.Errorf("download geo ip failed, err: %v", err)
132+
return
133+
}
134+
global.LOG.Info("download geo ip successful")
135+
}

backend/server/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"crypto/tls"
55
"encoding/gob"
66
"fmt"
7-
"github.com/1Panel-dev/1Panel/backend/constant"
8-
"github.com/1Panel-dev/1Panel/backend/i18n"
97
"net"
108
"net/http"
119
"os"
1210
"path"
1311

12+
"github.com/1Panel-dev/1Panel/backend/constant"
13+
"github.com/1Panel-dev/1Panel/backend/i18n"
14+
1415
"github.com/1Panel-dev/1Panel/backend/init/app"
1516
"github.com/1Panel-dev/1Panel/backend/init/business"
17+
"github.com/1Panel-dev/1Panel/backend/init/lang"
1618

1719
"github.com/1Panel-dev/1Panel/backend/cron"
1820
"github.com/1Panel-dev/1Panel/backend/init/cache"
@@ -38,6 +40,7 @@ func Start() {
3840
db.Init()
3941
migration.Init()
4042
app.Init()
43+
lang.Init()
4144
validator.Init()
4245
gob.Register(psession.SessionUser{})
4346
cache.Init()

cmd/server/cmd/restore.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/1Panel-dev/1Panel/backend/global"
1112
"github.com/1Panel-dev/1Panel/backend/i18n"
1213
cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd"
1314
"github.com/1Panel-dev/1Panel/backend/utils/common"
@@ -54,6 +55,7 @@ var restoreCmd = &cobra.Command{
5455
return err
5556
}
5657
_, _ = cmdUtils.Execf("cp -r %s /usr/local/bin", path.Join(tmpPath, "lang"))
58+
_, _ = cmdUtils.Execf("cp %s %s", path.Join(tmpPath, "GeoIP.mmdb"), path.Join(global.CONF.System.BaseDir, "1panel/geo/"))
5759
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep3"))
5860
if err := common.CopyFile(path.Join(tmpPath, "1panel.service"), "/etc/systemd/system"); err != nil {
5961
return err

0 commit comments

Comments
 (0)