diff --git a/app/handler/helper.go b/app/handler/helper.go index dfbbdc9..14a31e4 100644 --- a/app/handler/helper.go +++ b/app/handler/helper.go @@ -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" ) @@ -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) } @@ -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 } diff --git a/config.example.yml b/config.example.yml index 92e19b0..7421b59 100644 --- a/config.example.yml +++ b/config.example.yml @@ -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: test@test.com DNSEnv: CLOUDFLARE_EMAIL: YOUR_EMAIL - CLOUDFLARE_API_KEY: YOUR_API_KEY -# - CertMode: http -# CertDomain: test2.example.com -# Provider: cloudflare -# Email: test@test.com -# DNSEnv: -# CLOUDFLARE_EMAIL: YOUR_EMAIL -# CLOUDFLARE_API_KEY: YOUR_API_KEY -# - CertMode: tls -# CertDomain: test3.example.com -# Provider: cloudflare -# Email: test@test.com -# DNSEnv: -# CLOUDFLARE_EMAIL: YOUR_EMAIL -# CLOUDFLARE_API_KEY: YOUR_API_KEY \ No newline at end of file + CLOUDFLARE_API_KEY: YOUR_API_KEY \ No newline at end of file diff --git a/config/config.go b/config/config.go index f8c2877..c60cc97 100644 --- a/config/config.go +++ b/config/config.go @@ -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, } } diff --git a/config/model.go b/config/model.go index f530d92..3f967a0 100644 --- a/config/model.go +++ b/config/model.go @@ -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"` -} diff --git a/controller/controller.go b/controller/controller.go index 9f24077..a7f4885 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -1,7 +1,6 @@ package controller import ( - "errors" "fmt" "net/http" @@ -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) } @@ -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() } diff --git a/controller/model.go b/controller/model.go index f55b0c0..aa5f4de 100644 --- a/controller/model.go +++ b/controller/model.go @@ -6,7 +6,6 @@ import ( "github.com/robfig/cron/v3" "github.com/julydate/acmeDeliver/app/mylego" - "github.com/julydate/acmeDeliver/config" ) type Controller struct { @@ -14,5 +13,4 @@ type Controller struct { myLego []*mylego.LegoCMD cronJob *cron.Cron interval int - tlsConfig *config.TlsConfig }