Skip to content

Commit

Permalink
feat: Hide secret
Browse files Browse the repository at this point in the history
  • Loading branch information
jie authored and jie committed Sep 2, 2020
1 parent e279c53 commit 1c0b738
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
17 changes: 15 additions & 2 deletions web/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@ import (
func Save(writer http.ResponseWriter, request *http.Request) {

conf := &config.Config{}
// 初始化以前的配置
conf.InitConfigFromFile()

idNew := request.FormValue("DnsID")
secretNew := request.FormValue("DnsSecret")

idHide, secretHide := getHideIDSecret(conf)

if idNew != idHide {
conf.DNS.ID = idNew
}
if secretNew != secretHide {
conf.DNS.Secret = secretNew
}

// 覆盖以前的配置
conf.DNS.Name = request.FormValue("DnsName")
conf.DNS.ID = request.FormValue("DnsID")
conf.DNS.Secret = request.FormValue("DnsSecret")

conf.Ipv4.Enable = request.FormValue("Ipv4Enable") == "on"
conf.Ipv4.URL = request.FormValue("Ipv4Url")
Expand Down
41 changes: 23 additions & 18 deletions web/writing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import (
"ddns-go/config"
"ddns-go/util"
"log"
"strings"

"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"

"gopkg.in/yaml.v2"
)

// Writing 步骤二,填写信息
// Writing 填写信息
func Writing(writer http.ResponseWriter, request *http.Request) {
tempPath, err := util.GetStaticResourcePath("static/pages/writing.html")
if err != nil {
Expand All @@ -29,20 +26,14 @@ func Writing(writer http.ResponseWriter, request *http.Request) {
}

conf := &config.Config{}

// 解析文件
var configFile string = util.GetConfigFilePath()
_, err = os.Stat(configFile)
err = conf.InitConfigFromFile()
if err == nil {
// 不为空,解析文件
byt, err := ioutil.ReadFile(configFile)
if err == nil {
err = yaml.Unmarshal(byt, conf)
if err == nil {
tmpl.Execute(writer, conf)
return
}
}
// 隐藏真实的ID、Secret
idHide, secretHide := getHideIDSecret(conf)
conf.DNS.ID = idHide
conf.DNS.Secret = secretHide
tmpl.Execute(writer, conf)
return
}

// 默认值
Expand All @@ -59,3 +50,17 @@ func Writing(writer http.ResponseWriter, request *http.Request) {

tmpl.Execute(writer, conf)
}

// 显示的数量
const displayCount int = 3

// hideIDSecret 隐藏真实的ID、Secret
func getHideIDSecret(conf *config.Config) (idHide string, secretHide string) {
if len(conf.DNS.ID) > displayCount {
idHide = conf.DNS.ID[:displayCount] + strings.Repeat("*", len(conf.DNS.ID)-displayCount)
}
if len(conf.DNS.Secret) > displayCount {
secretHide = conf.DNS.Secret[:displayCount] + strings.Repeat("*", len(conf.DNS.Secret)-displayCount)
}
return
}

0 comments on commit 1c0b738

Please sign in to comment.