Browse Source

Merge pull request #1382 from Hurricanezwf/feature/add-httpstohttp-header

add support for customized http header when using https2http plugin
fatedier 5 years ago
parent
commit
ca8a5b753c
4 changed files with 18 additions and 0 deletions
  1. 1 0
      README.md
  2. 1 0
      README_zh.md
  3. 1 0
      conf/frpc_full.ini
  4. 15 0
      models/plugin/https2http.go

+ 1 - 0
README.md

@@ -265,6 +265,7 @@ Configure frps same as above.
   plugin_crt_path = ./server.crt
   plugin_key_path = ./server.key
   plugin_host_header_rewrite = 127.0.0.1
+  plugin_header_X-From-Where = frp
   ```
 
 2. Visit `https://test.yourdomain.com`.

+ 1 - 0
README_zh.md

@@ -270,6 +270,7 @@ frps 的部署步骤同上。
   plugin_crt_path = ./server.crt
   plugin_key_path = ./server.key
   plugin_host_header_rewrite = 127.0.0.1
+  plugin_header_X-From-Where = frp
   ```
 
 2. 通过浏览器访问 `https://test.yourdomain.com` 即可。

+ 1 - 0
conf/frpc_full.ini

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

+ 15 - 0
models/plugin/https2http.go

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