From 811536bea4763811697f0f51adce979cf6b42d77 Mon Sep 17 00:00:00 2001 From: kenneth <1185230223@qq.com> Date: Thu, 7 Apr 2022 15:20:21 +0800 Subject: [PATCH] 0.0.3 --- go.mod | 33 +++-- go.sum | 38 +++-- pkg/{util => cache}/cache.go | 6 +- pkg/cache/manager.go | 17 --- pkg/cache/memory_manager.go | 70 --------- pkg/cache/redis_manager.go | 135 ----------------- pkg/conver/conver.go | 7 + pkg/database/mssql.go | 16 ++ pkg/database/postgres.go | 15 ++ pkg/database/query.go | 44 ++++++ pkg/dt/time.go | 12 ++ pkg/{util => }/encrypt/aes.go | 4 +- pkg/{util => }/encrypt/base64.go | 0 pkg/{util => }/encrypt/des.go | 0 pkg/encrypt/md5.go | 12 ++ pkg/encrypt/password.go | 72 +++++++++ pkg/{util => }/encrypt/rsa.go | 0 pkg/{util => }/encrypt/sha.go | 0 pkg/env/env.go | 12 ++ pkg/fetcher/fetcher.go | 80 ++++++++++ pkg/{util => ft}/file.go | 4 +- pkg/ip2region/ip2Region.go | 201 +++++++++++++------------- pkg/ip2region/ip2Region_test.go | 49 ------- pkg/{util => logger}/log.go | 12 +- pkg/{util => random}/random.go | 22 +-- pkg/{util/string.go => st/strings.go} | 9 +- pkg/token/jwt_maker.go | 9 +- pkg/token/maker.go | 2 +- pkg/token/paseto_maker.go | 9 +- pkg/token/payload.go | 13 +- pkg/util/encrypt/md5.go | 27 ---- pkg/util/md5.go | 17 --- pkg/util/pager.go | 20 --- pkg/util/password.go | 21 --- pkg/validator/phone.go | 21 +++ 35 files changed, 466 insertions(+), 543 deletions(-) rename pkg/{util => cache}/cache.go (88%) delete mode 100644 pkg/cache/manager.go delete mode 100644 pkg/cache/memory_manager.go delete mode 100644 pkg/cache/redis_manager.go create mode 100644 pkg/conver/conver.go create mode 100644 pkg/database/mssql.go create mode 100644 pkg/database/postgres.go create mode 100644 pkg/database/query.go create mode 100644 pkg/dt/time.go rename pkg/{util => }/encrypt/aes.go (96%) rename pkg/{util => }/encrypt/base64.go (100%) rename pkg/{util => }/encrypt/des.go (100%) create mode 100644 pkg/encrypt/md5.go create mode 100644 pkg/encrypt/password.go rename pkg/{util => }/encrypt/rsa.go (100%) rename pkg/{util => }/encrypt/sha.go (100%) create mode 100644 pkg/env/env.go create mode 100644 pkg/fetcher/fetcher.go rename pkg/{util => ft}/file.go (97%) delete mode 100644 pkg/ip2region/ip2Region_test.go rename pkg/{util => logger}/log.go (81%) rename pkg/{util => random}/random.go (53%) rename pkg/{util/string.go => st/strings.go} (93%) delete mode 100644 pkg/util/encrypt/md5.go delete mode 100644 pkg/util/md5.go delete mode 100644 pkg/util/pager.go delete mode 100644 pkg/util/password.go create mode 100644 pkg/validator/phone.go diff --git a/go.mod b/go.mod index 75905f1..e7d58e7 100644 --- a/go.mod +++ b/go.mod @@ -3,26 +3,27 @@ module github.com/lyydhl-zhang/common-module go 1.17 require ( + github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 + github.com/denisenkom/go-mssqldb v0.12.0 + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/lib/pq v1.10.4 + github.com/natefinch/lumberjack v2.0.0+incompatible + github.com/o1egl/paseto v1.0.0 + go.uber.org/zap v1.19.1 + golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 + golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 + golang.org/x/text v0.3.7 +) + +require ( + github.com/BurntSushi/toml v1.1.0 // indirect github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect - github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 // indirect github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect - github.com/gomodule/redigo v1.8.5 // indirect - github.com/google/uuid v1.3.0 // indirect - github.com/matoous/go-nanoid v1.5.0 // indirect - github.com/natefinch/lumberjack v2.0.0+incompatible // indirect - github.com/o1egl/paseto v1.0.0 // indirect - github.com/patrickmn/go-cache v2.1.0+incompatible // indirect + github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect + github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.7.0 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.7.0 // indirect - go.uber.org/zap v1.19.1 // indirect - golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 // indirect - golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 // indirect golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 // indirect - golang.org/x/text v0.3.7 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect ) diff --git a/go.sum b/go.sum index 420cb8d..b3f8cae 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,8 @@ +github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0/go.mod h1:h6H6c8enJmmocHUbLiiGY6sx7f9i+X3m1CHdd5c6Rdw= +github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0/go.mod h1:HcM1YX14R7CJcghJGOYCgdezslRSVzqwLf/q+4Y2r/0= +github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8= +github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I= +github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY= github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA= github.com/aead/chacha20poly1305 v0.0.0-20170617001512-233f39982aeb/go.mod h1:UzH9IX1MMqOcwhoNOIjmTQeAxrFgzs50j4golQtXXxU= @@ -5,27 +10,31 @@ github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 h1:1DcvRPZOd github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29/go.mod h1:UzH9IX1MMqOcwhoNOIjmTQeAxrFgzs50j4golQtXXxU= github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 h1:52m0LGchQBBVqJRyYYufQuIbVqRawmubW3OFGqK1ekw= github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635/go.mod h1:lmLxL+FV291OopO93Bwf9fQLQeLyt33VJRUg5VJ30us= +github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denisenkom/go-mssqldb v0.12.0 h1:VtrkII767ttSPNRfFekePK3sctr+joXgO58stqQbtUA= +github.com/denisenkom/go-mssqldb v0.12.0/go.mod h1:iiK0YP1ZeepvmBQk/QpLEhhTNJgfzrpArPY/aFvc9yU= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/gomodule/redigo v1.8.5 h1:nRAxCa+SVsyjSBrtZmG/cqb6VbTmuRzpg/PoTFlpumc= -github.com/gomodule/redigo v1.8.5/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 h1:+eHOFJl1BaXrQxKX+T06f78590z4qA2ZzBTqahsKSE4= +github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188/go.mod h1:vXjM/+wXQnTPR4KqTKDgJukSZ6amVRtWMPEjE6sQoK8= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/matoous/go-nanoid v1.5.0 h1:VRorl6uCngneC4oUQqOYtO3S0H5QKFtKuKycFG3euek= -github.com/matoous/go-nanoid v1.5.0/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U= +github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk= +github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8= github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM= github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk= github.com/o1egl/paseto v1.0.0 h1:bwpvPu2au176w4IBlhbyUv/S5VPptERIA99Oap5qUd0= github.com/o1egl/paseto v1.0.0/go.mod h1:5HxsZPmw/3RI2pAwGo1HhOOwSdvBpcuVzO7uDkm+CLU= -github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= -github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= +github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -35,13 +44,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4= go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec= @@ -51,6 +60,7 @@ go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI= golang.org/x/crypto v0.0.0-20181025213731-e84da0312774/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 h1:3erb+vDS8lU1sxfDHF4/hhWyaXnhIaO+7RgL4fDZORA= golang.org/x/crypto v0.0.0-20210915214749-c084706c2272/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -58,7 +68,9 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 h1:/6y1LfuqNuQdHAm0jjtPtgRcxIxjVZgm5OTu8/QhZvk= golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -68,12 +80,15 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 h1:7ZDGnxgHAMw7thfC5bEos0RDAccZKxioiWBhfIe+tvw= golang.org/x/sys v0.0.0-20210915083310-ed5796bab164/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -85,8 +100,11 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= +gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/util/cache.go b/pkg/cache/cache.go similarity index 88% rename from pkg/util/cache.go rename to pkg/cache/cache.go index 03f2ee0..75d5b77 100644 --- a/pkg/util/cache.go +++ b/pkg/cache/cache.go @@ -1,4 +1,4 @@ -package util +package cache // 简单缓存 适合小数据量 @@ -27,7 +27,3 @@ func GetCache(key string) (interface{}, bool) { func DeleteCache(key string) { globalMap.Delete(key) } - -//func LenCache() int { -// return int(cacheLen) -//} diff --git a/pkg/cache/manager.go b/pkg/cache/manager.go deleted file mode 100644 index f4d0f00..0000000 --- a/pkg/cache/manager.go +++ /dev/null @@ -1,17 +0,0 @@ -package cache - -// Manager 管理缓存的接口定义 -type Manager interface { - // Set 设置缓存 - Set(key string, value interface{}) error - // SetDefault 设置缓存 - SetDefault(key string, value interface{}, expire int64) error - // Get 获取缓存 - Get(key string) (interface{}, error) - // GetString 获取缓存 - GetString(key string) (string, error) - // Exists key是否存在 - Exists(key string) bool - // Delete 删除key - Delete(key string) error -} diff --git a/pkg/cache/memory_manager.go b/pkg/cache/memory_manager.go deleted file mode 100644 index 33c6aa7..0000000 --- a/pkg/cache/memory_manager.go +++ /dev/null @@ -1,70 +0,0 @@ -package cache - -import ( - "errors" - "fmt" - "time" - - "github.com/patrickmn/go-cache" -) - -// MemoryManager memory缓存管理 -type MemoryManager struct { - cache *cache.Cache -} - -// NewMemoryManager 获取一个新的内存管理对象 -// defaultExpiration 缓存过期时间 -// cleanupInterval 缓存清理时间 -func NewMemoryManager(defaultExpiration, cleanupInterval time.Duration) Manager { - return &MemoryManager{cache: cache.New(defaultExpiration, cleanupInterval)} -} - -// Set 设置缓存 -// key 键 -// value 值 -func (manager *MemoryManager) Set(key string, value interface{}) error { - manager.cache.Set(key, value, time.Hour*24*365*100) // 10年 - return nil -} - -// SetDefault 设置缓存 -// key 键 -// value 值 -// expire 过期时间 (秒) -func (manager *MemoryManager) SetDefault(key string, value interface{}, expire int64) error { - manager.cache.Set(key, value, time.Second*time.Duration(expire)) - return nil -} - -// Get 获取缓存 -func (manager *MemoryManager) Get(key string) (interface{}, error) { - value, found := manager.cache.Get(key) - if found { - return value, nil - } - return nil, errors.New("no found data with key") -} - -// GetString 获取缓存 -func (manager *MemoryManager) GetString(key string) (string, error) { - value, found := manager.cache.Get(key) - if found { - return fmt.Sprintf("%v", value), nil - } - return "", errors.New("no found data with key") -} - -// Exists key是否存在 -func (manager *MemoryManager) Exists(key string) bool { - if _, found := manager.cache.Get(key); found { - return true - } - return false -} - -// Delete 删除key -func (manager *MemoryManager) Delete(key string) error { - manager.cache.Delete(key) - return nil -} diff --git a/pkg/cache/redis_manager.go b/pkg/cache/redis_manager.go deleted file mode 100644 index 4c608ea..0000000 --- a/pkg/cache/redis_manager.go +++ /dev/null @@ -1,135 +0,0 @@ -package cache - -import ( - "fmt" - "time" - - "github.com/gomodule/redigo/redis" -) - -// RedisManager redis缓存管理 -type RedisManager struct { - pool *redis.Pool -} - -// NewRedisManager 获取一个新的redis管理对象 -func NewRedisManager(address string) Manager { - pool := &redis.Pool{ - // 连接方法 - Dial: func() (redis.Conn, error) { - c, err := redis.Dial("tcp", address) - if err != nil { - return nil, err - } - c.Do("SELECT", 0) - return c, nil - }, - //DialContext: nil, - //TestOnBorrow: nil, - MaxIdle: 10, // 最大的空闲连接数,表示即使没有redis连接时依然可以保持N个空闲的连接,而不被清除,随时处于待命状态。 - MaxActive: 10, // 最大的激活连接数,表示同时最多有N个连接 - IdleTimeout: 360 * time.Second, // 最大的空闲连接等待时间,超过此时间后,空闲连接将被关闭 - //Wait: false, - //MaxConnLifetime: 0, - } - return &RedisManager{pool} -} - -// Set 设置缓存 -// key 键 -// value 值 -func (manager *RedisManager) Set(key string, value interface{}) error { - conn := manager.pool.Get() - defer conn.Close() - - _, err := conn.Do("Set", key, value) - if err != nil { - return fmt.Errorf("set cache error: %v", err) - } - return nil -} - -// SetDefault 设置缓存 -// key 键 -// value 值 -// expire 过期时间 (秒) -func (manager *RedisManager) SetDefault(key string, value interface{}, expire int64) error { - conn := manager.pool.Get() - defer conn.Close() - - //_, err := conn.Do("set", key, value) - //if err != nil { - // return fmt.Errorf("set default cache error: %v", err) - //} - // - //// 设置过期时间 - //conn.Do("expire", key, expire) - //return nil - - _, err := conn.Do("set", key, value, "ex", expire) - if err != nil { - return fmt.Errorf("set default cache error: %v", err) - } - return nil -} - -// Get 获取缓存 -func (manager *RedisManager) Get(key string) (interface{}, error) { - conn := manager.pool.Get() - defer conn.Close() - - // 检查key是否存在 - exit, err := redis.Bool(conn.Do("exists", key)) - if err != nil || !exit { - return nil, fmt.Errorf("key is not exists") - } - - value, err := conn.Do("get", key) - if err != nil { - return nil, fmt.Errorf("get cache error: %v", err) - } - return value, nil -} - -// GetString 获取缓存 -func (manager *RedisManager) GetString(key string) (string, error) { - conn := manager.pool.Get() - defer conn.Close() - - // 检查key是否存在 - exit, err := redis.Bool(conn.Do("exists", key)) - if err != nil || !exit { - return "", fmt.Errorf("key is not exists") - } - - value, err := redis.String(conn.Do("get", key)) - if err != nil { - return "", fmt.Errorf("get cache error: %v", err) - } - return value, nil -} - -// Exists key是否存在 -func (manager *RedisManager) Exists(key string) bool { - conn := manager.pool.Get() - defer conn.Close() - - // 检查key是否存在 - exit, err := redis.Bool(conn.Do("expire", key)) - if err != nil { - return false - } - return exit -} - -// Delete 删除key -func (manager *RedisManager) Delete(key string) error { - conn := manager.pool.Get() - defer conn.Close() - - _, err := conn.Do("del", key) - if err != nil { - return fmt.Errorf("删除key:[%s]异常:%v", key, err) - } - return nil -} diff --git a/pkg/conver/conver.go b/pkg/conver/conver.go new file mode 100644 index 0000000..7fcd53a --- /dev/null +++ b/pkg/conver/conver.go @@ -0,0 +1,7 @@ +package conver + +import "strconv" + +func FloatToString(val float64) string { + return strconv.FormatFloat(val, 'f', 1, 64) +} diff --git a/pkg/database/mssql.go b/pkg/database/mssql.go new file mode 100644 index 0000000..a3d722c --- /dev/null +++ b/pkg/database/mssql.go @@ -0,0 +1,16 @@ +package database + +import ( + mssql "github.com/denisenkom/go-mssqldb" +) + +// MSIsUniqueViolation 唯一值是否冲突 ture: 是, false: 否 +func MSIsUniqueViolation(err error) bool { + msErr, ok := err.(mssql.Error) + return ok && msErr.Number == 2627 +} + +//func MsIsForeignKeyViolation(err error) bool { +// msErr, ok := err.(mssql.Error) +// return ok && msErr.Number == 0 +//} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go new file mode 100644 index 0000000..7eae74e --- /dev/null +++ b/pkg/database/postgres.go @@ -0,0 +1,15 @@ +package database + +import ( + "github.com/lib/pq" +) + +func PGIsUniqueViolation(err error) bool { + pqErr, ok := err.(*pq.Error) + return ok && pqErr.Code == "23505" +} + +func PGIsForeignKeyViolation(err error) bool { + pqErr, ok := err.(*pq.Error) + return ok && pqErr.Code == "23503" +} diff --git a/pkg/database/query.go b/pkg/database/query.go new file mode 100644 index 0000000..0afc222 --- /dev/null +++ b/pkg/database/query.go @@ -0,0 +1,44 @@ +package database + +import ( + "bytes" + "fmt" + "strings" + "sync" + "text/template" +) + +var queriesCache sync.Map + +func BuildQuery(text string, data map[string]interface{}) (string, []interface{}, error) { + var t *template.Template + v, ok := queriesCache.Load(text) + if !ok { + var err error + t, err = template.New("query").Parse(text) + if err != nil { + return "", nil, fmt.Errorf("could not parse sql query template: %w", err) + } + + queriesCache.Store(text, t) + } else { + t = v.(*template.Template) + } + + var wr bytes.Buffer + if err := t.Execute(&wr, data); err != nil { + return "", nil, fmt.Errorf("could not apply sql query data: %w", err) + } + + query := wr.String() + var args []interface{} + for key, val := range data { + if !strings.Contains(query, "@"+key) { + continue + } + + args = append(args, val) + query = strings.Replace(query, "@"+key, fmt.Sprintf("$%d", len(args)), -1) + } + return query, args, nil +} diff --git a/pkg/dt/time.go b/pkg/dt/time.go new file mode 100644 index 0000000..7505b65 --- /dev/null +++ b/pkg/dt/time.go @@ -0,0 +1,12 @@ +package dt + +import "time" + +func GetTime(t string) (time.Time, error) { + now := time.Now() + d, err := time.ParseDuration(t) + if err != nil { + return now, err + } + return now.Add(d), nil +} diff --git a/pkg/util/encrypt/aes.go b/pkg/encrypt/aes.go similarity index 96% rename from pkg/util/encrypt/aes.go rename to pkg/encrypt/aes.go index 5fd729b..d1115c9 100644 --- a/pkg/util/encrypt/aes.go +++ b/pkg/encrypt/aes.go @@ -70,6 +70,6 @@ func PKCS7Padding(ciphertext []byte, blockSize int) []byte { // PKCS7UnPadding 去码 func PKCS7UnPadding(origData []byte) []byte { length := len(origData) - unpadding := int(origData[length-1]) - return origData[:(length - unpadding)] + unPadding := int(origData[length-1]) + return origData[:(length - unPadding)] } diff --git a/pkg/util/encrypt/base64.go b/pkg/encrypt/base64.go similarity index 100% rename from pkg/util/encrypt/base64.go rename to pkg/encrypt/base64.go diff --git a/pkg/util/encrypt/des.go b/pkg/encrypt/des.go similarity index 100% rename from pkg/util/encrypt/des.go rename to pkg/encrypt/des.go diff --git a/pkg/encrypt/md5.go b/pkg/encrypt/md5.go new file mode 100644 index 0000000..2caccc2 --- /dev/null +++ b/pkg/encrypt/md5.go @@ -0,0 +1,12 @@ +package encrypt + +import ( + "crypto/md5" + "encoding/hex" +) + +func MD5(value string) string { + m := md5.New() + m.Write([]byte(value)) + return hex.EncodeToString(m.Sum(nil)) +} diff --git a/pkg/encrypt/password.go b/pkg/encrypt/password.go new file mode 100644 index 0000000..cc96854 --- /dev/null +++ b/pkg/encrypt/password.go @@ -0,0 +1,72 @@ +package encrypt + +import ( + "crypto/rand" + "encoding/hex" + "fmt" + "strings" + + "golang.org/x/crypto/bcrypt" + "golang.org/x/crypto/scrypt" +) + +// ******************** scrypt ******************** + +// ScryptHashPassword scrypt 加密 +// password 原始密码 +func ScryptHashPassword(password string) (string, error) { + // example for making salt - https://play.golang.org/p/_Aw6WeWC42I + salt := make([]byte, 32) + _, err := rand.Read(salt) + if err != nil { + return "", err + } + + // using recommended cost parameters from - https://godoc.org/golang.org/x/crypto/scrypt + shash, err := scrypt.Key([]byte(password), salt, 32768, 8, 1, 32) + if err != nil { + return "", err + } + + // return hex-encoded string with salt appended to password + hashedPW := fmt.Sprintf("%s.%s", hex.EncodeToString(shash), hex.EncodeToString(salt)) + + return hashedPW, nil +} + +// ScryptComparePassword 判断密码是否正确 +// storedPassword 加密密码 +// suppliedPassword 原始密码 +func ScryptComparePassword(storedPassword string, suppliedPassword string) (bool, error) { + pwsalt := strings.Split(storedPassword, ".") + + // check supplied password salted with hash + salt, err := hex.DecodeString(pwsalt[1]) + + if err != nil { + return false, fmt.Errorf("unable to verify user password") + } + + shash, err := scrypt.Key([]byte(suppliedPassword), salt, 32768, 8, 1, 32) + + return hex.EncodeToString(shash) == pwsalt[0], nil +} + +// ******************** bcrypt ******************** + +// BcryptHashPassword bcrypt 加密 +// password 原始密码 +func BcryptHashPassword(password string) (string, error) { + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + return "", fmt.Errorf("failed to hash password: %w", err) + } + return string(hashedPassword), nil +} + +// BcryptComparePassword 判断密码是否正确 +// hashedPassword 加密密码 +// password 原始密码 +func BcryptComparePassword(hashedPassword string, password string) error { + return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) +} diff --git a/pkg/util/encrypt/rsa.go b/pkg/encrypt/rsa.go similarity index 100% rename from pkg/util/encrypt/rsa.go rename to pkg/encrypt/rsa.go diff --git a/pkg/util/encrypt/sha.go b/pkg/encrypt/sha.go similarity index 100% rename from pkg/util/encrypt/sha.go rename to pkg/encrypt/sha.go diff --git a/pkg/env/env.go b/pkg/env/env.go new file mode 100644 index 0000000..b20b7c5 --- /dev/null +++ b/pkg/env/env.go @@ -0,0 +1,12 @@ +package env + +import "os" + +func GetEnvWithDefault(name, def string) string { + env := os.Getenv(name) + if len(env) != 0 { + return env + } + + return def +} diff --git a/pkg/fetcher/fetcher.go b/pkg/fetcher/fetcher.go new file mode 100644 index 0000000..44a13c0 --- /dev/null +++ b/pkg/fetcher/fetcher.go @@ -0,0 +1,80 @@ +package fetcher + +import ( + "bufio" + "bytes" + "errors" + "io" + "io/ioutil" + "log" + "net/http" + "time" + + "golang.org/x/net/html/charset" + "golang.org/x/text/encoding" + "golang.org/x/text/encoding/unicode" +) + +// Get get 请求 +func Get(url string) ([]byte, error) { + client := &http.Client{} + req, _ := http.NewRequest("GET", url, nil) + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + res, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + return res, nil +} + +// PostJson application/json post 请求 +func PostJson(url, parameter string, timeout int) ([]byte, error) { + client := &http.Client{Timeout: time.Second * time.Duration(timeout)} + byteParameter := bytes.NewBuffer([]byte(parameter)) + request, _ := http.NewRequest("POST", url, byteParameter) + request.Header.Set("Content-type", "application/json") + response, _ := client.Do(request) + if response.StatusCode != 200 { + return nil, errors.New("网络请求失败") + } + all, err := io.ReadAll(response.Body) + if err != nil { + return nil, errors.New("读取网络内容失败") + } + return all, nil +} + +// PostString application/x-www-form-urlencoded 请求 +func PostString(url, parameter string, timeout int) ([]byte, error) { + client := &http.Client{Timeout: time.Second * time.Duration(timeout)} + byteParameter := bytes.NewBuffer([]byte(parameter)) + request, _ := http.NewRequest("POST", url, byteParameter) + request.Header.Set("Content-type", "application/x-www-form-urlencoded") + response, _ := client.Do(request) + if response.StatusCode != 200 { + return nil, errors.New("网络请求失败") + } + all, err := ioutil.ReadAll(response.Body) + if err != nil { + return nil, errors.New("读取网络内容失败") + } + return all, nil +} + +func determinEncoding(r *bufio.Reader) encoding.Encoding { + bytes, err := r.Peek(1024) + if err != nil && err != io.EOF { + log.Printf("Fetcher error: %v", err) + return unicode.UTF8 + } + + e, _, _ := charset.DetermineEncoding(bytes, "") + return e +} diff --git a/pkg/util/file.go b/pkg/ft/file.go similarity index 97% rename from pkg/util/file.go rename to pkg/ft/file.go index 0d25def..c05fe0d 100644 --- a/pkg/util/file.go +++ b/pkg/ft/file.go @@ -1,4 +1,4 @@ -package util +package ft import "os" @@ -31,4 +31,4 @@ func MkDir(src string) error { return err } return nil -} \ No newline at end of file +} diff --git a/pkg/ip2region/ip2Region.go b/pkg/ip2region/ip2Region.go index de32f83..99bf466 100644 --- a/pkg/ip2region/ip2Region.go +++ b/pkg/ip2region/ip2Region.go @@ -13,19 +13,17 @@ const ( TOTAL_HEADER_LENGTH = 8192 ) -var err error -var ipInfo IpInfo +var IpRegion *Ip2Region type Ip2Region struct { // db file handler dbFileHandler *os.File - //header block info - headerSip []int64 - headerPtr []int64 - headerLen int64 + headerSip []int64 + headerPtr []int64 + headerLen int64 // super block index info firstIndexPtr int64 @@ -35,20 +33,20 @@ type Ip2Region struct { // for memory mode only // the original db binary string - dbBinStr []byte - dbFile string + dbBinStr []byte + dbFile string } type IpInfo struct { - CityId int64 - Country string - Region string - Province string - City string - ISP string + CityId int64 `json:"city_id"` + Country string `json:"country"` + Region string `json:"region"` + Province string `json:"province"` + City string `json:"city"` + ISP string `json:"isp"` } -func (ip IpInfo)String() string { +func (ip IpInfo) String() string { return strconv.FormatInt(ip.CityId, 10) + "|" + ip.Country + "|" + ip.Region + "|" + ip.Province + "|" + ip.City + "|" + ip.ISP } @@ -59,7 +57,7 @@ func getIpInfo(cityId int64, line []byte) IpInfo { length := len(lineSlice) ipInfo.CityId = cityId if length < 5 { - for i := 0; i <= 5 - length; i++ { + for i := 0; i <= 5-length; i++ { lineSlice = append(lineSlice, "") } } @@ -72,37 +70,39 @@ func getIpInfo(cityId int64, line []byte) IpInfo { return ipInfo } -func New(path string) (*Ip2Region, error) { +func New(path string) error { file, err := os.Open(path) if err != nil { - return nil, err + return err } - return &Ip2Region{ - dbFile:path, - dbFileHandler:file, - }, nil + IpRegion = &Ip2Region{ + dbFile: path, + dbFileHandler: file, + } + + return nil } -func (this *Ip2Region) Close() { - this.dbFileHandler.Close() +func (region *Ip2Region) Close() error { + return region.dbFileHandler.Close() } -func (this *Ip2Region) MemorySearch(ipStr string) (ipInfo IpInfo, err error) { +func (region *Ip2Region) MemorySearch(ipStr string) (ipInfo IpInfo, err error) { ipInfo = IpInfo{} - if this.totalBlocks == 0 { - this.dbBinStr, err = ioutil.ReadFile(this.dbFile) + if region.totalBlocks == 0 { + region.dbBinStr, err = ioutil.ReadFile(region.dbFile) if err != nil { return ipInfo, err } - this.firstIndexPtr = getLong(this.dbBinStr, 0) - this.lastIndexPtr = getLong(this.dbBinStr, 4) - this.totalBlocks = (this.lastIndexPtr - this.firstIndexPtr) /INDEX_BLOCK_LENGTH + 1 + region.firstIndexPtr = getLong(region.dbBinStr, 0) + region.lastIndexPtr = getLong(region.dbBinStr, 4) + region.totalBlocks = (region.lastIndexPtr-region.firstIndexPtr)/INDEX_BLOCK_LENGTH + 1 } ip, err := ip2long(ipStr) @@ -110,22 +110,22 @@ func (this *Ip2Region) MemorySearch(ipStr string) (ipInfo IpInfo, err error) { return ipInfo, err } - h := this.totalBlocks - var dataPtr, l int64; - for (l <= h) { + h := region.totalBlocks + var dataPtr, l int64 + for l <= h { m := (l + h) >> 1 - p := this.firstIndexPtr + m *INDEX_BLOCK_LENGTH - sip := getLong(this.dbBinStr, p) + p := region.firstIndexPtr + m*INDEX_BLOCK_LENGTH + sip := getLong(region.dbBinStr, p) if ip < sip { h = m - 1 } else { - eip := getLong(this.dbBinStr, p + 4) + eip := getLong(region.dbBinStr, p+4) if ip > eip { l = m + 1 } else { - dataPtr = getLong(this.dbBinStr, p + 8) - break; + dataPtr = getLong(region.dbBinStr, p+8) + break } } } @@ -134,26 +134,26 @@ func (this *Ip2Region) MemorySearch(ipStr string) (ipInfo IpInfo, err error) { } dataLen := ((dataPtr >> 24) & 0xFF) - dataPtr = (dataPtr & 0x00FFFFFF); - ipInfo = getIpInfo(getLong(this.dbBinStr, dataPtr), this.dbBinStr[(dataPtr) + 4:dataPtr + dataLen]) + dataPtr = (dataPtr & 0x00FFFFFF) + ipInfo = getIpInfo(getLong(region.dbBinStr, dataPtr), region.dbBinStr[(dataPtr)+4:dataPtr+dataLen]) return ipInfo, nil } -func (this *Ip2Region)BinarySearch(ipStr string) (ipInfo IpInfo, err error) { +func (region *Ip2Region) BinarySearch(ipStr string) (ipInfo IpInfo, err error) { ipInfo = IpInfo{} - if this.totalBlocks == 0 { - this.dbFileHandler.Seek(0, 0) + if region.totalBlocks == 0 { + region.dbFileHandler.Seek(0, 0) superBlock := make([]byte, 8) - this.dbFileHandler.Read(superBlock) - this.firstIndexPtr = getLong(superBlock, 0) - this.lastIndexPtr = getLong(superBlock, 4) - this.totalBlocks = (this.lastIndexPtr - this.firstIndexPtr) /INDEX_BLOCK_LENGTH + 1 + region.dbFileHandler.Read(superBlock) + region.firstIndexPtr = getLong(superBlock, 0) + region.lastIndexPtr = getLong(superBlock, 4) + region.totalBlocks = (region.lastIndexPtr-region.firstIndexPtr)/INDEX_BLOCK_LENGTH + 1 } var l, dataPtr, p int64 - h := this.totalBlocks + h := region.totalBlocks ip, err := ip2long(ipStr) @@ -161,21 +161,21 @@ func (this *Ip2Region)BinarySearch(ipStr string) (ipInfo IpInfo, err error) { return } - for (l <= h) { + for l <= h { m := (l + h) >> 1 p = m * INDEX_BLOCK_LENGTH - _, err = this.dbFileHandler.Seek(this.firstIndexPtr + p, 0) + _, err = region.dbFileHandler.Seek(region.firstIndexPtr+p, 0) if err != nil { return } buffer := make([]byte, INDEX_BLOCK_LENGTH) - _, err = this.dbFileHandler.Read(buffer) + _, err = region.dbFileHandler.Read(buffer) if err != nil { - + return ipInfo, err } sip := getLong(buffer, 0) if ip < sip { @@ -186,7 +186,7 @@ func (this *Ip2Region)BinarySearch(ipStr string) (ipInfo IpInfo, err error) { l = m + 1 } else { dataPtr = getLong(buffer, 8) - break; + break } } @@ -198,76 +198,76 @@ func (this *Ip2Region)BinarySearch(ipStr string) (ipInfo IpInfo, err error) { } dataLen := ((dataPtr >> 24) & 0xFF) - dataPtr = (dataPtr & 0x00FFFFFF); + dataPtr = (dataPtr & 0x00FFFFFF) - this.dbFileHandler.Seek(dataPtr, 0) + region.dbFileHandler.Seek(dataPtr, 0) data := make([]byte, dataLen) - this.dbFileHandler.Read(data) + region.dbFileHandler.Read(data) ipInfo = getIpInfo(getLong(data, 0), data[4:dataLen]) err = nil return } -func (this *Ip2Region) BtreeSearch(ipStr string) (ipInfo IpInfo, err error) { +func (region *Ip2Region) BtreeSearch(ipStr string) (ipInfo IpInfo, err error) { ipInfo = IpInfo{} ip, err := ip2long(ipStr) - if this.headerLen == 0 { - this.dbFileHandler.Seek(8, 0) + if region.headerLen == 0 { + region.dbFileHandler.Seek(8, 0) buffer := make([]byte, TOTAL_HEADER_LENGTH) - this.dbFileHandler.Read(buffer) - var idx int64; + region.dbFileHandler.Read(buffer) + var idx int64 for i := 0; i < TOTAL_HEADER_LENGTH; i += 8 { startIp := getLong(buffer, int64(i)) - dataPar := getLong(buffer, int64(i + 4)) + dataPar := getLong(buffer, int64(i+4)) if dataPar == 0 { break } - this.headerSip = append(this.headerSip, startIp) - this.headerPtr = append(this.headerPtr, dataPar) - idx ++; + region.headerSip = append(region.headerSip, startIp) + region.headerPtr = append(region.headerPtr, dataPar) + idx++ } - this.headerLen = idx + region.headerLen = idx } var l, sptr, eptr int64 - h := this.headerLen + h := region.headerLen for l <= h { - m := int64(l + h) >> 1 - if m < this.headerLen { - if ip == this.headerSip[m] { + m := int64(l+h) >> 1 + if m < region.headerLen { + if ip == region.headerSip[m] { if m > 0 { - sptr = this.headerPtr[m - 1] - eptr = this.headerPtr[m] + sptr = region.headerPtr[m-1] + eptr = region.headerPtr[m] } else { - sptr = this.headerPtr[m] - eptr = this.headerPtr[m + 1] + sptr = region.headerPtr[m] + eptr = region.headerPtr[m+1] } break } - if ip < this.headerSip[m] { + if ip < region.headerSip[m] { if m == 0 { - sptr = this.headerPtr[m] - eptr = this.headerPtr[m + 1] + sptr = region.headerPtr[m] + eptr = region.headerPtr[m+1] break - } else if ip > this.headerSip[m - 1] { - sptr = this.headerPtr[m - 1] - eptr = this.headerPtr[m] + } else if ip > region.headerSip[m-1] { + sptr = region.headerPtr[m-1] + eptr = region.headerPtr[m] break } h = m - 1 } else { - if m == this.headerLen - 1 { - sptr = this.headerPtr[m - 1] - eptr = this.headerPtr[m] + if m == region.headerLen-1 { + sptr = region.headerPtr[m-1] + eptr = region.headerPtr[m] break - } else if ip <= this.headerSip[m + 1] { - sptr = this.headerPtr[m ] - eptr = this.headerPtr[m + 1] + } else if ip <= region.headerSip[m+1] { + sptr = region.headerPtr[m] + eptr = region.headerPtr[m+1] break } l = m + 1 @@ -282,25 +282,25 @@ func (this *Ip2Region) BtreeSearch(ipStr string) (ipInfo IpInfo, err error) { } blockLen := eptr - sptr - this.dbFileHandler.Seek(sptr, 0) - index := make([]byte, blockLen +INDEX_BLOCK_LENGTH) - this.dbFileHandler.Read(index) + region.dbFileHandler.Seek(sptr, 0) + index := make([]byte, blockLen+INDEX_BLOCK_LENGTH) + region.dbFileHandler.Read(index) var dataptr int64 h = blockLen / INDEX_BLOCK_LENGTH l = 0 for l <= h { - m := int64(l + h) >> 1 + m := int64(l+h) >> 1 p := m * INDEX_BLOCK_LENGTH sip := getLong(index, p) if ip < sip { - h = m - 1; + h = m - 1 } else { - eip := getLong(index, p + 4) + eip := getLong(index, p+4) if ip > eip { l = m + 1 } else { - dataptr = getLong(index, p + 8) + dataptr = getLong(index, p+8) break } } @@ -314,19 +314,19 @@ func (this *Ip2Region) BtreeSearch(ipStr string) (ipInfo IpInfo, err error) { dataLen := (dataptr >> 24) & 0xFF dataPtr := dataptr & 0x00FFFFFF - this.dbFileHandler.Seek(dataPtr, 0) + region.dbFileHandler.Seek(dataPtr, 0) data := make([]byte, dataLen) - this.dbFileHandler.Read(data) + region.dbFileHandler.Read(data) ipInfo = getIpInfo(getLong(data, 0), data[4:]) return } func getLong(b []byte, offset int64) int64 { - val := (int64(b[offset ]) | - int64(b[offset + 1]) << 8 | - int64(b[offset + 2]) << 16 | - int64(b[offset + 3]) << 24) + val := (int64(b[offset]) | + int64(b[offset+1])<<8 | + int64(b[offset+2])<<16 | + int64(b[offset+3])<<24) return val @@ -341,9 +341,8 @@ func ip2long(IpStr string) (int64, error) { var sum int64 for i, n := range bits { bit, _ := strconv.ParseInt(n, 10, 64) - sum += bit << uint(24 - 8 * i) + sum += bit << uint(24-8*i) } return sum, nil } - diff --git a/pkg/ip2region/ip2Region_test.go b/pkg/ip2region/ip2Region_test.go deleted file mode 100644 index 5fb717c..0000000 --- a/pkg/ip2region/ip2Region_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package ip2region - -import ( - "testing" -) - -func BenchmarkBtreeSearch(B *testing.B) { - region, err := New("../../data/ip2region.db ") - if err != nil { - B.Error(err) - } - for i:=0;i 20 { - pageSize = 20 - } - return pageSize -} diff --git a/pkg/util/password.go b/pkg/util/password.go deleted file mode 100644 index 8237e44..0000000 --- a/pkg/util/password.go +++ /dev/null @@ -1,21 +0,0 @@ -package util - -import ( - "fmt" - - "golang.org/x/crypto/bcrypt" -) - -// HashPassword returns the bcrypt hash of the password -func HashPassword(password string) (string, error) { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) - if err != nil { - return "", fmt.Errorf("failed to hash password: %w", err) - } - return string(hashedPassword), nil -} - -// CheckPassword checks if the provided password is correct or not -func CheckPassword(password string, hashedPassword string) error { - return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) -} diff --git a/pkg/validator/phone.go b/pkg/validator/phone.go new file mode 100644 index 0000000..d5efe0e --- /dev/null +++ b/pkg/validator/phone.go @@ -0,0 +1,21 @@ +package validator + +import ( + "errors" + "regexp" + "strings" +) + +var ( + rxPhone = regexp.MustCompile(`^(13|14|15|16|17|18|19)\d{9}$`) + ErrPhoneFormat = errors.New("phone format error") +) + +func ValidateRxPhone(phone string) error { + phone = strings.TrimSpace(phone) + if !rxPhone.MatchString(phone) { + return ErrPhoneFormat + } + + return nil +}