mirror of
https://github.com/eolinker/apinto
synced 2025-10-07 09:41:01 +08:00
转发换库
This commit is contained in:
@@ -37,7 +37,8 @@ func main() {
|
|||||||
client := &fasthttp.Client{ReadTimeout: 30 * time.Second, MaxConnsPerHost: 4000}
|
client := &fasthttp.Client{ReadTimeout: 30 * time.Second, MaxConnsPerHost: 4000}
|
||||||
err := http.ListenAndServe(":8082", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
err := http.ListenAndServe(":8082", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
req := fasthttp.AcquireRequest()
|
req := fasthttp.AcquireRequest()
|
||||||
req.SetRequestURI("http://172.18.189.60/")
|
req.Header.SetMethod("POST")
|
||||||
|
req.SetRequestURI("http://47.95.203.198:8080/Web/Test/params/print")
|
||||||
var resp fasthttp.Response
|
var resp fasthttp.Response
|
||||||
err := client.Do(req, &resp)
|
err := client.Do(req, &resp)
|
||||||
//status, resp, err := fasthttp.Get(nil, "http://172.18.189.60/")
|
//status, resp, err := fasthttp.Get(nil, "http://172.18.189.60/")
|
||||||
@@ -50,6 +51,7 @@ func main() {
|
|||||||
fmt.Println("请求没有成功:", resp.StatusCode())
|
fmt.Println("请求没有成功:", resp.StatusCode())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Println(string(resp.Header.Header()))
|
||||||
w.WriteHeader(resp.StatusCode())
|
w.WriteHeader(resp.StatusCode())
|
||||||
w.Write(resp.Body())
|
w.Write(resp.Body())
|
||||||
|
|
||||||
|
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
goku_plugin "github.com/eolinker/goku-standard-plugin"
|
goku_plugin "github.com/eolinker/goku-standard-plugin"
|
||||||
|
|
||||||
|
|
||||||
access_field "github.com/eolinker/goku-eosc/node/common/access-field"
|
access_field "github.com/eolinker/goku-eosc/node/common/access-field"
|
||||||
"github.com/eolinker/goku-eosc/utils"
|
"github.com/eolinker/goku-eosc/utils"
|
||||||
)
|
)
|
||||||
|
@@ -3,6 +3,10 @@ package http_context
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
//ResponseReader 响应结构体
|
//ResponseReader 响应结构体
|
||||||
@@ -35,12 +39,27 @@ func newResponseReader(response *http.Response) *ResponseReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//NewResponseReader 新增ResponseReader
|
//NewResponseReader 新增ResponseReader
|
||||||
func NewResponseReader(header http.Header, statusCode int, status string, body []byte) *ResponseReader {
|
func NewResponseReader(header *fasthttp.ResponseHeader, statusCode int, body []byte) *ResponseReader {
|
||||||
r := new(ResponseReader)
|
r := new(ResponseReader)
|
||||||
r.Header = NewHeader(header)
|
tmpHeader := http.Header{}
|
||||||
r.CookiesHandler = newCookieHandle(header)
|
|
||||||
|
hs := strings.Split(string(header.Header()), "\r\n")
|
||||||
|
for i, h := range hs {
|
||||||
|
if i == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
values := strings.Split(h, ":")
|
||||||
|
vLen := len(values)
|
||||||
|
if vLen < 2 {
|
||||||
|
tmpHeader.Set(values[0], "")
|
||||||
|
} else {
|
||||||
|
tmpHeader.Set(values[0], values[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.Header = &Header{header: tmpHeader}
|
||||||
|
//r.CookiesHandler = newCookieHandle(header)
|
||||||
r.StatusHandler = NewStatusHandler()
|
r.StatusHandler = NewStatusHandler()
|
||||||
r.SetStatus(statusCode, status)
|
r.SetStatus(statusCode, strconv.Itoa(statusCode))
|
||||||
// if response.ContentLength > 0 {
|
// if response.ContentLength > 0 {
|
||||||
// body, _ := ioutil.ReadAll(response.body)
|
// body, _ := ioutil.ReadAll(response.body)
|
||||||
// r.BodyHandler = NewBodyHandler(body)
|
// r.BodyHandler = NewBodyHandler(body)
|
||||||
|
@@ -5,9 +5,11 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
http_context "github.com/eolinker/goku-eosc/node/http-context"
|
http_context "github.com/eolinker/goku-eosc/node/http-context"
|
||||||
|
|
||||||
@@ -19,27 +21,31 @@ import (
|
|||||||
var Version = "2.0"
|
var Version = "2.0"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
transport = &http.Transport{TLSClientConfig: &tls.Config{
|
//transport = &http.Transport{TLSClientConfig: &tls.Config{
|
||||||
InsecureSkipVerify: false,
|
// InsecureSkipVerify: false,
|
||||||
},
|
//},
|
||||||
DialContext: (&net.Dialer{
|
// DialContext: (&net.Dialer{
|
||||||
Timeout: 30 * time.Second, // 连接超时时间
|
// Timeout: 30 * time.Second, // 连接超时时间
|
||||||
KeepAlive: 60 * time.Second, // 保持长连接的时间
|
// KeepAlive: 60 * time.Second, // 保持长连接的时间
|
||||||
}).DialContext, // 设置连接的参数
|
// }).DialContext, // 设置连接的参数
|
||||||
MaxIdleConns: 500, // 最大空闲连接
|
// MaxIdleConns: 500, // 最大空闲连接
|
||||||
IdleConnTimeout: 60 * time.Second, // 空闲连接的超时时间
|
// IdleConnTimeout: 60 * time.Second, // 空闲连接的超时时间
|
||||||
ExpectContinueTimeout: 30 * time.Second, // 等待服务第一个响应的超时时间
|
// ExpectContinueTimeout: 30 * time.Second, // 等待服务第一个响应的超时时间
|
||||||
MaxIdleConnsPerHost: 100, // 每个host保持的空闲连接数
|
// MaxIdleConnsPerHost: 100, // 每个host保持的空闲连接数
|
||||||
}
|
//}
|
||||||
httpClient = &http.Client{
|
|
||||||
Transport: transport,
|
httpClient = &fasthttp.Client{
|
||||||
|
TLSConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: false,
|
||||||
|
},
|
||||||
|
MaxConnsPerHost: 4000,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
//SetCert 设置证书配置
|
//SetCert 设置证书配置
|
||||||
func SetCert(skip int, clientCerts []tls.Certificate) {
|
func SetCert(skip int, clientCerts []tls.Certificate) {
|
||||||
tlsConfig := &tls.Config{InsecureSkipVerify: skip == 1, Certificates: clientCerts}
|
tlsConfig := &tls.Config{InsecureSkipVerify: skip == 1, Certificates: clientCerts}
|
||||||
transport.TLSClientConfig = tlsConfig
|
httpClient.TLSConfig = tlsConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
//Request http-proxy 请求结构体
|
//Request http-proxy 请求结构体
|
||||||
@@ -102,7 +108,6 @@ func newRequest(method string, URL *url.URL) (*Request, error) {
|
|||||||
urlPath = URL.Scheme + "://" + URL.Host + URL.Path
|
urlPath = URL.Scheme + "://" + URL.Host + URL.Path
|
||||||
|
|
||||||
r := &Request{
|
r := &Request{
|
||||||
client: httpClient,
|
|
||||||
method: method,
|
method: method,
|
||||||
url: urlPath,
|
url: urlPath,
|
||||||
headers: make(map[string][]string),
|
headers: make(map[string][]string),
|
||||||
@@ -144,15 +149,20 @@ func (r *Request) SetTimeout(timeout time.Duration) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Send 发送请求
|
//Send 发送请求
|
||||||
func (r *Request) Send(ctx *http_context.Context) (*http.Response, error) {
|
func (r *Request) Send(ctx *http_context.Context) (*fasthttp.Response, error) {
|
||||||
req := r.HTTPRequest()
|
req := fasthttp.AcquireRequest()
|
||||||
req.Header.Set("Accept-Encoding", "gzip")
|
req.SetRequestURI(r.url)
|
||||||
|
req.Header.SetMethod(r.method)
|
||||||
req.Header = parseHeaders(r.headers)
|
req.Header = parseHeaders(r.headers)
|
||||||
|
req.Header.Set("Accept-Encoding", "gzip")
|
||||||
|
|
||||||
r.client.Timeout = r.timeout
|
r.client.Timeout = r.timeout
|
||||||
httpResponse, err := r.client.Do(req)
|
resp := fasthttp.AcquireResponse()
|
||||||
|
defer fasthttp.ReleaseResponse(resp) // 用完需要释放资源
|
||||||
|
|
||||||
return httpResponse, err
|
err := httpClient.Do(req, resp)
|
||||||
|
|
||||||
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//QueryParams 获取query参数
|
//QueryParams 获取query参数
|
||||||
@@ -183,19 +193,26 @@ func (r *Request) SetRawBody(body []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 解析请求头
|
// 解析请求头
|
||||||
func parseHeaders(headers map[string][]string) http.Header {
|
func parseHeaders(headers map[string][]string) fasthttp.RequestHeader {
|
||||||
h := http.Header{}
|
h := fasthttp.RequestHeader{}
|
||||||
|
hasAccept := false
|
||||||
|
hasAgent := false
|
||||||
for key, values := range headers {
|
for key, values := range headers {
|
||||||
|
key = strings.ToLower(key)
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
|
if key == "accept" {
|
||||||
|
hasAccept = true
|
||||||
|
}
|
||||||
|
if key == "user-agent" {
|
||||||
|
hasAgent = true
|
||||||
|
}
|
||||||
h.Add(key, value)
|
h.Add(key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, hasAccept := h["Accept"]
|
|
||||||
if !hasAccept {
|
if !hasAccept {
|
||||||
h.Add("Accept", "*/*")
|
h.Add("Accept", "*/*")
|
||||||
}
|
}
|
||||||
_, hasAgent := h["User-Agent"]
|
|
||||||
if !hasAgent {
|
if !hasAgent {
|
||||||
h.Add("User-Agent", "goku-requests/"+Version)
|
h.Add("User-Agent", "goku-requests/"+Version)
|
||||||
}
|
}
|
||||||
|
@@ -5,14 +5,14 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eolinker/goku-eosc/node/http-proxy/backend"
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
http_context "github.com/eolinker/goku-eosc/node/http-context"
|
http_context "github.com/eolinker/goku-eosc/node/http-context"
|
||||||
http_proxy_request "github.com/eolinker/goku-eosc/node/http-proxy/http-proxy-request"
|
http_proxy_request "github.com/eolinker/goku-eosc/node/http-proxy/http-proxy-request"
|
||||||
)
|
)
|
||||||
|
|
||||||
//DoRequest 构造请求
|
//DoRequest 构造请求
|
||||||
func DoRequest(ctx *http_context.Context, uri string, timeout time.Duration) (backend.IResponse, error) {
|
func DoRequest(ctx *http_context.Context, uri string, timeout time.Duration) (*fasthttp.Response, error) {
|
||||||
if uri == "" {
|
if uri == "" {
|
||||||
return nil, fmt.Errorf("invaild url")
|
return nil, fmt.Errorf("invaild url")
|
||||||
}
|
}
|
||||||
@@ -54,5 +54,5 @@ func DoRequest(ctx *http_context.Context, uri string, timeout time.Duration) (ba
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewResponse(response)
|
return response, nil
|
||||||
}
|
}
|
||||||
|
@@ -7,8 +7,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
http_proxy "github.com/eolinker/goku-eosc/node/http-proxy"
|
http_proxy "github.com/eolinker/goku-eosc/node/http-proxy"
|
||||||
"github.com/eolinker/goku-eosc/node/http-proxy/backend"
|
|
||||||
"github.com/eolinker/goku-eosc/utils"
|
"github.com/eolinker/goku-eosc/utils"
|
||||||
|
|
||||||
"github.com/eolinker/eosc/log"
|
"github.com/eolinker/eosc/log"
|
||||||
@@ -184,13 +185,13 @@ func (s *serviceWorker) Handle(w http.ResponseWriter, r *http.Request, router se
|
|||||||
ctx.SetBody([]byte(err.Error()))
|
ctx.SetBody([]byte(err.Error()))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ctx.SetProxyResponseHandler(http_context.NewResponseReader(response.Header(), response.StatusCode(), response.Status(), response.Body()))
|
ctx.SetProxyResponseHandler(http_context.NewResponseReader(&response.Header, response.StatusCode(), response.Body()))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *serviceWorker) send(ctx *http_context.Context, serviceDetail service.IServiceDetail) (backend.IResponse, error) {
|
func (s *serviceWorker) send(ctx *http_context.Context, serviceDetail service.IServiceDetail) (*fasthttp.Response, error) {
|
||||||
if s.upstream == nil {
|
if s.upstream == nil {
|
||||||
var response backend.IResponse
|
var response *fasthttp.Response
|
||||||
var err error
|
var err error
|
||||||
path := utils.TrimPrefixAll(ctx.ProxyRequest.TargetURL(), "/")
|
path := utils.TrimPrefixAll(ctx.ProxyRequest.TargetURL(), "/")
|
||||||
for doTrice := serviceDetail.Retry() + 1; doTrice > 0; doTrice-- {
|
for doTrice := serviceDetail.Retry() + 1; doTrice > 0; doTrice-- {
|
||||||
|
@@ -4,7 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/eolinker/goku-eosc/node/http-proxy/backend"
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
"github.com/eolinker/goku-eosc/upstream"
|
"github.com/eolinker/goku-eosc/upstream"
|
||||||
|
|
||||||
@@ -99,8 +99,8 @@ func (h *httpUpstream) CheckSkill(skill string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Send 请求发送,忽略重试
|
//Send 请求发送,忽略重试
|
||||||
func (h *httpUpstream) Send(ctx *http_context.Context, serviceDetail service.IServiceDetail) (backend.IResponse, error) {
|
func (h *httpUpstream) Send(ctx *http_context.Context, serviceDetail service.IServiceDetail) (*fasthttp.Response, error) {
|
||||||
var response backend.IResponse
|
var response *fasthttp.Response
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
path := utils.TrimPrefixAll(ctx.ProxyRequest.TargetURL(), "/")
|
path := utils.TrimPrefixAll(ctx.ProxyRequest.TargetURL(), "/")
|
||||||
|
@@ -3,7 +3,7 @@ package upstream_http_anonymous
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/eolinker/goku-eosc/node/http-proxy/backend"
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
"github.com/eolinker/goku-eosc/service"
|
"github.com/eolinker/goku-eosc/service"
|
||||||
|
|
||||||
@@ -23,8 +23,8 @@ type httpUpstream struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//send 请求发送,忽略重试
|
//send 请求发送,忽略重试
|
||||||
func (h *httpUpstream) Send(ctx *http_context.Context, serviceDetail service.IServiceDetail) (backend.IResponse, error) {
|
func (h *httpUpstream) Send(ctx *http_context.Context, serviceDetail service.IServiceDetail) (*fasthttp.Response, error) {
|
||||||
var response backend.IResponse
|
var response *fasthttp.Response
|
||||||
var err error
|
var err error
|
||||||
path := utils.TrimPrefixAll(ctx.ProxyRequest.TargetURL(), "/")
|
path := utils.TrimPrefixAll(ctx.ProxyRequest.TargetURL(), "/")
|
||||||
for doTrice := serviceDetail.Retry() + 1; doTrice > 0; doTrice-- {
|
for doTrice := serviceDetail.Retry() + 1; doTrice > 0; doTrice-- {
|
||||||
|
@@ -2,8 +2,8 @@ package upstream
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
http_context "github.com/eolinker/goku-eosc/node/http-context"
|
http_context "github.com/eolinker/goku-eosc/node/http-context"
|
||||||
"github.com/eolinker/goku-eosc/node/http-proxy/backend"
|
|
||||||
"github.com/eolinker/goku-eosc/service"
|
"github.com/eolinker/goku-eosc/service"
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
//CheckSkill 检测目标技能是否符合
|
//CheckSkill 检测目标技能是否符合
|
||||||
@@ -13,5 +13,5 @@ func CheckSkill(skill string) bool {
|
|||||||
|
|
||||||
//IUpstream 实现了负载发送请求方法
|
//IUpstream 实现了负载发送请求方法
|
||||||
type IUpstream interface {
|
type IUpstream interface {
|
||||||
Send(ctx *http_context.Context, serviceDetail service.IServiceDetail) (backend.IResponse, error)
|
Send(ctx *http_context.Context, serviceDetail service.IServiceDetail) (*fasthttp.Response, error)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user