Created
March 4, 2020 07:31
-
-
Save zeusro/0f6b2a8c2896c355c0ea2772ab2284cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"regexp" | |
"strings" | |
) | |
func main() { | |
files, err := ioutil.ReadDir("./nginx") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, f := range files { | |
filePath := "nginx/" + f.Name() | |
dat, err := ioutil.ReadFile(filePath) | |
if err != nil { | |
fmt.Print(err) | |
} | |
content := string(dat) | |
reServerName := regexp.MustCompile(`server_name[\.\s\w-/:]+`) | |
reProxy := regexp.MustCompile(`proxy_pass[\.\s\w-/:]+`) | |
host1 := strings.Replace(reServerName.FindString(content), "server_name", "", -1) | |
host1 = strings.TrimSpace(host1) | |
host2 := strings.ReplaceAll(host1, "old.io", "new.fun") | |
proxy := strings.Replace(reProxy.FindString(content), "proxy_pass", "", -1) | |
proxy = strings.TrimSpace(proxy) | |
caddyTemplate := ` | |
https://$host1 https://$host2 { | |
gzip | |
tls { | |
dns dnspod | |
wildcard | |
} | |
proxy / $proxy { | |
transparent | |
} | |
} | |
` | |
caddyTemplate = strings.ReplaceAll(caddyTemplate, "$host1", host1) | |
caddyTemplate = strings.ReplaceAll(caddyTemplate, "$host2", host2) | |
caddyTemplate = strings.ReplaceAll(caddyTemplate, "$proxy", proxy) | |
// fmt.Println(caddyTemplate) | |
ioutil.WriteFile("output/"+host2, []byte(caddyTemplate), 0644) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment