1
0

acme.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "math/rand"
  7. "net/http"
  8. "regexp"
  9. "strconv"
  10. "time"
  11. "imuslab.com/zoraxy/mod/acme"
  12. "imuslab.com/zoraxy/mod/dynamicproxy"
  13. "imuslab.com/zoraxy/mod/utils"
  14. )
  15. /*
  16. acme.go
  17. This script handle special routing required for acme auto cert renew functions
  18. */
  19. // Helper function to generate a random port above a specified value
  20. func getRandomPort(minPort int) int {
  21. return rand.Intn(65535-minPort) + minPort
  22. }
  23. // init the new ACME instance
  24. func initACME() *acme.ACMEHandler {
  25. log.Println("Starting ACME handler")
  26. rand.Seed(time.Now().UnixNano())
  27. // Generate a random port above 30000
  28. port := getRandomPort(30000)
  29. // Check if the port is already in use
  30. for acme.IsPortInUse(port) {
  31. port = getRandomPort(30000)
  32. }
  33. return acme.NewACME("https://acme-staging-v02.api.letsencrypt.org/directory", strconv.Itoa(port))
  34. }
  35. // create the special routing rule for ACME
  36. func acmeRegisterSpecialRoutingRule() {
  37. log.Println("Assigned temporary port:" + acmeHandler.Getport())
  38. err := dynamicProxyRouter.AddRoutingRules(&dynamicproxy.RoutingRule{
  39. ID: "acme-autorenew",
  40. MatchRule: func(r *http.Request) bool {
  41. found, _ := regexp.MatchString("/.well-known/acme-challenge/*", r.RequestURI)
  42. return found
  43. },
  44. RoutingHandler: func(w http.ResponseWriter, r *http.Request) {
  45. req, err := http.NewRequest(http.MethodGet, "http://localhost:"+acmeHandler.Getport()+r.RequestURI, nil)
  46. req.Host = r.Host
  47. if err != nil {
  48. fmt.Printf("client: could not create request: %s\n", err)
  49. return
  50. }
  51. res, err := http.DefaultClient.Do(req)
  52. if err != nil {
  53. fmt.Printf("client: error making http request: %s\n", err)
  54. return
  55. }
  56. resBody, err := ioutil.ReadAll(res.Body)
  57. defer res.Body.Close()
  58. if err != nil {
  59. fmt.Printf("error reading: %s\n", err)
  60. return
  61. }
  62. w.Write(resBody)
  63. },
  64. Enabled: true,
  65. UseSystemAccessControl: false,
  66. })
  67. if err != nil {
  68. log.Println("[Err] " + err.Error())
  69. }
  70. }
  71. // This function check if the renew setup is satisfied. If not, toggle them automatically
  72. func AcmeCheckAndHandleRenewCertificate(w http.ResponseWriter, r *http.Request) {
  73. isForceHttpsRedirectEnabledOriginally := false
  74. if dynamicProxyRouter.Option.Port == 443 {
  75. //Enable port 80 to 443 redirect
  76. if !dynamicProxyRouter.Option.ForceHttpsRedirect {
  77. log.Println("Temporary enabling HTTP to HTTPS redirect for ACME certificate renew requests")
  78. dynamicProxyRouter.UpdateHttpToHttpsRedirectSetting(true)
  79. } else {
  80. //Set this to true, so after renew, do not turn it off
  81. isForceHttpsRedirectEnabledOriginally = true
  82. }
  83. } else if dynamicProxyRouter.Option.Port == 80 {
  84. //Go ahead
  85. } else {
  86. //This port do not support ACME
  87. utils.SendErrorResponse(w, "ACME renew only support web server listening on port 80 (http) or 443 (https)")
  88. }
  89. // Pass over to the acmeHandler to deal with the communication
  90. acmeHandler.HandleRenewCertificate(w, r)
  91. if dynamicProxyRouter.Option.Port == 443 {
  92. if !isForceHttpsRedirectEnabledOriginally {
  93. //Default is off. Turn the redirection off
  94. log.Println("Restoring HTTP to HTTPS redirect settings")
  95. dynamicProxyRouter.UpdateHttpToHttpsRedirectSetting(false)
  96. }
  97. }
  98. }