v2.0
This commit is contained in:
1 parent
4a26f52fde
commit
318fea24ad
42 files changed
+890
-375
No files matched your search
@@ -51,7 +51,7 @@ func DkimGen() string {
|
||||
err error
|
||||
)
|
||||
|
||||
privKey, err = rsa.GenerateKey(rand.Reader, 3072)
|
||||
privKey, err = rsa.GenerateKey(rand.Reader, 1024)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to generate key: %v", err)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package auth
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDkimGen(t *testing.T) {
|
||||
DkimGen()
|
||||
}
|
||||
@@ -4,22 +4,62 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"pmail/config"
|
||||
"pmail/db"
|
||||
"pmail/dto"
|
||||
"pmail/models"
|
||||
"pmail/utils/array"
|
||||
"pmail/utils/errors"
|
||||
"pmail/utils/file"
|
||||
"pmail/utils/password"
|
||||
)
|
||||
|
||||
func GetDatabaseSettings() (string, string, error) {
|
||||
configData, err := readConfig()
|
||||
func GetDatabaseSettings(ctx *dto.Context) (string, string, error) {
|
||||
configData, err := ReadConfig()
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err)
|
||||
}
|
||||
|
||||
if configData.DbType == "" && configData.DbDSN == "" {
|
||||
return config.DBTypeSQLite, "./pmail.db", nil
|
||||
}
|
||||
|
||||
return configData.DbType, configData.DbDSN, nil
|
||||
}
|
||||
|
||||
func SetDatabaseSettings(dbType, dbDSN string) error {
|
||||
configData, err := readConfig()
|
||||
func GetAdminPassword(ctx *dto.Context) (string, error) {
|
||||
|
||||
users := []*models.User{}
|
||||
err := db.Instance.Select(&users, "select * from user")
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err)
|
||||
}
|
||||
|
||||
if len(users) > 0 {
|
||||
return users[0].Account, nil
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func SetAdminPassword(ctx *dto.Context, account, pwd string) error {
|
||||
encodePwd := password.Encode(pwd)
|
||||
res, err := db.Instance.Exec(db.WithContext(ctx, "INSERT INTO user (account, name, password) VALUES (?, 'admin',?)"), account, encodePwd)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
_, err = db.Instance.Exec(db.WithContext(ctx, "INSERT INTO user_auth (user_id, email_account) VALUES (?, '*')"), id)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetDatabaseSettings(ctx *dto.Context, dbType, dbDSN string) error {
|
||||
configData, err := ReadConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
@@ -31,16 +71,20 @@ func SetDatabaseSettings(dbType, dbDSN string) error {
|
||||
configData.DbType = dbType
|
||||
configData.DbDSN = dbDSN
|
||||
|
||||
// 检查数据库是否能正确连接 todo
|
||||
|
||||
err = writeConfig(configData)
|
||||
err = WriteConfig(configData)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
config.Init()
|
||||
// 检查数据库是否能正确连接
|
||||
err = db.Init()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeConfig(cfg *config.Config) error {
|
||||
func WriteConfig(cfg *config.Config) error {
|
||||
bytes, _ := json.Marshal(cfg)
|
||||
err := os.WriteFile("./config/config.json", bytes, 0666)
|
||||
if err != nil {
|
||||
@@ -49,7 +93,7 @@ func writeConfig(cfg *config.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readConfig() (*config.Config, error) {
|
||||
func ReadConfig() (*config.Config, error) {
|
||||
configData := config.Config{
|
||||
DkimPrivateKeyPath: "config/dkim/dkim.priv",
|
||||
SSLPrivateKeyPath: "config/ssl/private.key",
|
||||
|
||||
@@ -20,7 +20,7 @@ type DNSItem struct {
|
||||
}
|
||||
|
||||
func GetDNSSettings(ctx *dto.Context) ([]*DNSItem, error) {
|
||||
configData, err := readConfig()
|
||||
configData, err := ReadConfig()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
)
|
||||
|
||||
func GetDomainSettings() (string, string, error) {
|
||||
configData, err := readConfig()
|
||||
configData, err := ReadConfig()
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err)
|
||||
}
|
||||
@@ -14,17 +14,25 @@ func GetDomainSettings() (string, string, error) {
|
||||
}
|
||||
|
||||
func SetDomainSettings(smtpDomain, webDomain string) error {
|
||||
configData, err := readConfig()
|
||||
configData, err := ReadConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
if smtpDomain == "" {
|
||||
return errors.New("domain must not empty!")
|
||||
}
|
||||
|
||||
if webDomain == "" {
|
||||
return errors.New("web domain must not empty!")
|
||||
}
|
||||
|
||||
configData.Domain = smtpDomain
|
||||
configData.WebDomain = webDomain
|
||||
|
||||
// 检查域名是否指向本机 todo
|
||||
|
||||
err = writeConfig(configData)
|
||||
err = WriteConfig(configData)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"pmail/dto"
|
||||
"pmail/signal"
|
||||
"pmail/utils/errors"
|
||||
)
|
||||
|
||||
// Finish 标记初始化完成
|
||||
func Finish(ctx *dto.Context) error {
|
||||
cfg, err := ReadConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
cfg.IsInit = true
|
||||
|
||||
err = WriteConfig(cfg)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
// 初始化完成
|
||||
signal.InitChan <- true
|
||||
return nil
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"pmail/utils/errors"
|
||||
|
||||
"github.com/go-acme/lego/v4/certcrypto"
|
||||
"github.com/go-acme/lego/v4/certificate"
|
||||
"github.com/go-acme/lego/v4/challenge/http01"
|
||||
"github.com/go-acme/lego/v4/challenge/tlsalpn01"
|
||||
"github.com/go-acme/lego/v4/lego"
|
||||
"github.com/go-acme/lego/v4/registration"
|
||||
)
|
||||
|
||||
type MyUser struct {
|
||||
Email string
|
||||
Registration *registration.Resource
|
||||
key crypto.PrivateKey
|
||||
}
|
||||
|
||||
func (u *MyUser) GetEmail() string {
|
||||
return u.Email
|
||||
}
|
||||
func (u MyUser) GetRegistration() *registration.Resource {
|
||||
return u.Registration
|
||||
}
|
||||
func (u *MyUser) GetPrivateKey() crypto.PrivateKey {
|
||||
return u.key
|
||||
}
|
||||
|
||||
func GenSSL() error {
|
||||
|
||||
configData, err := readConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
// Create a user. New accounts need an email and private key to start.
|
||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
myUser := MyUser{
|
||||
Email: "i@" + configData.Domain,
|
||||
key: privateKey,
|
||||
}
|
||||
|
||||
config := lego.NewConfig(&myUser)
|
||||
|
||||
config.Certificate.KeyType = certcrypto.RSA2048
|
||||
|
||||
// A client facilitates communication with the CA server.
|
||||
client, err := lego.NewClient(config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// We specify an HTTP port of 5002 and an TLS port of 5001 on all interfaces
|
||||
// because we aren't running as root and can't bind a listener to port 80 and 443
|
||||
// (used later when we attempt to pass challenges). Keep in mind that you still
|
||||
// need to proxy challenge traffic to port 5002 and 5001.
|
||||
err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", "5001"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = client.Challenge.SetTLSALPN01Provider(tlsalpn01.NewProviderServer("", "443"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// New users will need to register
|
||||
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
myUser.Registration = reg
|
||||
|
||||
request := certificate.ObtainRequest{
|
||||
Domains: []string{
|
||||
fmt.Sprintf("smtp.%s", configData.Domain),
|
||||
configData.WebDomain,
|
||||
},
|
||||
Bundle: true,
|
||||
}
|
||||
certificates, err := client.Certificate.Obtain(request)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Each certificate comes back with the cert bytes, the bytes of the client's
|
||||
// private key, and a certificate URL. SAVE THESE TO DISK.
|
||||
fmt.Printf("%#v\n", certificates)
|
||||
|
||||
// ... all done.
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package ssl
|
||||
|
||||
type authInfo struct {
|
||||
Domain string
|
||||
Token string
|
||||
KeyAuth string
|
||||
}
|
||||
|
||||
type HttpChallenge struct {
|
||||
AuthInfo map[string]*authInfo
|
||||
}
|
||||
|
||||
var instance *HttpChallenge
|
||||
|
||||
func (h *HttpChallenge) Present(domain, token, keyAuth string) error {
|
||||
h.AuthInfo[token] = &authInfo{
|
||||
Domain: domain,
|
||||
Token: token,
|
||||
KeyAuth: keyAuth,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HttpChallenge) CleanUp(domain, token, keyAuth string) error {
|
||||
delete(h.AuthInfo, token)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetHttpChallengeInstance() *HttpChallenge {
|
||||
if instance == nil {
|
||||
instance = &HttpChallenge{
|
||||
AuthInfo: map[string]*authInfo{},
|
||||
}
|
||||
}
|
||||
return instance
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package ssl
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"github.com/go-acme/lego/v4/certificate"
|
||||
"github.com/spf13/cast"
|
||||
"os"
|
||||
"pmail/config"
|
||||
"pmail/services/setup"
|
||||
"pmail/utils/errors"
|
||||
"time"
|
||||
|
||||
"github.com/go-acme/lego/v4/certcrypto"
|
||||
"github.com/go-acme/lego/v4/lego"
|
||||
"github.com/go-acme/lego/v4/registration"
|
||||
)
|
||||
|
||||
type MyUser struct {
|
||||
Email string
|
||||
Registration *registration.Resource
|
||||
key crypto.PrivateKey
|
||||
}
|
||||
|
||||
func (u *MyUser) GetEmail() string {
|
||||
return u.Email
|
||||
}
|
||||
func (u MyUser) GetRegistration() *registration.Resource {
|
||||
return u.Registration
|
||||
}
|
||||
func (u *MyUser) GetPrivateKey() crypto.PrivateKey {
|
||||
return u.key
|
||||
}
|
||||
|
||||
func GetSSL() string {
|
||||
cfg, err := setup.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if cfg.SSLType == "" {
|
||||
return config.SSLTypeAuto
|
||||
}
|
||||
|
||||
return cfg.SSLType
|
||||
}
|
||||
|
||||
func SetSSL(sslType string) error {
|
||||
cfg, err := setup.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if sslType == config.SSLTypeAuto || sslType == config.SSLTypeUser {
|
||||
cfg.SSLType = sslType
|
||||
} else {
|
||||
return errors.New("SSL Type Error!")
|
||||
}
|
||||
|
||||
err = setup.WriteConfig(cfg)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
myUser := MyUser{
|
||||
Email: "i@" + cfg.Domain,
|
||||
key: privateKey,
|
||||
}
|
||||
|
||||
config := lego.NewConfig(&myUser)
|
||||
|
||||
config.Certificate.KeyType = certcrypto.RSA2048
|
||||
|
||||
// A client facilitates communication with the CA server.
|
||||
client, err := lego.NewClient(config)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
err = client.Challenge.SetHTTP01Provider(GetHttpChallengeInstance())
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
myUser.Registration = reg
|
||||
|
||||
request := certificate.ObtainRequest{
|
||||
Domains: []string{"smtp." + cfg.Domain, cfg.WebDomain},
|
||||
Bundle: true,
|
||||
}
|
||||
certificates, err := client.Certificate.Obtain(request)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("./config/ssl/private.key", certificates.PrivateKey, 0666)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("./config/ssl/public.crt", certificates.Certificate, 0666)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("./config/ssl/issuerCert.crt", certificates.IssuerCertificate, 0666)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckSSLCrtInfo 返回证书过期剩余天数
|
||||
func CheckSSLCrtInfo() (int, error) {
|
||||
|
||||
cfg, err := setup.ReadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// load cert and key by tls.LoadX509KeyPair
|
||||
tlsCert, err := tls.LoadX509KeyPair(cfg.SSLPublicKeyPath, cfg.SSLPrivateKeyPath)
|
||||
if err != nil {
|
||||
return -1, errors.Wrap(err)
|
||||
}
|
||||
|
||||
cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
|
||||
|
||||
if err != nil {
|
||||
return -1, errors.Wrap(err)
|
||||
}
|
||||
|
||||
// 检查过期时间
|
||||
hours := cert.NotAfter.Sub(time.Now()).Hours()
|
||||
|
||||
if hours <= 0 {
|
||||
return -1, errors.New("Certificate has expired")
|
||||
}
|
||||
|
||||
return cast.ToInt(hours / 24), nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package ssl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenSSL(t *testing.T) {
|
||||
err := GenSSL(false)
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
func TestGetSSLCrtInfo(t *testing.T) {
|
||||
days, err := CheckSSLCrtInfo()
|
||||
|
||||
fmt.Println(days, err)
|
||||
}
|
||||
Reference in new issue
Block a user