Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Set the reverse proxy domain name

lotk3eaa8a908142
diff --git a/core/api/settings/settings.go b/core/api/settings/settings.go
index e05f7f05..769373a7 100644
--- a/core/api/settings/settings.go
+++ b/core/api/settings/settings.go
@@ -20,4 +20,6 @@ type ISettingsV1 interface {
SetIPWhitelist(ctx context.Context, req *v1.SetIPWhitelistReq) (res *v1.SetIPWhitelistRes, err error)
DeleteIPWhitelist(ctx context.Context, req *v1.DeleteIPWhitelistReq) (res *v1.DeleteIPWhitelistRes, err error)
AddIPWhitelist(ctx context.Context, req *v1.AddIPWhitelistReq) (res *v1.AddIPWhitelistRes, err error)
+ SetReverseProxyDomain(ctx context.Context, req *v1.SetReverseProxyDomainReq) (res *v1.SetReverseProxyDomainRes, err error)
+ GetReverseProxyDomain(ctx context.Context, req *v1.GetReverseProxyDomainReq) (res *v1.GetReverseProxyDomainRes, err error)
}
diff --git a/core/api/settings/v1/settings.go b/core/api/settings/v1/settings.go
index 09f23613..dd05fab5 100644
--- a/core/api/settings/v1/settings.go
+++ b/core/api/settings/v1/settings.go
@@ -173,3 +173,22 @@ type AddIPWhitelistReq struct {
type AddIPWhitelistRes struct {
api_v1.StandardRes
}
+
+type SetReverseProxyDomainReq struct {
+ g.Meta `path:"/settings/set_reverse_proxy_domain" tags:"Settings" method:"post" summary:"Set reverse proxy domain"`
+ Authorization string `json:"authorization" in:"header" dc:"Authorization" v:"required"`
+ Domain string `json:"domain" dc:"Domain" v:"required"`
+}
+
+type SetReverseProxyDomainRes struct {
+ api_v1.StandardRes
+}
+
+type GetReverseProxyDomainReq struct {
+ g.Meta `path:"/settings/get_reverse_proxy_domain" tags:"Settings" method:"get" summary:"Get reverse proxy domain"`
+ Authorization string `json:"authorization" in:"header" dc:"Authorization" v:"required"`
+}
+
+type GetReverseProxyDomainRes struct {
+ api_v1.StandardRes
+}
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
index 85416f56..b09483dd 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
@@ -2,6 +2,7 @@ package batch_mail
import (
"billionmail-core/api/batch_mail/v1"
+ "billionmail-core/internal/service/domains"
"billionmail-core/internal/service/public"
"context"
"github.com/gogf/gf/v2/frame/g"
@@ -135,7 +136,7 @@ func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplate
ips = append(ips, row.Ip)
}
item.IpWhitelist = ips
- item.ServerAddresser = public.GethostUrl() + "/api/batch_mail/api/send"
+ item.ServerAddresser = domains.GetBaseURL() + "/api/batch_mail/api/send"
}
res.Data.Total = total
diff --git a/core/internal/controller/contact/contact_v1_get_group_info.go b/core/internal/controller/contact/contact_v1_get_group_info.go
index 02143865..456e1e8e 100644
--- a/core/internal/controller/contact/contact_v1_get_group_info.go
+++ b/core/internal/controller/contact/contact_v1_get_group_info.go
@@ -1,6 +1,7 @@
package contact
import (
+ "billionmail-core/internal/service/domains"
"context"
"github.com/gogf/gf/v2/errors/gerror"
@@ -22,7 +23,7 @@ func (c *ControllerV1) GetGroupInfo(ctx context.Context, req *v1.GetGroupInfoReq
return res, nil
}
- hostUrl := public.GethostUrl()
+ hostUrl := domains.GetBaseURL()
if group.Token == "" {
token := GfMd5Short()
diff --git a/core/internal/controller/contact/contact_v1_list_groups.go b/core/internal/controller/contact/contact_v1_list_groups.go
index 6d6de916..ed327d70 100644
--- a/core/internal/controller/contact/contact_v1_list_groups.go
+++ b/core/internal/controller/contact/contact_v1_list_groups.go
@@ -30,7 +30,7 @@ func (c *ControllerV1) ListGroups(ctx context.Context, req *v1.ListGroupsReq) (r
res.SetError(gerror.New(public.LangCtx(ctx, "Failed to get group list {}", err.Error())))
return
}
- //hostUrl := public.GethostUrl()
+ //hostUrl := domains.GetBaseURL()
// Convert group information
groupInfos := make([]*v1.ContactGroupInfo, 0, len(groups))
for _, group := range groups {
diff --git a/core/internal/controller/settings/settings_v1_get_reverse_proxy_domain.go b/core/internal/controller/settings/settings_v1_get_reverse_proxy_domain.go
new file mode 100644
index 00000000..ab3b687b
--- /dev/null
+++ b/core/internal/controller/settings/settings_v1_get_reverse_proxy_domain.go
@@ -0,0 +1,28 @@
+package settings
+
+import (
+ "billionmail-core/internal/service/domains"
+ "context"
+
+ "billionmail-core/api/settings/v1"
+ "billionmail-core/internal/service/public"
+)
+
+func (c *ControllerV1) GetReverseProxyDomain(ctx context.Context, req *v1.GetReverseProxyDomainReq) (res *v1.GetReverseProxyDomainRes, err error) {
+ res = &v1.GetReverseProxyDomainRes{}
+
+ var reverseProxyDomain string
+ err = public.OptionsMgrInstance.GetOption(ctx, "reverse_proxy_domain", &reverseProxyDomain)
+ if err != nil {
+ reverseProxyDomain = ""
+ }
+
+ currentUrl := domains.GetBaseURL()
+
+ res.SetSuccess(public.LangCtx(ctx, "Success"))
+ res.Data = map[string]interface{}{
+ "current_url": currentUrl,
+ "reverse_proxy_domain": reverseProxyDomain,
+ }
+ return res, nil
+}
diff --git a/core/internal/controller/settings/settings_v1_set_reverse_proxy_domain.go b/core/internal/controller/settings/settings_v1_set_reverse_proxy_domain.go
new file mode 100644
index 00000000..02dec13f
--- /dev/null
+++ b/core/internal/controller/settings/settings_v1_set_reverse_proxy_domain.go
@@ -0,0 +1,47 @@
+package settings
+
+import (
+ "billionmail-core/internal/consts"
+ "context"
+ "github.com/gogf/gf/v2/errors/gerror"
+
+ "billionmail-core/api/settings/v1"
+ "billionmail-core/internal/service/public"
+ "fmt"
+)
+
+func (c *ControllerV1) SetReverseProxyDomain(ctx context.Context, req *v1.SetReverseProxyDomainReq) (res *v1.SetReverseProxyDomainRes, err error) {
+ res = &v1.SetReverseProxyDomainRes{}
+
+ domain := req.Domain
+
+ // Test connectivity to https://<domain>/api/languages/get
+ testUrl := fmt.Sprintf("%s/api/languages/get", domain)
+ var respData map[string]interface{}
+ err = public.HttpGetJson(testUrl, 5, &respData)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to connect to reverse proxy domain: {}", err.Error())))
+ return res, nil
+ }
+
+ // Check code==0 in response
+ code, ok := respData["code"].(float64)
+ if !ok || int(code) != 0 {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Reverse proxy domain test failed, unexpected response: {}", err.Error())))
+ return res, nil
+ }
+
+ // Save to bm_options
+ err = public.OptionsMgrInstance.SetOption(ctx, "reverse_proxy_domain", domain)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to save reverse proxy domain: {}", err.Error())))
+ return res, nil
+ }
+ _ = public.WriteLog(ctx, public.LogParams{
+ Type: consts.LOGTYPE.Settings,
+ Log: "Reverse proxy domain set successfully :[" + domain + "]",
+ })
+
+ res.SetSuccess(public.LangCtx(ctx, "Reverse proxy domain set successfully"))
+ return res, nil
+}
diff --git a/core/internal/controller/subscribe_list/subscribe_list.go b/core/internal/controller/subscribe_list/subscribe_list.go
index dd52cf9c..e18c1e57 100644
--- a/core/internal/controller/subscribe_list/subscribe_list.go
+++ b/core/internal/controller/subscribe_list/subscribe_list.go
@@ -8,6 +8,7 @@ import (
mail_v1 "billionmail-core/api/mail_boxes/v1"
"billionmail-core/internal/model/entity"
"billionmail-core/internal/service/batch_mail"
+ "billionmail-core/internal/service/domains"
"billionmail-core/internal/service/mail_boxes"
"billionmail-core/internal/service/mail_service"
"billionmail-core/internal/service/public"
@@ -239,7 +240,7 @@ func getEmailFromToken(token string) (string, string, error) {
// Build confirmation URL
func buildConfirmUrl(token string) string {
- hostUrl := public.GethostUrl()
+ hostUrl := domains.GetBaseURL()
return fmt.Sprintf("%s/api/subscribe/confirm?token=%s", hostUrl, token)
}
@@ -315,7 +316,7 @@ func GetSubscribeFormCode(groupToken string) string {
// Link to corresponding page
var submitUrl string
- hostUrl := public.GethostUrl()
+ hostUrl := domains.GetBaseURL()
submitUrl = hostUrl + "/api/subscribe/submit?token=" + groupToken
newContent := strings.ReplaceAll(content, "{{ SubmitURL . }}", submitUrl)
diff --git a/core/internal/controller/subscribe_list/subscribe_list_v1_subscribe_confirm.go b/core/internal/controller/subscribe_list/subscribe_list_v1_subscribe_confirm.go
index ad874bff..9e40c96f 100644
--- a/core/internal/controller/subscribe_list/subscribe_list_v1_subscribe_confirm.go
+++ b/core/internal/controller/subscribe_list/subscribe_list_v1_subscribe_confirm.go
@@ -1,6 +1,7 @@
package subscribe_list
import (
+ "billionmail-core/internal/service/domains"
"billionmail-core/internal/service/public"
"context"
"fmt"
@@ -38,7 +39,7 @@ func (c *ControllerV1) SubscribeConfirm(ctx context.Context, req *v1.SubscribeCo
g.RequestFromCtx(ctx).Response.RedirectTo("/invalid.html", 302)
return
}
- hostUrl := public.GethostUrl()
+ hostUrl := domains.GetBaseURL()
if contact.Status == 1 {
if group.AlreadyUrl != "" {
g.RequestFromCtx(ctx).Response.RedirectTo(group.AlreadyUrl, 302)
diff --git a/core/internal/controller/subscribe_list/subscribe_list_v1_subscribe_submit.go b/core/internal/controller/subscribe_list/subscribe_list_v1_subscribe_submit.go
index e08e5384..80312ac1 100644
--- a/core/internal/controller/subscribe_list/subscribe_list_v1_subscribe_submit.go
+++ b/core/internal/controller/subscribe_list/subscribe_list_v1_subscribe_submit.go
@@ -2,6 +2,7 @@ package subscribe_list
import (
"billionmail-core/api/subscribe_list/v1"
+ "billionmail-core/internal/service/domains"
"billionmail-core/internal/service/public"
"context"
"fmt"
@@ -20,7 +21,7 @@ func (c *ControllerV1) SubscribeSubmit(ctx context.Context, req *v1.SubscribeSub
return
}
- hostUrl := public.GethostUrl()
+ hostUrl := domains.GetBaseURL()
// 2. Find contact by email and group
contact, err := getContactByEmailAndGroup(req.Email, group.Id)
diff --git a/core/internal/service/domains/baseurl.go b/core/internal/service/domains/baseurl.go
index d1e34bea..153b35cc 100644
--- a/core/internal/service/domains/baseurl.go
+++ b/core/internal/service/domains/baseurl.go
@@ -19,6 +19,12 @@ var (
// GetBaseURL get baseurl of console panel
func GetBaseURL() string {
+ // Prioritize the verification of the reverse proxy domain name
+ var reverseProxyDomain string
+ err := public.OptionsMgrInstance.GetOption(context.Background(), "reverse_proxy_domain", &reverseProxyDomain)
+ if err == nil && reverseProxyDomain != "" {
+ return reverseProxyDomain
+ }
return baseurl
}