diff --git a/launch.sh b/launch.sh
index 8550373..c85986e 100755
--- a/launch.sh
+++ b/launch.sh
@@ -1,2 +1,2 @@
docker image build -t bs_build .
-docker run -it -d -p 4000:4000/udp bs_build
+docker run -it -d --restart=always -p 10903:4000/udp bs_build
\ No newline at end of file
diff --git a/main.go b/main.go
index 32c4c52..b6f2605 100644
--- a/main.go
+++ b/main.go
@@ -42,13 +42,82 @@ func randString(n int) string {
return string(b)
}
+const htmlTemplate = `
+
+
+
+
+
+ 端口查询
+
+
+
+
+
当前端口
+
Port
+
{{.Port}}
+
每分钟可更换一次端口
+
+
+
+`
+
func main() {
rand.Seed(time.Now().Unix())
secret = os.Getenv("GIN_SECRET")
if secret == "" {
- // 随机生成字符串作为secret
secret = randString(32)
}
@@ -71,11 +140,13 @@ func main() {
return
}
- ctx.JSON(http.StatusOK, fmt.Sprintf("current port is %v", matches[1]))
+ port := matches[1]
+ html := strings.Replace(htmlTemplate, "{{.Port}}", port, 1)
+ ctx.Header("Content-Type", "text/html; charset=utf-8")
+ ctx.String(http.StatusOK, html)
})
r.GET("/change-port", func(c *gin.Context) {
- // 每分钟只能运行一次
if time.Now().Before(lastSuccessRunTime.Add(time.Minute)) {
c.JSON(http.StatusInternalServerError, "fuck you!!! you can only change the port once every minute!!")
return
@@ -88,13 +159,11 @@ func main() {
return
}
- // 判断密码是否正确
if req.Secret != secret {
c.JSON(http.StatusInternalServerError, "fuck you!!! the secret is wrong!!")
return
}
- // 读取模板文件
content, err := os.ReadFile("launch.sh.template")
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
@@ -102,13 +171,10 @@ func main() {
}
contentStr := string(content)
- // 随机生成port
newPort := strconv.Itoa(10000 + rand.Intn(2000))
- // 写入新的port到模板里
contentStr = strings.ReplaceAll(contentStr, "{{port}}", newPort)
- // 写入到launch.sh
file, err := os.OpenFile("launch.sh", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0744)
_, err = file.WriteString(contentStr)
if err != nil {
@@ -128,7 +194,6 @@ func main() {
log.Printf("old container id: %s", containerID)
if containerID != "" {
- // kill掉当前docker
cmd = exec.Command("docker", "kill", containerID)
output, err := cmd.CombinedOutput()
log.Printf("docker kill output: %s", output)
@@ -138,7 +203,6 @@ func main() {
}
}
- // 重新启动docker
cmd = exec.Command("bash", "./launch.sh")
err = cmd.Run()
if err != nil {
diff --git a/start_gin_server.sh b/start_gin_server.sh
index 46d073f..89bb75e 100755
--- a/start_gin_server.sh
+++ b/start_gin_server.sh
@@ -1,2 +1,2 @@
go build .
-sudo setsid ./changeport > ginlog.txt 2>&1 &
\ No newline at end of file
+setsid ./changeport > ginlog.txt 2>&1 &