TUN-3456: New protocol option auto to automatically select between http2 and h2mux

This commit is contained in:
cthuang
2020-10-14 11:28:07 +01:00
parent 6886e5f90a
commit b5cdf3b2c7
7 changed files with 176 additions and 19 deletions

View File

@@ -1,6 +1,8 @@
package connection
import (
"fmt"
"hash/fnv"
"io"
"net/http"
"strconv"
@@ -25,10 +27,9 @@ type Config struct {
}
type NamedTunnelConfig struct {
Auth pogs.TunnelAuth
ID uuid.UUID
Client pogs.ClientInfo
Protocol Protocol
Auth pogs.TunnelAuth
ID uuid.UUID
Client pogs.ClientInfo
}
type ClassicTunnelConfig struct {
@@ -49,17 +50,28 @@ const (
HTTP2
)
func ParseProtocol(s string) (Protocol, bool) {
func SelectProtocol(s string, accountTag string, http2Percentage uint32) (Protocol, bool) {
switch s {
case "h2mux":
return H2mux, true
case "http2":
return HTTP2, true
case "auto":
if tryHTTP2(accountTag, http2Percentage) {
return HTTP2, true
}
return H2mux, true
default:
return 0, false
}
}
func tryHTTP2(accountTag string, http2Percentage uint32) bool {
h := fnv.New32a()
h.Write([]byte(accountTag))
return h.Sum32()%100 < http2Percentage
}
func (p Protocol) ServerName() string {
switch p {
case H2mux:
@@ -71,6 +83,17 @@ func (p Protocol) ServerName() string {
}
}
func (p Protocol) String() string {
switch p {
case H2mux:
return "h2mux"
case HTTP2:
return "http2"
default:
return fmt.Sprintf("unknown protocol")
}
}
type OriginClient interface {
Proxy(w ResponseWriter, req *http.Request, isWebsocket bool) error
}