add support for add http header when using https2http plugin

This commit is contained in:
zhouwenfeng 2019-08-13 21:14:06 +08:00
parent 50796643fb
commit 934ac2b836
2 changed files with 16 additions and 0 deletions

View File

@ -198,6 +198,7 @@ plugin_local_addr = 127.0.0.1:80
plugin_crt_path = ./server.crt plugin_crt_path = ./server.crt
plugin_key_path = ./server.key plugin_key_path = ./server.key
plugin_host_header_rewrite = 127.0.0.1 plugin_host_header_rewrite = 127.0.0.1
plugin_header_X-From-Where = frp
[secret_tcp] [secret_tcp]
# If the type is secret tcp, remote_port is useless # If the type is secret tcp, remote_port is useless

View File

@ -20,6 +20,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"strings"
frpNet "github.com/fatedier/frp/utils/net" frpNet "github.com/fatedier/frp/utils/net"
) )
@ -35,6 +36,7 @@ type HTTPS2HTTPPlugin struct {
keyPath string keyPath string
hostHeaderRewrite string hostHeaderRewrite string
localAddr string localAddr string
headers map[string]string
l *Listener l *Listener
s *http.Server s *http.Server
@ -45,6 +47,15 @@ func NewHTTPS2HTTPPlugin(params map[string]string) (Plugin, error) {
keyPath := params["plugin_key_path"] keyPath := params["plugin_key_path"]
localAddr := params["plugin_local_addr"] localAddr := params["plugin_local_addr"]
hostHeaderRewrite := params["plugin_host_header_rewrite"] hostHeaderRewrite := params["plugin_host_header_rewrite"]
headers := make(map[string]string)
for k, v := range params {
if !strings.HasPrefix(k, "plugin_header_") {
continue
}
if k = strings.TrimPrefix(k, "plugin_header_"); k != "" {
headers[k] = v
}
}
if crtPath == "" { if crtPath == "" {
return nil, fmt.Errorf("plugin_crt_path is required") return nil, fmt.Errorf("plugin_crt_path is required")
@ -63,6 +74,7 @@ func NewHTTPS2HTTPPlugin(params map[string]string) (Plugin, error) {
keyPath: keyPath, keyPath: keyPath,
localAddr: localAddr, localAddr: localAddr,
hostHeaderRewrite: hostHeaderRewrite, hostHeaderRewrite: hostHeaderRewrite,
headers: headers,
l: listener, l: listener,
} }
@ -73,6 +85,9 @@ func NewHTTPS2HTTPPlugin(params map[string]string) (Plugin, error) {
if p.hostHeaderRewrite != "" { if p.hostHeaderRewrite != "" {
req.Host = p.hostHeaderRewrite req.Host = p.hostHeaderRewrite
} }
for k, v := range p.headers {
req.Header.Add(k, v)
}
}, },
} }