Add expectedResponseCode to BurstObservatory

This commit is contained in:
yuhan6665 2024-11-24 16:45:14 -05:00
parent 513f18bf53
commit 8c111180d2
5 changed files with 73 additions and 38 deletions

View File

@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// versions: // versions:
// protoc-gen-go v1.35.1 // protoc-gen-go v1.35.2
// protoc v5.28.2 // protoc v5.28.2
// source: app/observatory/burst/config.proto // source: app/observatory/burst/config.proto
@ -90,6 +90,7 @@ type HealthPingConfig struct {
SamplingCount int32 `protobuf:"varint,4,opt,name=samplingCount,proto3" json:"samplingCount,omitempty"` SamplingCount int32 `protobuf:"varint,4,opt,name=samplingCount,proto3" json:"samplingCount,omitempty"`
// ping timeout, int64 values of time.Duration // ping timeout, int64 values of time.Duration
Timeout int64 `protobuf:"varint,5,opt,name=timeout,proto3" json:"timeout,omitempty"` Timeout int64 `protobuf:"varint,5,opt,name=timeout,proto3" json:"timeout,omitempty"`
ExpectedResponseCode []int32 `protobuf:"varint,6,rep,packed,name=expectedResponseCode,proto3" json:"expectedResponseCode,omitempty"`
} }
func (x *HealthPingConfig) Reset() { func (x *HealthPingConfig) Reset() {
@ -157,6 +158,13 @@ func (x *HealthPingConfig) GetTimeout() int64 {
return 0 return 0
} }
func (x *HealthPingConfig) GetExpectedResponseCode() []int32 {
if x != nil {
return x.ExpectedResponseCode
}
return nil
}
var File_app_observatory_burst_config_proto protoreflect.FileDescriptor var File_app_observatory_burst_config_proto protoreflect.FileDescriptor
var file_app_observatory_burst_config_proto_rawDesc = []byte{ var file_app_observatory_burst_config_proto_rawDesc = []byte{
@ -173,7 +181,7 @@ var file_app_observatory_burst_config_proto_rawDesc = []byte{
0x2e, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72, 0x79, 0x2e, 0x62, 0x75, 0x72, 0x2e, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72, 0x79, 0x2e, 0x62, 0x75, 0x72,
0x73, 0x74, 0x2e, 0x48, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x50, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x2e, 0x48, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x50, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6e,
0x66, 0x69, 0x67, 0x52, 0x0a, 0x70, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x66, 0x69, 0x67, 0x52, 0x0a, 0x70, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22,
0xb4, 0x01, 0x0a, 0x10, 0x48, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x50, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0xe8, 0x01, 0x0a, 0x10, 0x48, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x50, 0x69, 0x6e, 0x67, 0x43, 0x6f,
0x6e, 0x66, 0x69, 0x67, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x74, 0x69, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x74, 0x69,
0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x22, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x22, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63,
@ -184,14 +192,18 @@ var file_app_observatory_burst_config_proto_rawDesc = []byte{
0x6e, 0x67, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0d, 0x73, 0x6e, 0x67, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0d, 0x73,
0x61, 0x6d, 0x70, 0x6c, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x6d, 0x70, 0x6c, 0x69, 0x6e, 0x67, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x18, 0x0a, 0x07,
0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x74, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x74,
0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x42, 0x70, 0x0a, 0x1e, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x32, 0x0a, 0x14, 0x65, 0x78, 0x70, 0x65, 0x63, 0x74,
0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x65, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x06,
0x72, 0x79, 0x2e, 0x62, 0x75, 0x72, 0x73, 0x74, 0x50, 0x01, 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x20, 0x03, 0x28, 0x05, 0x52, 0x14, 0x65, 0x78, 0x70, 0x65, 0x63, 0x74, 0x65, 0x64, 0x52, 0x65,
0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x43, 0x6f, 0x64, 0x65, 0x42, 0x70, 0x0a, 0x1e, 0x63, 0x6f,
0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x6f, 0x62, 0x73, 0x65, 0x72,
0x61, 0x74, 0x6f, 0x72, 0x79, 0x2f, 0x62, 0x75, 0x72, 0x73, 0x74, 0xaa, 0x02, 0x1a, 0x58, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72, 0x79, 0x2e, 0x62, 0x75, 0x72, 0x73, 0x74, 0x50, 0x01, 0x5a, 0x2f,
0x61, 0x79, 0x2e, 0x41, 0x70, 0x70, 0x2e, 0x4f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f,
0x72, 0x79, 0x2e, 0x42, 0x75, 0x72, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x6f, 0x62,
0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72, 0x79, 0x2f, 0x62, 0x75, 0x72, 0x73, 0x74, 0xaa,
0x02, 0x1a, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x41, 0x70, 0x70, 0x2e, 0x4f, 0x62, 0x73, 0x65, 0x72,
0x76, 0x61, 0x74, 0x6f, 0x72, 0x79, 0x2e, 0x42, 0x75, 0x72, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@ -26,4 +26,5 @@ message HealthPingConfig {
int32 samplingCount = 4; int32 samplingCount = 4;
// ping timeout, int64 values of time.Duration // ping timeout, int64 values of time.Duration
int64 timeout = 5; int64 timeout = 5;
repeated int32 expectedResponseCode = 6;
} }

View File

@ -18,6 +18,7 @@ type HealthPingSettings struct {
Interval time.Duration `json:"interval"` Interval time.Duration `json:"interval"`
SamplingCount int `json:"sampling"` SamplingCount int `json:"sampling"`
Timeout time.Duration `json:"timeout"` Timeout time.Duration `json:"timeout"`
ExpectedResponseCode []int32 `json:"expectedResponseCode"`
} }
// HealthPing is the health checker for balancers // HealthPing is the health checker for balancers
@ -41,6 +42,7 @@ func NewHealthPing(ctx context.Context, config *HealthPingConfig) *HealthPing {
Interval: time.Duration(config.Interval), Interval: time.Duration(config.Interval),
SamplingCount: int(config.SamplingCount), SamplingCount: int(config.SamplingCount),
Timeout: time.Duration(config.Timeout), Timeout: time.Duration(config.Timeout),
ExpectedResponseCode: config.ExpectedResponseCode,
} }
} }
if settings.Destination == "" { if settings.Destination == "" {
@ -48,6 +50,7 @@ func NewHealthPing(ctx context.Context, config *HealthPingConfig) *HealthPing {
// https://github.com/chromium/chromium/blob/main/components/safety_check/url_constants.cc#L10 // https://github.com/chromium/chromium/blob/main/components/safety_check/url_constants.cc#L10
// https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/safety_check/url_constants.cc#10 // https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/safety_check/url_constants.cc#10
settings.Destination = "https://connectivitycheck.gstatic.com/generate_204" settings.Destination = "https://connectivitycheck.gstatic.com/generate_204"
settings.ExpectedResponseCode = []int32{ 204 }
} }
if settings.Interval == 0 { if settings.Interval == 0 {
settings.Interval = time.Duration(1) * time.Minute settings.Interval = time.Duration(1) * time.Minute
@ -152,6 +155,7 @@ func (h *HealthPing) doCheck(tags []string, duration time.Duration, rounds int)
h.Settings.Destination, h.Settings.Destination,
h.Settings.Timeout, h.Settings.Timeout,
handler, handler,
h.Settings.ExpectedResponseCode,
) )
for i := 0; i < rounds; i++ { for i := 0; i < rounds; i++ {
delay := time.Duration(0) delay := time.Duration(0)

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet/tagged" "github.com/xtls/xray-core/transport/internet/tagged"
) )
@ -12,12 +13,14 @@ import (
type pingClient struct { type pingClient struct {
destination string destination string
httpClient *http.Client httpClient *http.Client
expectedResponseCode []int32
} }
func newPingClient(ctx context.Context, destination string, timeout time.Duration, handler string) *pingClient { func newPingClient(ctx context.Context, destination string, timeout time.Duration, handler string, expectedResponseCode []int32) *pingClient {
return &pingClient{ return &pingClient{
destination: destination, destination: destination,
httpClient: newHTTPClient(ctx, handler, timeout), httpClient: newHTTPClient(ctx, handler, timeout),
expectedResponseCode: expectedResponseCode,
} }
} }
@ -25,6 +28,7 @@ func newDirectPingClient(destination string, timeout time.Duration) *pingClient
return &pingClient{ return &pingClient{
destination: destination, destination: destination,
httpClient: &http.Client{Timeout: timeout}, httpClient: &http.Client{Timeout: timeout},
expectedResponseCode: []int32{},
} }
} }
@ -63,6 +67,18 @@ func (s *pingClient) MeasureDelay() (time.Duration, error) {
if err != nil { if err != nil {
return rttFailed, err return rttFailed, err
} }
if len(s.expectedResponseCode) > 0 {
found := false
for _, c := range s.expectedResponseCode {
if c == int32(resp.StatusCode) {
found = true
break
}
}
if !found {
return rttFailed, errors.New("Unexpected response code: ", resp.StatusCode, " expected: ", s.expectedResponseCode)
}
}
// don't wait for body // don't wait for body
resp.Body.Close() resp.Body.Close()
return time.Since(start), nil return time.Since(start), nil

View File

@ -51,6 +51,7 @@ type healthCheckSettings struct {
Interval duration.Duration `json:"interval"` Interval duration.Duration `json:"interval"`
SamplingCount int `json:"sampling"` SamplingCount int `json:"sampling"`
Timeout duration.Duration `json:"timeout"` Timeout duration.Duration `json:"timeout"`
ExpectedResponseCode []int32 `json:"expectedResponseCode"`
} }
func (h healthCheckSettings) Build() (proto.Message, error) { func (h healthCheckSettings) Build() (proto.Message, error) {
@ -60,6 +61,7 @@ func (h healthCheckSettings) Build() (proto.Message, error) {
Interval: int64(h.Interval), Interval: int64(h.Interval),
Timeout: int64(h.Timeout), Timeout: int64(h.Timeout),
SamplingCount: int32(h.SamplingCount), SamplingCount: int32(h.SamplingCount),
ExpectedResponseCode: h.ExpectedResponseCode,
}, nil }, nil
} }