Translated error msg

This commit is contained in:
0xJacky 2022-08-11 21:55:29 +08:00
parent b2e837f4b1
commit c747390f94
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
14 changed files with 259 additions and 189 deletions

View file

@ -48,8 +48,8 @@ const props = defineProps({
const visible = ref(false) const visible = ref(false)
const update = ref(0) const update = ref(0)
let data = reactive({id: null}) const data: any = reactive({id: null})
let error = reactive({}) const error: any = reactive({})
const params = reactive({}) const params = reactive({})
const selected = reactive([]) const selected = reactive([])
@ -64,9 +64,11 @@ function editableColumns() {
} }
function add() { function add() {
data = reactive({ Object.keys(data).forEach(v => {
id: null delete data[v]
}) })
clear_error()
visible.value = true visible.value = true
} }
@ -76,8 +78,15 @@ interface Table {
get_list(): void get_list(): void
} }
function clear_error() {
Object.keys(error).forEach(v => {
delete error[v]
})
}
const ok = async () => { const ok = async () => {
error = reactive({}) clear_error()
props.api!.save(data.id, data).then((r: any) => { props.api!.save(data.id, data).then((r: any) => {
message.success($gettext('Save Successfully')) message.success($gettext('Save Successfully'))
Object.assign(data, r) Object.assign(data, r)
@ -85,14 +94,15 @@ const ok = async () => {
t!.get_list() t!.get_list()
}).catch((e: any) => { }).catch((e: any) => {
message.error((e?.message ?? $gettext('Server error')), 5) message.error($gettext(e?.message ?? 'Server error'), 5)
error = e.errors Object.assign(error, e.errors)
}) })
} }
function cancel() { function cancel() {
visible.value = false visible.value = false
error = reactive({})
clear_error()
} }
function edit(id: any) { function edit(id: any) {
@ -100,7 +110,7 @@ function edit(id: any) {
Object.assign(data, r) Object.assign(data, r)
visible.value = true visible.value = true
}).catch((e: any) => { }).catch((e: any) => {
message.error((e?.message ?? $gettext('Server error')), 5) message.error($gettext(e?.message ?? 'Server error'), 5)
}) })
} }

View file

@ -74,7 +74,7 @@ let params = reactive({
...route.query, ...route.query,
...props.get_params ...props.get_params
}) })
let selectedRowKeys = ref([]) const selectedRowKeys = ref([])
const rowSelection = reactive({}) const rowSelection = reactive({})
const searchColumns = getSearchColumns() const searchColumns = getSearchColumns()
@ -91,7 +91,7 @@ function destroy(id: any) {
get_list() get_list()
message.success(interpolate($gettext('Delete ID: %{id}'), {id: id})) message.success(interpolate($gettext('Delete ID: %{id}'), {id: id}))
}).catch((e: any) => { }).catch((e: any) => {
message.error(e?.message ?? $gettext('Server error')) message.error($gettext(e?.message ?? 'Server error'))
}) })
} }
@ -149,7 +149,7 @@ function checked(c: any) {
} }
function onSelectChange(_selectedRowKeys: any) { function onSelectChange(_selectedRowKeys: any) {
selectedRowKeys = reactive(_selectedRowKeys) selectedRowKeys.value = _selectedRowKeys
// this.$emit('selected', selectedRowKeys) // this.$emit('selected', selectedRowKeys)
} }

View file

@ -3,5 +3,8 @@ import gettext from '@/gettext'
const {$gettext} = gettext const {$gettext} = gettext
export const msg = [ export const msg = [
$gettext('The username or password is incorrect') $gettext('The username or password is incorrect'),
$gettext('Prohibit changing root password in demo'),
$gettext('Prohibit deleting the default user'),
$gettext('Failed to get certificate information'),
] ]

View file

@ -18,7 +18,7 @@ msgstr "About"
msgid "Action" msgid "Action"
msgstr "Action" msgstr "Action"
#: src/components/StdDataDisplay/StdCurd.vue:113 #: src/components/StdDataDisplay/StdCurd.vue:123
#: src/components/StdDataDisplay/StdCurd.vue:24 #: src/components/StdDataDisplay/StdCurd.vue:24
msgid "Add" msgid "Add"
msgstr "" msgstr ""
@ -247,6 +247,10 @@ msgstr "Failed to disable %{msg}"
msgid "Failed to enable %{msg}" msgid "Failed to enable %{msg}"
msgstr "Failed to enable %{msg}" msgstr "Failed to enable %{msg}"
#: src/language/constants.ts:9
msgid "Failed to get certificate information"
msgstr ""
#: src/views/other/Error.vue:3 src/views/other/Error.vue:4 #: src/views/other/Error.vue:3 src/views/other/Error.vue:4
msgid "File Not Found" msgid "File Not Found"
msgstr "File Not Found" msgstr "File Not Found"
@ -431,6 +435,14 @@ msgstr "Please input your password!"
msgid "Please input your username!" msgid "Please input your username!"
msgstr "Please input your username!" msgstr "Please input your username!"
#: src/language/constants.ts:7
msgid "Prohibit changing root password in demo"
msgstr ""
#: src/language/constants.ts:8
msgid "Prohibit deleting the default user"
msgstr ""
#: src/views/other/About.vue:19 #: src/views/other/About.vue:19
msgid "Project Team" msgid "Project Team"
msgstr "Project Team" msgstr "Project Team"
@ -461,7 +473,7 @@ msgstr "Save Directive"
msgid "Save error %{msg}" msgid "Save error %{msg}"
msgstr "Save error %{msg}" msgstr "Save error %{msg}"
#: src/components/StdDataDisplay/StdCurd.vue:82 #: src/components/StdDataDisplay/StdCurd.vue:91
#, fuzzy #, fuzzy
msgid "Save Successfully" msgid "Save Successfully"
msgstr "Saved successfully" msgstr "Saved successfully"
@ -475,8 +487,6 @@ msgstr "Saved successfully"
msgid "Send" msgid "Send"
msgstr "Send" msgstr "Send"
#: src/components/StdDataDisplay/StdCurd.vue:103
#: src/components/StdDataDisplay/StdCurd.vue:88
#: src/components/StdDataDisplay/StdTable.vue:112 #: src/components/StdDataDisplay/StdTable.vue:112
#: src/components/StdDataDisplay/StdTable.vue:94 #: src/components/StdDataDisplay/StdTable.vue:94
#: src/views/config/ConfigEdit.vue:22 src/views/domain/DomainEdit.vue:44 #: src/views/config/ConfigEdit.vue:22 src/views/domain/DomainEdit.vue:44

View file

@ -12,7 +12,7 @@ msgstr ""
msgid "Action" msgid "Action"
msgstr "" msgstr ""
#: src/components/StdDataDisplay/StdCurd.vue:113 #: src/components/StdDataDisplay/StdCurd.vue:123
#: src/components/StdDataDisplay/StdCurd.vue:24 #: src/components/StdDataDisplay/StdCurd.vue:24
msgid "Add" msgid "Add"
msgstr "" msgstr ""
@ -249,6 +249,10 @@ msgstr ""
msgid "Failed to enable %{msg}" msgid "Failed to enable %{msg}"
msgstr "" msgstr ""
#: src/language/constants.ts:9
msgid "Failed to get certificate information"
msgstr ""
#: src/views/other/Error.vue:3 #: src/views/other/Error.vue:3
#: src/views/other/Error.vue:4 #: src/views/other/Error.vue:4
msgid "File Not Found" msgid "File Not Found"
@ -433,6 +437,14 @@ msgstr ""
msgid "Please input your username!" msgid "Please input your username!"
msgstr "" msgstr ""
#: src/language/constants.ts:7
msgid "Prohibit changing root password in demo"
msgstr ""
#: src/language/constants.ts:8
msgid "Prohibit deleting the default user"
msgstr ""
#: src/views/other/About.vue:19 #: src/views/other/About.vue:19
msgid "Project Team" msgid "Project Team"
msgstr "" msgstr ""
@ -466,7 +478,7 @@ msgstr ""
msgid "Save error %{msg}" msgid "Save error %{msg}"
msgstr "" msgstr ""
#: src/components/StdDataDisplay/StdCurd.vue:82 #: src/components/StdDataDisplay/StdCurd.vue:91
msgid "Save Successfully" msgid "Save Successfully"
msgstr "" msgstr ""
@ -481,8 +493,6 @@ msgstr ""
msgid "Send" msgid "Send"
msgstr "" msgstr ""
#: src/components/StdDataDisplay/StdCurd.vue:103
#: src/components/StdDataDisplay/StdCurd.vue:88
#: src/components/StdDataDisplay/StdTable.vue:112 #: src/components/StdDataDisplay/StdTable.vue:112
#: src/components/StdDataDisplay/StdTable.vue:94 #: src/components/StdDataDisplay/StdTable.vue:94
#: src/views/config/ConfigEdit.vue:22 #: src/views/config/ConfigEdit.vue:22

File diff suppressed because one or more lines are too long

Binary file not shown.

View file

@ -21,7 +21,7 @@ msgstr "关于"
msgid "Action" msgid "Action"
msgstr "操作" msgstr "操作"
#: src/components/StdDataDisplay/StdCurd.vue:113 #: src/components/StdDataDisplay/StdCurd.vue:123
#: src/components/StdDataDisplay/StdCurd.vue:24 #: src/components/StdDataDisplay/StdCurd.vue:24
msgid "Add" msgid "Add"
msgstr "添加" msgstr "添加"
@ -248,6 +248,10 @@ msgstr "禁用失败 %{msg}"
msgid "Failed to enable %{msg}" msgid "Failed to enable %{msg}"
msgstr "启用失败 %{msg}" msgstr "启用失败 %{msg}"
#: src/language/constants.ts:9
msgid "Failed to get certificate information"
msgstr "获取证书信息失败"
#: src/views/other/Error.vue:3 src/views/other/Error.vue:4 #: src/views/other/Error.vue:3 src/views/other/Error.vue:4
msgid "File Not Found" msgid "File Not Found"
msgstr "未找到文件" msgstr "未找到文件"
@ -428,6 +432,14 @@ msgstr "请输入您的密码!"
msgid "Please input your username!" msgid "Please input your username!"
msgstr "请输入您的用户名!" msgstr "请输入您的用户名!"
#: src/language/constants.ts:7
msgid "Prohibit changing root password in demo"
msgstr "禁止在演示模式下修改 root 账户的密码"
#: src/language/constants.ts:8
msgid "Prohibit deleting the default user"
msgstr "禁止删除默认用户"
#: src/views/other/About.vue:19 #: src/views/other/About.vue:19
msgid "Project Team" msgid "Project Team"
msgstr "项目团队" msgstr "项目团队"
@ -458,7 +470,7 @@ msgstr "保存指令"
msgid "Save error %{msg}" msgid "Save error %{msg}"
msgstr "保存错误 %{msg}" msgstr "保存错误 %{msg}"
#: src/components/StdDataDisplay/StdCurd.vue:82 #: src/components/StdDataDisplay/StdCurd.vue:91
msgid "Save Successfully" msgid "Save Successfully"
msgstr "保存成功" msgstr "保存成功"
@ -471,8 +483,6 @@ msgstr "保存成功"
msgid "Send" msgid "Send"
msgstr "上传" msgstr "上传"
#: src/components/StdDataDisplay/StdCurd.vue:103
#: src/components/StdDataDisplay/StdCurd.vue:88
#: src/components/StdDataDisplay/StdTable.vue:112 #: src/components/StdDataDisplay/StdTable.vue:112
#: src/components/StdDataDisplay/StdTable.vue:94 #: src/components/StdDataDisplay/StdTable.vue:94
#: src/views/config/ConfigEdit.vue:22 src/views/domain/DomainEdit.vue:44 #: src/views/config/ConfigEdit.vue:22 src/views/domain/DomainEdit.vue:44

View file

@ -22,7 +22,7 @@ msgstr "關於"
msgid "Action" msgid "Action"
msgstr "操作" msgstr "操作"
#: src/components/StdDataDisplay/StdCurd.vue:113 #: src/components/StdDataDisplay/StdCurd.vue:123
#: src/components/StdDataDisplay/StdCurd.vue:24 #: src/components/StdDataDisplay/StdCurd.vue:24
msgid "Add" msgid "Add"
msgstr "" msgstr ""
@ -252,6 +252,10 @@ msgstr "禁用失敗 %{msg}"
msgid "Failed to enable %{msg}" msgid "Failed to enable %{msg}"
msgstr "啟用失敗 %{msg}" msgstr "啟用失敗 %{msg}"
#: src/language/constants.ts:9
msgid "Failed to get certificate information"
msgstr ""
#: src/views/other/Error.vue:3 src/views/other/Error.vue:4 #: src/views/other/Error.vue:3 src/views/other/Error.vue:4
msgid "File Not Found" msgid "File Not Found"
msgstr "未找到檔案" msgstr "未找到檔案"
@ -436,6 +440,14 @@ msgstr "請輸入您的密碼!"
msgid "Please input your username!" msgid "Please input your username!"
msgstr "請輸入您的使用者名稱!" msgstr "請輸入您的使用者名稱!"
#: src/language/constants.ts:7
msgid "Prohibit changing root password in demo"
msgstr ""
#: src/language/constants.ts:8
msgid "Prohibit deleting the default user"
msgstr ""
#: src/views/other/About.vue:19 #: src/views/other/About.vue:19
msgid "Project Team" msgid "Project Team"
msgstr "專案團隊" msgstr "專案團隊"
@ -466,7 +478,7 @@ msgstr "儲存指令"
msgid "Save error %{msg}" msgid "Save error %{msg}"
msgstr "儲存錯誤 %{msg}" msgstr "儲存錯誤 %{msg}"
#: src/components/StdDataDisplay/StdCurd.vue:82 #: src/components/StdDataDisplay/StdCurd.vue:91
#, fuzzy #, fuzzy
msgid "Save Successfully" msgid "Save Successfully"
msgstr "儲存成功" msgstr "儲存成功"
@ -480,8 +492,6 @@ msgstr "儲存成功"
msgid "Send" msgid "Send"
msgstr "上傳" msgstr "上傳"
#: src/components/StdDataDisplay/StdCurd.vue:103
#: src/components/StdDataDisplay/StdCurd.vue:88
#: src/components/StdDataDisplay/StdTable.vue:112 #: src/components/StdDataDisplay/StdTable.vue:112
#: src/components/StdDataDisplay/StdTable.vue:94 #: src/components/StdDataDisplay/StdTable.vue:94
#: src/views/config/ConfigEdit.vue:22 src/views/domain/DomainEdit.vue:44 #: src/views/config/ConfigEdit.vue:22 src/views/domain/DomainEdit.vue:44

View file

@ -17,7 +17,8 @@ func CertInfo(c *gin.Context) {
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"error": err, "message": "Failed to get cert information",
"error": err,
}) })
return return
} }

View file

@ -1,129 +1,133 @@
package api package api
import ( import (
"errors" "github.com/0xJacky/Nginx-UI/server/model"
"github.com/0xJacky/Nginx-UI/server/model" "github.com/0xJacky/Nginx-UI/server/settings"
"github.com/0xJacky/Nginx-UI/server/settings" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin" "github.com/spf13/cast"
"github.com/spf13/cast" "golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/bcrypt" "net/http"
"net/http"
) )
func GetUsers(c *gin.Context) { func GetUsers(c *gin.Context) {
data := model.GetUserList(c, c.Query("name")) data := model.GetUserList(c, c.Query("name"))
c.JSON(http.StatusOK, data) c.JSON(http.StatusOK, data)
} }
func GetUser(c *gin.Context) { func GetUser(c *gin.Context) {
curd := model.NewCurd(&model.Auth{}) curd := model.NewCurd(&model.Auth{})
id := c.Param("id") id := c.Param("id")
var user model.Auth var user model.Auth
err := curd.First(&user, id) err := curd.First(&user, id)
if err != nil { if err != nil {
ErrHandler(c, err) ErrHandler(c, err)
return return
} }
c.JSON(http.StatusOK, user) c.JSON(http.StatusOK, user)
} }
type UserJson struct { type UserJson struct {
Name string `json:"name" binding:"required,max=255"` Name string `json:"name" binding:"required,max=255"`
Password string `json:"password" binding:"max=255"` Password string `json:"password" binding:"max=255"`
} }
func AddUser(c *gin.Context) { func AddUser(c *gin.Context) {
var json UserJson var json UserJson
ok := BindAndValid(c, &json) ok := BindAndValid(c, &json)
if !ok { if !ok {
return return
} }
curd := model.NewCurd(&model.Auth{}) curd := model.NewCurd(&model.Auth{})
pwd, err := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost) pwd, err := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
if err != nil { if err != nil {
ErrHandler(c, err) ErrHandler(c, err)
return return
} }
json.Password = string(pwd) json.Password = string(pwd)
user := model.Auth{ user := model.Auth{
Name: json.Name, Name: json.Name,
Password: json.Password, Password: json.Password,
} }
err = curd.Add(&user) err = curd.Add(&user)
if err != nil { if err != nil {
ErrHandler(c, err) ErrHandler(c, err)
return return
} }
c.JSON(http.StatusOK, user) c.JSON(http.StatusOK, user)
} }
func EditUser(c *gin.Context) { func EditUser(c *gin.Context) {
userId := cast.ToInt(c.Param("id")) userId := cast.ToInt(c.Param("id"))
if settings.ServerSettings.Demo && userId == 1 { if settings.ServerSettings.Demo && userId == 1 {
ErrHandler(c, errors.New("not allow to change the root password in demo")) c.JSON(http.StatusNotAcceptable, gin.H{
return "message": "Prohibit changing root password in demo",
} })
return
}
var json UserJson var json UserJson
ok := BindAndValid(c, &json) ok := BindAndValid(c, &json)
if !ok { if !ok {
return return
} }
curd := model.NewCurd(&model.Auth{}) curd := model.NewCurd(&model.Auth{})
var user, edit model.Auth var user, edit model.Auth
err := curd.First(&user, userId) err := curd.First(&user, userId)
if err != nil { if err != nil {
ErrHandler(c, err) ErrHandler(c, err)
return return
} }
edit.Name = json.Name edit.Name = json.Name
// encrypt passowrd // encrypt password
if json.Password != "" { if json.Password != "" {
var pwd []byte var pwd []byte
pwd, err = bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost) pwd, err = bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
if err != nil { if err != nil {
ErrHandler(c, err) ErrHandler(c, err)
return return
} }
edit.Password = string(pwd) edit.Password = string(pwd)
} }
err = curd.Edit(&user, &edit) err = curd.Edit(&user, &edit)
if err != nil { if err != nil {
ErrHandler(c, err) ErrHandler(c, err)
return return
} }
c.JSON(http.StatusOK, user) c.JSON(http.StatusOK, user)
} }
func DeleteUser(c *gin.Context) { func DeleteUser(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
if cast.ToInt(id) == 1 { if cast.ToInt(id) == 1 {
ErrHandler(c, errors.New("not allow to delete the default user")) c.JSON(http.StatusNotAcceptable, gin.H{
return "message": "Prohibit deleting the default user",
} })
curd := model.NewCurd(&model.Auth{}) return
err := curd.Delete(&model.Auth{}, "id", id) }
if err != nil {
ErrHandler(c, err) curd := model.NewCurd(&model.Auth{})
return err := curd.Delete(&model.Auth{}, "id", id)
} if err != nil {
c.JSON(http.StatusNoContent, gin.H{}) ErrHandler(c, err)
return
}
c.JSON(http.StatusNoContent, gin.H{})
} }

View file

@ -0,0 +1,35 @@
package cert
import (
"github.com/0xJacky/Nginx-UI/server/model"
"log"
"time"
)
func AutoCert() {
defer func() {
if err := recover(); err != nil {
log.Println("[AutoCert] Recover", err)
}
}()
log.Println("[AutoCert] Start")
autoCertList := model.GetAutoCertList()
for i := range autoCertList {
domain := autoCertList[i].Domain
key, err := GetCertInfo(domain)
if err != nil {
log.Println("GetCertInfo Err", err)
// Get certificate info error, ignore this domain
continue
}
// before 1 mo
if time.Now().Before(key.NotBefore.AddDate(0, 1, 0)) {
continue
}
// after 1 mo, reissue certificate
err = IssueCert(domain)
if err != nil {
log.Println(err)
}
}
}

View file

@ -5,9 +5,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"crypto/tls"
"crypto/x509"
"github.com/0xJacky/Nginx-UI/server/model"
"github.com/0xJacky/Nginx-UI/server/pkg/nginx" "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
"github.com/0xJacky/Nginx-UI/server/settings" "github.com/0xJacky/Nginx-UI/server/settings"
"github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/certcrypto"
@ -16,13 +13,9 @@ import (
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration" "github.com/go-acme/lego/v4/registration"
"github.com/pkg/errors" "github.com/pkg/errors"
"io"
"log" "log"
"net"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"time"
) )
// MyUser You'll need a user or account type that implements acme.User // MyUser You'll need a user or account type that implements acme.User
@ -42,69 +35,6 @@ func (u *MyUser) GetPrivateKey() crypto.PrivateKey {
return u.key return u.key
} }
func AutoCert() {
defer func() {
if err := recover(); err != nil {
log.Println("[AutoCert] Recover", err)
}
}()
log.Println("[AutoCert] Start")
autoCertList := model.GetAutoCertList()
for i := range autoCertList {
domain := autoCertList[i].Domain
key, err := GetCertInfo(domain)
if err != nil {
log.Println("GetCertInfo Err", err)
// Get certificate info error, ignore this domain
continue
}
// before 1 mo
if time.Now().Before(key.NotBefore.AddDate(0, 1, 0)) {
continue
}
// after 1 mo, reissue certificate
err = IssueCert(domain)
if err != nil {
log.Println(err)
}
}
}
func GetCertInfo(domain string) (key *x509.Certificate, err error) {
var response *http.Response
client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 5 * time.Second,
}
response, err = client.Get("https://" + domain)
if err != nil {
err = errors.Wrap(err, "get cert info error")
return
}
defer func(Body io.ReadCloser) {
err = Body.Close()
if err != nil {
log.Println(err)
return
}
}(response.Body)
key = response.TLS.PeerCertificates[0]
return
}
func IssueCert(domain string) error { func IssueCert(domain string) error {
// Create a user. New accounts need an email and private key to start. // Create a user. New accounts need an email and private key to start.
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)

View file

@ -0,0 +1,47 @@
package cert
import (
"crypto/tls"
"crypto/x509"
"github.com/pkg/errors"
"io"
"log"
"net"
"net/http"
"time"
)
func GetCertInfo(domain string) (key *x509.Certificate, err error) {
var response *http.Response
client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 5 * time.Second,
}
response, err = client.Get("https://" + domain)
if err != nil {
err = errors.Wrap(err, "get cert info error")
return
}
defer func(Body io.ReadCloser) {
err = Body.Close()
if err != nil {
log.Println(err)
return
}
}(response.Body)
key = response.TLS.PeerCertificates[0]
return
}