1、重写SSL证书申请逻辑,支持DNS记录验证方式
2、修复无法查看详情的bug 3、优化手动设置证书的逻辑 4、补充详情接口的单元测试
This commit is contained in:
1 parent
6bdd659dcb
commit
7860908f3a
10 files changed
+313
-259
No files matched your search
+37
-2
@@ -1,7 +1,12 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -48,8 +53,8 @@ func (c *Config) SetSetupPort(setupPort int) {
|
||||
const DBTypeMySQL = "mysql"
|
||||
const DBTypeSQLite = "sqlite"
|
||||
const SSLTypeAutoHTTP = "0" //自动生成证书
|
||||
// const SSLTypeAutoDNS = "2" //自动生成证书,DNS api验证
|
||||
const SSLTypeUser = "1" //用户上传证书
|
||||
const SSLTypeAutoDNS = "2" //自动生成证书,DNS api验证
|
||||
const SSLTypeUser = "1" //用户上传证书
|
||||
|
||||
var DBTypes []string = []string{DBTypeMySQL, DBTypeSQLite}
|
||||
|
||||
@@ -86,3 +91,33 @@ func Init() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func ReadPrivateKey() (*ecdsa.PrivateKey, bool) {
|
||||
key, err := os.ReadFile("./config/ssl/account_private.pem")
|
||||
if err != nil {
|
||||
return createNewPrivateKey(), true
|
||||
}
|
||||
|
||||
block, _ := pem.Decode(key)
|
||||
x509Encoded := block.Bytes
|
||||
privateKey, _ := x509.ParseECPrivateKey(x509Encoded)
|
||||
|
||||
return privateKey, false
|
||||
}
|
||||
|
||||
func createNewPrivateKey() *ecdsa.PrivateKey {
|
||||
// Create a user. New accounts need an email and private key to start.
|
||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
x509Encoded, _ := x509.MarshalECPrivateKey(privateKey)
|
||||
|
||||
// 将ec 密钥写入到 pem文件里
|
||||
keypem, _ := os.OpenFile("./config/ssl/account_private.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
err = pem.Encode(keypem, &pem.Block{Type: "EC PRIVATE KEY", Bytes: x509Encoded})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return privateKey
|
||||
}
|
||||
+22
-25
@@ -5,6 +5,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"pmail/config"
|
||||
"pmail/dto/response"
|
||||
"pmail/services/setup"
|
||||
@@ -134,40 +135,36 @@ func Setup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
//if reqData["step"] == "ssl" && reqData["action"] == "getParams" {
|
||||
// params, err := ssl.GetServerParamsList(reqData["serverName"])
|
||||
// if err != nil {
|
||||
// response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
// return
|
||||
// }
|
||||
// response.NewSuccessResponse(params).FPrint(w)
|
||||
// return
|
||||
//}
|
||||
if reqData["step"] == "ssl" && reqData["action"] == "getParams" {
|
||||
dnsChallenge := ssl.GetDnsChallengeInstance()
|
||||
|
||||
//if reqData["step"] == "ssl" && reqData["action"] == "setParams" {
|
||||
// for key, v := range reqData {
|
||||
// if key != "step" && key != "action" {
|
||||
// ssl.SetDomainServerParams(key, v)
|
||||
// }
|
||||
// }
|
||||
// response.NewSuccessResponse("Succ").FPrint(w)
|
||||
// return
|
||||
//}
|
||||
response.NewSuccessResponse(dnsChallenge.GetDNSSettings(ctx)).FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "ssl" && reqData["action"] == "set" {
|
||||
keyPath := reqData["key_path"]
|
||||
crtPath := reqData["crt_path"]
|
||||
|
||||
serviceName, ok := reqData["serviceName"]
|
||||
if !ok {
|
||||
serviceName = ""
|
||||
}
|
||||
err := ssl.SetSSL(reqData["ssl_type"], reqData["key_path"], reqData["crt_path"], serviceName)
|
||||
_, err := os.Stat(keyPath)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
//if reqData["ssl_type"] == config.SSLTypeAutoHTTP || reqData["ssl_type"] == config.SSLTypeAutoDNS {
|
||||
if reqData["ssl_type"] == config.SSLTypeAutoHTTP {
|
||||
_, err = os.Stat(crtPath)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
err = ssl.SetSSL(reqData["ssl_type"], reqData["key_path"], reqData["crt_path"])
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["ssl_type"] == config.SSLTypeAutoHTTP || reqData["ssl_type"] == config.SSLTypeAutoDNS {
|
||||
err = ssl.GenSSL(false)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
|
||||
@@ -22,7 +22,7 @@ func Start() {
|
||||
}
|
||||
}
|
||||
|
||||
if config.Instance.SSLType == "0" {
|
||||
if config.Instance.SSLType == config.SSLTypeAutoHTTP || config.Instance.SSLType == config.SSLTypeAutoDNS {
|
||||
go sslUpdateLoop()
|
||||
} else {
|
||||
go sslCheck()
|
||||
@@ -45,6 +45,7 @@ func sslCheck() {
|
||||
log.Errorf("SSL Check Error! %+v", err)
|
||||
}
|
||||
if newExpTime != expiredTime {
|
||||
expiredTime = newExpTime
|
||||
log.Infoln("SSL certificate had update! restarting")
|
||||
signal.RestartChan <- true
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ func TestMaster(t *testing.T) {
|
||||
t.Run("testSendEmail", testSendEmail)
|
||||
time.Sleep(8 * time.Second)
|
||||
t.Run("testEmailList", testEmailList)
|
||||
t.Run("testGetDetail", testGetEmailDetail)
|
||||
t.Run("testDelEmail", testDelEmail)
|
||||
|
||||
t.Run("testSendEmail2User1", testSendEmail2User1)
|
||||
@@ -108,6 +109,23 @@ func testCheckRule(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func testGetEmailDetail(t *testing.T) {
|
||||
ret, err := httpClient.Post(TestHost+"/api/email/detail", "application/json", strings.NewReader(`{
|
||||
"id":1
|
||||
}`))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
data, err := readResponse(ret.Body)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if data.ErrorNo != 0 {
|
||||
t.Error("GetEmailDetail Error! ", data)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func testCreateRule(t *testing.T) {
|
||||
ret, err := httpClient.Post(TestHost+"/api/rule/add", "application/json", strings.NewReader(`{
|
||||
"name":"Move Group",
|
||||
@@ -703,6 +721,7 @@ func testSendEmail2User3(t *testing.T) {
|
||||
t.Logf("testSendEmail2User3 Success! Response: %+v", data)
|
||||
|
||||
}
|
||||
|
||||
func testEmailList(t *testing.T) {
|
||||
ret, err := httpClient.Post(TestHost+"/api/email/list", "application/json", strings.NewReader(`{}`))
|
||||
if err != nil {
|
||||
|
||||
@@ -27,7 +27,7 @@ func GetEmailDetail(ctx *context.Context, id int, markRead bool) (*response.Emai
|
||||
|
||||
//获取邮件内容
|
||||
var email response.EmailResponseData
|
||||
_, err = db.Instance.ID(id).Get(&email)
|
||||
_, err = db.Instance.Select("*,1 as is_read").Table("email").Where("id=?", id).Get(&email)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL error:%+v", err)
|
||||
return nil, err
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package ssl
|
||||
|
||||
import (
|
||||
"github.com/go-acme/lego/v4/challenge/dns01"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"pmail/utils/context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type authInfo struct {
|
||||
Domain string
|
||||
Token string
|
||||
@@ -35,3 +42,61 @@ func GetHttpChallengeInstance() *HttpChallenge {
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
type DNSChallenge struct {
|
||||
AuthInfo map[string]*authInfo
|
||||
}
|
||||
|
||||
var dnsInstance *DNSChallenge
|
||||
|
||||
func GetDnsChallengeInstance() *DNSChallenge {
|
||||
if dnsInstance == nil {
|
||||
dnsInstance = &DNSChallenge{
|
||||
AuthInfo: map[string]*authInfo{},
|
||||
}
|
||||
}
|
||||
return dnsInstance
|
||||
}
|
||||
|
||||
func (h *DNSChallenge) Present(domain, token, keyAuth string) error {
|
||||
info := dns01.GetChallengeInfo(domain, keyAuth)
|
||||
log.Infof("Presenting challenge Info : %+v", info)
|
||||
h.AuthInfo[token] = &authInfo{
|
||||
Domain: info.FQDN,
|
||||
Token: token,
|
||||
KeyAuth: info.Value,
|
||||
}
|
||||
log.Infof("SSL Log:%s %s %s", domain, token, keyAuth)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *DNSChallenge) CleanUp(domain, token, keyAuth string) error {
|
||||
delete(h.AuthInfo, token)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *DNSChallenge) Timeout() (timeout, interval time.Duration) {
|
||||
return 60 * time.Minute, 5 * time.Second
|
||||
}
|
||||
|
||||
type DNSItem struct {
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Value string `json:"value"`
|
||||
TTL int `json:"ttl"`
|
||||
Tips string `json:"tips"`
|
||||
}
|
||||
|
||||
func (h *DNSChallenge) GetDNSSettings(ctx *context.Context) []*DNSItem {
|
||||
ret := []*DNSItem{}
|
||||
for _, info := range h.AuthInfo {
|
||||
ret = append(ret, &DNSItem{
|
||||
Type: "TXT",
|
||||
Host: info.Domain,
|
||||
Value: info.KeyAuth,
|
||||
TTL: 3600,
|
||||
})
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
@@ -3,11 +3,10 @@ package ssl
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"github.com/go-acme/lego/v4/certificate"
|
||||
"github.com/go-acme/lego/v4/challenge/dns01"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
"os"
|
||||
@@ -51,15 +50,13 @@ func GetSSL() string {
|
||||
return cfg.SSLType
|
||||
}
|
||||
|
||||
func SetSSL(sslType, priKey, crtKey, serviceName string) error {
|
||||
func SetSSL(sslType, priKey, crtKey string) error {
|
||||
cfg, err := setup.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//if sslType == config.SSLTypeAutoHTTP || sslType == config.SSLTypeUser || sslType == config.SSLTypeAutoDNS {
|
||||
if sslType == config.SSLTypeAutoHTTP || sslType == config.SSLTypeUser {
|
||||
if sslType == config.SSLTypeAutoHTTP || sslType == config.SSLTypeUser || sslType == config.SSLTypeAutoDNS {
|
||||
cfg.SSLType = sslType
|
||||
//cfg.DomainServiceName = serviceName
|
||||
} else {
|
||||
return errors.New("SSL Type Error!")
|
||||
}
|
||||
@@ -67,6 +64,8 @@ func SetSSL(sslType, priKey, crtKey, serviceName string) error {
|
||||
if cfg.SSLType == config.SSLTypeUser {
|
||||
cfg.SSLPrivateKeyPath = priKey
|
||||
cfg.SSLPublicKeyPath = crtKey
|
||||
// 手动设置证书的情况下后台地址默认不启用https
|
||||
cfg.HttpsEnabled = 2
|
||||
}
|
||||
|
||||
err = setup.WriteConfig(cfg)
|
||||
@@ -77,27 +76,7 @@ func SetSSL(sslType, priKey, crtKey, serviceName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GenSSL(update bool) error {
|
||||
|
||||
cfg, err := setup.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if !update {
|
||||
privateFile, errpi := os.ReadFile(cfg.SSLPrivateKeyPath)
|
||||
public, errpu := os.ReadFile(cfg.SSLPublicKeyPath)
|
||||
// 当前存在证书数据,就不生成了
|
||||
if errpi == nil && errpu == nil && len(privateFile) > 0 && len(public) > 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Create a user. New accounts need an email and private key to start.
|
||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
func renewCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config) error {
|
||||
|
||||
myUser := MyUser{
|
||||
Email: "i@" + cfg.Domain,
|
||||
@@ -114,31 +93,86 @@ func GenSSL(update bool) error {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
if cfg.SSLType == "0" {
|
||||
var reg *registration.Resource
|
||||
|
||||
reg, err = client.Registration.ResolveAccountByKey()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
myUser.Registration = reg
|
||||
|
||||
request := certificate.ObtainRequest{
|
||||
Domains: []string{"smtp." + cfg.Domain, cfg.WebDomain, "pop." + cfg.Domain},
|
||||
Bundle: true,
|
||||
}
|
||||
|
||||
log.Infof("wait ssl renew")
|
||||
certificates, err := client.Certificate.Obtain(request)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = os.WriteFile("./config/ssl/private.key", certificates.PrivateKey, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("./config/ssl/public.crt", certificates.Certificate, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("./config/ssl/issuerCert.crt", certificates.IssuerCertificate, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateCertificate(privateKey *ecdsa.PrivateKey, cfg *config.Config, newAccount bool) error {
|
||||
|
||||
myUser := MyUser{
|
||||
Email: "i@" + cfg.Domain,
|
||||
key: privateKey,
|
||||
}
|
||||
|
||||
conf := lego.NewConfig(&myUser)
|
||||
conf.UserAgent = "PMail"
|
||||
conf.Certificate.KeyType = certcrypto.RSA2048
|
||||
|
||||
// A client facilitates communication with the CA server.
|
||||
client, err := lego.NewClient(conf)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
if cfg.SSLType == config.SSLTypeAutoHTTP {
|
||||
err = client.Challenge.SetHTTP01Provider(GetHttpChallengeInstance())
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
} else if cfg.SSLType == config.SSLTypeAutoDNS {
|
||||
err = client.Challenge.SetDNS01Provider(GetDnsChallengeInstance(), dns01.AddDNSTimeout(60*time.Minute))
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
}
|
||||
//else if cfg.SSLType == "2" {
|
||||
// err = os.Setenv(strings.ToUpper(cfg.DomainServiceName)+"_PROPAGATION_TIMEOUT", "900")
|
||||
// if err != nil {
|
||||
// log.Errorf("Set ENV Variable Error: %s", err.Error())
|
||||
// }
|
||||
// dnspodProvider, err := dns.NewDNSChallengeProviderByName(cfg.DomainServiceName)
|
||||
// if err != nil {
|
||||
// return errors.Wrap(err)
|
||||
// }
|
||||
// err = client.Challenge.SetDNS01Provider(dnspodProvider, dns01.AddDNSTimeout(15*time.Minute))
|
||||
// if err != nil {
|
||||
// return errors.Wrap(err)
|
||||
// }
|
||||
//}
|
||||
|
||||
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
var reg *registration.Resource
|
||||
|
||||
if newAccount {
|
||||
reg, err = client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
} else {
|
||||
reg, err = client.Registration.ResolveAccountByKey()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
myUser.Registration = reg
|
||||
|
||||
request := certificate.ObtainRequest{
|
||||
@@ -154,7 +188,7 @@ func GenSSL(update bool) error {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Infof("证书校验通过!")
|
||||
err = os.WriteFile("./config/ssl/private.key", certificates.PrivateKey, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -177,6 +211,31 @@ func GenSSL(update bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GenSSL(update bool) error {
|
||||
|
||||
cfg, err := setup.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if !update {
|
||||
privateFile, errpi := os.ReadFile(cfg.SSLPrivateKeyPath)
|
||||
public, errpu := os.ReadFile(cfg.SSLPublicKeyPath)
|
||||
// 当前存在证书数据,就不生成了
|
||||
if errpi == nil && errpu == nil && len(privateFile) > 0 && len(public) > 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
privateKey, newAccount := config.ReadPrivateKey()
|
||||
|
||||
if !update {
|
||||
return generateCertificate(privateKey, cfg, newAccount)
|
||||
}
|
||||
|
||||
return renewCertificate(privateKey, cfg)
|
||||
}
|
||||
|
||||
// CheckSSLCrtInfo 返回证书过期剩余天数
|
||||
func CheckSSLCrtInfo() (int, time.Time, error) {
|
||||
|
||||
@@ -207,7 +266,7 @@ func CheckSSLCrtInfo() (int, time.Time, error) {
|
||||
}
|
||||
|
||||
func Update(needRestart bool) {
|
||||
if config.Instance != nil && config.Instance.IsInit && config.Instance.SSLType == "0" {
|
||||
if config.Instance != nil && config.Instance.IsInit && (config.Instance.SSLType == config.SSLTypeAutoHTTP || config.Instance.SSLType == config.SSLTypeAutoDNS) {
|
||||
days, _, err := CheckSSLCrtInfo()
|
||||
if days < 30 || err != nil {
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user