Skip to content

Commit

Permalink
Revert "Refactor TLS configuration and improve error logging"
Browse files Browse the repository at this point in the history
  • Loading branch information
julydate authored Nov 18, 2023
1 parent 66212a3 commit 919e786
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 77 deletions.
9 changes: 5 additions & 4 deletions app/handler/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package handler

import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"golang.org/x/net/idna"
)

Expand All @@ -23,8 +24,8 @@ func checkValue(response http.ResponseWriter, form url.Values, key string) (stri
}

func handleErrorResponse(response http.ResponseWriter, ip string, statusCode int, errorMessage, printMessage string) {
log.Infof("Access from IP: %s", ip)
log.Infof(printMessage)
logrus.Infof("Access from IP: %s", ip)
logrus.Infof(printMessage)
response.WriteHeader(statusCode)
fmt.Fprintf(response, errorMessage)
}
Expand All @@ -46,7 +47,7 @@ func validateFileAndDomain(ip string, domain string, file string, response http.
func sanitizedDomain(domain string) string {
safe, err := idna.ToASCII(strings.ReplaceAll(domain, "*", "_"))
if err != nil {
log.Error(err)
log.Panic(err)
}
return safe
}
26 changes: 3 additions & 23 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
Bind: 0.0.0.0
Port: 9090
TimeDiff: 60
key: passwd
key: 123456
Interval: 3600

TlsConfig:
Enable: false
Domain: test.example.com
Bind: 0.0.0.0
Port: 9443

CertConfig:
- CertMode: dns
CertDomain: test1.example.com
CertDomain: test.example.com
Provider: cloudflare
Email: [email protected]
DNSEnv:
CLOUDFLARE_EMAIL: YOUR_EMAIL
CLOUDFLARE_API_KEY: YOUR_API_KEY
# - CertMode: http
# CertDomain: test2.example.com
# Provider: cloudflare
# Email: [email protected]
# DNSEnv:
# CLOUDFLARE_EMAIL: YOUR_EMAIL
# CLOUDFLARE_API_KEY: YOUR_API_KEY
# - CertMode: tls
# CertDomain: test3.example.com
# Provider: cloudflare
# Email: [email protected]
# DNSEnv:
# CLOUDFLARE_EMAIL: YOUR_EMAIL
# CLOUDFLARE_API_KEY: YOUR_API_KEY
CLOUDFLARE_API_KEY: YOUR_API_KEY
7 changes: 2 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ func DefaultConfig() *Config {
return &Config{
Bind: "",
Port: 9090,
Tls: false,
TlsPort: 9443,
Key: "passwd",
TimeDiff: 60,
Interval: 3600,
TlsConfig: TlsConfig{
Enable: false,
},
CertConfig: nil,
}
}
20 changes: 7 additions & 13 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ package config
import "github.com/julydate/acmeDeliver/app/mylego"

type Config struct {
Bind string `yaml:"Bind"`
Port int `yaml:"Port"`
Key string `yaml:"Key"`
TimeDiff int64 `yaml:"TimeDiff"`
Interval int `yaml:"Interval"`
TlsConfig TlsConfig `yaml:"TlsConfig"`
Bind string `yaml:"Bind"`
Port int `yaml:"Port"`
Tls bool `yaml:"Tls"`
TlsPort int `yaml:"TlsPort"`
Key string `yaml:"Key"`
TimeDiff int64 `yaml:"TimeDiff"`
Interval int `yaml:"Interval"`

CertConfig []*mylego.CertConfig `yaml:"CertConfig"`
}

type TlsConfig struct {
Enable bool `yaml:"Enable"`
Domain string `yaml:"Domain"`
Bind string `yaml:"Bind"`
Port int `yaml:"Port"`
}
36 changes: 6 additions & 30 deletions controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"errors"
"fmt"
"net/http"

Expand All @@ -28,39 +27,27 @@ func New(c *config.Config) *Controller {
Addr: fmt.Sprintf("%s:%d", c.Bind, c.Port),
Handler: handler.New(c),
},
myLego: legos,
cronJob: cron.New(),
interval: c.Interval,
tlsConfig: &c.TlsConfig,
myLego: legos,
cronJob: cron.New(),
interval: c.Interval,
}
}

func (c *Controller) Start() error {
var certPath, keyPath string
log.Infof("Start server on: \033[32m%s\033[0m", c.httpServe.Addr)

// Apply certs on start
for i := range c.myLego {

l := c.myLego[i]
switch l.Conf.CertMode {
case "dns":
cert, key, err := l.DNSCert()
if err != nil {
if _, _, err := l.DNSCert(); err != nil {
log.Error(err)
}
if l.Conf.CertDomain == c.tlsConfig.Domain {
certPath = cert
keyPath = key
}
case "http", "tls":
cert, key, err := l.HTTPCert()
if err != nil {
if _, _, err := l.HTTPCert(); err != nil {
log.Error(err)
}
if l.Conf.CertDomain == c.tlsConfig.Domain {
certPath = cert
keyPath = key
}
default:
log.Errorf("unsupported certmode: %s", l.Conf.CertMode)
}
Expand All @@ -72,17 +59,6 @@ func (c *Controller) Start() error {
log.Error(err)
}

if c.tlsConfig.Enable {
if certPath == "" && keyPath == "" {
return errors.New("cert file is not exist")
}

c.httpServe.Addr = fmt.Sprintf("%s:%d", c.tlsConfig.Bind, c.tlsConfig.Port)
log.Infof("Start tls server on: \033[32m%s\033[0m (%s)", c.httpServe.Addr, c.tlsConfig.Domain)
return c.httpServe.ListenAndServeTLS(certPath, keyPath)
}

log.Infof("Start server on: \033[32m%s\033[0m", c.httpServe.Addr)
return c.httpServe.ListenAndServe()
}

Expand Down
2 changes: 0 additions & 2 deletions controller/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"github.com/robfig/cron/v3"

"github.com/julydate/acmeDeliver/app/mylego"
"github.com/julydate/acmeDeliver/config"
)

type Controller struct {
httpServe *http.Server
myLego []*mylego.LegoCMD
cronJob *cron.Cron
interval int
tlsConfig *config.TlsConfig
}

0 comments on commit 919e786

Please sign in to comment.