-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathhelpers.go
62 lines (51 loc) · 1.31 KB
/
helpers.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
package main
import (
"log"
"net"
"strconv"
"strings"
"github.com/boynux/squid-exporter/config"
proxyproto "github.com/pires/go-proxyproto"
)
func createProxyHeader(cfg *config.Config) string {
la := strings.Split(cfg.ListenAddress, ":")
if len(la) < 2 {
log.Printf("Cannot parse listen address (%s). Failed to create proxy header\n", cfg.ListenAddress)
return ""
}
spt, err := strconv.Atoi(la[1])
if err != nil {
log.Printf("Failed to create proxy header: %v\n", err.Error())
return ""
}
sip, err := net.LookupIP(la[0])
if err != nil {
log.Printf("Failed to create proxy header: %v\n", err.Error())
return ""
}
dip, err := net.LookupIP(cfg.SquidHostname)
if err != nil {
log.Printf("Failed to create proxy header: %v\n", err.Error())
return ""
}
ph := &proxyproto.Header{
Version: 1,
Command: proxyproto.PROXY,
TransportProtocol: proxyproto.TCPv4,
SourceAddr: &net.TCPAddr{
IP: sip[0],
Port: spt,
},
DestinationAddr: &net.TCPAddr{
IP: dip[0],
Port: cfg.SquidPort,
},
}
phs, err := ph.Format()
if err != nil {
log.Printf("Failed to create proxy header: %v\n", err.Error())
}
// proxyproto adds crlf to the end of the header string, but we will add this later
// we are triming it here.
return strings.TrimSuffix(string(phs), "\r\n")
}