Skip to content

Commit

Permalink
feat: refactor out health-check and provider
Browse files Browse the repository at this point in the history
  • Loading branch information
love98ooo committed Sep 25, 2024
1 parent ea61614 commit 4b7ef76
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 88 deletions.
10 changes: 0 additions & 10 deletions controllers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,3 @@ func (c *ApiController) GetAccount() {

c.ResponseOk(claims, hostname)
}

func (c *ApiController) GetProviders() {
providers, err := casdoorsdk.GetProviders()
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(providers)
}
27 changes: 27 additions & 0 deletions controllers/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package controllers

import "github.com/casdoor/casdoor-go-sdk/casdoorsdk"

func (c *ApiController) GetProviders() {
providers, err := casdoorsdk.GetProviders()
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(providers)
}
4 changes: 3 additions & 1 deletion object/site_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func refreshSiteMap() error {
}

newSiteMap := map[string]*Site{}
newHealthCheckNeededDomains := make([]string, 0)
sites, err := GetGlobalSites()
if err != nil {
return err
Expand Down Expand Up @@ -107,7 +108,7 @@ func refreshSiteMap() error {

newSiteMap[strings.ToLower(site.Domain)] = site
if site.EnableAlert {
healthCheckNeededDomains = append(healthCheckNeededDomains, site.Domain)
newHealthCheckNeededDomains = append(newHealthCheckNeededDomains, site.Domain)
}
for _, domain := range site.OtherDomains {
if domain != "" {
Expand All @@ -117,6 +118,7 @@ func refreshSiteMap() error {
}

siteMap = newSiteMap
healthCheckNeededDomains = newHealthCheckNeededDomains
return nil
}

Expand Down
70 changes: 2 additions & 68 deletions object/site_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import (
"time"

"github.com/casbin/caswaf/util"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
)

var (
siteUpdateMap = map[string]string{}
lock = &sync.Mutex{}
healthCheckTryTimesMap = map[string]int{}
siteUpdateMap = map[string]string{}
lock = &sync.Mutex{}
)

func monitorSiteNodes() error {
Expand Down Expand Up @@ -79,41 +77,6 @@ func monitorSiteCerts() error {
return err
}

func healthCheck(site *Site, domain string) error {
var flag bool
var log string
switch site.SslMode {
case "HTTPS Only":
flag, log = pingUrl("https://" + domain)
case "HTTP":
flag, log = pingUrl("http://" + domain)
case "HTTPS and HTTP":
flag, log = pingUrl("https://" + domain)
flagHttp, logHttp := pingUrl("http://" + domain)
flag = flag || flagHttp
log = log + logHttp
}
if !flag {
fmt.Println(log)
healthCheckTryTimesMap[domain]--
if healthCheckTryTimesMap[domain] != 0 {
return nil
}
log = fmt.Sprintf("CasWAF health check failed for domain %s, %s", domain, log)
user, err := casdoorsdk.GetUser(site.Owner)
if err != nil {
fmt.Println(err)
}
err = casdoorsdk.SendEmail("CasWAF HealthCheck Check Alert", log, "CasWAF", user.Email)
if err != nil {
fmt.Println(err)
}
} else {
healthCheckTryTimesMap[domain] = GetSiteByDomain(domain).AlertTryTimes
}
return nil
}

func StartMonitorSitesLoop() {
fmt.Printf("StartMonitorSitesLoop() Start!\n\n")
go func() {
Expand Down Expand Up @@ -149,32 +112,3 @@ func StartMonitorSitesLoop() {
}
}()
}

func startHealthCheckLoop() {
for _, domain := range healthCheckNeededDomains {
domain := domain
if _, ok := healthCheckTryTimesMap[domain]; ok {
continue
}
healthCheckTryTimesMap[domain] = GetSiteByDomain(domain).AlertTryTimes
go func() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("[%s] Recovered from healthCheck() panic: %v\n", util.GetCurrentTime(), r)
}
}()
for {
site := GetSiteByDomain(domain)
if site == nil || !site.EnableAlert {
return
}

err := healthCheck(site, domain)
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Duration(site.AlertInterval) * time.Second)
}
}()
}
}
89 changes: 89 additions & 0 deletions object/site_timer_health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package object

import (
"fmt"
"time"

"github.com/casbin/caswaf/util"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
)

var healthCheckTryTimesMap = map[string]int{}

func healthCheck(site *Site, domain string) error {
var flag bool
var log string
switch site.SslMode {
case "HTTPS Only":
flag, log = pingUrl("https://" + domain)
case "HTTP":
flag, log = pingUrl("http://" + domain)
case "HTTPS and HTTP":
flag, log = pingUrl("https://" + domain)
flagHttp, logHttp := pingUrl("http://" + domain)
flag = flag || flagHttp
log = log + logHttp
}
if !flag {
fmt.Println(log)
healthCheckTryTimesMap[domain]--
if healthCheckTryTimesMap[domain] != 0 {
return nil
}
log = fmt.Sprintf("CasWAF health check failed for domain %s, %s", domain, log)
user, err := casdoorsdk.GetUser(site.Owner)
if err != nil {
fmt.Println(err)
}
err = casdoorsdk.SendEmail("CasWAF HealthCheck Check Alert", log, "CasWAF", user.Email)
if err != nil {
fmt.Println(err)
}
} else {
healthCheckTryTimesMap[domain] = GetSiteByDomain(domain).AlertTryTimes
}
return nil
}

func startHealthCheckLoop() {
for _, domain := range healthCheckNeededDomains {
domain := domain
if _, ok := healthCheckTryTimesMap[domain]; ok {
continue
}
healthCheckTryTimesMap[domain] = GetSiteByDomain(domain).AlertTryTimes
go func() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("[%s] Recovered from healthCheck() panic: %v\n", util.GetCurrentTime(), r)
}
}()
for {
site := GetSiteByDomain(domain)
if site == nil || !site.EnableAlert {
return
}

err := healthCheck(site, domain)
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Duration(site.AlertInterval) * time.Second)
}
}()
}
}
4 changes: 2 additions & 2 deletions web/src/SiteEditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import React from "react";
import {Button, Card, Col, Input, InputNumber, Row, Select, Switch} from "antd";
import {LinkOutlined} from "@ant-design/icons";
import * as AccountBackend from "./backend/AccountBackend";
import * as ProviderBackend from "./backend/ProviderBackend";
import * as SiteBackend from "./backend/SiteBackend";
import * as CertBackend from "./backend/CertBackend";
import * as RuleBackend from "./backend/RuleBackend";
Expand Down Expand Up @@ -103,7 +103,7 @@ class SiteEditPage extends React.Component {
}

getAlertProviders() {
AccountBackend.getProviders()
ProviderBackend.getProviders()
.then((res) => {
if (res.status === "ok") {
const data = [];
Expand Down
7 changes: 0 additions & 7 deletions web/src/backend/AccountBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,3 @@ export function signout() {
credentials: "include",
}).then(res => res.json());
}

export function getProviders() {
return fetch(`${Setting.ServerUrl}/api/get-providers`, {
method: "GET",
credentials: "include",
}).then(res => res.json());
}
22 changes: 22 additions & 0 deletions web/src/backend/ProviderBackend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as Setting from "../Setting";

export function getProviders() {
return fetch(`${Setting.ServerUrl}/api/get-providers`, {
method: "GET",
credentials: "include",
}).then(res => res.json());
}

0 comments on commit 4b7ef76

Please sign in to comment.