Skip to content

Instantly share code, notes, and snippets.

@yaroot
Created May 2, 2018 06:26
Show Gist options
  • Save yaroot/cb1dc0b2b245213c2674dfe226599c8e to your computer and use it in GitHub Desktop.
Save yaroot/cb1dc0b2b245213c2674dfe226599c8e to your computer and use it in GitHub Desktop.
package main
import (
"log"
"os"
"net/http"
"net"
"crypto/tls"
"golang.org/x/net/http2"
"fmt"
)
func newLocalListener(addr string) net.Listener {
l, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
return l
}
type fakeTLSConn struct {
net.Conn
}
func (c *fakeTLSConn) ConnectionState() tls.ConnectionState {
return tls.ConnectionState{
Version: tls.VersionTLS12,
CipherSuite: 0xC02F, // cipher_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
}
}
func startH2cServer(addr string, handler http.Handler) net.Listener {
h2Server := &http2.Server{MaxConcurrentStreams: 5000}
l := newLocalListener(addr)
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Println("accepting error", err)
continue
}
h2Server.ServeConn(&fakeTLSConn{conn}, &http2.ServeConnOpts{Handler: handler})
}
}
func main() {
if len(os.Args) != 2 {
log.Printf("Usage %s :8080 \n", os.Args[0])
return
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "url: %v, http: %v", r.URL.Path, r.TLS == nil)
})
startH2cServer(os.Args[1], mux)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment