init
This commit is contained in:
1 parent
7014f56cbb
commit
fe6f1f5e5a
25 files changed
+1918
No files matched your search
@@ -0,0 +1,90 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"github.com/valyala/fasthttp"
|
||||
"isp-gate-service/log_code"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type httpProxy struct {
|
||||
client *fasthttp.HostClient
|
||||
}
|
||||
|
||||
func NewProxy() *httpProxy {
|
||||
return &httpProxy{client: nil}
|
||||
}
|
||||
|
||||
func (p *httpProxy) Consumer(addressList []structure.AddressConfiguration) bool {
|
||||
addresses := make([]string, len(addressList))
|
||||
for key, addr := range addressList {
|
||||
addresses[key] = addr.GetAddress()
|
||||
}
|
||||
|
||||
p.client = &fasthttp.HostClient{
|
||||
Addr: strings.Join(addresses, `,`),
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) {
|
||||
req := &ctx.Request
|
||||
res := &ctx.Response
|
||||
|
||||
if clientIP, _, err := net.SplitHostPort(ctx.RemoteAddr().String()); err == nil {
|
||||
req.Header.Add("X-Forwarded-For", clientIP)
|
||||
}
|
||||
|
||||
resHeaders := make(map[string]string)
|
||||
res.Header.VisitAll(func(k, v []byte) {
|
||||
key := string(k)
|
||||
value := string(v)
|
||||
if val, ok := resHeaders[key]; ok {
|
||||
resHeaders[key] = val + "," + value
|
||||
}
|
||||
resHeaders[key] = value
|
||||
})
|
||||
|
||||
for _, h := range hopHeaders {
|
||||
// if h == "Te" && hv == "trailers" {
|
||||
// continue
|
||||
// }
|
||||
req.Header.Del(h)
|
||||
}
|
||||
|
||||
if err := p.client.Do(req, res); err != nil {
|
||||
log.Error(log_code.ErrorClientHttp, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, h := range hopHeaders {
|
||||
res.Header.Del(h)
|
||||
}
|
||||
for k, v := range resHeaders {
|
||||
res.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *httpProxy) Close() {
|
||||
p.client = nil
|
||||
p = nil
|
||||
}
|
||||
|
||||
// Hop-by-hop headers. These are removed when sent to the backend.
|
||||
// As of RFC 7230, hop-by-hop headers are required to appear in the
|
||||
// Connection header field. These are the headers defined by the
|
||||
// obsoleted RFC 2616 (section 13.5.1) and are used for backward
|
||||
// compatibility.
|
||||
var hopHeaders = []string{
|
||||
"Connection",
|
||||
"Proxy-Connection", // non-standard but still sent by libcurl and rejected by e.g. google
|
||||
"Keep-Alive",
|
||||
"Proxy-Authenticate",
|
||||
"Proxy-Authorization",
|
||||
"Te", // canonicalized version of "TE"
|
||||
"Trailer", // not Trailers per URL above; https://www.rfc-editor.org/errata_search.php?eid=4522
|
||||
"Transfer-Encoding",
|
||||
"Upgrade",
|
||||
}
|
||||
Reference in new issue
Block a user