1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package main
- import (
- "fmt"
- "io/ioutil"
- "log"
- "math/rand"
- "net/http"
- "regexp"
- "strconv"
- "time"
- "imuslab.com/zoraxy/mod/acme"
- "imuslab.com/zoraxy/mod/dynamicproxy"
- )
- /*
- acme.go
- This script handle special routing required for acme auto cert renew functions
- */
- // Helper function to generate a random port above a specified value
- func getRandomPort(minPort int) int {
- return rand.Intn(65535-minPort) + minPort
- }
- // init the new ACME instance
- func initACME() *acme.ACMEHandler {
- log.Println("Start initializing ACME")
- rand.Seed(time.Now().UnixNano())
- // Generate a random port above 30000
- port := getRandomPort(30000)
- // Check if the port is already in use
- for acme.IsPortInUse(port) {
- port = getRandomPort(30000)
- }
- return acme.NewACME("[email protected]", "https://acme-staging-v02.api.letsencrypt.org/directory", strconv.Itoa(port))
- }
- // create the special routing rule for ACME
- func acmeRegisterSpecialRoutingRule() {
- log.Println("Assigned temporary port:" + acmeHandler.Getport())
- err := dynamicProxyRouter.AddRoutingRules(&dynamicproxy.RoutingRule{
- ID: "acme-autorenew",
- MatchRule: func(r *http.Request) bool {
- found, _ := regexp.MatchString("/.well-known/*", r.RequestURI)
- return found
- },
- RoutingHandler: func(w http.ResponseWriter, r *http.Request) {
- req, err := http.NewRequest(http.MethodGet, "http://localhost:"+acmeHandler.Getport()+r.RequestURI, nil)
- req.Host = r.Host
- if err != nil {
- fmt.Printf("client: could not create request: %s\n", err)
- }
- res, err := http.DefaultClient.Do(req)
- if err != nil {
- fmt.Printf("client: error making http request: %s\n", err)
- }
- resBody, err := ioutil.ReadAll(res.Body)
- if err != nil {
- fmt.Printf("error reading: %s\n", err)
- }
- w.Write(resBody)
- },
- Enabled: true,
- })
- if err != nil {
- log.Println("[Err] " + err.Error())
- }
- }
|