Fixed minor code style in PR

- Fixed minor coding style in PR
- Updated geoip list
This commit is contained in:
Toby Chui
2024-05-06 18:50:12 +08:00
parent 1db2ca61fa
commit 46817d0664
10 changed files with 80305 additions and 88686 deletions

View File

@@ -33,7 +33,7 @@ type CertificateInfoJSON struct {
AcmeName string `json:"acme_name"`
AcmeUrl string `json:"acme_url"`
SkipTLS bool `json:"skip_tls"`
DNS bool `json:"dns"`
UseDNS bool `json:"dns"`
}
// ACMEUser represents a user in the ACME system.
@@ -80,7 +80,7 @@ func NewACME(acmeServer string, port string, database *database.Database) *ACMEH
}
// ObtainCert obtains a certificate for the specified domains.
func (a *ACMEHandler) ObtainCert(domains []string, certificateName string, email string, caName string, caUrl string, skipTLS bool, dns bool) (bool, error) {
func (a *ACMEHandler) ObtainCert(domains []string, certificateName string, email string, caName string, caUrl string, skipTLS bool, useDNS bool) (bool, error) {
log.Println("[ACME] Obtaining certificate...")
// generate private key
@@ -146,7 +146,7 @@ func (a *ACMEHandler) ObtainCert(domains []string, certificateName string, email
}
// setup how to receive challenge
if dns {
if useDNS {
if !a.Database.TableExists("acme") {
a.Database.NewTable("acme")
return false, errors.New("DNS Provider and DNS Credenital configuration required for ACME Provider (Error -1)")
@@ -279,7 +279,7 @@ func (a *ACMEHandler) ObtainCert(domains []string, certificateName string, email
AcmeName: caName,
AcmeUrl: caUrl,
SkipTLS: skipTLS,
DNS: dns,
UseDNS: useDNS,
}
certInfoBytes, err := json.Marshal(certInfo)
@@ -392,6 +392,8 @@ func (a *ACMEHandler) HandleRenewCertificate(w http.ResponseWriter, r *http.Requ
utils.SendErrorResponse(w, jsonEscape(err.Error()))
return
}
//Make sure the wildcard * do not goes into the filename
filename = strings.ReplaceAll(filename, "*", "_")
email, err := utils.PostPara(r, "email")
if err != nil {

View File

@@ -1,6 +1,7 @@
package acme
import (
"errors"
"log"
"os"
"strings"
@@ -10,7 +11,10 @@ import (
)
func GetDnsChallengeProviderByName(dnsProvider string, dnsCredentials string) (challenge.Provider, error) {
credentials := extractDnsCredentials(dnsCredentials)
credentials, err := extractDnsCredentials(dnsCredentials)
if err != nil {
return nil, err
}
setCredentialsIntoEnvironmentVariables(credentials)
provider, err := dns.NewDNSChallengeProviderByName(dnsProvider)
@@ -28,7 +32,7 @@ func setCredentialsIntoEnvironmentVariables(credentials map[string]string) {
}
}
func extractDnsCredentials(input string) map[string]string {
func extractDnsCredentials(input string) (map[string]string, error) {
result := make(map[string]string)
// Split the input string by newline character
@@ -37,7 +41,8 @@ func extractDnsCredentials(input string) map[string]string {
// Iterate over each line
for _, line := range lines {
// Split the line by "=" character
parts := strings.Split(line, "=")
//use SpliyN to make sure not to split the value if the value is base64
parts := strings.SplitN(line, "=", 1)
// Check if the line is in the correct format
if len(parts) == 2 {
@@ -46,8 +51,13 @@ func extractDnsCredentials(input string) map[string]string {
// Add the key-value pair to the map
result[key] = value
if value == "" || key == "" {
//invalid config
return result, errors.New("DNS credential extract failed")
}
}
}
return result
return result, nil
}

View File

@@ -356,7 +356,7 @@ func (a *AutoRenewer) renewExpiredDomains(certs []*ExpiredCerts) ([]string, erro
}
}
_, err = a.AcmeHandler.ObtainCert(expiredCert.Domains, certName, a.RenewerConfig.Email, certInfo.AcmeName, certInfo.AcmeUrl, certInfo.SkipTLS, certInfo.DNS)
_, err = a.AcmeHandler.ObtainCert(expiredCert.Domains, certName, a.RenewerConfig.Email, certInfo.AcmeName, certInfo.AcmeUrl, certInfo.SkipTLS, certInfo.UseDNS)
if err != nil {
log.Println("Renew " + fileName + "(" + strings.Join(expiredCert.Domains, ",") + ") failed: " + err.Error())
} else {

View File

@@ -51,8 +51,29 @@ func TestResolveCountryCodeFromIP(t *testing.T) {
}
// Test an IP address that should return a valid country code
ip := "8.8.8.8"
expected := "US"
knownIpCountryMap := [][]string{
{"3.224.220.101", "US"},
{"176.113.115.113", "RU"},
{"65.21.233.213", "FI"},
{"94.23.207.193", "FR"},
}
for _, testcase := range knownIpCountryMap {
ip := testcase[0]
expected := testcase[1]
info, err := store.ResolveCountryCodeFromIP(ip)
if err != nil {
t.Errorf("error resolving country code for IP %s: %v", ip, err)
return
}
if info.CountryIsoCode != expected {
t.Errorf("expected country code %s, but got %s for IP %s", expected, info.CountryIsoCode, ip)
}
}
// Test an IP address that should return an empty country code
ip := "127.0.0.1"
expected := ""
info, err := store.ResolveCountryCodeFromIP(ip)
if err != nil {
t.Errorf("error resolving country code for IP %s: %v", ip, err)
@@ -61,16 +82,4 @@ func TestResolveCountryCodeFromIP(t *testing.T) {
if info.CountryIsoCode != expected {
t.Errorf("expected country code %s, but got %s for IP %s", expected, info.CountryIsoCode, ip)
}
// Test an IP address that should return an empty country code
ip = "127.0.0.1"
expected = ""
info, err = store.ResolveCountryCodeFromIP(ip)
if err != nil {
t.Errorf("error resolving country code for IP %s: %v", ip, err)
return
}
if info.CountryIsoCode != expected {
t.Errorf("expected country code %s, but got %s for IP %s", expected, info.CountryIsoCode, ip)
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff