mirror of
https://github.com/bolucat/Archive.git
synced 2025-12-24 13:28:37 +08:00
Update On Tue Jul 2 20:34:19 CEST 2024
This commit is contained in:
3
echo/.github/workflows/cd.yaml
vendored
3
echo/.github/workflows/cd.yaml
vendored
@@ -26,7 +26,8 @@ jobs:
|
||||
steps:
|
||||
- id: set-matrix
|
||||
run: |
|
||||
echo '::set-output name=matrix::{"platform":["amd64","arm64"]}'
|
||||
echo '{"platform": ["amd64", "arm64"]}' > matrix.json
|
||||
echo "matrix=$(cat matrix.json)" >> $GITHUB_OUTPUT
|
||||
|
||||
build-bin:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@@ -10,5 +10,5 @@ type Reloader interface {
|
||||
|
||||
type HealthChecker interface {
|
||||
// get relay by ID and check the connection health
|
||||
HealthCheck(ctx context.Context, RelayID string) error
|
||||
HealthCheck(ctx context.Context, RelayID string) (int64, error)
|
||||
}
|
||||
|
||||
@@ -55,6 +55,10 @@ func (r *Config) GetWSRemoteAddr(baseAddr string) (string, error) {
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
func (r *Config) GetTCPRemotes() string {
|
||||
return fmt.Sprintf("%v", r.TCPRemotes)
|
||||
}
|
||||
|
||||
func (r *Config) Validate() error {
|
||||
if r.Adjust() != nil {
|
||||
return errors.New("adjust config failed")
|
||||
|
||||
@@ -9,10 +9,10 @@ import (
|
||||
|
||||
var _ glue.HealthChecker = (*Server)(nil)
|
||||
|
||||
func (r *Server) HealthCheck(ctx context.Context, relayID string) error {
|
||||
func (r *Server) HealthCheck(ctx context.Context, relayID string) (int64, error) {
|
||||
rs, ok := r.relayM.Load(relayID)
|
||||
if !ok {
|
||||
return fmt.Errorf("label for relay: %s not found,can not health check", relayID)
|
||||
return 0, fmt.Errorf("label for relay: %s not found,can not health check", relayID)
|
||||
}
|
||||
inner, _ := rs.(*Relay)
|
||||
return inner.relayServer.HealthCheck(ctx)
|
||||
|
||||
@@ -101,6 +101,7 @@ func (b *baseTransporter) RelayTCPConn(c net.Conn, handshakeF TCPHandShakeF) err
|
||||
return relayConn.Transport(remote.Label)
|
||||
}
|
||||
|
||||
func (b *baseTransporter) HealthCheck(ctx context.Context) error {
|
||||
return b.relayer.HealthCheck(ctx, b.GetRemote().Clone())
|
||||
func (b *baseTransporter) HealthCheck(ctx context.Context) (int64, error) {
|
||||
remote := b.GetRemote().Clone()
|
||||
return remote.HandShakeDuration.Milliseconds(), b.relayer.HealthCheck(ctx, remote)
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func newRelayClient(cfg *conf.Config) (RelayClient, error) {
|
||||
type RelayServer interface {
|
||||
ListenAndServe() error
|
||||
Close() error
|
||||
HealthCheck(ctx context.Context) error
|
||||
HealthCheck(ctx context.Context) (int64, error) // latency in ms
|
||||
}
|
||||
|
||||
func NewRelayServer(cfg *conf.Config, cmgr cmgr.Cmgr) (RelayServer, error) {
|
||||
|
||||
@@ -103,11 +103,12 @@ func (s *Server) HandleHealthCheck(c echo.Context) error {
|
||||
if relayLabel == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "relay_label is required")
|
||||
}
|
||||
if err := s.HealthCheck(c.Request().Context(), relayLabel); err != nil {
|
||||
res := CommonResp{Message: err.Error()}
|
||||
latency, err := s.HealthCheck(c.Request().Context(), relayLabel)
|
||||
if err != nil {
|
||||
res := HealthCheckResp{Message: err.Error(), ErrorCode: -1}
|
||||
return c.JSON(http.StatusBadRequest, res)
|
||||
}
|
||||
return c.JSON(http.StatusOK, CommonResp{Message: "connect success"})
|
||||
return c.JSON(http.StatusOK, HealthCheckResp{Message: "connect success", Latency: latency})
|
||||
}
|
||||
|
||||
func (s *Server) CurrentConfig(c echo.Context) error {
|
||||
@@ -158,3 +159,9 @@ func (s *Server) ListConnections(c echo.Context) error {
|
||||
"AllCount": s.connMgr.CountConnection("active") + s.connMgr.CountConnection("closed"),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) ListRules(c echo.Context) error {
|
||||
return c.Render(http.StatusOK, "rule_list.html", map[string]interface{}{
|
||||
"Configs": s.cfg.RelayConfigs,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -102,6 +102,7 @@ func NewServer(
|
||||
|
||||
e.GET("/", s.index)
|
||||
e.GET("/connections/", s.ListConnections)
|
||||
e.GET("/rules/", s.ListRules)
|
||||
e.GET("/clash_proxy_provider/", s.HandleClashProxyProvider)
|
||||
|
||||
// api group
|
||||
|
||||
@@ -70,6 +70,13 @@
|
||||
>Connections</a
|
||||
>
|
||||
</li>
|
||||
<li>
|
||||
<a
|
||||
href="/rules/"
|
||||
class="button is-info is-light"
|
||||
>Rule List</a
|
||||
>
|
||||
</li>
|
||||
<li>
|
||||
<a
|
||||
href="/api/v1/config/"
|
||||
|
||||
86
echo/internal/web/templates/rule_list.html
Normal file
86
echo/internal/web/templates/rule_list.html
Normal file
@@ -0,0 +1,86 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="ehco web" />
|
||||
<meta name="keywords" content="ehco-relay" />
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://cdnjs.cloudflare.com/ajax/libs/bulma/1.0.1/css/bulma.min.css"
|
||||
/>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
<title>Rules</title>
|
||||
</head>
|
||||
<body>
|
||||
<section class="section">
|
||||
<div class="container">
|
||||
<h1 class="title">Rules</h1>
|
||||
<table class="table is-striped is-fullwidth">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Label</th>
|
||||
<th>Listen</th>
|
||||
<th>Listen Type</th>
|
||||
<th>Transport Type</th>
|
||||
<th>Remote</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Configs}}
|
||||
<tr>
|
||||
<td>{{.Label}}</td>
|
||||
<td>{{.Listen}}</td>
|
||||
<td>{{.ListenType}}</td>
|
||||
<td>{{.TransportType}}</td>
|
||||
<td>{{.GetTCPRemotes}}</td>
|
||||
<td>
|
||||
<button
|
||||
class="button is-small is-primary health-check"
|
||||
data-label="{{.Label}}"
|
||||
onclick="checkHealth('{{.Label}}')"
|
||||
>
|
||||
Check Health
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<script>
|
||||
function checkHealth(label) {
|
||||
$.ajax({
|
||||
url: "/api/v1/health_check/?relay_label=" + label,
|
||||
method: "GET",
|
||||
success: function (response) {
|
||||
// Check if the response includes an error code
|
||||
if (response.error_code === 0) {
|
||||
// If no error, show success message with latency
|
||||
alert(
|
||||
"Health Check for " +
|
||||
label +
|
||||
": " +
|
||||
response.msg + // Use 'msg' as per Go struct
|
||||
" (Latency: " +
|
||||
response.latency + // Ensure this matches the Go struct field name
|
||||
"ms)"
|
||||
);
|
||||
} else {
|
||||
// If error code is not 0, show error message
|
||||
alert("Error for " + label + ": " + response.msg);
|
||||
}
|
||||
},
|
||||
error: function (xhr) {
|
||||
// Parse the response JSON in case of HTTP error
|
||||
var response = JSON.parse(xhr.responseText);
|
||||
alert("Error: " + response.msg); // Use 'msg' as per Go struct
|
||||
},
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,5 +1,7 @@
|
||||
package web
|
||||
|
||||
type CommonResp struct {
|
||||
Message string `json:"msg"`
|
||||
type HealthCheckResp struct {
|
||||
ErrorCode int `json:"error_code"` // code = 0 means success
|
||||
Message string `json:"msg"`
|
||||
Latency int64 `json:"latency"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user