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

Fix host #99

Merged
merged 2 commits into from
Jan 15, 2024
Merged
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
59 changes: 34 additions & 25 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/flanksource/commons/is"
"github.com/flanksource/commons/logger"
)

var (
Expand Down Expand Up @@ -47,201 +48,209 @@ var (
BrightWhite = "\x1b[38;5;244m"
)

var (
isTTY = is.TTY()
)

func ColorOff() {
isTTY = false
}

// DarkWhitef prints and formats msg as dark white
func DarkWhitef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return DarkWhite + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// DarkWhitef prints and formats msg as dark white
func LightWhitef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return white + Light + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// DarkWhitef prints and formats msg as dark white
func BrightWhitef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return white + Bright + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// DarkWhitef prints and formats msg as dark white
func DarkF(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return DarkWhite + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// DarkWhitef prints and formats msg as dark white
func BrightF(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return DarkWhite + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Redf prints and formats msg as red text
func Redf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Red + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightRedf prints and formats msg as red text
func LightRedf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightRed + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Yellowf prints and formats msg as red text
func Yellowf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Yellow + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// BrightYellowf prints and formats msg as red text
func BrightYellowf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return BrightYellow + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightYellowf prints and formats msg as red text
func LightYellowf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightYellow + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Grayf prints and formats msg as red text
func Grayf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Gray + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// BrightGrayf prints and formats msg as red text
func BrightGrayf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return BrightGray + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightGrayf prints and formats msg as red text
func LightGrayf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightGray + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Bluef prints and formats msg as red text
func Bluef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Blue + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightBluef prints and formats msg as red text
func LightBluef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightBlue + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// BrightBluef prints and formats msg as red text
func BrightBluef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return blue + Light + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Greenf prints and formats msg as green text
func Greenf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Green + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// BrightGreenf prints and formats msg as light green text
func BrightGreenf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return green + Bright + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightGreenf prints and formats msg as light green text
func LightGreenf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return green + Light + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightCyanf prints and formats msg as light cyan text
func LightCyanf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightCyan + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightCyanf prints and formats msg as light cyan text
func Cyanf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Cyan + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightCyanf prints and formats msg as light cyan text
func BrightCyanf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return BrightCyan + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Magentaf prints and formats msg as green text
func Magentaf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Magenta + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Magentaf prints and formats msg as green text
func LightMagentaf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightMagenta + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Magentaf prints and formats msg as green text
func BrightMagentaf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return BrightMagenta + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
Expand Down
6 changes: 4 additions & 2 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ func (c Context) WithTimeout(timeout time.Duration) (Context, gocontext.CancelFu
}

func (c Context) IsDebug() bool {
return (c.debug != nil && *c.debug) || (c.isDebugFn != nil && c.isDebugFn(c))
return c.logger.IsLevelEnabled(5) ||
(c.debug != nil && *c.debug) ||
(c.isDebugFn != nil && c.isDebugFn(c))
}

func (c Context) IsTrace() bool {
return (c.trace != nil && *c.trace) || (c.isTraceFn != nil && c.isTraceFn(c))
return c.logger.IsLevelEnabled(6) || (c.trace != nil && *c.trace) || (c.isTraceFn != nil && c.isTraceFn(c))
}

func (c Context) Debugf(format string, args ...interface{}) {
Expand Down
31 changes: 25 additions & 6 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type Client struct {
cacheDNS bool

userAgent string

tlsConfig *tls.Config
}

// NewClient configures a new HTTP client using given configuration
Expand Down Expand Up @@ -186,6 +188,7 @@ func (c *Client) InsecureSkipVerify(val bool) *Client {
}

customTransport.TLSClientConfig.InsecureSkipVerify = val
c.tlsConfig = customTransport.TLSClientConfig
c.httpClient.Transport = customTransport
return c
}
Expand Down Expand Up @@ -253,31 +256,49 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
var host string
if r.client.connectTo != "" {
host = r.client.connectTo
} else if h := r.getHeader("Host"); h != "" {
host = h
} else {
host = r.url.Hostname()
}

if c.cacheDNS {
if ips, _ := dns.CacheLookup("A", host); len(ips) > 0 {
host = ips[0].String()
if c.headers.Get("Host") == "" && r.headers.Get("Host") == "" {
// add hostname back as a header
r = r.Header("Host", r.url.Hostname())
}
}
}

req, err := http.NewRequestWithContext(r.ctx, r.method, r.url.String(), r.body)
if r.url.Scheme == "https" && c.tlsConfig == nil {
// initialize default TLS settings
c.InsecureSkipVerify(true)
}

uri := *r.url
uri.Host = host
req, err := http.NewRequestWithContext(r.ctx, r.method, uri.String(), r.body)
if err != nil {
return nil, err
}

// use the headers from the client & add/overwrite them with headers from the request
// use the headers from the client
req.Header = c.headers.Clone()
// add/overwrite them with headers from the request
for k, v := range r.headers.Clone() {
for _, vv := range v {
req.Header.Set(k, vv)
}
}

if h := req.Header.Get("Host"); h != "" {
req.Host = h
if c.tlsConfig != nil {
c.tlsConfig.ServerName = h
}
req.Header.Del("Host")
}

if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", c.userAgent)
}
Expand All @@ -293,8 +314,6 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
}
}
req.URL.RawQuery = queryParam.Encode()

req.Host = host
if r.client.authConfig != nil && !r.client.authConfig.IsEmpty() {
req.SetBasicAuth(r.client.authConfig.Username, r.client.authConfig.Password)
}
Expand Down
Loading
Loading