| 1 | // Copyright 2019 fatedier, fatedier@gmail.com |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //go:build !frps |
| 16 | |
| 17 | package client |
| 18 | |
| 19 | import ( |
| 20 | "net/http/httputil" |
| 21 | |
| 22 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | Register(v1.PluginHTTPS2HTTP, NewHTTPS2HTTPPlugin) |
| 27 | } |
| 28 | |
| 29 | type HTTPS2HTTPPlugin struct { |
| 30 | opts *v1.HTTPS2HTTPPluginOptions |
| 31 | |
| 32 | *httpBridgePlugin |
| 33 | } |
| 34 | |
| 35 | func NewHTTPS2HTTPPlugin(_ PluginContext, options v1.ClientPluginOptions) (Plugin, error) { |
| 36 | opts := options.(*v1.HTTPS2HTTPPluginOptions) |
| 37 | |
| 38 | p := &HTTPS2HTTPPlugin{ |
| 39 | opts: opts, |
| 40 | } |
| 41 | |
| 42 | rp := newHTTPBridgeReverseProxy( |
| 43 | func(r *httputil.ProxyRequest) { |
| 44 | r.Out.Header["X-Forwarded-For"] = r.In.Header["X-Forwarded-For"] |
| 45 | r.SetXForwarded() |
| 46 | req := r.Out |
| 47 | rewriteHTTPPluginRequest(req, "http", p.opts.LocalAddr, p.opts.HostHeaderRewrite, p.opts.RequestHeaders) |
| 48 | }, |
| 49 | nil, |
| 50 | ) |
| 51 | |
| 52 | server, err := newHTTPSBridgePluginServer(rp, p.opts.CrtPath, p.opts.KeyPath, opts.EnableHTTP2, true) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | p.httpBridgePlugin = server |
| 57 | |
| 58 | return p, nil |
| 59 | } |
| 60 | |
| 61 | func (p *HTTPS2HTTPPlugin) Name() string { |
| 62 | return v1.PluginHTTPS2HTTP |
| 63 | } |