v2.0
This commit is contained in:
1 parent
4a26f52fde
commit
318fea24ad
42 files changed
+890
-375
No files matched your search
@@ -1,11 +1,13 @@
|
||||
{
|
||||
"domain": "",
|
||||
"webDomain": "",
|
||||
"logLevel": "debug",
|
||||
"domain": "domain.com",
|
||||
"webDomain": "mail.domain.com",
|
||||
"dkimPrivateKeyPath": "config/dkim/dkim.priv",
|
||||
"sslType": "0",
|
||||
"SSLPrivateKeyPath": "config/ssl/private.key",
|
||||
"SSLPublicKeyPath": "config/ssl/public.crt",
|
||||
"dbDSN": "",
|
||||
"dbType": "",
|
||||
"dbDSN": "./pmail.db",
|
||||
"dbType": "sqlite",
|
||||
"weChatPushAppId": "",
|
||||
"weChatPushSecret": "",
|
||||
"weChatPushTemplateId": "",
|
||||
|
||||
@@ -11,9 +11,11 @@ import (
|
||||
var IsInit bool
|
||||
|
||||
type Config struct {
|
||||
LogLevel string `json:"logLevel"`
|
||||
Domain string `json:"domain"`
|
||||
WebDomain string `json:"webDomain"`
|
||||
DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
|
||||
SSLType string `json:"sslType"` // 0表示自动生成证书,1表示用户上传证书
|
||||
SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
|
||||
SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
|
||||
DbDSN string `json:"dbDSN"`
|
||||
@@ -30,10 +32,12 @@ type Config struct {
|
||||
//go:embed tables/*
|
||||
var tableConfig embed.FS
|
||||
|
||||
const Version = "1.1.0"
|
||||
const Version = "2.0.0"
|
||||
|
||||
const DBTypeMySQL = "mysql"
|
||||
const DBTypeSQLite = "sqlite"
|
||||
const SSLTypeAuto = "0" //自动生成证书
|
||||
const SSLTypeUser = "1" //用户上传证书
|
||||
|
||||
var DBTypes []string = []string{DBTypeMySQL, DBTypeSQLite}
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
{
|
||||
"domain": "",
|
||||
"webDomain": "",
|
||||
"logLevel": "info",
|
||||
"domain": "domain.com",
|
||||
"webDomain": "mail.domain.com",
|
||||
"dkimPrivateKeyPath": "config/dkim/dkim.priv",
|
||||
"sslType": "0",
|
||||
"SSLPrivateKeyPath": "config/ssl/private.key",
|
||||
"SSLPublicKeyPath": "config/ssl/public.crt",
|
||||
"dbDSN": "",
|
||||
"dbType": "",
|
||||
"dbDSN": "./pmail.db",
|
||||
"dbType": "sqlite",
|
||||
"weChatPushAppId": "",
|
||||
"weChatPushSecret": "",
|
||||
"weChatPushTemplateId": "",
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
使用[go-msgauth](https://github.com/emersion/go-msgauth)项目的dkim-keygen工具生成公钥和私钥
|
||||
@@ -1,2 +0,0 @@
|
||||
INSERT INTO user (account, name, password) VALUES ('admin', 'admin', 'faddb6ec2efe16116a342f5512583c48');
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
INSERT INTO pmail.user_auth (user_id, email_account) VALUES (1, '*');
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
INSERT INTO user (account, name, password) VALUES ('admin', 'admin', 'faddb6ec2efe16116a342f5512583c48');
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
INSERT INTO user_auth (user_id, email_account) VALUES (1, '*');
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"pmail/config"
|
||||
)
|
||||
|
||||
func Interceptor(w http.ResponseWriter, r *http.Request) {
|
||||
URL := "https://" + config.Instance.WebDomain + r.URL.Path
|
||||
http.Redirect(w, r, URL, http.StatusMovedPermanently)
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
@@ -14,6 +12,7 @@ import (
|
||||
"pmail/i18n"
|
||||
"pmail/models"
|
||||
"pmail/session"
|
||||
"pmail/utils/password"
|
||||
)
|
||||
|
||||
type loginRequest struct {
|
||||
@@ -35,7 +34,7 @@ func Login(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
var user models.User
|
||||
|
||||
encodePwd := md5Encode(md5Encode(reqData.Password+"pmail") + "pmail2023")
|
||||
encodePwd := password.Encode(reqData.Password)
|
||||
|
||||
err = db.Instance.Get(&user, db.WithContext(ctx, "select * from user where account =? and password =?"),
|
||||
reqData.Account, encodePwd)
|
||||
@@ -51,9 +50,3 @@ func Login(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "aperror"), "").FPrint(w)
|
||||
}
|
||||
}
|
||||
|
||||
func md5Encode(str string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(str))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/i18n"
|
||||
"pmail/utils/password"
|
||||
)
|
||||
|
||||
type modifyPasswordRequest struct {
|
||||
@@ -27,7 +28,7 @@ func ModifyPassword(ctx *dto.Context, w http.ResponseWriter, req *http.Request)
|
||||
}
|
||||
|
||||
if retData.Password != "" {
|
||||
encodePwd := md5Encode(md5Encode(retData.Password+"pmail") + "pmail2023")
|
||||
encodePwd := password.Encode(retData.Password)
|
||||
|
||||
_, err := db.Instance.Exec(db.WithContext(ctx, "update user set password = ? where id =?"), encodePwd, ctx.UserInfo.ID)
|
||||
if err != nil {
|
||||
|
||||
+67
-14
@@ -4,13 +4,23 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"pmail/config"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/services/setup"
|
||||
"pmail/services/setup/ssl"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Proxy(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("proxy"))
|
||||
func AcmeChallenge(w http.ResponseWriter, r *http.Request) {
|
||||
instance := ssl.GetHttpChallengeInstance()
|
||||
token := strings.ReplaceAll(r.URL.Path, "/.well-known/acme-challenge/", "")
|
||||
auth, exist := instance.AuthInfo[token]
|
||||
if exist {
|
||||
w.Write([]byte(auth.KeyAuth))
|
||||
} else {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func Setup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
@@ -29,9 +39,10 @@ func Setup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
if reqData["step"] == "database" && reqData["action"] == "get" {
|
||||
dbType, dbDSN, err := setup.GetDatabaseSettings()
|
||||
dbType, dbDSN, err := setup.GetDatabaseSettings(ctx)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "")
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
response.NewSuccessResponse(map[string]string{
|
||||
@@ -42,19 +53,41 @@ func Setup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
if reqData["step"] == "database" && reqData["action"] == "set" {
|
||||
err := setup.SetDatabaseSettings(reqData["db_type"], reqData["db_dsn"])
|
||||
err := setup.SetDatabaseSettings(ctx, reqData["db_type"], reqData["db_dsn"])
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "")
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
response.NewSuccessResponse("Succ").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "password" && reqData["action"] == "get" {
|
||||
ok, err := setup.GetAdminPassword(ctx)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(ok).FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "password" && reqData["action"] == "set" {
|
||||
err := setup.SetAdminPassword(ctx, reqData["account"], reqData["password"])
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse("Succ").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "domain" && reqData["action"] == "get" {
|
||||
smtpDomain, webDomain, err := setup.GetDomainSettings()
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "")
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]string{
|
||||
"smtp_domain": smtpDomain,
|
||||
@@ -66,7 +99,8 @@ func Setup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if reqData["step"] == "domain" && reqData["action"] == "set" {
|
||||
err := setup.SetDomainSettings(reqData["smtp_domain"], reqData["web_domain"])
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "")
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse("Succ").FPrint(w)
|
||||
return
|
||||
@@ -75,18 +109,37 @@ func Setup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if reqData["step"] == "dns" && reqData["action"] == "get" {
|
||||
dnsInfos, err := setup.GetDNSSettings(ctx)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "")
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(dnsInfos).FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "ssl" && reqData["action"] == "get" {
|
||||
err := setup.GenSSL()
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "")
|
||||
}
|
||||
response.NewSuccessResponse("").FPrint(w)
|
||||
sslType := ssl.GetSSL()
|
||||
response.NewSuccessResponse(sslType).FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "ssl" && reqData["action"] == "set" {
|
||||
err := ssl.SetSSL(reqData["ssl_type"])
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["ssl_type"] == config.SSLTypeAuto {
|
||||
err = ssl.GenSSL(false)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
response.NewSuccessResponse("Succ").FPrint(w)
|
||||
setup.Finish(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package cron_server
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"pmail/config"
|
||||
"pmail/services/setup/ssl"
|
||||
"pmail/signal"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Start() {
|
||||
for {
|
||||
if config.Instance.IsInit {
|
||||
days, err := ssl.CheckSSLCrtInfo()
|
||||
if days < 30 || err != nil {
|
||||
if err != nil {
|
||||
log.Errorf("SSL Check Error, Update SSL Certificate. Error Info :%+v", err)
|
||||
} else {
|
||||
log.Infof("SSL certificate remaining time is only %d days, renew SSL certificate.", days)
|
||||
}
|
||||
err = ssl.GenSSL(true)
|
||||
if err != nil {
|
||||
log.Errorf("SSL Update Error! %+v", err)
|
||||
}
|
||||
// 更新完证书,重启服务
|
||||
signal.RestartChan <- true
|
||||
} else {
|
||||
log.Debugf("SSL Check.")
|
||||
}
|
||||
}
|
||||
// 每24小时检测一次证书有效期
|
||||
time.Sleep(24 * time.Hour)
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -8,11 +8,12 @@ import (
|
||||
_ "modernc.org/sqlite"
|
||||
"pmail/config"
|
||||
"pmail/dto"
|
||||
"pmail/utils/errors"
|
||||
)
|
||||
|
||||
var Instance *sqlx.DB
|
||||
|
||||
func Init() {
|
||||
func Init() error {
|
||||
dsn := config.Instance.DbDSN
|
||||
var err error
|
||||
|
||||
@@ -22,15 +23,16 @@ func Init() {
|
||||
case "sqlite":
|
||||
Instance, err = sqlx.Open("sqlite", dsn)
|
||||
default:
|
||||
return
|
||||
return errors.New("Database Type Error!")
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
Instance.SetMaxOpenConns(100)
|
||||
Instance.SetMaxIdleConns(10)
|
||||
//showMySQLCharacterSet()
|
||||
checkTable()
|
||||
return nil
|
||||
}
|
||||
|
||||
func WithContext(ctx *dto.Context, sql string) string {
|
||||
|
||||
@@ -41,6 +41,10 @@ func (w *WeChatPushHook) ReceiveParseBefore(email []byte) {
|
||||
}
|
||||
|
||||
func (w *WeChatPushHook) ReceiveParseAfter(email *parsemail.Email) {
|
||||
if w.appId == "" || w.secret == "" || w.pushUser == "" {
|
||||
return
|
||||
}
|
||||
|
||||
w.sendUserMsg(nil, w.pushUser, string(email.Text))
|
||||
}
|
||||
|
||||
@@ -91,18 +95,13 @@ func (w *WeChatPushHook) sendUserMsg(ctx *dto.Context, userId string, content st
|
||||
|
||||
}
|
||||
func NewWechatPushHook() *WeChatPushHook {
|
||||
if config.Instance.WeChatPushAppId != "" &&
|
||||
config.Instance.WeChatPushSecret != "" &&
|
||||
config.Instance.WeChatPushTemplateId != "" &&
|
||||
config.Instance.WeChatPushUserId != "" {
|
||||
ret := &WeChatPushHook{
|
||||
appId: config.Instance.WeChatPushAppId,
|
||||
secret: config.Instance.WeChatPushSecret,
|
||||
templateId: config.Instance.WeChatPushTemplateId,
|
||||
pushUser: config.Instance.WeChatPushUserId,
|
||||
}
|
||||
return ret
|
||||
|
||||
ret := &WeChatPushHook{
|
||||
appId: config.Instance.WeChatPushAppId,
|
||||
secret: config.Instance.WeChatPushSecret,
|
||||
templateId: config.Instance.WeChatPushTemplateId,
|
||||
pushUser: config.Instance.WeChatPushUserId,
|
||||
}
|
||||
return nil
|
||||
return ret
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"pmail/controllers"
|
||||
"time"
|
||||
)
|
||||
|
||||
const HttpPort = 80
|
||||
|
||||
// 这个服务是为了拦截http请求转发到https
|
||||
var httpServer *http.Server
|
||||
|
||||
func HttpStop() {
|
||||
if httpServer != nil {
|
||||
httpServer.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func HttpStart() {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", controllers.Interceptor)
|
||||
httpServer = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", HttpPort),
|
||||
Handler: mux,
|
||||
ReadTimeout: time.Second * 60,
|
||||
WriteTimeout: time.Second * 60,
|
||||
}
|
||||
|
||||
err := httpServer.ListenAndServe()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
"io/fs"
|
||||
olog "log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -26,43 +27,19 @@ import (
|
||||
//go:embed dist/*
|
||||
var local embed.FS
|
||||
|
||||
var ip string
|
||||
const HttpsPort = 443
|
||||
|
||||
const HttpPort = 80
|
||||
var httpsServer *http.Server
|
||||
|
||||
var setupServer *http.Server
|
||||
|
||||
func SetupStart() {
|
||||
mux := http.NewServeMux()
|
||||
fe, err := fs.Sub(local, "dist")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
mux.Handle("/", http.FileServer(http.FS(fe)))
|
||||
mux.HandleFunc("/api/", contextIterceptor(controllers.Setup))
|
||||
mux.HandleFunc("/", controllers.Proxy)
|
||||
|
||||
setupServer := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", HttpPort),
|
||||
Handler: mux,
|
||||
ReadTimeout: time.Second * 60,
|
||||
WriteTimeout: time.Second * 60,
|
||||
}
|
||||
err = setupServer.ListenAndServe()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
type nullWrite struct {
|
||||
}
|
||||
|
||||
func SetupStop() {
|
||||
err := setupServer.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
func (w *nullWrite) Write(p []byte) (int, error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func Start() {
|
||||
log.Infof("Http Server Start at :%d", HttpPort)
|
||||
func HttpsStart() {
|
||||
log.Infof("Http Server Start")
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
@@ -82,36 +59,27 @@ func Start() {
|
||||
mux.HandleFunc("/attachments/", contextIterceptor(controllers.GetAttachments))
|
||||
mux.HandleFunc("/attachments/download/", contextIterceptor(controllers.Download))
|
||||
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", HttpPort),
|
||||
// go http server会打一堆没用的日志,写一个空的日志处理器,屏蔽掉日志输出
|
||||
nullLog := olog.New(&nullWrite{}, "", olog.Ldate)
|
||||
|
||||
httpsServer = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", HttpsPort),
|
||||
Handler: session.Instance.LoadAndSave(mux),
|
||||
ReadTimeout: time.Second * 60,
|
||||
WriteTimeout: time.Second * 60,
|
||||
ErrorLog: nullLog,
|
||||
}
|
||||
|
||||
//err := server.ListenAndServeTLS( "config/ssl/public.crt", "config/ssl/private.key", nil)
|
||||
err = server.ListenAndServe()
|
||||
err = httpsServer.ListenAndServeTLS("config/ssl/public.crt", "config/ssl/private.key")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getLocalIP() string {
|
||||
ip := "127.0.0.1"
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ip
|
||||
func HttpsStop() {
|
||||
if httpsServer != nil {
|
||||
httpsServer.Close()
|
||||
}
|
||||
for _, a := range addrs {
|
||||
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
ip = ipnet.IP.String()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ip
|
||||
}
|
||||
|
||||
func genLogID() string {
|
||||
@@ -158,7 +126,7 @@ func contextIterceptor(h controllers.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
if ctx.UserInfo == nil || ctx.UserInfo.ID == 0 {
|
||||
if r.URL.Path != "/api/ping" && r.URL.Path != "/api/login" {
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "login_exp"), "").FPrint(w)
|
||||
response.NewErrorResponse(response.NeedLogin, i18n.GetText(ctx.Lang, "login_exp"), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
"pmail/controllers"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ip string
|
||||
|
||||
// 项目初始化引导用的服务,初始化引导结束后即退出
|
||||
var setupServer *http.Server
|
||||
|
||||
func SetupStart() {
|
||||
mux := http.NewServeMux()
|
||||
fe, err := fs.Sub(local, "dist")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
mux.Handle("/", http.FileServer(http.FS(fe)))
|
||||
mux.HandleFunc("/api/", contextIterceptor(controllers.Setup))
|
||||
// 挑战请求类似这样 /.well-known/acme-challenge/QPyMAyaWw9s5JvV1oruyqWHG7OqkHMJEHPoUz2046KM
|
||||
mux.HandleFunc("/.well-known/", controllers.AcmeChallenge)
|
||||
|
||||
setupServer = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", HttpPort),
|
||||
Handler: mux,
|
||||
ReadTimeout: time.Second * 60,
|
||||
WriteTimeout: time.Second * 60,
|
||||
}
|
||||
err = setupServer.ListenAndServe()
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func SetupStop() {
|
||||
err := setupServer.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getLocalIP() string {
|
||||
ip := "127.0.0.1"
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ip
|
||||
}
|
||||
for _, a := range addrs {
|
||||
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
ip = ipnet.IP.String()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ip
|
||||
}
|
||||
+23
-3
@@ -6,6 +6,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"pmail/config"
|
||||
"pmail/cron_server"
|
||||
"pmail/dto"
|
||||
"pmail/res_init"
|
||||
"time"
|
||||
@@ -47,12 +48,25 @@ func main() {
|
||||
// 日志消息输出可以是任意的io.writer类型
|
||||
log.SetOutput(os.Stdout)
|
||||
|
||||
// 设置日志级别为warn以上
|
||||
log.SetLevel(log.DebugLevel)
|
||||
var cst, _ = time.LoadLocation("Asia/Shanghai")
|
||||
time.Local = cst
|
||||
|
||||
res_init.Init()
|
||||
config.Init()
|
||||
|
||||
switch config.Instance.LogLevel {
|
||||
case "":
|
||||
log.SetLevel(log.InfoLevel)
|
||||
case "debug":
|
||||
log.SetLevel(log.DebugLevel)
|
||||
case "info":
|
||||
log.SetLevel(log.InfoLevel)
|
||||
case "warn":
|
||||
log.SetLevel(log.WarnLevel)
|
||||
case "error":
|
||||
log.SetLevel(log.ErrorLevel)
|
||||
default:
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
|
||||
log.Infoln("***************************************************")
|
||||
log.Infof("***\tServer Start Success Version:%s\n", config.Version)
|
||||
@@ -61,6 +75,12 @@ func main() {
|
||||
log.Infof("***\tBuild GoLang Version: %s ", goVersion)
|
||||
log.Infoln("***************************************************")
|
||||
|
||||
// 定时任务启动
|
||||
go cron_server.Start()
|
||||
|
||||
// 核心服务启动
|
||||
res_init.Init()
|
||||
|
||||
s := make(chan bool)
|
||||
<-s
|
||||
}
|
||||
+25
-7
@@ -1,6 +1,7 @@
|
||||
package res_init
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"pmail/config"
|
||||
"pmail/db"
|
||||
@@ -8,26 +9,43 @@ import (
|
||||
"pmail/hooks"
|
||||
"pmail/http_server"
|
||||
"pmail/session"
|
||||
"pmail/signal"
|
||||
"pmail/smtp_server"
|
||||
"pmail/utils/file"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
config.Init()
|
||||
|
||||
if config.IsInit {
|
||||
if !config.IsInit {
|
||||
dirInit()
|
||||
|
||||
log.Infof("Please click http://127.0.0.1 to continue.\n")
|
||||
go http_server.SetupStart()
|
||||
<-signal.InitChan
|
||||
http_server.SetupStop()
|
||||
}
|
||||
|
||||
for {
|
||||
config.Init()
|
||||
parsemail.Init()
|
||||
db.Init()
|
||||
err := db.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
session.Init()
|
||||
hooks.Init()
|
||||
// smtp server start
|
||||
go smtp_server.Start()
|
||||
// http server start
|
||||
go http_server.Start()
|
||||
} else {
|
||||
dirInit()
|
||||
go http_server.SetupStart()
|
||||
go http_server.HttpsStart()
|
||||
go http_server.HttpStart()
|
||||
<-signal.RestartChan
|
||||
log.Infof("Server Restart!")
|
||||
smtp_server.Stop()
|
||||
http_server.HttpsStop()
|
||||
http_server.HttpStop()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func dirInit() {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package signal
|
||||
|
||||
var InitChan = make(chan bool)
|
||||
var RestartChan = make(chan bool)
|
||||
+19
-11
@@ -43,19 +43,21 @@ func (s *Session) Logout() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var instance *smtp.Server
|
||||
|
||||
func Start() {
|
||||
be := &Backend{}
|
||||
|
||||
s := smtp.NewServer(be)
|
||||
instance = smtp.NewServer(be)
|
||||
|
||||
s.Addr = ":25"
|
||||
s.Domain = config.Instance.Domain
|
||||
s.ReadTimeout = 10 * time.Second
|
||||
s.WriteTimeout = 10 * time.Second
|
||||
s.MaxMessageBytes = 1024 * 1024
|
||||
s.MaxRecipients = 50
|
||||
instance.Addr = ":25"
|
||||
instance.Domain = config.Instance.Domain
|
||||
instance.ReadTimeout = 10 * time.Second
|
||||
instance.WriteTimeout = 10 * time.Second
|
||||
instance.MaxMessageBytes = 1024 * 1024
|
||||
instance.MaxRecipients = 50
|
||||
// force TLS for auth
|
||||
s.AllowInsecureAuth = false
|
||||
instance.AllowInsecureAuth = false
|
||||
// Load the certificate and key
|
||||
cer, err := tls.LoadX509KeyPair(config.Instance.SSLPublicKeyPath, config.Instance.SSLPrivateKeyPath)
|
||||
if err != nil {
|
||||
@@ -63,10 +65,16 @@ func Start() {
|
||||
return
|
||||
}
|
||||
// Configure the TLS support
|
||||
s.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
|
||||
instance.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
|
||||
|
||||
log.Println("Starting server at", s.Addr)
|
||||
if err := s.ListenAndServe(); err != nil {
|
||||
log.Println("Starting server at", instance.Addr)
|
||||
if err := instance.ListenAndServe(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Stop() {
|
||||
if instance != nil {
|
||||
instance.Close()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package password
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// Encode 对密码两次md5加盐
|
||||
func Encode(password string) string {
|
||||
encodePwd := md5Encode(md5Encode(password+"pmail") + "pmail2023")
|
||||
return encodePwd
|
||||
}
|
||||
|
||||
func md5Encode(str string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(str))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
Reference in new issue
Block a user