acmewizard.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package acmewizard
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "imuslab.com/zoraxy/mod/utils"
  13. )
  14. /*
  15. ACME Wizard
  16. This wizard help validate the acme settings and configurations
  17. */
  18. func HandleGuidedStepCheck(w http.ResponseWriter, r *http.Request) {
  19. stepNoStr, err := utils.GetPara(r, "step")
  20. if err != nil {
  21. utils.SendErrorResponse(w, "invalid step number given")
  22. return
  23. }
  24. stepNo, err := strconv.Atoi(stepNoStr)
  25. if err != nil {
  26. utils.SendErrorResponse(w, "invalid step number given")
  27. return
  28. }
  29. if stepNo == 1 {
  30. isListening, err := isLocalhostListening()
  31. if err != nil {
  32. utils.SendErrorResponse(w, err.Error())
  33. return
  34. }
  35. js, _ := json.Marshal(isListening)
  36. utils.SendJSONResponse(w, string(js))
  37. } else if stepNo == 2 {
  38. publicIp, err := getPublicIPAddress()
  39. if err != nil {
  40. utils.SendErrorResponse(w, err.Error())
  41. return
  42. }
  43. publicIp = strings.TrimSpace(publicIp)
  44. httpServerReachable := isHTTPServerAvailable(publicIp)
  45. js, _ := json.Marshal(httpServerReachable)
  46. utils.SendJSONResponse(w, string(js))
  47. } else if stepNo == 3 {
  48. domain, err := utils.GetPara(r, "domain")
  49. if err != nil {
  50. utils.SendErrorResponse(w, "domain cannot be empty")
  51. return
  52. }
  53. domain = strings.TrimSpace(domain)
  54. //Check if the domain is reachable
  55. reachable := isDomainReachable(domain)
  56. if !reachable {
  57. utils.SendErrorResponse(w, "domain is not reachable")
  58. return
  59. }
  60. //Check http is setup correctly
  61. httpServerReachable := isHTTPServerAvailable(domain)
  62. js, _ := json.Marshal(httpServerReachable)
  63. utils.SendJSONResponse(w, string(js))
  64. } else {
  65. utils.SendErrorResponse(w, "invalid step number")
  66. }
  67. }
  68. // Step 1
  69. func isLocalhostListening() (isListening bool, err error) {
  70. timeout := 2 * time.Second
  71. isListening = false
  72. // Check if localhost is listening on port 80 (HTTP)
  73. conn, err := net.DialTimeout("tcp", "localhost:80", timeout)
  74. if err == nil {
  75. isListening = true
  76. conn.Close()
  77. }
  78. // Check if localhost is listening on port 443 (HTTPS)
  79. conn, err = net.DialTimeout("tcp", "localhost:443", timeout)
  80. if err == nil {
  81. isListening = true
  82. conn.Close()
  83. }
  84. return isListening, err
  85. }
  86. // Step 2
  87. func getPublicIPAddress() (string, error) {
  88. resp, err := http.Get("http://checkip.amazonaws.com/")
  89. if err != nil {
  90. return "", err
  91. }
  92. defer resp.Body.Close()
  93. ip, err := ioutil.ReadAll(resp.Body)
  94. if err != nil {
  95. return "", err
  96. }
  97. return string(ip), nil
  98. }
  99. func isHTTPServerAvailable(ipAddress string) bool {
  100. client := http.Client{
  101. Timeout: 5 * time.Second, // Timeout for the HTTP request
  102. }
  103. urls := []string{
  104. "http://" + ipAddress + ":80",
  105. "https://" + ipAddress + ":443",
  106. }
  107. for _, url := range urls {
  108. req, err := http.NewRequest("GET", url, nil)
  109. if err != nil {
  110. fmt.Println(err, url)
  111. continue // Ignore invalid URLs
  112. }
  113. // Disable TLS verification to handle invalid certificates
  114. client.Transport = &http.Transport{
  115. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  116. }
  117. resp, err := client.Do(req)
  118. if err == nil {
  119. resp.Body.Close()
  120. return true // HTTP server is available
  121. }
  122. }
  123. return false // HTTP server is not available
  124. }
  125. // Step 3
  126. func isDomainReachable(domain string) bool {
  127. _, err := net.LookupHost(domain)
  128. if err != nil {
  129. return false // Domain is not reachable
  130. }
  131. return true // Domain is reachable
  132. }