acme_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package test
  2. import (
  3. "fmt"
  4. "github.com/0xJacky/Nginx-UI/tool"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. "testing"
  11. )
  12. func TestAcme(t *testing.T) {
  13. const acmePath = "/usr/local/acme.sh"
  14. _, err := os.Stat(acmePath)
  15. log.Println("[found] acme.sh ", acmePath)
  16. if err != nil {
  17. log.Println(err)
  18. if os.IsNotExist(err) {
  19. log.Println("[not found] acme.sh, installing...")
  20. if _, err := os.Stat("../tmp"); os.IsNotExist(err) {
  21. _ = os.Mkdir("../tmp", 0644)
  22. }
  23. out, err := exec.Command("curl", "-o", "../tmp/acme.sh", "https://get.acme.sh").
  24. CombinedOutput()
  25. if err != nil {
  26. log.Println(err)
  27. return
  28. }
  29. fmt.Printf("%s\n", out)
  30. log.Println("[acme.sh] downloaded")
  31. file, _ := ioutil.ReadFile("../tmp/acme.sh")
  32. fileString := string(file)
  33. fileString = strings.Replace(fileString, "https://raw.githubusercontent.com",
  34. "https://ghproxy.com/https://raw.githubusercontent.com", -1)
  35. _ = ioutil.WriteFile("../tmp/acme.sh", []byte(fileString), 0644)
  36. out, err = exec.Command("bash", "../tmp/acme.sh",
  37. "install",
  38. "--log",
  39. "--home", "/usr/local/acme.sh",
  40. "--cert-home", tool.GetNginxConfPath("ssl")).
  41. CombinedOutput()
  42. if err != nil {
  43. log.Println(err)
  44. return
  45. }
  46. fmt.Printf("%s\n", out)
  47. }
  48. }
  49. }