frp/pkg/plugin/client/https2http.go

140 lines
3.2 KiB
Go
Raw Normal View History

2019-04-10 06:02:22 +02:00
// Copyright 2019 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plugin
import (
"crypto/tls"
"fmt"
"io"
2019-10-12 14:13:12 +02:00
"net"
2019-04-10 06:02:22 +02:00
"net/http"
"net/http/httputil"
"strings"
2019-04-10 06:02:22 +02:00
"github.com/fatedier/frp/pkg/transport"
2020-09-23 07:49:14 +02:00
frpNet "github.com/fatedier/frp/pkg/util/net"
2019-04-10 06:02:22 +02:00
)
const PluginHTTPS2HTTP = "https2http"
func init() {
Register(PluginHTTPS2HTTP, NewHTTPS2HTTPPlugin)
}
type HTTPS2HTTPPlugin struct {
crtPath string
keyPath string
hostHeaderRewrite string
localAddr string
headers map[string]string
2019-04-10 06:02:22 +02:00
l *Listener
s *http.Server
}
func NewHTTPS2HTTPPlugin(params map[string]string) (Plugin, error) {
crtPath := params["plugin_crt_path"]
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
}
}
2019-04-10 06:02:22 +02:00
if localAddr == "" {
return nil, fmt.Errorf("plugin_local_addr is required")
}
listener := NewProxyListener()
p := &HTTPS2HTTPPlugin{
crtPath: crtPath,
keyPath: keyPath,
localAddr: localAddr,
hostHeaderRewrite: hostHeaderRewrite,
headers: headers,
2019-04-10 06:02:22 +02:00
l: listener,
}
rp := &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = p.localAddr
if p.hostHeaderRewrite != "" {
req.Host = p.hostHeaderRewrite
}
for k, v := range p.headers {
req.Header.Set(k, v)
}
2019-04-10 06:02:22 +02:00
},
}
p.s = &http.Server{
Handler: rp,
}
var (
tlsConfig *tls.Config
err error
)
if crtPath != "" || keyPath != "" {
tlsConfig, err = p.genTLSConfig()
} else {
tlsConfig, err = transport.NewServerTLSConfig("", "", "")
tlsConfig.InsecureSkipVerify = true
}
2019-04-10 06:02:22 +02:00
if err != nil {
return nil, fmt.Errorf("gen TLS config error: %v", err)
}
ln := tls.NewListener(listener, tlsConfig)
2022-08-28 19:02:53 +02:00
go func() {
_ = p.s.Serve(ln)
}()
2019-04-10 06:02:22 +02:00
return p, nil
}
func (p *HTTPS2HTTPPlugin) genTLSConfig() (*tls.Config, error) {
cert, err := tls.LoadX509KeyPair(p.crtPath, p.keyPath)
if err != nil {
return nil, err
}
config := &tls.Config{Certificates: []tls.Certificate{cert}}
return config, nil
}
2019-10-12 14:13:12 +02:00
func (p *HTTPS2HTTPPlugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte) {
2019-04-10 06:02:22 +02:00
wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
2022-08-28 19:02:53 +02:00
_ = p.l.PutConn(wrapConn)
2019-04-10 06:02:22 +02:00
}
func (p *HTTPS2HTTPPlugin) Name() string {
return PluginHTTPS2HTTP
}
func (p *HTTPS2HTTPPlugin) Close() error {
2019-12-08 14:01:58 +01:00
if err := p.s.Close(); err != nil {
2019-11-22 08:18:20 +01:00
return err
}
2019-04-10 06:02:22 +02:00
return nil
}