Pārlūkot izejas kodu

Merge branch 'master' of https://jygit.jydev.jianyu360.cn/moapp/jybase

wangchuanjin 1 gadu atpakaļ
vecāks
revīzija
fa0f75e528
5 mainītis faili ar 144 papildinājumiem un 18 dzēšanām
  1. 26 18
      go-xweb/xweb/action.go
  2. 2 0
      go.mod
  3. 8 0
      go.sum
  4. 1 0
      ipmatch/README.md
  5. 107 0
      ipmatch/ipmatch.go

+ 26 - 18
go-xweb/xweb/action.go

@@ -56,6 +56,7 @@ type Action struct {
 	RootTemplate *template.Template
 	RequestBody  []byte
 	StatusCode   int
+	SessionMap   map[string]interface{}
 }
 
 type Mapper struct {
@@ -67,7 +68,7 @@ func XsrfName() string {
 	return XSRF_TAG
 }
 
-//[SWH|+]:
+// [SWH|+]:
 // Protocol returns request protocol name, such as HTTP/1.1 .
 func (c *Action) Protocol() string {
 	return c.Request.Proto
@@ -378,21 +379,21 @@ func (c *Action) NotFound(message string) error {
 
 // ParseStruct mapping forms' name and values to struct's field
 // For example:
-//		<form>
-//			<input name="user.id"/>
-//			<input name="user.name"/>
-//			<input name="user.age"/>
-//		</form>
 //
-//		type User struct {
-//			Id int64
-//			Name string
-//			Age string
-//		}
+//	<form>
+//		<input name="user.id"/>
+//		<input name="user.name"/>
+//		<input name="user.age"/>
+//	</form>
 //
-//		var user User
-//		err := action.MapForm(&user)
+//	type User struct {
+//		Id int64
+//		Name string
+//		Age string
+//	}
 //
+//	var user User
+//	err := action.MapForm(&user)
 func (c *Action) MapForm(st interface{}, names ...string) error {
 	v := reflect.ValueOf(st)
 	var name string
@@ -708,7 +709,7 @@ func (c *Action) Render(tmpl string, params ...*T) error {
 	return err
 }
 
-//仅生成网页内容
+// 仅生成网页内容
 var regInclude = regexp.MustCompile(`\{\{\s*include\s*"(.*?\.html)".*\}\}`)
 
 func (c *Action) NamedRender4Cache(name, content string, params ...*T) ([]byte, error) {
@@ -754,7 +755,7 @@ func (c *Action) NamedRender4Cache(name, content string, params ...*T) ([]byte,
 	return nil, err
 }
 
-//生成可缓存的数据但并未写到流中
+// 生成可缓存的数据但并未写到流中
 func (c *Action) Render4Cache(tmpl string, params ...*T) ([]byte, error) {
 	content, err := c.getTemplate(tmpl)
 
@@ -858,7 +859,7 @@ func (c *Action) GetForm() url.Values {
 	return c.Request.Form
 }
 
-//增加_过滤脚本等
+// 增加_过滤脚本等
 func FilterXSS(str string) string {
 	str = strings.Replace(str, "<", "&#60;", -1)
 	str = strings.Replace(str, ">", "&#62;", -1)
@@ -869,7 +870,7 @@ func FilterXSS(str string) string {
 	return str
 }
 
-//增加_原GetString方法
+// 增加_原GetString方法
 func (c *Action) GetStringComm(key string) string {
 	s := c.GetSlice(key)
 	if len(s) > 0 {
@@ -878,7 +879,7 @@ func (c *Action) GetStringComm(key string) string {
 	return ""
 }
 
-//修改_防Xss注入
+// 修改_防Xss注入
 func (c *Action) GetString(key string) string {
 	return FilterXSS(c.GetStringComm(key))
 }
@@ -950,14 +951,21 @@ func (c *Action) Session() *httpsession.Session {
 }
 
 func (c *Action) GetSession(key string) interface{} {
+	//if c.SessionMap == nil {
+	//	c.SessionMap = c.session.GetMultiple()
+	//}
 	return c.Session().Get(key)
 }
 
 func (c *Action) SetSession(key string, value interface{}) {
+	//if c.SessionMap != nil {
+	//	c.SessionMap[key] = value
+	//}
 	c.Session().Set(key, value)
 }
 
 func (c *Action) DelSession(key string) {
+	//c.SessionMap = nil
 	c.Session().Del(key)
 }
 

+ 2 - 0
go.mod

@@ -4,6 +4,7 @@ go 1.13
 
 require (
 	app.yhyue.com/moapp/esv1 v0.0.0-20220414031211-3da4123e648d
+	github.com/RoaringBitmap/roaring v1.5.0
 	github.com/coscms/tagfast v0.0.0-20150925144250-2b69b2496250
 	github.com/donnie4w/go-logger v0.0.0-20170827050443-4740c51383f4
 	github.com/fsnotify/fsnotify v1.4.9
@@ -13,6 +14,7 @@ require (
 	github.com/gomodule/redigo v1.8.9
 	github.com/howeyc/fsnotify v0.9.0
 	github.com/olivere/elastic/v7 v7.0.22
+	github.com/yl2chen/cidranger v1.0.2
 	github.com/zeromicro/go-zero v1.3.5
 	go.etcd.io/etcd/client/v3 v3.5.4
 	go.mongodb.org/mongo-driver v1.9.1

+ 8 - 0
go.sum

@@ -48,6 +48,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q
 github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
 github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
 github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
+github.com/RoaringBitmap/roaring v1.5.0 h1:V0VCSiHjroItEYCM3guC8T83ehi5QMt3oM9EefTTOms=
+github.com/RoaringBitmap/roaring v1.5.0/go.mod h1:plvDsJQpxOC5bw8LRteu/MLWHsHez/3y6cubLI4/1yE=
 github.com/Shopify/sarama v1.30.0/go.mod h1:zujlQQx1kzHsh4jfV1USnptCQrHAEZ2Hk8fTKCulPVs=
 github.com/Shopify/toxiproxy/v2 v2.1.6-0.20210914104332-15ea381dcdae/go.mod h1:/cvHQkZ1fst0EmZnA5dFtiQdWCNCFYzb+uE2vqVgvx0=
 github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
@@ -66,6 +68,8 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
 github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
+github.com/bits-and-blooms/bitset v1.2.0 h1:Kn4yilvwNtMACtf1eYDlG8H77R07mZSPbMjLyS07ChA=
+github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
 github.com/bkaradzic/go-lz4 v1.0.0/go.mod h1:0YdlkowM3VswSROI7qDxhRvJ3sLhlFrRRwjwegp5jy4=
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
 github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
@@ -320,6 +324,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
 github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
 github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
 github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
+github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM=
+github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
 github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
 github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
 github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
@@ -432,6 +438,8 @@ github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w=
 github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs=
 github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
 github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
+github.com/yl2chen/cidranger v1.0.2 h1:lbOWZVCG1tCRX4u24kuM1Tb4nHqWkDxwLdoS+SevawU=
+github.com/yl2chen/cidranger v1.0.2/go.mod h1:9U1yz7WPYDwf0vpNWFaeRh0bjwz5RVgRy/9UEQfHl0g=
 github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
 github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
 github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

+ 1 - 0
ipmatch/README.md

@@ -0,0 +1 @@
+IP黑白名单检测

+ 107 - 0
ipmatch/ipmatch.go

@@ -0,0 +1,107 @@
+package ipmatch
+
+import (
+	_ "embed"
+	"errors"
+	"github.com/RoaringBitmap/roaring"
+	"github.com/yl2chen/cidranger"
+	"net"
+	"strconv"
+	"strings"
+)
+
+type WhiteIp struct {
+	Rb     *roaring.Bitmap
+	Ranger cidranger.Ranger
+}
+
+type IpParameter struct {
+	Ip      string
+	IpType  int
+	IsWhite int
+}
+
+// ip2Uint32
+func ip2Uint32(rawIp string) (uint32, error) {
+	w := strings.Split(rawIp, ".")
+	if len(w) != 4 {
+		return 0, errors.New("error data type")
+	}
+	var ipInt uint32
+	for i, s := range w {
+		tmp, err := strconv.Atoi(s)
+		if err != nil {
+			return 0, err
+		}
+		ipInt += uint32(tmp << ((3 - i) * 8))
+	}
+	return ipInt, nil
+}
+
+// NewRb init
+func NewRb(ips []IpParameter) *WhiteIp {
+	newIp := new(WhiteIp)
+	rb := roaring.NewBitmap()
+	for _, ipData := range ips {
+		if len(ipData.Ip) < 8 {
+			continue
+		}
+		if ipData.IpType == 1 && isIP(ipData.Ip) { //精准ip添加
+			if ipUint, err := ip2Uint32(ipData.Ip); err == nil {
+				rb.Add(ipUint)
+			}
+		} else if ipData.IpType == 2 && isIPv4Segment(ipData.Ip) { //ip段添加
+			for _, v := range cidrToIPList(ipData.Ip) {
+				if ipUint, err1 := ip2Uint32(v); err1 == nil {
+					rb.Add(ipUint)
+				}
+			}
+		}
+	}
+	newIp.Rb = rb
+	return newIp
+}
+
+func cidrToIPList(cidr string) []string {
+	ip, ipNet, err := net.ParseCIDR(cidr)
+	if err != nil {
+		return nil
+	}
+
+	var ipList []string
+	for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); incIP(ip) {
+		ipList = append(ipList, ip.String())
+	}
+
+	return ipList
+}
+
+func incIP(ip net.IP) {
+	for j := len(ip) - 1; j >= 0; j-- {
+		ip[j]++
+		if ip[j] > 0 {
+			break
+		}
+	}
+}
+
+// 判断字符串是否为有效的 IP 地址
+func isIP(s string) bool {
+	ip := net.ParseIP(s)
+	return ip != nil
+}
+
+// 判断字符串是否为有效的 IPv4 段
+func isIPv4Segment(s string) bool {
+	_, _, err := net.ParseCIDR(s)
+	return err == nil
+}
+
+// Match
+func (ip *WhiteIp) Match(rawIp string) bool {
+	if ipUint, err := ip2Uint32(rawIp); err != nil {
+		return false
+	} else {
+		return ip.Rb.Contains(ipUint)
+	}
+}