acmewizard.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. if isListening {
  85. return true, nil
  86. }
  87. return isListening, err
  88. }
  89. // Step 2
  90. func getPublicIPAddress() (string, error) {
  91. resp, err := http.Get("http://checkip.amazonaws.com/")
  92. if err != nil {
  93. return "", err
  94. }
  95. defer resp.Body.Close()
  96. ip, err := ioutil.ReadAll(resp.Body)
  97. if err != nil {
  98. return "", err
  99. }
  100. return string(ip), nil
  101. }
  102. func isHTTPServerAvailable(ipAddress string) bool {
  103. client := http.Client{
  104. Timeout: 5 * time.Second, // Timeout for the HTTP request
  105. }
  106. urls := []string{
  107. "http://" + ipAddress + ":80",
  108. "https://" + ipAddress + ":443",
  109. }
  110. for _, url := range urls {
  111. req, err := http.NewRequest("GET", url, nil)
  112. if err != nil {
  113. fmt.Println(err, url)
  114. continue // Ignore invalid URLs
  115. }
  116. // Disable TLS verification to handle invalid certificates
  117. client.Transport = &http.Transport{
  118. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  119. }
  120. resp, err := client.Do(req)
  121. if err == nil {
  122. resp.Body.Close()
  123. return true // HTTP server is available
  124. }
  125. }
  126. return false // HTTP server is not available
  127. }
  128. // Step 3
  129. func isDomainReachable(domain string) bool {
  130. _, err := net.LookupHost(domain)
  131. if err != nil {
  132. return false // Domain is not reachable
  133. }
  134. return true // Domain is reachable
  135. }