init http2+tls

This commit is contained in:
rui.zheng 2016-09-23 12:11:56 +08:00
parent 7b84fc6c26
commit 9ae36e42af
4 changed files with 39 additions and 3 deletions

13
conn.go
View File

@ -18,6 +18,7 @@ import (
"strings" "strings"
"sync" "sync"
//"sync/atomic" //"sync/atomic"
"golang.org/x/net/http2"
"time" "time"
) )
@ -50,6 +51,9 @@ func listenAndServe(arg Args) error {
case "wss": // websocket security connection case "wss": // websocket security connection
return NewWs(arg).listenAndServeTLS() return NewWs(arg).listenAndServeTLS()
case "tls": // tls connection case "tls": // tls connection
if arg.Protocol == "http2" || arg.Protocol == "h2" { // only support http2 over TLS
return listenAndServeHttp2(arg)
}
ln, err = tls.Listen("tcp", arg.Addr, ln, err = tls.Listen("tcp", arg.Addr,
&tls.Config{Certificates: []tls.Certificate{arg.Cert}}) &tls.Config{Certificates: []tls.Certificate{arg.Cert}})
case "tcp": // Local TCP port forwarding case "tcp": // Local TCP port forwarding
@ -83,6 +87,15 @@ func listenAndServe(arg Args) error {
} }
} }
func listenAndServeHttp2(arg Args) error {
srv := http.Server{
Addr: arg.Addr,
Handler: http.HandlerFunc(handlerHttp2Request),
}
http2.ConfigureServer(&srv, nil)
return srv.ListenAndServeTLS(certFile, keyFile)
}
func listenAndServeTcpForward(arg Args) error { func listenAndServeTcpForward(arg Args) error {
raddr, err := net.ResolveTCPAddr("tcp", arg.Remote) raddr, err := net.ResolveTCPAddr("tcp", arg.Remote)
if err != nil { if err != nil {

View File

@ -10,6 +10,8 @@ import (
) )
func handleHttpRequest(req *http.Request, conn net.Conn, arg Args) { func handleHttpRequest(req *http.Request, conn net.Conn, arg Args) {
glog.V(LINFO).Infof("[http] %s - %s", conn.RemoteAddr(), req.Host)
if glog.V(LDEBUG) { if glog.V(LDEBUG) {
dump, err := httputil.DumpRequest(req, false) dump, err := httputil.DumpRequest(req, false)
if err != nil { if err != nil {
@ -18,7 +20,6 @@ func handleHttpRequest(req *http.Request, conn net.Conn, arg Args) {
glog.Infoln(string(dump)) glog.Infoln(string(dump))
} }
} }
glog.V(LINFO).Infof("[http] %s - %s", conn.RemoteAddr(), req.Host)
var username, password string var username, password string
if arg.User != nil { if arg.User != nil {

21
http2.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"github.com/golang/glog"
"net/http"
"net/http/httputil"
)
func handlerHttp2Request(w http.ResponseWriter, r *http.Request) {
glog.V(LINFO).Infof("[http2] %s - %s", r.RemoteAddr, r.Host)
if glog.V(LDEBUG) {
dump, err := httputil.DumpRequest(r, false)
if err != nil {
glog.Infoln(err)
} else {
glog.Infoln(string(dump))
}
}
}

5
tls.go
View File

@ -6,7 +6,8 @@ import (
) )
const ( const (
certFile = "cert.pem"
keyFile = "key.pem"
// This is the default cert file for convenience, providing your own cert is recommended. // This is the default cert file for convenience, providing your own cert is recommended.
rawCert = `-----BEGIN CERTIFICATE----- rawCert = `-----BEGIN CERTIFICATE-----
MIIC5jCCAdCgAwIBAgIBADALBgkqhkiG9w0BAQUwEjEQMA4GA1UEChMHQWNtZSBD MIIC5jCCAdCgAwIBAgIBADALBgkqhkiG9w0BAQUwEjEQMA4GA1UEChMHQWNtZSBD
@ -58,7 +59,7 @@ nh/BAoGBAMY5z2f1pmMhrvtPDSlEVjgjELbaInxFaxPLR4Pdyzn83gtIIU14+R8X
func init() { func init() {
var err error var err error
if tlsCert, err = tls.LoadX509KeyPair("cert.pem", "key.pem"); err != nil { if tlsCert, err = tls.LoadX509KeyPair(certFile, keyFile); err != nil {
glog.V(LWARNING).Infoln(err) glog.V(LWARNING).Infoln(err)
tlsCert, err = tls.X509KeyPair([]byte(rawCert), []byte(rawKey)) tlsCert, err = tls.X509KeyPair([]byte(rawCert), []byte(rawKey))