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 update = ref(0)
let data = reactive({id: null})
let error = reactive({})
const data: any = reactive({id: null})
const error: any = reactive({})
const params = reactive({})
const selected = reactive([])
@ -64,9 +64,11 @@ function editableColumns() {
}
function add() {
data = reactive({
id: null
Object.keys(data).forEach(v => {
delete data[v]
})
clear_error()
visible.value = true
}
@ -76,8 +78,15 @@ interface Table {
get_list(): void
}
function clear_error() {
Object.keys(error).forEach(v => {
delete error[v]
})
}
const ok = async () => {
error = reactive({})
clear_error()
props.api!.save(data.id, data).then((r: any) => {
message.success($gettext('Save Successfully'))
Object.assign(data, r)
@ -85,14 +94,15 @@ const ok = async () => {
t!.get_list()
}).catch((e: any) => {
message.error((e?.message ?? $gettext('Server error')), 5)
error = e.errors
message.error($gettext(e?.message ?? 'Server error'), 5)
Object.assign(error, e.errors)
})
}
function cancel() {
visible.value = false
error = reactive({})
clear_error()
}
function edit(id: any) {
@ -100,7 +110,7 @@ function edit(id: any) {
Object.assign(data, r)
visible.value = true
}).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,
...props.get_params
})
let selectedRowKeys = ref([])
const selectedRowKeys = ref([])
const rowSelection = reactive({})
const searchColumns = getSearchColumns()
@ -91,7 +91,7 @@ function destroy(id: any) {
get_list()
message.success(interpolate($gettext('Delete ID: %{id}'), {id: id}))
}).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) {
selectedRowKeys = reactive(_selectedRowKeys)
selectedRowKeys.value = _selectedRowKeys
// this.$emit('selected', selectedRowKeys)
}

View file

@ -3,5 +3,8 @@ import gettext from '@/gettext'
const {$gettext} = gettext
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"
msgstr "Action"
#: src/components/StdDataDisplay/StdCurd.vue:113
#: src/components/StdDataDisplay/StdCurd.vue:123
#: src/components/StdDataDisplay/StdCurd.vue:24
msgid "Add"
msgstr ""
@ -247,6 +247,10 @@ msgstr "Failed to disable %{msg}"
msgid "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
msgid "File Not Found"
msgstr "File Not Found"
@ -431,6 +435,14 @@ msgstr "Please input your password!"
msgid "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
msgid "Project Team"
msgstr "Project Team"
@ -461,7 +473,7 @@ msgstr "Save Directive"
msgid "Save error %{msg}"
msgstr "Save error %{msg}"
#: src/components/StdDataDisplay/StdCurd.vue:82
#: src/components/StdDataDisplay/StdCurd.vue:91
#, fuzzy
msgid "Save Successfully"
msgstr "Saved successfully"
@ -475,8 +487,6 @@ msgstr "Saved successfully"
msgid "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:94
#: src/views/config/ConfigEdit.vue:22 src/views/domain/DomainEdit.vue:44

View file

@ -12,7 +12,7 @@ msgstr ""
msgid "Action"
msgstr ""
#: src/components/StdDataDisplay/StdCurd.vue:113
#: src/components/StdDataDisplay/StdCurd.vue:123
#: src/components/StdDataDisplay/StdCurd.vue:24
msgid "Add"
msgstr ""
@ -249,6 +249,10 @@ msgstr ""
msgid "Failed to enable %{msg}"
msgstr ""
#: src/language/constants.ts:9
msgid "Failed to get certificate information"
msgstr ""
#: src/views/other/Error.vue:3
#: src/views/other/Error.vue:4
msgid "File Not Found"
@ -433,6 +437,14 @@ msgstr ""
msgid "Please input your username!"
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
msgid "Project Team"
msgstr ""
@ -466,7 +478,7 @@ msgstr ""
msgid "Save error %{msg}"
msgstr ""
#: src/components/StdDataDisplay/StdCurd.vue:82
#: src/components/StdDataDisplay/StdCurd.vue:91
msgid "Save Successfully"
msgstr ""
@ -481,8 +493,6 @@ msgstr ""
msgid "Send"
msgstr ""
#: src/components/StdDataDisplay/StdCurd.vue:103
#: src/components/StdDataDisplay/StdCurd.vue:88
#: src/components/StdDataDisplay/StdTable.vue:112
#: src/components/StdDataDisplay/StdTable.vue:94
#: 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"
msgstr "操作"
#: src/components/StdDataDisplay/StdCurd.vue:113
#: src/components/StdDataDisplay/StdCurd.vue:123
#: src/components/StdDataDisplay/StdCurd.vue:24
msgid "Add"
msgstr "添加"
@ -248,6 +248,10 @@ msgstr "禁用失败 %{msg}"
msgid "Failed to enable %{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
msgid "File Not Found"
msgstr "未找到文件"
@ -428,6 +432,14 @@ msgstr "请输入您的密码!"
msgid "Please input your username!"
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
msgid "Project Team"
msgstr "项目团队"
@ -458,7 +470,7 @@ msgstr "保存指令"
msgid "Save error %{msg}"
msgstr "保存错误 %{msg}"
#: src/components/StdDataDisplay/StdCurd.vue:82
#: src/components/StdDataDisplay/StdCurd.vue:91
msgid "Save Successfully"
msgstr "保存成功"
@ -471,8 +483,6 @@ msgstr "保存成功"
msgid "Send"
msgstr "上传"
#: src/components/StdDataDisplay/StdCurd.vue:103
#: src/components/StdDataDisplay/StdCurd.vue:88
#: src/components/StdDataDisplay/StdTable.vue:112
#: src/components/StdDataDisplay/StdTable.vue:94
#: src/views/config/ConfigEdit.vue:22 src/views/domain/DomainEdit.vue:44

View file

@ -22,7 +22,7 @@ msgstr "關於"
msgid "Action"
msgstr "操作"
#: src/components/StdDataDisplay/StdCurd.vue:113
#: src/components/StdDataDisplay/StdCurd.vue:123
#: src/components/StdDataDisplay/StdCurd.vue:24
msgid "Add"
msgstr ""
@ -252,6 +252,10 @@ msgstr "禁用失敗 %{msg}"
msgid "Failed to enable %{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
msgid "File Not Found"
msgstr "未找到檔案"
@ -436,6 +440,14 @@ msgstr "請輸入您的密碼!"
msgid "Please input your username!"
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
msgid "Project Team"
msgstr "專案團隊"
@ -466,7 +478,7 @@ msgstr "儲存指令"
msgid "Save error %{msg}"
msgstr "儲存錯誤 %{msg}"
#: src/components/StdDataDisplay/StdCurd.vue:82
#: src/components/StdDataDisplay/StdCurd.vue:91
#, fuzzy
msgid "Save Successfully"
msgstr "儲存成功"
@ -480,8 +492,6 @@ msgstr "儲存成功"
msgid "Send"
msgstr "上傳"
#: src/components/StdDataDisplay/StdCurd.vue:103
#: src/components/StdDataDisplay/StdCurd.vue:88
#: src/components/StdDataDisplay/StdTable.vue:112
#: src/components/StdDataDisplay/StdTable.vue:94
#: 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 {
c.JSON(http.StatusOK, gin.H{
"error": err,
"message": "Failed to get cert information",
"error": err,
})
return
}

View file

@ -1,129 +1,133 @@
package api
import (
"errors"
"github.com/0xJacky/Nginx-UI/server/model"
"github.com/0xJacky/Nginx-UI/server/settings"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"golang.org/x/crypto/bcrypt"
"net/http"
"github.com/0xJacky/Nginx-UI/server/model"
"github.com/0xJacky/Nginx-UI/server/settings"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"golang.org/x/crypto/bcrypt"
"net/http"
)
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) {
curd := model.NewCurd(&model.Auth{})
id := c.Param("id")
curd := model.NewCurd(&model.Auth{})
id := c.Param("id")
var user model.Auth
err := curd.First(&user, id)
var user model.Auth
err := curd.First(&user, id)
if err != nil {
ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, user)
if err != nil {
ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, user)
}
type UserJson struct {
Name string `json:"name" binding:"required,max=255"`
Password string `json:"password" binding:"max=255"`
Name string `json:"name" binding:"required,max=255"`
Password string `json:"password" binding:"max=255"`
}
func AddUser(c *gin.Context) {
var json UserJson
ok := BindAndValid(c, &json)
if !ok {
return
}
curd := model.NewCurd(&model.Auth{})
var json UserJson
ok := BindAndValid(c, &json)
if !ok {
return
}
curd := model.NewCurd(&model.Auth{})
pwd, err := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
if err != nil {
ErrHandler(c, err)
return
}
json.Password = string(pwd)
pwd, err := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
if err != nil {
ErrHandler(c, err)
return
}
json.Password = string(pwd)
user := model.Auth{
Name: json.Name,
Password: json.Password,
}
user := model.Auth{
Name: json.Name,
Password: json.Password,
}
err = curd.Add(&user)
err = curd.Add(&user)
if err != nil {
ErrHandler(c, err)
return
}
if err != nil {
ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, user)
c.JSON(http.StatusOK, user)
}
func EditUser(c *gin.Context) {
userId := cast.ToInt(c.Param("id"))
userId := cast.ToInt(c.Param("id"))
if settings.ServerSettings.Demo && userId == 1 {
ErrHandler(c, errors.New("not allow to change the root password in demo"))
return
}
if settings.ServerSettings.Demo && userId == 1 {
c.JSON(http.StatusNotAcceptable, gin.H{
"message": "Prohibit changing root password in demo",
})
return
}
var json UserJson
ok := BindAndValid(c, &json)
if !ok {
return
}
curd := model.NewCurd(&model.Auth{})
var json UserJson
ok := BindAndValid(c, &json)
if !ok {
return
}
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 {
ErrHandler(c, err)
return
}
edit.Name = json.Name
if err != nil {
ErrHandler(c, err)
return
}
edit.Name = json.Name
// encrypt passowrd
if json.Password != "" {
var pwd []byte
pwd, err = bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
if err != nil {
ErrHandler(c, err)
return
}
edit.Password = string(pwd)
}
// encrypt password
if json.Password != "" {
var pwd []byte
pwd, err = bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.DefaultCost)
if err != nil {
ErrHandler(c, err)
return
}
edit.Password = string(pwd)
}
err = curd.Edit(&user, &edit)
err = curd.Edit(&user, &edit)
if err != nil {
ErrHandler(c, err)
return
}
if err != nil {
ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, user)
c.JSON(http.StatusOK, user)
}
func DeleteUser(c *gin.Context) {
id := c.Param("id")
id := c.Param("id")
if cast.ToInt(id) == 1 {
ErrHandler(c, errors.New("not allow to delete the default user"))
return
}
curd := model.NewCurd(&model.Auth{})
err := curd.Delete(&model.Auth{}, "id", id)
if err != nil {
ErrHandler(c, err)
return
}
c.JSON(http.StatusNoContent, gin.H{})
if cast.ToInt(id) == 1 {
c.JSON(http.StatusNotAcceptable, gin.H{
"message": "Prohibit deleting the default user",
})
return
}
curd := model.NewCurd(&model.Auth{})
err := curd.Delete(&model.Auth{}, "id", id)
if err != nil {
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/elliptic"
"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/settings"
"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/registration"
"github.com/pkg/errors"
"io"
"log"
"net"
"net/http"
"os"
"path/filepath"
"time"
)
// 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
}
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 {
// Create a user. New accounts need an email and private key to start.
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
}