1
0

acmewizard.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 if stepNo == 10 {
  65. //Resolve public Ip address for tour
  66. publicIp, err := getPublicIPAddress()
  67. if err != nil {
  68. utils.SendErrorResponse(w, err.Error())
  69. return
  70. }
  71. js, _ := json.Marshal(publicIp)
  72. utils.SendJSONResponse(w, string(js))
  73. } else {
  74. utils.SendErrorResponse(w, "invalid step number")
  75. }
  76. }
  77. // Step 1
  78. func isLocalhostListening() (isListening bool, err error) {
  79. timeout := 2 * time.Second
  80. isListening = false
  81. // Check if localhost is listening on port 80 (HTTP)
  82. conn, err := net.DialTimeout("tcp", "localhost:80", timeout)
  83. if err == nil {
  84. isListening = true
  85. conn.Close()
  86. }
  87. // Check if localhost is listening on port 443 (HTTPS)
  88. conn, err = net.DialTimeout("tcp", "localhost:443", timeout)
  89. if err == nil {
  90. isListening = true
  91. conn.Close()
  92. }
  93. if isListening {
  94. return true, nil
  95. }
  96. return isListening, err
  97. }
  98. // Step 2
  99. func getPublicIPAddress() (string, error) {
  100. resp, err := http.Get("http://checkip.amazonaws.com/")
  101. if err != nil {
  102. return "", err
  103. }
  104. defer resp.Body.Close()
  105. ip, err := ioutil.ReadAll(resp.Body)
  106. if err != nil {
  107. return "", err
  108. }
  109. return string(ip), nil
  110. }
  111. func isHTTPServerAvailable(ipAddress string) bool {
  112. client := http.Client{
  113. Timeout: 5 * time.Second, // Timeout for the HTTP request
  114. }
  115. urls := []string{
  116. "http://" + ipAddress + ":80",
  117. "https://" + ipAddress + ":443",
  118. }
  119. for _, url := range urls {
  120. req, err := http.NewRequest("GET", url, nil)
  121. if err != nil {
  122. fmt.Println(err, url)
  123. continue // Ignore invalid URLs
  124. }
  125. // Disable TLS verification to handle invalid certificates
  126. client.Transport = &http.Transport{
  127. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  128. }
  129. resp, err := client.Do(req)
  130. if err == nil {
  131. resp.Body.Close()
  132. return true // HTTP server is available
  133. }
  134. }
  135. return false // HTTP server is not available
  136. }
  137. // Step 3
  138. func isDomainReachable(domain string) bool {
  139. _, err := net.LookupHost(domain)
  140. if err != nil {
  141. return false // Domain is not reachable
  142. }
  143. return true // Domain is reachable
  144. }