Skip to content

Commit 5a319da

Browse files
authored
Listener directive refactor (#6377)
1 parent 79223d9 commit 5a319da

File tree

1 file changed

+77
-39
lines changed

1 file changed

+77
-39
lines changed

internal/configs/version2/template_helper.go

Lines changed: 77 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ const (
2323
ipv6
2424
)
2525

26+
type listen struct {
27+
ipAddress string
28+
port string
29+
tls bool
30+
proxyProtocol bool
31+
udp bool
32+
ipType ipType
33+
}
34+
2635
const spacing = " "
2736

2837
func headerListToCIMap(headers []Header) map[string]string {
@@ -69,77 +78,92 @@ func buildListenerDirectives(listenerType protocol, s Server, port string) strin
6978
var directives string
7079

7180
if listenerType == http {
72-
directives += buildListenDirective(s.HTTPIPv4, port, s.ProxyProtocol, ipv4)
81+
directives += buildListenDirective(listen{
82+
ipAddress: s.HTTPIPv4,
83+
port: port,
84+
tls: false,
85+
proxyProtocol: s.ProxyProtocol,
86+
udp: false,
87+
ipType: ipv4,
88+
})
7389
if !s.DisableIPV6 {
7490
directives += spacing
75-
directives += buildListenDirective(s.HTTPIPv6, port, s.ProxyProtocol, ipv6)
91+
directives += buildListenDirective(listen{
92+
ipAddress: s.HTTPIPv6,
93+
port: port,
94+
tls: false,
95+
proxyProtocol: s.ProxyProtocol,
96+
udp: false,
97+
ipType: ipv6,
98+
})
7699
}
77100
} else {
78-
directives += buildListenDirective(s.HTTPSIPv4, port, s.ProxyProtocol, ipv4)
101+
directives += buildListenDirective(listen{
102+
ipAddress: s.HTTPSIPv4,
103+
port: port,
104+
tls: true,
105+
proxyProtocol: s.ProxyProtocol,
106+
udp: false,
107+
ipType: ipv4,
108+
})
79109
if !s.DisableIPV6 {
80110
directives += spacing
81-
directives += buildListenDirective(s.HTTPSIPv6, port, s.ProxyProtocol, ipv6)
111+
directives += buildListenDirective(listen{
112+
ipAddress: s.HTTPSIPv6,
113+
port: port,
114+
tls: true,
115+
proxyProtocol: s.ProxyProtocol,
116+
udp: false,
117+
ipType: ipv6,
118+
})
82119
}
83120
}
84121

85122
return directives
86123
}
87124

88125
func getDefaultPort(listenerType protocol) string {
89-
if listenerType == http {
90-
return "80"
126+
s := Server{
127+
HTTPPort: 80,
128+
HTTPSPort: 443,
91129
}
92-
return "443 ssl"
130+
131+
return getCustomPort(listenerType, s)
93132
}
94133

95134
func getCustomPort(listenerType protocol, s Server) string {
96135
if listenerType == http {
97136
return strconv.Itoa(s.HTTPPort)
98137
}
99-
return strconv.Itoa(s.HTTPSPort) + " ssl"
138+
return strconv.Itoa(s.HTTPSPort)
100139
}
101140

102-
func buildListenDirective(ip string, port string, proxyProtocol bool, ipType ipType) string {
141+
func buildListenDirective(l listen) string {
103142
base := "listen"
104143
var directive string
105144

106-
if ipType == ipv6 {
107-
if ip != "" {
108-
directive = fmt.Sprintf("%s [%s]:%s", base, ip, port)
109-
} else {
110-
directive = fmt.Sprintf("%s [::]:%s", base, port)
111-
}
112-
} else {
113-
if ip != "" {
114-
directive = fmt.Sprintf("%s %s:%s", base, ip, port)
115-
} else {
116-
directive = fmt.Sprintf("%s %s", base, port)
145+
if l.ipType == ipv6 {
146+
if l.ipAddress == "" {
147+
l.ipAddress = "::"
117148
}
149+
l.ipAddress = fmt.Sprintf("[%s]", l.ipAddress)
118150
}
119151

120-
if proxyProtocol {
121-
directive += " proxy_protocol"
122-
}
123-
124-
directive += ";\n"
125-
return directive
126-
}
127-
128-
func buildTransportListenDirective(ipType ipType, port string, ssl *StreamSSL, udp bool) string {
129-
base := "listen"
130-
var directive string
131-
132-
if ipType == ipv6 {
133-
directive = base + " [::]:" + port
152+
if l.ipAddress != "" {
153+
directive = fmt.Sprintf("%s %s:%s", base, l.ipAddress, l.port)
134154
} else {
135-
directive = base + " " + port
155+
directive = fmt.Sprintf("%s %s", base, l.port)
136156
}
137157

138-
if ssl.Enabled {
158+
if l.tls {
139159
directive += " ssl"
140160
}
141161

142-
if udp {
162+
if l.proxyProtocol {
163+
directive += " proxy_protocol"
164+
}
165+
166+
if l.udp {
143167
directive += " udp"
144168
}
145169

@@ -159,11 +183,25 @@ func makeTransportListener(s StreamServer) string {
159183
var directives string
160184
port := strconv.Itoa(s.Port)
161185

162-
directives += buildTransportListenDirective(ipv4, port, s.SSL, s.UDP)
186+
directives += buildListenDirective(listen{
187+
ipAddress: "",
188+
port: port,
189+
tls: s.SSL.Enabled,
190+
proxyProtocol: false,
191+
udp: s.UDP,
192+
ipType: ipv4,
193+
})
163194

164195
if !s.DisableIPV6 {
165196
directives += spacing
166-
directives += buildTransportListenDirective(ipv6, port, s.SSL, s.UDP)
197+
directives += buildListenDirective(listen{
198+
ipAddress: "",
199+
port: port,
200+
tls: s.SSL.Enabled,
201+
proxyProtocol: false,
202+
udp: s.UDP,
203+
ipType: ipv6,
204+
})
167205
}
168206

169207
return directives

0 commit comments

Comments
 (0)