From f2c656843ec33167cc1bb395663108e1ac79dc83 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sat, 7 May 2016 10:36:36 +0200 Subject: [PATCH] allow tls connection in http proxy --- proxy/http/config.go | 19 +++++++++++++++++++ proxy/http/http.go | 7 ++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/proxy/http/config.go b/proxy/http/config.go index 839d3d67e..0f811ef92 100644 --- a/proxy/http/config.go +++ b/proxy/http/config.go @@ -16,6 +16,25 @@ type TlsConfig struct { Certs []*CertificateConfig } +func (this *TlsConfig) GetConfig() *tls.Config { + if !this.Enabled { + return nil + } + + config := &tls.Config{ + InsecureSkipVerify: false, + } + + config.Certificates = make([]tls.Certificate, len(this.Certs)) + for index, cert := range this.Certs { + config.Certificates[index] = cert.Certificate + } + + config.BuildNameToCertificate() + + return config +} + type Config struct { OwnHosts []v2net.Address TlsConfig *TlsConfig diff --git a/proxy/http/http.go b/proxy/http/http.go index 8e24d014a..4805d2fd4 100644 --- a/proxy/http/http.go +++ b/proxy/http/http.go @@ -2,6 +2,7 @@ package http import ( "bufio" + "crypto/tls" "io" "net" "net/http" @@ -60,7 +61,11 @@ func (this *HttpProxyServer) Listen(port v2net.Port) error { } this.listeningPort = port - tcpListener, err := hub.ListenTCP(port, this.handleConnection, nil) + var tlsConfig *tls.Config = nil + if this.config.TlsConfig != nil { + tlsConfig = this.config.TlsConfig.GetConfig() + } + tcpListener, err := hub.ListenTCP(port, this.handleConnection, tlsConfig) if err != nil { log.Error("Http: Failed listen on port ", port, ": ", err) return err