Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow custom origin header for Websocket #702

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,11 @@ func Ping(address string, config *Config) (bool, time.Duration) {
}

// QueryWebSocket opens a websocket connection, write `body` and return a message from the server
func QueryWebSocket(address, body string, config *Config) (bool, []byte, error) {
func QueryWebSocket(address, body string, origin string, config *Config) (bool, []byte, error) {
const (
Origin = "http://localhost/"
MaximumMessageSize = 1024 // in bytes
)
wsConfig, err := websocket.NewConfig(address, Origin)
wsConfig, err := websocket.NewConfig(address, origin)
if err != nil {
return false, nil, fmt.Errorf("error configuring websocket connection: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ func TestHttpClientProvidesOAuth2BearerToken(t *testing.T) {
}

func TestQueryWebSocket(t *testing.T) {
_, _, err := QueryWebSocket("", "body", &Config{Timeout: 2 * time.Second})
_, _, err := QueryWebSocket("", "body", "", &Config{Timeout: 2 * time.Second})
if err == nil {
t.Error("expected an error due to the address being invalid")
}
_, _, err = QueryWebSocket("ws://example.org", "body", &Config{Timeout: 2 * time.Second})
_, _, err = QueryWebSocket("ws://example.org", "body", "", &Config{Timeout: 2 * time.Second})
if err == nil {
t.Error("expected an error due to the target not being websocket-friendly")
}
Expand Down
13 changes: 12 additions & 1 deletion core/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const (
// GatusUserAgent is the default user agent that Gatus uses to send requests.
GatusUserAgent = "Gatus/1.0"

Origin = "http://localhost"
OriginHeader = "Origin"

EndpointTypeDNS EndpointType = "DNS"
EndpointTypeTCP EndpointType = "TCP"
EndpointTypeSCTP EndpointType = "SCTP"
Expand Down Expand Up @@ -221,6 +224,14 @@ func (endpoint *Endpoint) ValidateAndSetDefaults() error {
if _, contentTypeHeaderExists := endpoint.Headers[ContentTypeHeader]; !contentTypeHeaderExists && endpoint.GraphQL {
endpoint.Headers[ContentTypeHeader] = "application/json"
}

// Automatically add Origin header for websocket endpoints if there isn't one specified
if endpoint.Type() == EndpointTypeWS {
if _, originHeaderExists := endpoint.Headers[OriginHeader]; !originHeaderExists {
endpoint.Headers[OriginHeader] = Origin
}
}
Comment on lines +228 to +233
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that adding support for passing the origin header is worth it, I think perhaps we shouldn't add it by default here but instead add a note in the docs mentioning that users may have to specify an Origin header.


for _, endpointAlert := range endpoint.Alerts {
if err := endpointAlert.ValidateAndSetDefaults(); err != nil {
return err
Expand Down Expand Up @@ -376,7 +387,7 @@ func (endpoint *Endpoint) call(result *Result) {
} else if endpointType == EndpointTypeICMP {
result.Connected, result.Duration = client.Ping(strings.TrimPrefix(endpoint.URL, "icmp://"), endpoint.ClientConfig)
} else if endpointType == EndpointTypeWS {
result.Connected, result.Body, err = client.QueryWebSocket(endpoint.URL, endpoint.Body, endpoint.ClientConfig)
result.Connected, result.Body, err = client.QueryWebSocket(endpoint.URL, endpoint.Body, endpoint.Headers[OriginHeader], endpoint.ClientConfig)
if err != nil {
result.AddError(err.Error())
return
Expand Down