Skip to content

Commit

Permalink
优化json修改
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Mar 17, 2021
1 parent b6aefc8 commit a28981b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 42 deletions.
4 changes: 1 addition & 3 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ func upgradeConfig() {
if domain == "" {
return
}
config := core.Load("")
config.SSl.Sni = domain
core.Save(config, "")
core.WriteDomain(domain)
trojan.Restart()
}

Expand Down
66 changes: 34 additions & 32 deletions core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package core
import (
"encoding/json"
"fmt"
"github.com/tidwall/pretty"
"github.com/tidwall/sjson"
"io/ioutil"
)

Expand Down Expand Up @@ -34,7 +36,7 @@ type ServerTCP struct {
}

// Load 加载服务端配置文件
func Load(path string) *ServerConfig {
func Load(path string) []byte {
if path == "" {
path = configPath
}
Expand All @@ -43,71 +45,71 @@ func Load(path string) *ServerConfig {
fmt.Println(err)
return nil
}
config := ServerConfig{}
if err := json.Unmarshal(data, &config); err != nil {
fmt.Println(err)
return nil
}
return &config
return data
}

// Save 保存服务端配置文件
func Save(config *ServerConfig, path string) bool {
func Save(data []byte, path string) bool {
if path == "" {
path = configPath
}
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
if err := ioutil.WriteFile(path, pretty.Pretty(data), 0644); err != nil {
fmt.Println(err)
return false
}
if err = ioutil.WriteFile(path, data, 0644); err != nil {
return true
}

// GetConfig 获取config配置
func GetConfig() *ServerConfig {
data := Load("")
config := ServerConfig{}
if err := json.Unmarshal(data, &config); err != nil {
fmt.Println(err)
return false
return nil
}
return true
return &config
}

// GetMysql 获取mysql连接
func GetMysql() *Mysql {
config := Load("")
return &config.Mysql
return &GetConfig().Mysql
}

// WriteMysql 写mysql配置
func WriteMysql(mysql *Mysql) bool {
mysql.Enabled = true
config := Load("")
config.Mysql = *mysql
return Save(config, "")
data := Load("")
result, _ := sjson.SetBytes(data, "mysql", mysql)
return Save(result, "")
}

// WriteTls 写tls配置
func WriteTls(cert, key, domain string) bool {
config := Load("")
config.SSl.Cert = cert
config.SSl.Key = key
config.SSl.Sni = domain
return Save(config, "")
data := Load("")
data, _ = sjson.SetBytes(data, "ssl.cert", cert)
data, _ = sjson.SetBytes(data, "ssl.key", key)
data, _ = sjson.SetBytes(data, "ssl.sni", domain)
return Save(data, "")
}

// WriteDomain 写域名
func WriteDomain(domain string) bool {
config := Load("")
config.SSl.Sni = domain
return Save(config, "")
data := Load("")
data, _ = sjson.SetBytes(data, "ssl.sni", domain)
return Save(data, "")
}

// WritePassword 写密码
func WritePassword(pass []string) bool {
config := Load("")
config.Password = pass
return Save(config, "")
data := Load("")
data, _ = sjson.SetBytes(data, "password", pass)
return Save(data, "")
}

// WriteLogLevel 写日志等级
func WriteLogLevel(level int) bool {
config := Load("")
config.LogLevel = level
return Save(config, "")
data := Load("")
data, _ = sjson.SetBytes(data, "log_level", level)
return Save(data, "")
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ require (
github.com/shirou/gopsutil v3.21.2+incompatible
github.com/spf13/cobra v1.1.3
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954
github.com/tidwall/pretty v1.0.2
github.com/tidwall/sjson v1.1.5
github.com/tklauser/go-sysconf v0.3.4 // indirect
)
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,17 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 h1:xQdMZ1WLrgkkvOZ/LDQxjVxMLdby7osSh4ZEVa5sIjs=
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM=
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/gjson v1.6.8 h1:CTmXMClGYPAmln7652e69B7OLXfTi5ABcPPwjIWUv7w=
github.com/tidwall/gjson v1.6.8/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/sjson v1.1.5 h1:wsUceI/XDyZk3J1FUvuuYlK62zJv2HO2Pzb8A5EWdUE=
github.com/tidwall/sjson v1.1.5/go.mod h1:VuJzsZnTowhSxWdOgsAnb886i4AjEyTkk7tNtsL7EYE=
github.com/tklauser/go-sysconf v0.3.4 h1:HT8SVixZd3IzLdfs/xlpq0jeSfTX57g1v6wB1EuzV7M=
github.com/tklauser/go-sysconf v0.3.4/go.mod h1:Cl2c8ZRWfHD5IrfHo9VN+FX9kCFjIOyVklgXycLB6ek=
github.com/tklauser/numcpus v0.2.1 h1:ct88eFm+Q7m2ZfXJdan1xYoXKlmwsfP+k88q05KvlZc=
Expand Down
2 changes: 1 addition & 1 deletion trojan/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ func SetDomain(domain string) {

// GetDomainAndPort 获取域名和端口
func GetDomainAndPort() (string, int) {
config := core.Load("")
config := core.GetConfig()
return config.SSl.Sni, config.LocalPort
}
4 changes: 2 additions & 2 deletions web/controller/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func SetLogLevel(level int) *ResponseBody {
func GetLogLevel() *ResponseBody {
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
config := core.Load("")
config := core.GetConfig()
responseBody.Data = map[string]interface{}{
"loglevel": config.LogLevel,
"loglevel": &config.LogLevel,
}
return &responseBody
}
Expand Down
2 changes: 1 addition & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func Start(host string, port int, isSSL bool) {
controller.CollectTask()
util.OpenPort(port)
if isSSL {
config := core.Load("")
config := core.GetConfig()
ssl := &config.SSl
router.RunTLS(fmt.Sprintf("%s:%d", host, port), ssl.Cert, ssl.Key)
} else {
Expand Down

0 comments on commit a28981b

Please sign in to comment.