/
net.go
62 lines (55 loc) · 1.48 KB
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2022 <mzh.scnu@qq.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package proxy
import (
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
)
// ConnectionTimeout refers to connection timeout for health check
var ConnectionTimeout = 3 * time.Second
// GetIP get client IP
func GetIP(r *http.Request) string {
clientIP, _, _ := net.SplitHostPort(r.RemoteAddr)
if len(r.Header.Get(XForwardedFor)) != 0 {
xff := r.Header.Get(XForwardedFor)
s := strings.Index(xff, ", ")
if s == -1 {
s = len(r.Header.Get(XForwardedFor))
}
clientIP = xff[:s]
} else if len(r.Header.Get(XRealIP)) != 0 {
clientIP = r.Header.Get(XRealIP)
}
return clientIP
}
// GetHost get the hostname, looks like IP:Port
func GetHost(url *url.URL) string {
if _, _, err := net.SplitHostPort(url.Host); err == nil {
return url.Host
}
if url.Scheme == "http" {
return fmt.Sprintf("%s:%s", url.Host, "80")
} else if url.Scheme == "https" {
return fmt.Sprintf("%s:%s", url.Host, "443")
}
return url.Host
}
// IsBackendAlive Attempt to establish a tcp connection to determine whether the site is alive
func IsBackendAlive(host string) bool {
addr, err := net.ResolveTCPAddr("tcp", host)
if err != nil {
return false
}
resolveAddr := fmt.Sprintf("%s:%d", addr.IP, addr.Port)
conn, err := net.DialTimeout("tcp", resolveAddr, ConnectionTimeout)
if err != nil {
return false
}
_ = conn.Close()
return true
}