Files
cloudpods/pkg/proxy/http.go
Yousong Zhou c2bca8ab39 Feature/yousong text (#7487)
* notify: log with context info

* region: guests: log with context info

* region: hosts: log with context info

* region: misc: remove redundant log

* region: skus: use NewInternalServerError instead of NewGeneralError

* appsrv: dispatcher: preserve text id

* appsrv: dispatcher: remove commented-out code

* cloudcommon: db_dispatcher: note errors that won't be translated

* cloudcommon: db: rbac: preserve text id

* cloudcommon: caller: preserve text id

* treewide: fix fmt string

* region: guest_actions: preserve text id

* region: guest_actions: fix fmt string

* region: usages: fix fmt string

* mcclient: hosts: remove redundant return value

* cloudcommon: fetch: preserve text id

* region: guest_template: preserve text id

* cloudcommon: db: db_dispatcher: preserve text id

* region: guest_actions: preserve text id

* httperrors: add funcs for wrap lang tag into context

* vendor: golang.org/x/text

* httperrors: error with context

* httperrors: HTTPError

* treewide: fix typo

* treewide: use en in source code

* locales: initial version
2020-08-25 15:26:31 +08:00

81 lines
2.3 KiB
Go

// Copyright 2019 Yunion
//
// 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 proxy
import (
"context"
"crypto/tls"
"net/http"
"net/http/httputil"
"net/url"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/httperrors"
)
type EndpointGenerator func(context.Context, *http.Request) (string, error)
type RequestManipulator func(ctx context.Context, r *http.Request) (*http.Request, error)
type SEndpointFactory struct {
generator EndpointGenerator
serviceName string
}
func NewEndpointFactory(f EndpointGenerator, serviceName string) *SEndpointFactory {
return &SEndpointFactory{
generator: f,
serviceName: serviceName,
}
}
type SReverseProxy struct {
*SEndpointFactory
manipulator RequestManipulator
}
func NewHTTPReverseProxy(ef *SEndpointFactory, m RequestManipulator) *SReverseProxy {
return &SReverseProxy{
SEndpointFactory: ef,
manipulator: m,
}
}
func (p *SReverseProxy) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) {
endpoint, err := p.generator(ctx, r)
if err != nil {
httperrors.InternalServerError(httperrors.WithRequestLang(ctx, r), w, "%v", err)
return
}
remoteUrl, err := url.Parse(endpoint)
if err != nil {
httperrors.InternalServerError(httperrors.WithRequestLang(ctx, r), w, "failed parsing url %q: %v", endpoint, err)
return
}
log.Debugf("Forwarding to servie: %q, url: %q", p.serviceName, remoteUrl.String())
proxy := httputil.NewSingleHostReverseProxy(remoteUrl)
proxy.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
}
r, err = p.manipulator(ctx, r)
if err != nil {
httperrors.InternalServerError(httperrors.WithRequestLang(ctx, r), w, "%v", err)
return
}
proxy.ServeHTTP(w, r)
}