mirror of
https://github.com/KaySar12/NextZen-UserService.git
synced 2025-03-15 23:25:35 +07:00
update: add update domain
This commit is contained in:
parent
86d42e7363
commit
cb86b0862a
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -6,7 +6,7 @@
|
||||
"type": "go",
|
||||
"debugAdapter": "dlv-dap",
|
||||
"request": "launch",
|
||||
"port": 34353,
|
||||
"port": 40751,
|
||||
"host": "127.0.0.1",
|
||||
"mode": "exec",
|
||||
"program": "${workspaceFolder}/dist/casaos-user-service-amd64_linux_amd64_v1/build/sysroot/usr/bin/casaos-user-service"
|
||||
|
@ -56,6 +56,7 @@ func InitRouter() *gin.Engine {
|
||||
r.POST("/v1/1panel/website/create", v1.ExternalAPIMiddleware, v1.OnePanelCreateWebsite)
|
||||
r.POST("/v1/1panel/website/delete", v1.ExternalAPIMiddleware, v1.OnePanelDeleteWebsite)
|
||||
r.POST("/v1/1panel/website/update-proxy", v1.ExternalAPIMiddleware, v1.OnePanelUpdateProxyWebsite)
|
||||
r.POST("/v1/1panel/website/update", v1.ExternalAPIMiddleware, v1.OnePanelUpdateWebsite)
|
||||
v1Group := r.Group("/v1")
|
||||
|
||||
v1Group.Use(jwt.JWT(
|
||||
|
159
route/v1/user.go
159
route/v1/user.go
@ -242,6 +242,134 @@ func OnePanelUpdateProxyWebsite(c *gin.Context) {
|
||||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||||
})
|
||||
}
|
||||
func OnePanelUpdateWebsite(c *gin.Context) {
|
||||
json := make(map[string]string)
|
||||
c.ShouldBind(&json)
|
||||
domain := json["domain"]
|
||||
port := json["port"]
|
||||
protocol := json["protocol"]
|
||||
hostname := json["hostname"]
|
||||
sslProvider := json["sslProvider"]
|
||||
headers := make(map[string]string)
|
||||
for key, value := range c.Request.Header {
|
||||
headers[key] = value[0]
|
||||
}
|
||||
var searchParam model2.SearchWebsiteRequest
|
||||
searchParam.Name = domain
|
||||
searchParam.Page = 1
|
||||
searchParam.PageSize = 1
|
||||
searchParam.OrderBy = "created_at"
|
||||
searchParam.Order = "null"
|
||||
searchParam.WebsiteGroupID = 0
|
||||
search, err := service.MyService.OnePanel().SearchWebsite(searchParam, onePanelServer, headers)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
if search.Data.Total > 0 {
|
||||
//TODO get Website ProxyData
|
||||
var proxy model2.ProxyWebsiteRequest
|
||||
proxy.ID = search.Data.Items[0].ID
|
||||
var proxyResult model2.ProxyWebsiteResponse
|
||||
proxyResult, err := service.MyService.OnePanel().GetProxyWebsite(proxy, onePanelServer, headers)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
//TODO Update Website Proxies (update root)
|
||||
updateProxy := model2.ProxyDetail{}
|
||||
updateProxy = proxyResult.Data[0]
|
||||
|
||||
updateProxy.Operate = "edit"
|
||||
updateProxy.ProxyPass = "http://" + hostname + ":" + port
|
||||
updateProxyResult, err := service.MyService.OnePanel().UpdateProxyWebsite(updateProxy, onePanelServer, headers)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
fmt.Println(updateProxyResult)
|
||||
sslId := -1
|
||||
acmeId := 0
|
||||
var searchSSLParam model2.SearchSSLRequest
|
||||
if sslProvider == "selfSigned" {
|
||||
searchSSLParam.AcmeAccountID = strconv.Itoa(acmeId)
|
||||
}
|
||||
searchSSLParam.Page = 1
|
||||
searchSSLParam.PageSize = 50
|
||||
searchSSL, err := service.MyService.OnePanel().SearchWebsiteSSl(searchSSLParam, onePanelServer, headers)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
for _, item := range searchSSL.Data.Items {
|
||||
if item.Provider == sslProvider && item.PrimaryDomain == domain {
|
||||
sslId = item.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
if search.Data.Items[0].Protocol != protocol && protocol == "http" {
|
||||
//TODO disable HTTPS
|
||||
|
||||
if sslId > 0 {
|
||||
var updateHttps, err = UpdateWebsiteHttps(false, acmeId, sslId, search.Data.Items[0].ID, headers)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
fmt.Println(updateHttps)
|
||||
return
|
||||
}
|
||||
}
|
||||
if sslId < 0 {
|
||||
//TODO create new SSL if not exist
|
||||
if sslProvider == "selfSigned" {
|
||||
sslId, err = OnePanelApplyWebsiteSSl(domain, search.Data.Items[0].ID, headers)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
sslId, err = IssueSelfSignedCert(domain, search.Data.Items[0].ID, headers, 3)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
updateHttps, err := UpdateWebsiteHttps(true, acmeId, sslId, search.Data.Items[0].ID, headers)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
Success: common_err.SERVICE_ERROR,
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
fmt.Println(updateHttps)
|
||||
return
|
||||
|
||||
}
|
||||
}
|
||||
func OnePanelCreateWebsite(c *gin.Context) {
|
||||
json := make(map[string]string)
|
||||
c.ShouldBind(&json)
|
||||
@ -318,7 +446,7 @@ func OnePanelCreateWebsite(c *gin.Context) {
|
||||
}
|
||||
|
||||
for _, item := range ssl.Data.Items {
|
||||
if item.PrimaryDomain == domain && item.Organization == sslProvider {
|
||||
if item.PrimaryDomain == domain && item.Provider == sslProvider {
|
||||
sslId = item.ID
|
||||
break
|
||||
}
|
||||
@ -373,7 +501,7 @@ func OnePanelCreateWebsite(c *gin.Context) {
|
||||
Message: common_err.GetMsg(common_err.SERVICE_ERROR),
|
||||
})
|
||||
}
|
||||
updateWebsite, err := EnableWebsiteHttps(acmeId, sslId, search.Data.Items[0].ID, headers)
|
||||
updateWebsite, err := UpdateWebsiteHttps(true, acmeId, sslId, search.Data.Items[0].ID, headers)
|
||||
if err != nil {
|
||||
c.JSON(common_err.SERVICE_ERROR,
|
||||
model.Result{
|
||||
@ -400,7 +528,6 @@ func OnePanelCreateWebsite(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
c.JSON(common_err.SUCCESS,
|
||||
model.Result{
|
||||
@ -421,14 +548,14 @@ func IssueSelfSignedCert(domain string, websiteId int, headers map[string]string
|
||||
}
|
||||
if selfsignedCert.Data.Total == 0 {
|
||||
var createParam model2.CreateSelfSignedCertRequest
|
||||
createParam.Name = ""
|
||||
createParam.Name = "nextweb"
|
||||
createParam.KeyType = "P256"
|
||||
createParam.CommonName = ""
|
||||
createParam.Country = ""
|
||||
createParam.Organization = ""
|
||||
createParam.OrganizationUint = ""
|
||||
createParam.Province = ""
|
||||
createParam.City = ""
|
||||
createParam.CommonName = "nextweb"
|
||||
createParam.Country = "VN"
|
||||
createParam.Organization = "nextweb"
|
||||
createParam.OrganizationUint = "nextweb"
|
||||
createParam.Province = "HaDong"
|
||||
createParam.City = "HaNoi"
|
||||
createNewSelfSignCert, err := service.MyService.OnePanel().CreateSelfSignedCert(createParam, onePanelServer, headers)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@ -461,12 +588,8 @@ func IssueSelfSignedCert(domain string, websiteId int, headers map[string]string
|
||||
return 0, err
|
||||
}
|
||||
for _, item := range ssl.Data.Items {
|
||||
if item.Provider == "selfSigned" {
|
||||
for _, website := range item.Websites {
|
||||
if website.ID == websiteId {
|
||||
return item.ID, nil
|
||||
}
|
||||
}
|
||||
if item.PrimaryDomain == domain {
|
||||
return item.ID, nil
|
||||
}
|
||||
}
|
||||
return 0, err
|
||||
@ -503,10 +626,10 @@ func OnePanelApplyWebsiteSSl(domain string, websiteId int, headers map[string]st
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
func EnableWebsiteHttps(acmeAccountID int, websiteSSLID int, websiteId int, headers map[string]string) (model2.GenericResponse, error) {
|
||||
func UpdateWebsiteHttps(enable bool, acmeAccountID int, websiteSSLID int, websiteId int, headers map[string]string) (model2.GenericResponse, error) {
|
||||
var updateConfig model2.WebsiteHttpsConfigRequest
|
||||
updateConfig.AcmeAccountID = acmeAccountID
|
||||
updateConfig.Enable = true
|
||||
updateConfig.Enable = enable
|
||||
updateConfig.WebsiteSSLID = websiteSSLID
|
||||
updateConfig.WebsiteID = websiteId
|
||||
updateConfig.Type = "existed"
|
||||
|
@ -69,7 +69,7 @@ func (o *onePanelService) UpdateWebsiteProtocol(m model2.WebsiteHttpsConfigReque
|
||||
return result, nil
|
||||
}
|
||||
func (o *onePanelService) SelfSignedCertSearch(m model2.SelfSignedCertSearchRequest, baseUrl string, headers map[string]string) (model2.SelfSignedCertSearchResponse, error) {
|
||||
path := "/api/v1/websites/ca/search"
|
||||
path := baseUrl + "/api/v1/websites/ca/search"
|
||||
reqBody, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return model2.SelfSignedCertSearchResponse{}, fmt.Errorf("error marshaling request body: %v", err)
|
||||
@ -127,7 +127,7 @@ func (o *onePanelService) CreateSelfSignedCert(m model2.CreateSelfSignedCertRequ
|
||||
return result, nil
|
||||
}
|
||||
func (o *onePanelService) IssueSelfSignedCert(m model2.SelfSignedIssueRequest, baseUrl string, headers map[string]string) (model2.GenericResponse, error) {
|
||||
path := "/api/v1/websites/ca/obtain"
|
||||
path := baseUrl + "/api/v1/websites/ca/obtain"
|
||||
reqBody, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return model2.GenericResponse{}, fmt.Errorf("error marshaling request body: %v", err)
|
||||
|
@ -92,8 +92,9 @@ type SelfSignedCertSearchRequest struct {
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
type SearchSSLRequest struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
AcmeAccountID string `json:"acmeAccountID"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
type SearchSSLResponse struct {
|
||||
Code int `json:"code"`
|
||||
@ -183,6 +184,7 @@ type SelfSignedCertSearchResponse struct {
|
||||
} `json:"items"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type WebsiteDetail struct {
|
||||
ID int `json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
|
Loading…
Reference in New Issue
Block a user