Skip to content

Commit

Permalink
Updated acmedns tool
Browse files Browse the repository at this point in the history
Add support for automatically generating acmedns module for Windows 7 using older version of lego
  • Loading branch information
tobychui committed May 18, 2024
1 parent ec973eb commit fe48a9a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ src/rules/*
src/README.md
docker/ContainerTester.sh
docker/ImagePublisher.sh
src/mod/acme/test/stackoverflow.pem
src/mod/acme/test/stackoverflow.pem
/tools/dns_challenge_update/code-gen/acmedns
/tools/dns_challenge_update/code-gen/lego
20 changes: 15 additions & 5 deletions tools/dns_challenge_update/code-gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,22 @@ To update the module, simply run update.sh

The updated files will be written into the `acmedns` folder. Then, you can copy it to the ACME module folder (or later-on a CICD pipeline will be made to do this automatically, but for now you have to manually copy it into the module under `src/mod/acme/`)

### Build for Windows 7 (NT6.1)

If you want to build Zoraxy with NT6.1, the lego version must be kept on or below 4.15.0. To build acmedns module for win7, add -- "win7" as go run paramter as follows.

```bash
go run ./extract.go -- "win7"
```

After moving the generated modules into the acmedns folder, you will also need to force override the version of the lego in the go.mod file under `src/`. by adding this line at the bottom of the go.mod file

```
replace github.com/go-acme/lego/v4 v4.16.1 => github.com/go-acme/lego/v4 v4.15.0
```

Make sure your Go version is on or below `1.20` for Windows 7 support.

### Module Usage

Expand All @@ -25,14 +40,9 @@ func GetDNSProviderByJsonConfig(name string, js string)(challenge.Provider, erro
providersdef.GetDNSProviderByJsonConfig("gandi", "{\"Username\":\"far\",\"Password\":\"boo\"}")
```
This should be able to replace the default lego v4 build in one (the one attached below) that requires the use of environment variables
```go
// NewDNSChallengeProviderByName Factory for DNS providers.
func NewDNSChallengeProviderByName(name string) (challenge.Provider, error)
```
59 changes: 54 additions & 5 deletions tools/dns_challenge_update/code-gen/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
"strings"
)

/*
Usage
git clone {repo_link_for_lego}
go run extract.go
//go run extract.go -- "win7"
*/

var legoProvidersSourceFolder string = "./lego/providers/dns/"
var outputDir string = "./acmedns"
var defTemplate string = `package acmedns
Expand Down Expand Up @@ -72,6 +81,26 @@ func getExcludedDNSProviders() []string {
}
}

// Exclude list for Windows build, due to limitations for lego versions
func getExcludedDNSProvidersNT61() []string {
return []string{
"edgedns", //Too complex data structure
"exec", //Not a DNS provider
"httpreq", //Not a DNS provider
"hurricane", //Multi-credentials arch
"oraclecloud", //Evil company
"acmedns", //Not a DNS provider
"selectelv2", //Not sure why not working with our code generator
"designate", //OpenStack, if you are using this you shd not be using zoraxy
"mythicbeasts", //Module require url.URL, which cannot be automatically parsed

//The following suppliers was not in lego v1.15 (Windows 7 last supported version of lego)
"cpanel",
"mailinabox",
"shellrent",
}
}

func isInSlice(str string, slice []string) bool {
for _, s := range slice {
if s == str {
Expand All @@ -85,6 +114,10 @@ func isExcludedDNSProvider(providerName string) bool {
return isInSlice(providerName, getExcludedDNSProviders())
}

func isExcludedDNSProviderNT61(providerName string) bool {
return isInSlice(providerName, getExcludedDNSProvidersNT61())
}

// extractConfigStruct extracts the name of the config struct and its content as plain text from a given source code.
func extractConfigStruct(sourceCode string) (string, string) {
// Regular expression to match the struct declaration.
Expand All @@ -106,6 +139,7 @@ func extractConfigStruct(sourceCode string) (string, string) {
func main() {
// A map of provider name to information on what can be filled
extractedProviderList := map[string]*ProviderInfo{}
buildForWindowsSeven := (len(os.Args) > 2 && os.Args[2] == "win7")

//Search all providers
providers, err := filepath.Glob(filepath.Join(legoProvidersSourceFolder, "/*"))
Expand All @@ -123,10 +157,19 @@ func main() {
importList := ""
for _, provider := range providers {
providerName := filepath.Base(provider)
if isExcludedDNSProvider(providerName) {
//Ignore this provider
continue

if buildForWindowsSeven {
if isExcludedDNSProviderNT61(providerName) {
//Ignore this provider
continue
}
} else {
if isExcludedDNSProvider(providerName) {
//Ignore this provider
continue
}
}

//Check if {provider_name}.go exists
providerDef := filepath.Join(provider, providerName+".go")
if !fileExists(providerDef) {
Expand Down Expand Up @@ -230,11 +273,17 @@ func main() {
if err != nil {
panic(err)
}
os.WriteFile(filepath.Join(outputDir, "providers.json"), js, 0775)

fullCodeSnippet := strings.ReplaceAll(defTemplate, "{{magic}}", generatedConvertcode)
fullCodeSnippet = strings.ReplaceAll(fullCodeSnippet, "{{imports}}", importList)

os.WriteFile(filepath.Join(outputDir, "acmedns.go"), []byte(fullCodeSnippet), 0775)
outJsonFilename := "providers.json"
outGoFilename := "acmedns.go"
if buildForWindowsSeven {
outJsonFilename = "providers_nt61.json"
outGoFilename = "acmedns_nt61.go"
}
os.WriteFile(filepath.Join(outputDir, outJsonFilename), js, 0775)
os.WriteFile(filepath.Join(outputDir, outGoFilename), []byte(fullCodeSnippet), 0775)
fmt.Println("Output written to file")
}
1 change: 1 addition & 0 deletions tools/dns_challenge_update/code-gen/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fi
# Run the extract.go to get all the config from lego source code
echo "Generating code"
go run ./extract.go
go run ./extract.go -- "win7"

echo "Cleaning up lego"
# Comment the line below if you dont want to pull everytime update
Expand Down

0 comments on commit fe48a9a

Please sign in to comment.