Kaynağa Gözat

wip:无权限替换请求地址代码提交

wangkaiyue 2 yıl önce
ebeveyn
işleme
c89c1c1a27

+ 1 - 0
common/gatecode/errcode.go

@@ -25,6 +25,7 @@ const (
 	GLOBAL_ERR_RESOURCE_PORWE_NOTHAS                              // 没有权限
 	GLOBAL_ERR_RESOURCE_PORWE_RECHARGE                            // 资源充值异常
 	GLOBAL_ERR_RESOURCE_PORWE_RECHARGE_FULL                       // 资源充值已达上限
+	GLOBAL_ERR_CONFIG_FAIL                                        // 配置异常
 )
 const (
 	// GATEWAY_ERR_NIL 代理网关错误定义

+ 6 - 3
common/gatecode/errcode_string.go

@@ -23,6 +23,9 @@ func _() {
 	_ = x[GLOBAL_ERR_RESOURCE_PORWE_EXPIRED-1012]
 	_ = x[GLOBAL_ERR_RESOURCE_PORWE_NOTENOUGH-1013]
 	_ = x[GLOBAL_ERR_RESOURCE_PORWE_NOTHAS-1014]
+	_ = x[GLOBAL_ERR_RESOURCE_PORWE_RECHARGE-1015]
+	_ = x[GLOBAL_ERR_RESOURCE_PORWE_RECHARGE_FULL-1016]
+	_ = x[GLOBAL_ERR_CONFIG_FAIL-1017]
 	_ = x[GATEWAY_ERR_NIL-2000]
 	_ = x[GATEWAY_MODULE_UNDEFINED-2001]
 	_ = x[GATEWAY_MODULE_SERVER_ERR-2002]
@@ -41,14 +44,14 @@ func _() {
 }
 
 const (
-	_ErrCode_name_0 = "global OK无用户身份无选择企业没有权限正在开发中企业账户已被冻结企业未认证企业认证待审核企业认证未通过企业认证已过期无效资源code权限校验失败权益已过期权益余额不足没有权限"
+	_ErrCode_name_0 = "global OK无用户身份无选择企业没有权限正在开发中企业账户已被冻结企业未认证企业认证待审核企业认证未通过企业认证已过期无效资源code权限校验失败权益已过期权益余额不足没有权限资源充值异常资源充值已达上限配置异常"
 	_ErrCode_name_1 = "gateway ok未知节点节点无可用服务未注册路由地址接口维护中服务地址异常服务异常用户中心服务异常资源中心服务异常"
 	_ErrCode_name_2 = "server ok接口超时扣减异常"
 	_ErrCode_name_3 = "server ok未知异常请求频繁"
 )
 
 var (
-	_ErrCode_index_0 = [...]uint8{0, 9, 24, 39, 51, 66, 90, 105, 126, 147, 168, 184, 202, 217, 235, 247}
+	_ErrCode_index_0 = [...]uint16{0, 9, 24, 39, 51, 66, 90, 105, 126, 147, 168, 184, 202, 217, 235, 247, 265, 289, 301}
 	_ErrCode_index_1 = [...]uint8{0, 10, 22, 43, 64, 79, 97, 109, 133, 157}
 	_ErrCode_index_2 = [...]uint8{0, 9, 21, 33}
 	_ErrCode_index_3 = [...]uint8{0, 9, 21, 33}
@@ -56,7 +59,7 @@ var (
 
 func (i ErrCode) String() string {
 	switch {
-	case 1000 <= i && i <= 1014:
+	case 1000 <= i && i <= 1017:
 		i -= 1000
 		return _ErrCode_name_0[_ErrCode_index_0[i]:_ErrCode_index_0[i+1]]
 	case 2000 <= i && i <= 2008:

+ 15 - 9
core/proxy/middleware/filterFuncs.go

@@ -3,6 +3,7 @@ package middleware
 import (
 	"bp.jydev.jianyu360.cn/BaseService/gateway/core/proxy/vars"
 	"fmt"
+	"net/url"
 	"strings"
 
 	. "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
@@ -39,22 +40,27 @@ func filterBefore(r *ghttp.Request) error {
 	}
 	var useCount, surplusCount int64
 	//校验权益
+	var powerErr error
 	if rule.PowerCheck == 1 {
-		var err error
-		useCount, surplusCount, err = rpc.CheckResourcePower(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, r.GetHeader("functionCode"), rule.AppId)
-		if err != nil {
-			return err
-		}
+		useCount, surplusCount, powerErr = rpc.CheckResourcePower(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, r.GetHeader("functionCode"), rule.AppId)
 	}
 	//老剑鱼用户身份判断
-	if rule.PowerType > 0 {
-		if err := rpc.CheckUserPower(ctx.Sess.UserId, ctx.Sess.Phone, rule.PowerType); err != nil {
-			return err
+	if powerErr == nil && rule.PowerType > 0 {
+		powerErr = rpc.CheckUserPower(ctx.Sess.UserId, ctx.Sess.Phone, rule.PowerType)
+	}
+	if rule.NoPowerUrl == "" && powerErr != nil {
+		return powerErr
+	} else if powerErr != nil && rule.NoPowerUrl != "" { //无权限替换请求地址
+		newUrl, urlSwitchErr := url.Parse(rule.NoPowerUrl)
+		if urlSwitchErr != nil {
+			return NewErrorWithCode(GLOBAL_ERR_CONFIG_FAIL)
 		}
+		powerErr = nil //无权限异常置空
+		r.URL = newUrl
 	}
+
 	//注入用户身份
 	infusionIdentity(r, ctx.Sess, useCount, surplusCount, rule.AppId)
-
 	//外部程序过滤器
 	if rule.IsOutServer {
 		//赛思用户身份注入

+ 2 - 14
core/proxy/proxyClient/proxyClient.go

@@ -1,13 +1,13 @@
 package proxyClient
 
 import (
+	"bp.jydev.jianyu360.cn/BaseService/gateway/core/util"
 	"github.com/gogf/gf/v2/os/gcfg"
 	"github.com/gogf/gf/v2/os/gctx"
 	"net"
 	"net/http"
 	"net/http/httputil"
 	"net/url"
-	"strings"
 	"time"
 )
 
@@ -18,7 +18,7 @@ func CreateCustomProxyClient(target *url.URL, errFunc func(http.ResponseWriter,
 		Director: func(req *http.Request) {
 			req.URL.Scheme = target.Scheme
 			req.URL.Host = target.Host
-			req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
+			req.URL.Path = util.SingleJoiningSlash(target.Path, req.URL.Path)
 		},
 		Transport:      transport,
 		ModifyResponse: change,
@@ -40,15 +40,3 @@ func ReLoadClient() {
 		MaxIdleConnsPerHost:   gcfg.Instance().MustGet(gctx.New(), "proxy.maxIdleConnsPerHost", 5).Int(),                                //客户端可以持有的最大空闲连接
 	}
 }
-
-func singleJoiningSlash(a, b string) string {
-	aslash := strings.HasSuffix(a, "/")
-	bslash := strings.HasPrefix(b, "/")
-	switch {
-	case aslash && bslash:
-		return a + b[1:]
-	case !aslash && !bslash:
-		return a + "/" + b
-	}
-	return a + b
-}

+ 4 - 0
core/proxy/rpc/checkPower.go

@@ -69,6 +69,10 @@ func CheckUserPower(userid, phone string, power_type int) error {
 		if userPower.EntnicheStatus <= 0 {
 			return NewErrorWithCode(GLOBAL_ERR_NOPOWER, fmt.Sprintf("该用户非商机管理用户"))
 		}
+	case 22: //临时加大会员专家版、商机版判断
+		if !(userPower.Status == 1 || userPower.Status == 2 || userPower.Status == 6 || userPower.Status == 7) {
+			return NewErrorWithCode(GLOBAL_ERR_NOPOWER, fmt.Sprintf("该用户非大会员专家版、商机版用户"))
+		}
 	}
 	return nil
 }

+ 4 - 0
core/router/manager.go

@@ -22,6 +22,8 @@ type Manager struct {
 // InitRouterManager 初始化系统代理路由
 // 支持完全匹配和正则匹配
 func InitRouterManager() (*Manager, error) {
+	//无权限地址
+	urlSwitch := g.Cfg().MustGet(gctx.New(), "noPowerUrlSwitch").MapStrStr()
 	//加载规则
 	res := db.GateWatMySql.Query(fmt.Sprintf("SELECT status,middleground_code,url,function_code,check_sess,check_power,check_auth,check_status,check_blacklist,ploy_code,timeout,remark,appid,deduct_source,power_type FROM %s",
 		g.Cfg().MustGet(gctx.New(), "system.routerTable", "front_proxy").String()))
@@ -55,6 +57,7 @@ func InitRouterManager() (*Manager, error) {
 			TimeOut:      gconv.Int64(row["timeout"]),
 			Remark:       gconv.String(row["remark"]),
 			PowerType:    gconv.Int(row["power_type"]),
+			NoPowerUrl:   urlSwitch[router],
 		}
 
 		// 判断路由匹配方式是完全匹配还是正则匹配 (此处逻辑参考x-web框架)
@@ -79,6 +82,7 @@ func (m *Manager) GetRouterRule(url string) (*Router, error) {
 		for reg, thisRouterRule := range m.regexRouter {
 			if reg.MatchString(url) {
 				routerRule = thisRouterRule
+				break
 			}
 		}
 	}

+ 1 - 0
core/router/router.go

@@ -21,5 +21,6 @@ type Router struct {
 	ReqUrl       string             // 请求地址
 	Remark       string             // 路由备注信息
 	PowerType    int                // 权益判断方式、0走资源中台  1前置代理判断,是否是付费用户  2前置代理判断,是否是超级订阅  3前置代理判断,是否是大会员  4前置代理判断,是否是商机管理
+	NoPowerUrl   string             //无权限地址替换
 	//FilterRule *filterPoly.ReqFilterPoly //访问黑名单规则
 }

+ 12 - 0
core/util/http.go

@@ -106,3 +106,15 @@ func ChangeResponse(r *ghttp.Request, fn func([]byte) error) (err error) {
 	_, err = fmt.Fprint(r.Response.ResponseWriter, zBuf.Bytes())
 	return nil
 }
+
+func SingleJoiningSlash(a, b string) string {
+	aslash := strings.HasSuffix(a, "/")
+	bslash := strings.HasPrefix(b, "/")
+	switch {
+	case aslash && bslash:
+		return a + b[1:]
+	case !aslash && !bslash:
+		return a + "/" + b
+	}
+	return a + b
+}

+ 13 - 6
etc/config.yaml

@@ -22,17 +22,17 @@ etcd:
     userCenter:
       key: usercenter.rpc
       address:
-        - 192.168.3.240:2379
+        - 192.168.3.206:2379
     # 资源中台配置
     resourceCenter:
       key: resource.rpc
       address:
-        - 192.168.3.240:2379
+        - 192.168.3.206:2379
 
 #数据库配置
 databases:
   # redis配置
-  redis: session=192.168.3.206:1712    # 用户获取剑鱼程序用户session
+  redis: session=192.168.3.206:1712,other=192.168.3.11:1712    # 用户获取剑鱼程序用户session
   polyredis:
     address: 192.168.3.206:1712
     poolsize: 30
@@ -72,10 +72,10 @@ databases:
 
 # 系统配置
 system:
-  routerTable: front_proxy_dev
+  routerTable: front_proxy
   # 监听服务注册etcd配置
   etcdListen:
-    - 192.168.3.240:2379
+    - 192.168.3.206:2379
 
   response:
     head-clear: # 响应头删除
@@ -124,6 +124,13 @@ logger:
     text: 前置代理告警请查看               # 消息正文
 outServer:
   sussbi:
-    addr: http://192.168.3.10:8089
+    addr: http://192.168.3.217:8088
     user: jianyuweb
     password: 111111
+  local:
+    addr: http://127.0.0.1:8080
+
+# 仅限此程序代理的地址
+noPowerUrlSwitch:
+  '/nzj/app/nzj.app/nzj_detail_1.spg' : '/nzj/app/nzj.app/nzj_detail_0.spg'
+  '/nzj/app/nzj.app/nzj_search_1.spg': '/nzj/app/nzj.app/nzj_search_0.spg'

+ 1 - 1
go.mod

@@ -13,7 +13,7 @@ require (
 	github.com/golang/freetype v0.0.0
 	github.com/zeromicro/go-zero v1.3.5
 	go.etcd.io/etcd/client/v3 v3.5.4
-	golang.org/x/net v0.1.0 // indirect
+	golang.org/x/net v0.1.0
 )
 
 replace github.com/golang/freetype v0.0.0 => github.com/Kusers/freetype v0.0.0-20220714074437-04188d6f4e2c

+ 0 - 6
go.sum

@@ -649,7 +649,6 @@ golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPh
 golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
-golang.org/x/crypto v0.0.0-20210920023735-84f357641f63 h1:kETrAMYZq6WVGPa8IIixL0CaEcIUNi+1WX7grUoi3y8=
 golang.org/x/crypto v0.0.0-20210920023735-84f357641f63/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -730,7 +729,6 @@ golang.org/x/net v0.0.0-20210917221730-978cfadd31cf/go.mod h1:9nx3DQGgdP8bBQD5qx
 golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
 golang.org/x/net v0.0.0-20211209124913-491a49abca63/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
 golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
-golang.org/x/net v0.0.0-20220531201128-c960675eff93 h1:MYimHLfoXEpOhqd/zgoA/uoXzHB86AEky4LAx5ij9xA=
 golang.org/x/net v0.0.0-20220531201128-c960675eff93/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
@@ -754,7 +752,6 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
 golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -825,7 +822,6 @@ golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBc
 golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220429233432-b5fbb4746d32/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
 golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
@@ -833,7 +829,6 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
-golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
 golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@@ -845,7 +840,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
-golang.org/x/text v0.3.8-0.20211105212822-18b340fc7af2 h1:GLw7MR8AfAG2GmGcmVgObFOHXYypgGjnGno25RDwn3Y=
 golang.org/x/text v0.3.8-0.20211105212822-18b340fc7af2/go.mod h1:EFNZuWvGYxIRUEX+K8UmCFwYmZjqcrnq15ZuVldZkZ0=
 golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
 golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=