From cdf8c0c1b5ed613bdfb88e80ef03467125caeddd Mon Sep 17 00:00:00 2001 From: jager Date: Mon, 7 Feb 2022 18:15:01 +0800 Subject: [PATCH] m --- internal/api/api.go | 15 +++---- internal/apity/apity.go | 3 ++ internal/apity/apity_test.go | 2 +- internal/consts/consts.go | 2 +- internal/service/almanac/almanac.go | 4 +- internal/service/network/network.go | 63 +++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 internal/service/network/network.go diff --git a/internal/api/api.go b/internal/api/api.go index 00712f8..14f18d6 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -13,18 +13,16 @@ package api import ( + "github.com/gin-gonic/gin" "github.com/jageros/hawox/jwt" "idata/internal/service/almanac" "idata/internal/service/dirty" + "idata/internal/service/network" "idata/internal/service/soup" "idata/internal/service/user" - "net/http" - - "github.com/gin-gonic/gin" ) func Registry(engine *gin.Engine) { - engine.GET("/my-ip", myIp) r := engine.Group("/haw") ur := r.Group("/user") ur.POST("/register", user.Register) @@ -37,10 +35,7 @@ func Registry(engine *gin.Engine) { apiR.GET("/soup", soup.GetRandSoup) apiR.GET("/almanac", almanac.Almanac) apiR.GET("/dirty_word", dirty.IsDirtyWord) - apiR.Static("/video", "./static/") -} - -func myIp(ctx *gin.Context) { - addr := ctx.ClientIP() - ctx.String(http.StatusOK, addr) + apiR.GET("/my-ip", network.MyIp) + apiR.GET("/dns", network.Domain2IP) + apiR.GET("/domain", network.IP2Domain) } diff --git a/internal/apity/apity.go b/internal/apity/apity.go index 319510e..f78db0c 100644 --- a/internal/apity/apity.go +++ b/internal/apity/apity.go @@ -22,6 +22,9 @@ const ( Obscenity // 3:脏词检测 Almanac // 4:黄历 Dirty // 5:脏词检测 + SelfIp // 6:客户端自身ip + DomainIP // 7:域名的ip + IPDomain // 8:ip的域名 ) func Type(ty string) ApiType { diff --git a/internal/apity/apity_test.go b/internal/apity/apity_test.go index 1fe3b1f..5109aab 100644 --- a/internal/apity/apity_test.go +++ b/internal/apity/apity_test.go @@ -19,7 +19,7 @@ import ( func TestApiType(t *testing.T) { var opened int64 = 7 - opened = opened & int64(Astro) + opened = opened & int64(Obscenity) //opened = opened | int64(Obscenity) //opened = opened | int64(Weather) fmt.Printf("opened = %b\n", opened) diff --git a/internal/consts/consts.go b/internal/consts/consts.go index 7ee90cc..c47ed21 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -13,6 +13,6 @@ package consts const ( - DateFormat = "2006-01-02" + DateFormat = "2006-01-02_15" TimeFormat = "2006-01-02 15:04:05" ) diff --git a/internal/service/almanac/almanac.go b/internal/service/almanac/almanac.go index ddb27e4..aaccda7 100644 --- a/internal/service/almanac/almanac.go +++ b/internal/service/almanac/almanac.go @@ -29,8 +29,8 @@ func Almanac(ctx *gin.Context) { if !user.Auth(ctx, apity.Almanac) { return } - date, ok := httpx.DecodeUrlVal(ctx, "date") - if !ok || date == "" { + date := ctx.Query("date") + if date == "" { date = time.Now().Format(consts.DateFormat) } strs := strings.Split(date, "_") diff --git a/internal/service/network/network.go b/internal/service/network/network.go new file mode 100644 index 0000000..21770e0 --- /dev/null +++ b/internal/service/network/network.go @@ -0,0 +1,63 @@ +/** + * @Author: jager + * @Email: lhj168os@gmail.com + * @File: network + * @Date: 2022/2/7 5:35 下午 + * @package: network + * @Version: v1.0.0 + * + * @Description: + * + */ + +package network + +import ( + "github.com/gin-gonic/gin" + "github.com/jageros/hawox/errcode" + "github.com/jageros/hawox/httpx" + "idata/internal/apity" + "idata/internal/service/user" + "net" + "net/http" +) + +func MyIp(ctx *gin.Context) { + if !user.Auth(ctx, apity.SelfIp) { + return + } + addr := ctx.ClientIP() + ctx.String(http.StatusOK, addr) +} + +func Domain2IP(ctx *gin.Context) { + if !user.Auth(ctx, apity.DomainIP) { + return + } + domain, ok := httpx.DecodeUrlVal(ctx, "domain") + if !ok { + return + } + addrs, err := net.LookupHost(domain) + if err != nil { + httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err)) + return + } + httpx.PkgMsgWrite(ctx, map[string]interface{}{"addrs": addrs}) +} + +func IP2Domain(ctx *gin.Context) { + if !user.Auth(ctx, apity.IPDomain) { + return + } + ip, ok := httpx.DecodeUrlVal(ctx, "ip") + if !ok { + return + } + names, err := net.LookupAddr(ip) + if err != nil { + httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err)) + return + } + httpx.PkgMsgWrite(ctx, map[string]interface{}{"names": names}) +}