mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
embed frontend
This commit is contained in:
parent
882fe8c074
commit
d09f484790
86 changed files with 884 additions and 12326 deletions
59
test/acme_test.go
Normal file
59
test/acme_test.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/tool"
|
||||
"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://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", tool.GetNginxConfPath("ssl")).
|
||||
CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%s\n", out)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
36
test/analytic_test.go
Normal file
36
test/analytic_test.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
"github.com/shirou/gopsutil/v3/load"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGoPsutil(t *testing.T) {
|
||||
fmt.Println("os:", runtime.GOOS)
|
||||
fmt.Println("threads:", runtime.GOMAXPROCS(0))
|
||||
|
||||
v, _ := mem.VirtualMemory()
|
||||
|
||||
loadAvg, _ := load.Avg()
|
||||
|
||||
fmt.Println("loadavg", loadAvg.String())
|
||||
|
||||
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
|
||||
cpuTimesBefore, _ := cpu.Times(false)
|
||||
time.Sleep(1000*time.Millisecond)
|
||||
cpuTimesAfter, _ := cpu.Times(false)
|
||||
threadNum := runtime.GOMAXPROCS(0)
|
||||
fmt.Println(cpuTimesBefore[0].String(), "\n", cpuTimesAfter[0].String())
|
||||
cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
|
||||
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
|
||||
fmt.Printf("%.2f, %.2f\n", cpuUserUsage*100, cpuSystemUsage*100)
|
||||
|
||||
diskUsage, _ := disk.Usage(".")
|
||||
fmt.Println(diskUsage.String())
|
||||
}
|
40
test/cert_test.go
Normal file
40
test/cert_test.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/tool"
|
||||
"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(tool.GetNginxConfPath("ssl/test.ojbk.me/fullchain.cer"))
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Println("[found]", "fullchain.cer")
|
||||
_, err = os.Stat(tool.GetNginxConfPath("ssl/test.ojbk.me/test.ojbk.me.key"))
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("[found]", "cert key")
|
||||
|
||||
log.Println("申请成功")
|
||||
}
|
98
test/lego_test.go
Normal file
98
test/lego_test.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/go-acme/lego/v4/certcrypto"
|
||||
"github.com/go-acme/lego/v4/certificate"
|
||||
"github.com/go-acme/lego/v4/challenge/http01"
|
||||
"github.com/go-acme/lego/v4/lego"
|
||||
"github.com/go-acme/lego/v4/registration"
|
||||
)
|
||||
|
||||
// You'll need a user or account type that implements acme.User
|
||||
type MyUser struct {
|
||||
Email string
|
||||
Registration *registration.Resource
|
||||
key crypto.PrivateKey
|
||||
}
|
||||
|
||||
func (u *MyUser) GetEmail() string {
|
||||
return u.Email
|
||||
}
|
||||
func (u MyUser) GetRegistration() *registration.Resource {
|
||||
return u.Registration
|
||||
}
|
||||
func (u *MyUser) GetPrivateKey() crypto.PrivateKey {
|
||||
return u.key
|
||||
}
|
||||
|
||||
|
||||
func TestLego(t *testing.T) {
|
||||
// Create a user. New accounts need an email and private key to start.
|
||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
myUser := MyUser{
|
||||
Email: "me@jackyu.cn",
|
||||
key: privateKey,
|
||||
}
|
||||
|
||||
config := lego.NewConfig(&myUser)
|
||||
|
||||
// This CA URL is configured for a local dev instance of Boulder running in Docker in a VM.
|
||||
//config.CADirURL = "https://acme-staging-v02.api.letsencrypt.org/directory"
|
||||
config.Certificate.KeyType = certcrypto.RSA2048
|
||||
|
||||
// A client facilitates communication with the CA server.
|
||||
client, err := lego.NewClient(config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", "9180"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
// New users will need to register
|
||||
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
myUser.Registration = reg
|
||||
|
||||
request := certificate.ObtainRequest{
|
||||
Domains: []string{"shanghai2.ojbk.me"},
|
||||
Bundle: true,
|
||||
}
|
||||
certificates, err := client.Certificate.Obtain(request)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
// Each certificate comes back with the cert bytes, the bytes of the client's
|
||||
// private key, and a certificate URL. SAVE THESE TO DISK.
|
||||
fmt.Printf("%#v\n", certificates)
|
||||
err = ioutil.WriteFile("fullchain.cer", certificates.Certificate, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile("private.key", certificates.PrivateKey, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
20
test/nginx_test.go
Normal file
20
test/nginx_test.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetNginx(t *testing.T) {
|
||||
out, err := exec.Command("nginx", "-V").CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("%s\n", out)
|
||||
|
||||
r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
|
||||
fmt.Println(r.FindStringSubmatch(string(out))[1])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue