| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package health |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "net" |
| 23 | "net/http" |
| 24 | "strings" |
| 25 | "time" |
| 26 |
| 27 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 28 | "github.com/fatedier/frp/pkg/util/xlog" |
| 29 | ) |
| 30 |
| 31 | var ErrHealthCheckType = errors.New("error health check type") |
| 32 |
| 33 | type Monitor struct { |
| 34 | checkType string |
| 35 | interval time.Duration |
| 36 | timeout time.Duration |
| 37 | maxFailedTimes int |
| 38 |
| 39 | // For tcp |
| 40 | addr string |
| 41 |
| 42 | // For http |
| 43 | url string |
| 44 | header http.Header |
| 45 | failedTimes uint64 |
| 46 | statusOK bool |
| 47 | statusNormalFn func() |
| 48 | statusFailedFn func() |
| 49 |
| 50 | ctx context.Context |
| 51 | cancel context.CancelFunc |
| 52 | } |
| 53 |
| 54 | func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string, |
| 55 | statusNormalFn func(), statusFailedFn func(), |
| 56 | ) *Monitor { |
| 57 | if cfg.IntervalSeconds <= 0 { |
| 58 | cfg.IntervalSeconds = 10 |
| 59 | } |
| 60 | if cfg.TimeoutSeconds <= 0 { |
| 61 | cfg.TimeoutSeconds = 3 |
| 62 | } |
| 63 | if cfg.MaxFailed <= 0 { |
| 64 | cfg.MaxFailed = 1 |
| 65 | } |
| 66 | newctx, cancel := context.WithCancel(ctx) |
| 67 |
| 68 | var url string |
| 69 | if cfg.Type == "http" && cfg.Path != "" { |
| 70 | s := "http://" + addr |
| 71 | if !strings.HasPrefix(cfg.Path, "/") { |
| 72 | s += "/" |
| 73 | } |
| 74 | url = s + cfg.Path |
| 75 | } |
| 76 | header := make(http.Header) |
| 77 | for _, h := range cfg.HTTPHeaders { |
| 78 | header.Set(h.Name, h.Value) |
| 79 | } |
| 80 |
| 81 | return &Monitor{ |
| 82 | checkType: cfg.Type, |
| 83 | interval: time.Duration(cfg.IntervalSeconds) * time.Second, |
| 84 | timeout: time.Duration(cfg.TimeoutSeconds) * time.Second, |
| 85 | maxFailedTimes: cfg.MaxFailed, |
| 86 | addr: addr, |
| 87 | url: url, |
| 88 | header: header, |
| 89 | statusOK: false, |
| 90 | statusNormalFn: statusNormalFn, |
| 91 | statusFailedFn: statusFailedFn, |
| 92 | ctx: newctx, |
| 93 | cancel: cancel, |
| 94 | } |
| 95 | } |
| 96 |
| 97 | func (monitor *Monitor) Start() { |
| 98 | go monitor.checkWorker() |
| 99 | } |
| 100 |
| 101 | func (monitor *Monitor) Stop() { |
| 102 | monitor.cancel() |
| 103 | } |
| 104 |
| 105 | func (monitor *Monitor) checkWorker() { |
| 106 | xl := xlog.FromContextSafe(monitor.ctx) |
| 107 | for { |
| 108 | doCtx, cancel := context.WithDeadline(monitor.ctx, time.Now().Add(monitor.timeout)) |
| 109 | err := monitor.doCheck(doCtx) |
| 110 |
| 111 | // check if this monitor has been closed |
| 112 | select { |
| 113 | case <-monitor.ctx.Done(): |
| 114 | cancel() |
| 115 | return |
| 116 | default: |
| 117 | cancel() |
| 118 | } |
| 119 |
| 120 | if err == nil { |
| 121 | xl.Tracef("do one health check success") |
| 122 | if !monitor.statusOK && monitor.statusNormalFn != nil { |
| 123 | xl.Infof("health check status change to success") |
| 124 | monitor.statusOK = true |
| 125 | monitor.statusNormalFn() |
| 126 | } |
| 127 | } else { |
| 128 | xl.Warnf("do one health check failed: %v", err) |
| 129 | monitor.failedTimes++ |
| 130 | if monitor.statusOK && int(monitor.failedTimes) >= monitor.maxFailedTimes && monitor.statusFailedFn != nil { |
| 131 | xl.Warnf("health check status change to failed") |
| 132 | monitor.statusOK = false |
| 133 | monitor.statusFailedFn() |
| 134 | } |
| 135 | } |
| 136 |
| 137 | time.Sleep(monitor.interval) |
| 138 | } |
| 139 | } |
| 140 |
| 141 | func (monitor *Monitor) doCheck(ctx context.Context) error { |
| 142 | switch monitor.checkType { |
| 143 | case "tcp": |
| 144 | return monitor.doTCPCheck(ctx) |
| 145 | case "http": |
| 146 | return monitor.doHTTPCheck(ctx) |
| 147 | default: |
| 148 | return ErrHealthCheckType |
| 149 | } |
| 150 | } |
| 151 |
| 152 | func (monitor *Monitor) doTCPCheck(ctx context.Context) error { |
| 153 | // if tcp address is not specified, always return nil |
| 154 | if monitor.addr == "" { |
| 155 | return nil |
| 156 | } |
| 157 |
| 158 | var d net.Dialer |
| 159 | conn, err := d.DialContext(ctx, "tcp", monitor.addr) |
| 160 | if err != nil { |
| 161 | return err |
| 162 | } |
| 163 | conn.Close() |
| 164 | return nil |
| 165 | } |
| 166 |
| 167 | func (monitor *Monitor) doHTTPCheck(ctx context.Context) error { |
| 168 | req, err := http.NewRequestWithContext(ctx, "GET", monitor.url, nil) |
| 169 | if err != nil { |
| 170 | return err |
| 171 | } |
| 172 | req.Header = monitor.header |
| 173 | req.Host = monitor.header.Get("Host") |
| 174 | resp, err := http.DefaultClient.Do(req) |
| 175 | if err != nil { |
| 176 | return err |
| 177 | } |
| 178 | defer resp.Body.Close() |
| 179 | _, _ = io.Copy(io.Discard, resp.Body) |
| 180 |
| 181 | if resp.StatusCode/100 != 2 { |
| 182 | return fmt.Errorf("do http health check, StatusCode is [%d] not 2xx", resp.StatusCode) |
| 183 | } |
| 184 | return nil |
| 185 | } |