add secret token
This commit is contained in:
parent
bf86e70755
commit
ce0d1bbe41
|
|
@ -3,4 +3,4 @@
|
||||||
bindata.go
|
bindata.go
|
||||||
*.exe
|
*.exe
|
||||||
gin.log
|
gin.log
|
||||||
config/config.json
|
config.json
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var serverSecret string
|
||||||
var serverConfig Config
|
var serverConfig Config
|
||||||
var ledgerConfigMap map[string]Config
|
var ledgerConfigMap map[string]Config
|
||||||
var ledgerAccountsMap map[string][]Account
|
var ledgerAccountsMap map[string][]Account
|
||||||
|
|
@ -310,6 +311,19 @@ func WriteLedgerConfigMap(newLedgerConfigMap map[string]Config) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GenerateServerSecret(secret string) string {
|
||||||
|
if secret == "" {
|
||||||
|
serverSecret = RandChar(16)
|
||||||
|
} else {
|
||||||
|
serverSecret = secret
|
||||||
|
}
|
||||||
|
return serverSecret
|
||||||
|
}
|
||||||
|
|
||||||
|
func EqualServerSecret(secret string) bool {
|
||||||
|
return serverSecret == secret
|
||||||
|
}
|
||||||
|
|
||||||
func GetCommoditySymbol(commodity string) string {
|
func GetCommoditySymbol(commodity string) string {
|
||||||
switch commodity {
|
switch commodity {
|
||||||
case "CNY":
|
case "CNY":
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,11 @@ package script
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
func GetServerConfigFilePath() string {
|
func GetServerConfigFilePath() string {
|
||||||
return "./config/config.json"
|
return "./config.json"
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetServerWhiteListFilePath() string {
|
func GetServerWhiteListFilePath() string {
|
||||||
return "./config/white_list.json"
|
return "./white_list.json"
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetServerLedgerConfigFilePath() string {
|
func GetServerLedgerConfigFilePath() string {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package script
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func isWindows() bool {
|
||||||
|
os := runtime.GOOS
|
||||||
|
return os == "windows"
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpenBrowser(url string) {
|
||||||
|
if isWindows() {
|
||||||
|
cmd := exec.Command("cmd", "/C", "start", url)
|
||||||
|
err := cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
LogSystemError("Failed to open browser, error is " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
package script
|
package script
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetIpAddress() string {
|
func GetIpAddress() string {
|
||||||
|
|
@ -15,3 +18,14 @@ func GetIpAddress() string {
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
|
|
||||||
|
func RandChar(size int) string {
|
||||||
|
rand.NewSource(time.Now().UnixNano()) // 产生随机种子
|
||||||
|
var s bytes.Buffer
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
s.WriteByte(char[rand.Int63()%int64(len(char))])
|
||||||
|
}
|
||||||
|
return s.String()
|
||||||
|
}
|
||||||
|
|
|
||||||
32
server.go
32
server.go
|
|
@ -1,13 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"github.com/beancount-gs/script"
|
"github.com/beancount-gs/script"
|
||||||
"github.com/beancount-gs/service"
|
"github.com/beancount-gs/service"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitServerFiles() error {
|
func InitServerFiles() error {
|
||||||
|
|
@ -86,6 +87,12 @@ func RegisterRouter(router *gin.Engine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var secret string
|
||||||
|
var port int
|
||||||
|
flag.StringVar(&secret, "secret", "", "服务器密钥")
|
||||||
|
flag.IntVar(&port, "p", 3001, "端口号")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
// 读取配置文件
|
// 读取配置文件
|
||||||
err := script.LoadServerConfig()
|
err := script.LoadServerConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -115,24 +122,21 @@ func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
// 注册路由
|
// 注册路由
|
||||||
RegisterRouter(router)
|
RegisterRouter(router)
|
||||||
// 启动服务
|
|
||||||
var port = ":3001"
|
portStr := fmt.Sprintf(":%d", port)
|
||||||
url := "http://localhost" + port
|
url := "http://localhost" + portStr
|
||||||
ip := script.GetIpAddress()
|
ip := script.GetIpAddress()
|
||||||
startLog := "beancount-gs start at " + url
|
startLog := "beancount-gs start at " + url
|
||||||
if ip != "" {
|
if ip != "" {
|
||||||
startLog += " or http://" + ip + port
|
startLog += " or http://" + ip + portStr
|
||||||
}
|
}
|
||||||
script.LogSystemInfo(startLog)
|
script.LogSystemInfo(startLog)
|
||||||
|
// 打开浏览器
|
||||||
// cmd /c start
|
script.OpenBrowser(url)
|
||||||
cmd := exec.Command("cmd", "/C", "start", url)
|
// 打印密钥
|
||||||
err = cmd.Start()
|
script.LogSystemInfo("Secret token is " + script.GenerateServerSecret(secret))
|
||||||
if err != nil {
|
// 启动服务
|
||||||
script.LogSystemError("Failed to open browser, error is " + err.Error())
|
err = router.Run(portStr)
|
||||||
}
|
|
||||||
|
|
||||||
err = router.Run(port)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
script.LogSystemError("Failed to start server, " + err.Error())
|
script.LogSystemError("Failed to start server, " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,3 +36,7 @@ func LedgerIsNotAllowAccess(c *gin.Context) {
|
||||||
func DuplicateAccount(c *gin.Context) {
|
func DuplicateAccount(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{"code": 1007})
|
c.JSON(http.StatusOK, gin.H{"code": 1007})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ServerSecretNotMatch(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": 1008})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ func QueryServerConfig(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateConfigForm struct {
|
type UpdateConfigForm struct {
|
||||||
|
Secret string `form:"secret" binding:"required"`
|
||||||
StartDate string `form:"startDate" binding:"required"`
|
StartDate string `form:"startDate" binding:"required"`
|
||||||
DataPath string `form:"dataPath" binding:"required"`
|
DataPath string `form:"dataPath" binding:"required"`
|
||||||
OperatingCurrency string `form:"operatingCurrency" binding:"required"`
|
OperatingCurrency string `form:"operatingCurrency" binding:"required"`
|
||||||
|
|
@ -40,6 +41,10 @@ func UpdateServerConfig(c *gin.Context) {
|
||||||
BadRequest(c, err.Error())
|
BadRequest(c, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !script.EqualServerSecret(updateConfigForm.Secret) {
|
||||||
|
ServerSecretNotMatch(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
var serverConfig = script.Config{
|
var serverConfig = script.Config{
|
||||||
OperatingCurrency: updateConfigForm.OperatingCurrency,
|
OperatingCurrency: updateConfigForm.OperatingCurrency,
|
||||||
DataPath: updateConfigForm.DataPath,
|
DataPath: updateConfigForm.DataPath,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue