1 Star 0 Fork 0

CaptialSTeam/ubdframe

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
proxy.go 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
Souki 提交于 2024-12-17 08:59 +08:00 . !1初始化
package proxyhandler
import (
"gitee.com/captials-team/ubdframe/src/common"
"gitee.com/captials-team/ubdframe/src/domain/configstc"
"github.com/gin-gonic/gin"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type ItfProxyMode interface {
Match(req *http.Request) bool
ServeHTTP(rw http.ResponseWriter, req *http.Request)
}
// ProxyByPrefix 根据前缀转发的代理
type ProxyByPrefix struct {
rule *configstc.ProxyRule
proxy *httputil.ReverseProxy
}
func NewProxyByPrefix(rule configstc.ProxyRule) *ProxyByPrefix {
parseRootUrl, err := url.Parse(rule.Forward)
common.ErrPanic(err)
proxy := httputil.NewSingleHostReverseProxy(parseRootUrl)
return &ProxyByPrefix{
rule: &rule,
proxy: proxy,
}
}
func (p *ProxyByPrefix) Match(req *http.Request) bool {
if strings.HasPrefix(req.URL.Path, p.rule.Match) {
return true
}
return false
}
func (p *ProxyByPrefix) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
req.URL.Path = strings.Replace(req.URL.Path, p.rule.Match, p.rule.Replace, 1)
p.proxy.ServeHTTP(rw, req)
}
// ProxyByFixed 固定匹配转发
type ProxyByFixed struct {
rule *configstc.ProxyRule
proxy *httputil.ReverseProxy
}
func NewProxyByFixed(rule configstc.ProxyRule) *ProxyByFixed {
parseRootUrl, err := url.Parse(rule.Forward)
common.ErrPanic(err)
proxy := httputil.NewSingleHostReverseProxy(parseRootUrl)
return &ProxyByFixed{
rule: &rule,
proxy: proxy,
}
}
func (p *ProxyByFixed) Match(req *http.Request) bool {
return req.URL.Path == p.rule.Match
}
func (p *ProxyByFixed) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
req.URL.Path = strings.Replace(req.URL.Path, p.rule.Match, p.rule.Replace, 1)
p.proxy.ServeHTTP(rw, req)
}
// ProxyNone 空代理
type ProxyNone struct {
}
func (p *ProxyNone) Match(req *http.Request) bool {
return false
}
func (p *ProxyNone) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
func NewProxy(rule configstc.ProxyRule) ItfProxyMode {
switch rule.Mode {
case configstc.ProxyModePrefix:
return NewProxyByPrefix(rule)
case configstc.ProxyModeFix:
return NewProxyByFixed(rule)
}
return &ProxyNone{}
}
func NewProxyGroup(rules ...configstc.ProxyRule) []ItfProxyMode {
var arr []ItfProxyMode
for _, v := range rules {
arr = append(arr, NewProxy(v))
}
return arr
}
func ProxyHandler(rules ...ItfProxyMode) gin.HandlerFunc {
return func(ctx *gin.Context) {
for _, v := range rules {
if v.Match(ctx.Request) {
v.ServeHTTP(ctx.Writer, ctx.Request)
ctx.Abort()
break
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/captials-team/ubdframe.git
git@gitee.com:captials-team/ubdframe.git
captials-team
ubdframe
ubdframe
v1.0.2

搜索帮助