77 "time"
88
99 "github.com/rs/zerolog"
10-
11- "github.com/cloudflare/cloudflared/edgediscovery"
1210)
1311
1412const (
@@ -26,7 +24,7 @@ const (
2624
2725var (
2826 // ProtocolList represents a list of supported protocols for communication with the edge.
29- ProtocolList = []Protocol {H2mux , HTTP2 , HTTP2Warp , QUIC , QUICWarp }
27+ ProtocolList = []Protocol {H2mux , HTTP2 , QUIC }
3028)
3129
3230type Protocol int64
@@ -38,12 +36,6 @@ const (
3836 HTTP2
3937 // QUIC is used only with named tunnels.
4038 QUIC
41- // HTTP2Warp is used only with named tunnels. It's useful for warp-routing where we don't want to fallback to
42- // H2mux on HTTP2 failure to connect.
43- HTTP2Warp
44- //QUICWarp is used only with named tunnels. It's useful for warp-routing where we want to fallback to HTTP2 but
45- // dont' want HTTP2 to fallback to H2mux
46- QUICWarp
4739)
4840
4941// Fallback returns the fallback protocol and whether the protocol has a fallback
@@ -53,12 +45,8 @@ func (p Protocol) fallback() (Protocol, bool) {
5345 return 0 , false
5446 case HTTP2 :
5547 return H2mux , true
56- case HTTP2Warp :
57- return 0 , false
5848 case QUIC :
5949 return HTTP2 , true
60- case QUICWarp :
61- return HTTP2Warp , true
6250 default :
6351 return 0 , false
6452 }
@@ -68,9 +56,9 @@ func (p Protocol) String() string {
6856 switch p {
6957 case H2mux :
7058 return "h2mux"
71- case HTTP2 , HTTP2Warp :
59+ case HTTP2 :
7260 return "http2"
73- case QUIC , QUICWarp :
61+ case QUIC :
7462 return "quic"
7563 default :
7664 return fmt .Sprintf ("unknown protocol" )
@@ -83,11 +71,11 @@ func (p Protocol) TLSSettings() *TLSSettings {
8371 return & TLSSettings {
8472 ServerName : edgeH2muxTLSServerName ,
8573 }
86- case HTTP2 , HTTP2Warp :
74+ case HTTP2 :
8775 return & TLSSettings {
8876 ServerName : edgeH2TLSServerName ,
8977 }
90- case QUIC , QUICWarp :
78+ case QUIC :
9179 return & TLSSettings {
9280 ServerName : edgeQUICServerName ,
9381 NextProtos : []string {"argotunnel" },
@@ -120,36 +108,29 @@ func (s *staticProtocolSelector) Fallback() (Protocol, bool) {
120108}
121109
122110type autoProtocolSelector struct {
123- lock sync.RWMutex
124-
125- current Protocol
126-
127- // protocolPool is desired protocols in the order of priority they should be picked in.
128- protocolPool []Protocol
129-
130- switchThreshold int32
131- fetchFunc PercentageFetcher
132- refreshAfter time.Time
133- ttl time.Duration
134- log * zerolog.Logger
111+ lock sync.RWMutex
112+ current Protocol
113+ switchThrehold int32
114+ fetchFunc PercentageFetcher
115+ refreshAfter time.Time
116+ ttl time.Duration
117+ log * zerolog.Logger
135118}
136119
137120func newAutoProtocolSelector (
138121 current Protocol ,
139- protocolPool []Protocol ,
140- switchThreshold int32 ,
122+ switchThrehold int32 ,
141123 fetchFunc PercentageFetcher ,
142124 ttl time.Duration ,
143125 log * zerolog.Logger ,
144126) * autoProtocolSelector {
145127 return & autoProtocolSelector {
146- current : current ,
147- protocolPool : protocolPool ,
148- switchThreshold : switchThreshold ,
149- fetchFunc : fetchFunc ,
150- refreshAfter : time .Now ().Add (ttl ),
151- ttl : ttl ,
152- log : log ,
128+ current : current ,
129+ switchThrehold : switchThrehold ,
130+ fetchFunc : fetchFunc ,
131+ refreshAfter : time .Now ().Add (ttl ),
132+ ttl : ttl ,
133+ log : log ,
153134 }
154135}
155136
@@ -160,39 +141,28 @@ func (s *autoProtocolSelector) Current() Protocol {
160141 return s .current
161142 }
162143
163- protocol , err := getProtocol ( s . protocolPool , s . fetchFunc , s . switchThreshold )
144+ percentage , err := s . fetchFunc ( )
164145 if err != nil {
165146 s .log .Err (err ).Msg ("Failed to refresh protocol" )
166147 return s .current
167148 }
168- s .current = protocol
169149
150+ if s .switchThrehold < percentage {
151+ s .current = HTTP2
152+ } else {
153+ s .current = H2mux
154+ }
170155 s .refreshAfter = time .Now ().Add (s .ttl )
171156 return s .current
172157}
173158
174- func getProtocol (protocolPool []Protocol , fetchFunc PercentageFetcher , switchThreshold int32 ) (Protocol , error ) {
175- protocolPercentages , err := fetchFunc ()
176- if err != nil {
177- return 0 , err
178- }
179- for _ , protocol := range protocolPool {
180- protocolPercentage := protocolPercentages .GetPercentage (protocol .String ())
181- if protocolPercentage > switchThreshold {
182- return protocol , nil
183- }
184- }
185-
186- return protocolPool [len (protocolPool )- 1 ], nil
187- }
188-
189159func (s * autoProtocolSelector ) Fallback () (Protocol , bool ) {
190160 s .lock .RLock ()
191161 defer s .lock .RUnlock ()
192162 return s .current .fallback ()
193163}
194164
195- type PercentageFetcher func () (edgediscovery. ProtocolPercents , error )
165+ type PercentageFetcher func () (int32 , error )
196166
197167func NewProtocolSelector (
198168 protocolFlag string ,
@@ -209,76 +179,54 @@ func NewProtocolSelector(
209179 }, nil
210180 }
211181
212- threshold := switchThreshold (namedTunnel .Credentials .AccountTag )
213- fetchedProtocol , err := getProtocol ([]Protocol {QUIC , HTTP2 }, fetchFunc , threshold )
214- if err != nil {
215- log .Err (err ).Msg ("Unable to lookup protocol. Defaulting to `http2`. If this fails, you can set `--protocol h2mux` in your cloudflared command." )
216- return & staticProtocolSelector {
217- current : HTTP2 ,
218- }, nil
219- }
182+ // warp routing cannot be served over h2mux connections
220183 if warpRoutingEnabled {
221- if protocolFlag == H2mux .String () || fetchedProtocol == H2mux {
184+ if protocolFlag == H2mux .String () {
222185 log .Warn ().Msg ("Warp routing is not supported in h2mux protocol. Upgrading to http2 to allow it." )
223- protocolFlag = HTTP2 .String ()
224- fetchedProtocol = HTTP2Warp
225186 }
226- return selectWarpRoutingProtocols (protocolFlag , fetchFunc , ttl , log , threshold , fetchedProtocol )
227- }
228187
229- return selectNamedTunnelProtocols (protocolFlag , fetchFunc , ttl , log , threshold , fetchedProtocol )
230- }
188+ if protocolFlag == QUIC .String () {
189+ return & staticProtocolSelector {
190+ current : QUIC ,
191+ }, nil
192+ }
193+ return & staticProtocolSelector {
194+ current : HTTP2 ,
195+ }, nil
196+ }
231197
232- func selectNamedTunnelProtocols (
233- protocolFlag string ,
234- fetchFunc PercentageFetcher ,
235- ttl time.Duration ,
236- log * zerolog.Logger ,
237- threshold int32 ,
238- protocol Protocol ,
239- ) (ProtocolSelector , error ) {
240198 if protocolFlag == H2mux .String () {
241199 return & staticProtocolSelector {
242200 current : H2mux ,
243201 }, nil
244202 }
245203
246204 if protocolFlag == QUIC .String () {
247- return newAutoProtocolSelector (QUIC , []Protocol {QUIC , HTTP2 , H2mux }, explicitHTTP2FallbackThreshold , fetchFunc , ttl , log ), nil
248- }
249-
250- if protocolFlag == HTTP2 .String () {
251- return newAutoProtocolSelector (HTTP2 , []Protocol {HTTP2 , H2mux }, explicitHTTP2FallbackThreshold , fetchFunc , ttl , log ), nil
252- }
253-
254- if protocolFlag != autoSelectFlag {
255- return nil , fmt .Errorf ("Unknown protocol %s, %s" , protocolFlag , AvailableProtocolFlagMessage )
205+ return newAutoProtocolSelector (QUIC , explicitHTTP2FallbackThreshold , fetchFunc , ttl , log ), nil
256206 }
257207
258- return newAutoProtocolSelector (protocol , []Protocol {QUIC , HTTP2 , H2mux }, threshold , fetchFunc , ttl , log ), nil
259- }
260-
261- func selectWarpRoutingProtocols (
262- protocolFlag string ,
263- fetchFunc PercentageFetcher ,
264- ttl time.Duration ,
265- log * zerolog.Logger ,
266- threshold int32 ,
267- protocol Protocol ,
268- ) (ProtocolSelector , error ) {
269- if protocolFlag == QUIC .String () {
270- return newAutoProtocolSelector (QUICWarp , []Protocol {QUICWarp , HTTP2Warp }, explicitHTTP2FallbackThreshold , fetchFunc , ttl , log ), nil
208+ http2Percentage , err := fetchFunc ()
209+ if err != nil {
210+ log .Err (err ).Msg ("Unable to lookup protocol. Defaulting to `http2`. If this fails, you can set `--protocol h2mux` in your cloudflared command." )
211+ return & staticProtocolSelector {
212+ current : HTTP2 ,
213+ }, nil
271214 }
272-
273215 if protocolFlag == HTTP2 .String () {
274- return newAutoProtocolSelector (HTTP2Warp , []Protocol {HTTP2Warp }, explicitHTTP2FallbackThreshold , fetchFunc , ttl , log ), nil
216+ if http2Percentage < 0 {
217+ return newAutoProtocolSelector (H2mux , explicitHTTP2FallbackThreshold , fetchFunc , ttl , log ), nil
218+ }
219+ return newAutoProtocolSelector (HTTP2 , explicitHTTP2FallbackThreshold , fetchFunc , ttl , log ), nil
275220 }
276221
277222 if protocolFlag != autoSelectFlag {
278223 return nil , fmt .Errorf ("Unknown protocol %s, %s" , protocolFlag , AvailableProtocolFlagMessage )
279224 }
280-
281- return newAutoProtocolSelector (protocol , []Protocol {QUICWarp , HTTP2Warp }, threshold , fetchFunc , ttl , log ), nil
225+ threshold := switchThreshold (namedTunnel .Credentials .AccountTag )
226+ if threshold < http2Percentage {
227+ return newAutoProtocolSelector (HTTP2 , threshold , fetchFunc , ttl , log ), nil
228+ }
229+ return newAutoProtocolSelector (H2mux , threshold , fetchFunc , ttl , log ), nil
282230}
283231
284232func switchThreshold (accountTag string ) int32 {
0 commit comments