2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-03-18 03:07:08 +00:00
|
|
|
|
2020-03-18 00:21:50 +00:00
|
|
|
package tsweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-03-19 00:37:40 +00:00
|
|
|
// AccessLogRecord is a record of one HTTP request served.
|
|
|
|
type AccessLogRecord struct {
|
|
|
|
// Timestamp at which request processing started.
|
|
|
|
When time.Time `json:"when"`
|
|
|
|
// Time it took to finish processing the request. It does not
|
|
|
|
// include the entire lifetime of the underlying connection in
|
|
|
|
// cases like connection hijacking, only the lifetime of the HTTP
|
|
|
|
// request handler.
|
2022-08-02 04:17:38 +00:00
|
|
|
Seconds float64 `json:"duration,omitempty"`
|
2020-03-18 00:21:50 +00:00
|
|
|
|
2020-03-19 00:37:40 +00:00
|
|
|
// The client's ip:port.
|
2022-08-02 04:17:38 +00:00
|
|
|
RemoteAddr string `json:"remote_addr,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
// The HTTP protocol version, usually "HTTP/1.1 or HTTP/2".
|
2022-08-02 04:17:38 +00:00
|
|
|
Proto string `json:"proto,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
// Whether the request was received over TLS.
|
2022-08-02 04:17:38 +00:00
|
|
|
TLS bool `json:"tls,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
// The target hostname in the request.
|
2022-08-02 04:17:38 +00:00
|
|
|
Host string `json:"host,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
// The HTTP method invoked.
|
2022-08-02 04:17:38 +00:00
|
|
|
Method string `json:"method,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
// The unescaped request URI, including query parameters.
|
2022-08-02 04:17:38 +00:00
|
|
|
RequestURI string `json:"request_uri,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
|
|
|
|
// The client's user-agent
|
2022-08-02 04:17:38 +00:00
|
|
|
UserAgent string `json:"user_agent,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
// Where the client was before making this request.
|
2022-08-02 04:17:38 +00:00
|
|
|
Referer string `json:"referer,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
|
|
|
|
// The HTTP response code sent to the client.
|
2022-08-02 04:17:38 +00:00
|
|
|
Code int `json:"code,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
// Number of bytes sent in response body to client. If the request
|
|
|
|
// was hijacked, only includes bytes sent up to the point of
|
|
|
|
// hijacking.
|
2022-08-02 04:17:38 +00:00
|
|
|
Bytes int `json:"bytes,omitempty"`
|
2020-03-19 00:37:40 +00:00
|
|
|
// Error encountered during request processing.
|
2022-08-02 04:17:38 +00:00
|
|
|
Err string `json:"err,omitempty"`
|
2023-08-11 20:41:58 +00:00
|
|
|
// RequestID is a unique ID for this request. When a request fails due to an
|
|
|
|
// error, the ID is generated and displayed to the client immediately after
|
|
|
|
// the error text, as well as logged here. This makes it easier to correlate
|
|
|
|
// support requests with server logs. If a RequestID generator is not
|
|
|
|
// configured, RequestID will be empty.
|
|
|
|
RequestID RequestID `json:"request_id,omitempty"`
|
2020-03-18 00:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns m as a JSON string.
|
2020-03-19 00:37:40 +00:00
|
|
|
func (m AccessLogRecord) String() string {
|
2020-03-18 00:21:50 +00:00
|
|
|
if m.When.IsZero() {
|
|
|
|
m.When = time.Now()
|
|
|
|
}
|
|
|
|
var buf strings.Builder
|
|
|
|
json.NewEncoder(&buf).Encode(m)
|
2020-03-18 03:07:08 +00:00
|
|
|
return strings.TrimRight(buf.String(), "\n")
|
2020-03-18 00:21:50 +00:00
|
|
|
}
|