[sub] url support for reverse proxy #607

This commit is contained in:
Alireza Ahmadi
2023-11-26 18:37:45 +01:00
parent b41aef37e5
commit a8878bc1dc
12 changed files with 89 additions and 50 deletions

View File

@@ -29,6 +29,7 @@ class AllSetting {
this.subUpdates = 0; this.subUpdates = 0;
this.subEncrypt = true; this.subEncrypt = true;
this.subShowInfo = false; this.subShowInfo = false;
this.subURI = '';
this.timeLocation = "Asia/Tehran"; this.timeLocation = "Asia/Tehran";

View File

@@ -50,45 +50,11 @@ func (a *SettingController) getAllSetting(c *gin.Context) {
} }
func (a *SettingController) getDefaultSettings(c *gin.Context) { func (a *SettingController) getDefaultSettings(c *gin.Context) {
type settingFunc func() (interface{}, error) result, err := a.settingService.GetDefaultSettings(c.Request.Host)
if err != nil {
settings := map[string]settingFunc{ jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
"expireDiff": func() (interface{}, error) { return a.settingService.GetExpireDiff() }, return
"trafficDiff": func() (interface{}, error) { return a.settingService.GetTrafficDiff() },
"defaultCert": func() (interface{}, error) { return a.settingService.GetCertFile() },
"defaultKey": func() (interface{}, error) { return a.settingService.GetKeyFile() },
"tgBotEnable": func() (interface{}, error) { return a.settingService.GetTgbotenabled() },
"subEnable": func() (interface{}, error) { return a.settingService.GetSubEnable() },
"subPort": func() (interface{}, error) { return a.settingService.GetSubPort() },
"subPath": func() (interface{}, error) { return a.settingService.GetSubPath() },
"subDomain": func() (interface{}, error) { return a.settingService.GetSubDomain() },
"subKeyFile": func() (interface{}, error) { return a.settingService.GetSubKeyFile() },
"subCertFile": func() (interface{}, error) { return a.settingService.GetSubCertFile() },
"subEncrypt": func() (interface{}, error) { return a.settingService.GetSubEncrypt() },
"subShowInfo": func() (interface{}, error) { return a.settingService.GetSubShowInfo() },
"pageSize": func() (interface{}, error) { return a.settingService.GetPageSize() },
} }
result := make(map[string]interface{})
for key, fn := range settings {
value, err := fn()
if err != nil {
jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err)
return
}
result[key] = value
}
subTLS := false
if result["subKeyFile"] != "" || result["subCertFile"] != "" {
subTLS = true
}
result["subTLS"] = subTLS
delete(result, "subKeyFile")
delete(result, "subCertFile")
jsonObj(c, result, nil) jsonObj(c, result, nil)
} }

View File

@@ -44,6 +44,7 @@ type AllSetting struct {
SubUpdates int `json:"subUpdates" form:"subUpdates"` SubUpdates int `json:"subUpdates" form:"subUpdates"`
SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"` SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"`
SubShowInfo bool `json:"subShowInfo" form:"subShowInfo"` SubShowInfo bool `json:"subShowInfo" form:"subShowInfo"`
SubURI string `json:"subURI" form:"subURI"`
} }
func (s *AllSetting) CheckValid() error { func (s *AllSetting) CheckValid() error {
@@ -94,6 +95,13 @@ func (s *AllSetting) CheckValid() error {
s.WebBasePath += "/" s.WebBasePath += "/"
} }
if !strings.HasPrefix(s.SubPath, "/") {
s.SubPath = "/" + s.SubPath
}
if !strings.HasSuffix(s.SubPath, "/") {
s.SubPath += "/"
}
_, err := time.LoadLocation(s.TimeLocation) _, err := time.LoadLocation(s.TimeLocation)
if err != nil { if err != nil {
return common.NewError("time location not exist:", s.TimeLocation) return common.NewError("time location not exist:", s.TimeLocation)

View File

@@ -83,8 +83,7 @@
}); });
}, },
genSubLink(subID) { genSubLink(subID) {
const { domain: host, port, tls: isTLS, path: base } = app.subSettings; return app.subSettings.subURI+subID+'?name='+subID;
return buildURL({ host, port, isTLS, base, path: subID+'?name='+subID });
} }
}, },
updated() { updated() {

View File

@@ -296,8 +296,7 @@
infoModal.visible = false; infoModal.visible = false;
}, },
genSubLink(subID) { genSubLink(subID) {
const { domain: host, port, tls: isTLS, path: base } = app.subSettings; return app.subSettings.subURI+subID+'?name='+subID;
return buildURL({ host, port, isTLS, base, path: subID+'?name='+subID });
} }
}; };

View File

@@ -544,10 +544,7 @@
refreshInterval: Number(localStorage.getItem("refreshInterval")) || 5000, refreshInterval: Number(localStorage.getItem("refreshInterval")) || 5000,
subSettings: { subSettings: {
enable : false, enable : false,
port: 0, subURI : ''
path: '',
domain: '',
tls: false
}, },
tgBotEnable: false, tgBotEnable: false,
showAlert: false, showAlert: false,
@@ -591,10 +588,7 @@
this.tgBotEnable = tgBotEnable; this.tgBotEnable = tgBotEnable;
this.subSettings = { this.subSettings = {
enable : subEnable, enable : subEnable,
port: subPort, subURI: subURI
path: subPath,
domain: subDomain,
tls: subTLS
}; };
this.pageSize = pageSize; this.pageSize = pageSize;
} }

View File

@@ -176,6 +176,7 @@
<setting-list-item type="text" title='{{ i18n "pages.settings.subPath"}}' desc='{{ i18n "pages.settings.subPathDesc"}}' v-model="allSetting.subPath"></setting-list-item> <setting-list-item type="text" title='{{ i18n "pages.settings.subPath"}}' desc='{{ i18n "pages.settings.subPathDesc"}}' v-model="allSetting.subPath"></setting-list-item>
<setting-list-item type="text" title='{{ i18n "pages.settings.subCertPath"}}' desc='{{ i18n "pages.settings.subCertPathDesc"}}' v-model="allSetting.subCertFile"></setting-list-item> <setting-list-item type="text" title='{{ i18n "pages.settings.subCertPath"}}' desc='{{ i18n "pages.settings.subCertPathDesc"}}' v-model="allSetting.subCertFile"></setting-list-item>
<setting-list-item type="text" title='{{ i18n "pages.settings.subKeyPath"}}' desc='{{ i18n "pages.settings.subKeyPathDesc"}}' v-model="allSetting.subKeyFile"></setting-list-item> <setting-list-item type="text" title='{{ i18n "pages.settings.subKeyPath"}}' desc='{{ i18n "pages.settings.subKeyPathDesc"}}' v-model="allSetting.subKeyFile"></setting-list-item>
<setting-list-item type="text" title='{{ i18n "pages.settings.subURI"}}' desc='{{ i18n "pages.settings.subURIDesc"}}' v-model="allSetting.subURI"></setting-list-item>
<setting-list-item type="number" title='{{ i18n "pages.settings.subUpdates"}}' desc='{{ i18n "pages.settings.subUpdatesDesc"}}' v-model="allSetting.subUpdates"></setting-list-item> <setting-list-item type="number" title='{{ i18n "pages.settings.subUpdates"}}' desc='{{ i18n "pages.settings.subUpdatesDesc"}}' v-model="allSetting.subUpdates"></setting-list-item>
</a-list> </a-list>
</a-tab-pane> </a-tab-pane>

View File

@@ -53,6 +53,7 @@ var defaultValueMap = map[string]string{
"subUpdates": "12", "subUpdates": "12",
"subEncrypt": "true", "subEncrypt": "true",
"subShowInfo": "false", "subShowInfo": "false",
"subURI": "",
} }
type SettingService struct { type SettingService struct {
@@ -387,6 +388,10 @@ func (s *SettingService) GetPageSize() (int, error) {
return s.getInt("pageSize") return s.getInt("pageSize")
} }
func (s *SettingService) GetSubURI() (string, error) {
return s.getString("subURI")
}
func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error { func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error {
if err := allSetting.CheckValid(); err != nil { if err := allSetting.CheckValid(); err != nil {
return err return err
@@ -416,3 +421,61 @@ func (s *SettingService) GetDefaultXrayConfig() (interface{}, error) {
} }
return jsonData, nil return jsonData, nil
} }
func (s *SettingService) GetDefaultSettings(host string) (interface{}, error) {
type settingFunc func() (interface{}, error)
settings := map[string]settingFunc{
"expireDiff": func() (interface{}, error) { return s.GetExpireDiff() },
"trafficDiff": func() (interface{}, error) { return s.GetTrafficDiff() },
"pageSize": func() (interface{}, error) { return s.GetPageSize() },
"defaultCert": func() (interface{}, error) { return s.GetCertFile() },
"defaultKey": func() (interface{}, error) { return s.GetKeyFile() },
"tgBotEnable": func() (interface{}, error) { return s.GetTgbotenabled() },
"subEnable": func() (interface{}, error) { return s.GetSubEnable() },
"subURI": func() (interface{}, error) { return s.GetSubURI() },
}
result := make(map[string]interface{})
for key, fn := range settings {
value, err := fn()
if err != nil {
return "", err
}
result[key] = value
}
if result["subEnable"].(bool) && result["subURI"].(string) == "" {
subURI := ""
subPort, _ := s.GetSubPort()
subPath, _ := s.GetSubPath()
subDomain, _ := s.GetSubDomain()
subKeyFile, _ := s.GetSubKeyFile()
subCertFile, _ := s.GetSubCertFile()
subTLS := false
if subKeyFile != "" && subCertFile != "" {
subTLS = true
}
if subDomain == "" {
subDomain = strings.Split(host, ":")[0]
}
if subTLS {
subURI = "https://"
} else {
subURI = "http://"
}
if (subPort == 443 && subTLS) || (subPort == 80 && !subTLS) {
subURI += subDomain
} else {
subURI += fmt.Sprintf("%s:%d", subDomain, subPort)
}
if subPath[0] == byte('/') {
subURI += subPath
} else {
subURI += "/" + subPath
}
result["subURI"] = subURI
}
return result, nil
}

View File

@@ -279,6 +279,8 @@
"subEncryptDesc" = "Encrypt the returned configs in subscription" "subEncryptDesc" = "Encrypt the returned configs in subscription"
"subShowInfo" = "Show usage info" "subShowInfo" = "Show usage info"
"subShowInfoDesc" = "Show remianed traffic and date after config name" "subShowInfoDesc" = "Show remianed traffic and date after config name"
"subURI" = "Reverse Proxy URI"
"subURIDesc" = "Change base URI of subscription URL for using on behind of proxies"
[pages.settings.toasts] [pages.settings.toasts]
"modifySettings" = "Modify Settings " "modifySettings" = "Modify Settings "

View File

@@ -278,6 +278,8 @@
"subEncryptDesc" = "رمزگذاری کانفیگ های بازگشتی سابسکریپشن" "subEncryptDesc" = "رمزگذاری کانفیگ های بازگشتی سابسکریپشن"
"subShowInfo" = "نمایش اطلاعات مصرف" "subShowInfo" = "نمایش اطلاعات مصرف"
"subShowInfoDesc" = "ترافیک و زمان باقیمانده را در هر کانفیگ نمایش میدهد" "subShowInfoDesc" = "ترافیک و زمان باقیمانده را در هر کانفیگ نمایش میدهد"
"subURI" = "آدرس پایه پروکسی معکوس"
"subURIDesc" = "آدرس پایه سابسکریپشن را برای استفاده در پشت پراکسی ها تغییر میدهد"
[pages.settings.toasts] [pages.settings.toasts]
"modifySettings" = "ویرایش تنظیمات" "modifySettings" = "ویرایش تنظیمات"

View File

@@ -279,6 +279,8 @@
"subEncryptDesc" = "Шифрование возвращаемых конфигураций в подписке" "subEncryptDesc" = "Шифрование возвращаемых конфигураций в подписке"
"subShowInfo" = "Показать информацию об использовании" "subShowInfo" = "Показать информацию об использовании"
"subShowInfoDesc" = "Показывать восстановленный трафик и дату после имени конфигурации" "subShowInfoDesc" = "Показывать восстановленный трафик и дату после имени конфигурации"
"subURI" = "URI обратного прокси"
"subURIDesc" = "Изменить базовый URI URL-адреса подписки для использования за прокси-серверами"
[pages.settings.toasts] [pages.settings.toasts]
"modifySettings" = "Изменение настроек" "modifySettings" = "Изменение настроек"

View File

@@ -279,6 +279,8 @@
"subEncryptDesc" = "在订阅中加密返回的配置" "subEncryptDesc" = "在订阅中加密返回的配置"
"subShowInfo" = "显示使用信息" "subShowInfo" = "显示使用信息"
"subShowInfoDesc" = "在配置名称后显示剩余流量和日期" "subShowInfoDesc" = "在配置名称后显示剩余流量和日期"
"subURI" = "反向代理 URI"
"subURIDesc" = "更改订阅 URL 的基本 URI 以在代理后面使用"
[pages.settings.toasts] [pages.settings.toasts]
"modifySettings" = "修改设置" "modifySettings" = "修改设置"