@@ -23,6 +23,15 @@ const (
23
23
ipv6
24
24
)
25
25
26
+ type listen struct {
27
+ ipAddress string
28
+ port string
29
+ tls bool
30
+ proxyProtocol bool
31
+ udp bool
32
+ ipType ipType
33
+ }
34
+
26
35
const spacing = " "
27
36
28
37
func headerListToCIMap (headers []Header ) map [string ]string {
@@ -69,77 +78,92 @@ func buildListenerDirectives(listenerType protocol, s Server, port string) strin
69
78
var directives string
70
79
71
80
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
+ })
73
89
if ! s .DisableIPV6 {
74
90
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
+ })
76
99
}
77
100
} 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
+ })
79
109
if ! s .DisableIPV6 {
80
110
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
+ })
82
119
}
83
120
}
84
121
85
122
return directives
86
123
}
87
124
88
125
func getDefaultPort (listenerType protocol ) string {
89
- if listenerType == http {
90
- return "80"
126
+ s := Server {
127
+ HTTPPort : 80 ,
128
+ HTTPSPort : 443 ,
91
129
}
92
- return "443 ssl"
130
+
131
+ return getCustomPort (listenerType , s )
93
132
}
94
133
95
134
func getCustomPort (listenerType protocol , s Server ) string {
96
135
if listenerType == http {
97
136
return strconv .Itoa (s .HTTPPort )
98
137
}
99
- return strconv .Itoa (s .HTTPSPort ) + " ssl"
138
+ return strconv .Itoa (s .HTTPSPort )
100
139
}
101
140
102
- func buildListenDirective (ip string , port string , proxyProtocol bool , ipType ipType ) string {
141
+ func buildListenDirective (l listen ) string {
103
142
base := "listen"
104
143
var directive string
105
144
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 = "::"
117
148
}
149
+ l .ipAddress = fmt .Sprintf ("[%s]" , l .ipAddress )
118
150
}
119
151
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 )
134
154
} else {
135
- directive = base + " " + port
155
+ directive = fmt . Sprintf ( "%s %s" , base , l . port )
136
156
}
137
157
138
- if ssl . Enabled {
158
+ if l . tls {
139
159
directive += " ssl"
140
160
}
141
161
142
- if udp {
162
+ if l .proxyProtocol {
163
+ directive += " proxy_protocol"
164
+ }
165
+
166
+ if l .udp {
143
167
directive += " udp"
144
168
}
145
169
@@ -159,11 +183,25 @@ func makeTransportListener(s StreamServer) string {
159
183
var directives string
160
184
port := strconv .Itoa (s .Port )
161
185
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
+ })
163
194
164
195
if ! s .DisableIPV6 {
165
196
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
+ })
167
205
}
168
206
169
207
return directives
0 commit comments