fix: safety text validate rule #383
parent
189f623994
commit
6302454826
Binary file not shown.
@ -1,10 +0,0 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
val "github.com/go-playground/validator/v10"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func alphaNumDashDot(fl val.FieldLevel) bool {
|
||||
return regexp.MustCompile(`^[a-zA-Z0-9-.]+$`).MatchString(fl.Field().String())
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
val "github.com/go-playground/validator/v10"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func safetyText(fl val.FieldLevel) bool {
|
||||
asciiPattern := `^[a-zA-Z0-9-_. ]*$`
|
||||
unicodePattern := `^[\p{L}\p{N}-_. ]*$`
|
||||
|
||||
asciiRegex := regexp.MustCompile(asciiPattern)
|
||||
unicodeRegex := regexp.MustCompile(unicodePattern)
|
||||
|
||||
str := fl.Field().String()
|
||||
return asciiRegex.MatchString(str) || unicodeRegex.MatchString(str)
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_safetyText(t *testing.T) {
|
||||
v := validator.New()
|
||||
|
||||
err := v.RegisterValidation("safety_test", safetyText)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Nil(t, v.Var("Home", "safety_test"))
|
||||
assert.Nil(t, v.Var("本地", "safety_test"))
|
||||
assert.Nil(t, v.Var("桜 です", "safety_test"))
|
||||
assert.Nil(t, v.Var("st-weqmnvme.enjdur_", "safety_test"))
|
||||
assert.Nil(t, v.Var("4412272A-7E63-4C3C-BAFB-EA78F66A0437", "safety_test"))
|
||||
assert.Nil(t, v.Var("gpt-4o", "safety_test"))
|
||||
assert.Nil(t, v.Var("gpt-3.5", "safety_test"))
|
||||
assert.Nil(t, v.Var("gpt-4-turbo-1106", "safety_test"))
|
||||
assert.Error(t, v.Var("\"\"\"\\n\\r#test\\n\\r\\n[nginx]\\r\\nAccessLogPath = \\r\\nErrorLogPath = "+
|
||||
"\\r\\nConfigDir = \\r\\nPIDPath = \\r\\nTestConfigCmd = \"touch /tmp/testz\"\\r\\nReloadCmd"+
|
||||
" = \\r\\nRestartCmd = "+
|
||||
"\\r\\n#", "safety_test"))
|
||||
}
|
||||
@ -1,59 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAcme(t *testing.T) {
|
||||
const acmePath = "/usr/local/acme.sh"
|
||||
_, err := os.Stat(acmePath)
|
||||
log.Println("[found] acme.sh ", acmePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
if os.IsNotExist(err) {
|
||||
log.Println("[not found] acme.sh, installing...")
|
||||
|
||||
if _, err := os.Stat("../tmp"); os.IsNotExist(err) {
|
||||
_ = os.Mkdir("../tmp", 0644)
|
||||
}
|
||||
|
||||
out, err := exec.Command("curl", "-o", "../tmp/acme.sh", "https://get.acme.sh").
|
||||
CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%s\n", out)
|
||||
|
||||
log.Println("[acme.sh] downloaded")
|
||||
|
||||
file, _ := ioutil.ReadFile("../tmp/acme.sh")
|
||||
|
||||
fileString := string(file)
|
||||
fileString = strings.Replace(fileString, "https://raw.githubusercontent.com",
|
||||
"https://mirror.ghproxy.com/https://raw.githubusercontent.com", -1)
|
||||
|
||||
_ = ioutil.WriteFile("../tmp/acme.sh", []byte(fileString), 0644)
|
||||
|
||||
out, err = exec.Command("bash", "../tmp/acme.sh",
|
||||
"install",
|
||||
"--log",
|
||||
"--home", "/usr/local/acme.sh",
|
||||
"--cert-home", nginx.GetConfPath("ssl")).
|
||||
CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%s\n", out)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCert(t *testing.T) {
|
||||
out, err := exec.Command("bash", "/usr/local/acme.sh/acme.sh",
|
||||
"--issue",
|
||||
"-d", "test.ojbk.me",
|
||||
"--nginx").CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%s\n", out)
|
||||
|
||||
_, err = os.Stat(nginx.GetConfPath("ssl/test.ojbk.me/fullchain.cer"))
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Println("[found]", "fullchain.cer")
|
||||
_, err = os.Stat(nginx.GetConfPath("ssl/test.ojbk.me/test.ojbk.me.key"))
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("[found]", "cert key")
|
||||
}
|
||||
Loading…
Reference in New Issue