Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1344 from akatashev/ssl_support_for_reverse_proxy
Browse files Browse the repository at this point in the history
Use RaftHttpTransport for reverse-proxy
  • Loading branch information
shlomi-noach authored Apr 29, 2021
2 parents f818d8a + b0aa7b8 commit d536bc6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions go/http/raft_reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@ func raftReverseProxy(w http.ResponseWriter, r *http.Request, c martini.Context)
r.SetBasicAuth(config.Config.HTTPAuthUser, config.Config.HTTPAuthPassword)
}
proxy := httputil.NewSingleHostReverseProxy(url)
proxy.Transport, err = orcraft.GetRaftHttpTransport()
if err != nil {
log.Errore(err)
return
}
proxy.ServeHTTP(w, r)
}
26 changes: 21 additions & 5 deletions go/raft/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ import (
"github.com/openark/golib/log"
)

var httpClient *http.Client
var (
httpClient *http.Client
httpTransport *http.Transport
)

func setupHttpClient() error {
func GetRaftHttpTransport() (*http.Transport, error) {
// Checks whether there is a cached httpTransport to return:
if httpTransport != nil {
return httpTransport, nil
}
httpTimeout := time.Duration(config.ActiveNodeExpireSeconds) * time.Second
dialTimeout := func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, httpTimeout)
Expand All @@ -45,7 +52,7 @@ func setupHttpClient() error {
if config.Config.UseSSL {
caPool, err := ssl.ReadCAFile(config.Config.SSLCAFile)
if err != nil {
return err
return nil, err
}
tlsConfig.RootCAs = caPool

Expand All @@ -55,16 +62,25 @@ func setupHttpClient() error {
sslPEMPassword = ssl.GetPEMPassword(config.Config.SSLPrivateKeyFile)
}
if err := ssl.AppendKeyPairWithPassword(tlsConfig, config.Config.SSLCertFile, config.Config.SSLPrivateKeyFile, sslPEMPassword); err != nil {
return err
return nil, err
}
}
}

httpTransport := &http.Transport{
transport := &http.Transport{
TLSClientConfig: tlsConfig,
Dial: dialTimeout,
ResponseHeaderTimeout: httpTimeout,
}
return transport, nil
}

func setupHttpClient() error {
transport, err := GetRaftHttpTransport()
if err != nil {
return err
}
httpTransport = transport
httpClient = &http.Client{Transport: httpTransport}

return nil
Expand Down

0 comments on commit d536bc6

Please sign in to comment.