acme.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "regexp"
  8. "imuslab.com/zoraxy/mod/acme"
  9. "imuslab.com/zoraxy/mod/dynamicproxy"
  10. )
  11. /*
  12. acme.go
  13. This script handle special routing required for acme auto cert renew functions
  14. */
  15. func acmeRegisterSpecialRoutingRule() {
  16. a := acme.NewACME("[email protected]", []string{"r5desktop.alanyeung.co"})
  17. err := dynamicProxyRouter.AddRoutingRules(&dynamicproxy.RoutingRule{
  18. ID: "acme-autorenew",
  19. MatchRule: func(r *http.Request) bool {
  20. found, _ := regexp.MatchString("/.well-known/*", r.RequestURI)
  21. /*
  22. if r.RequestURI == "/.well-known/" {
  23. return true
  24. }
  25. return false
  26. */
  27. return found
  28. },
  29. RoutingHandler: func(w http.ResponseWriter, r *http.Request) {
  30. req, err := http.NewRequest(http.MethodGet, "http://localhost:5002"+r.RequestURI, nil)
  31. req.Host = "r5desktop.alanyeung.co"
  32. if err != nil {
  33. fmt.Printf("client: could not create request: %s\n", err)
  34. }
  35. res, err := http.DefaultClient.Do(req)
  36. if err != nil {
  37. fmt.Printf("client: error making http request: %s\n", err)
  38. }
  39. resBody, err := ioutil.ReadAll(res.Body)
  40. if err != nil {
  41. fmt.Printf("error reading: %s\n", err)
  42. }
  43. w.Write(resBody)
  44. //w.Write([]byte("HELLO WORLD, THIS IS ACME REQUEST HANDLER"))
  45. },
  46. Enabled: true,
  47. })
  48. if err != nil {
  49. log.Println("[Err] " + err.Error())
  50. }
  51. a.ObtainCert()
  52. }