From 72c61505178103c4f5c3b3502184bf48d7cc656f Mon Sep 17 00:00:00 2001
From: Li4n0 <34324462+Li4n0@users.noreply.github.com>
Date: Thu, 22 Apr 2021 16:40:22 +0800
Subject: [PATCH] fix some bug and standardize the code (#2)
---
frontend/src/views/Home.vue | 2 +-
frontend/src/views/logs/Http.vue | 1 +
internal/cli/cli.go | 7 ++---
internal/notice/ding.go | 11 +++----
internal/notice/lark.go | 7 +++--
internal/notice/slack.go | 7 +++--
internal/notice/weixin.go | 9 +++---
internal/qqwry/query.go | 5 ++--
internal/rule/rule.go | 50 ++++++++++++--------------------
pkg/server/router.go | 1 -
10 files changed, 45 insertions(+), 55 deletions(-)
diff --git a/frontend/src/views/Home.vue b/frontend/src/views/Home.vue
index 44553c4..f9d82a4 100644
--- a/frontend/src/views/Home.vue
+++ b/frontend/src/views/Home.vue
@@ -1,6 +1,6 @@
-

+
diff --git a/frontend/src/views/logs/Http.vue b/frontend/src/views/logs/Http.vue
index e37364c..4326286 100644
--- a/frontend/src/views/logs/Http.vue
+++ b/frontend/src/views/logs/Http.vue
@@ -159,6 +159,7 @@ export default {
data: [],
pagination: {current: 1},
filters: {},
+ order: "desc",
loading: false,
columns,
colors
diff --git a/internal/cli/cli.go b/internal/cli/cli.go
index abfb9c5..7ad4ef2 100644
--- a/internal/cli/cli.go
+++ b/internal/cli/cli.go
@@ -1,7 +1,6 @@
package cli
import (
- "io/ioutil"
"os"
"sort"
@@ -35,12 +34,12 @@ func Start() {
},
Action: func(c *cli.Context) error {
conf := &server.Config{}
- if content, err := ioutil.ReadFile("config.yaml"); err == nil {
+ if content, err := os.ReadFile("config.yaml"); err == nil {
if err := yaml.Unmarshal(content, conf); err != nil {
- log.Fatal(err.Error())
+ return err
}
} else {
- log.Fatal(err.Error())
+ return err
}
if c.String("addr") != "" {
conf.Addr = c.String("addr")
diff --git a/internal/notice/ding.go b/internal/notice/ding.go
index 3a548cb..b3e31ae 100644
--- a/internal/notice/ding.go
+++ b/internal/notice/ding.go
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/li4n0/revsuit/internal/record"
+ "github.com/pkg/errors"
)
var _ Bot = (*DingTalk)(nil)
@@ -37,8 +38,8 @@ func (d *DingTalk) buildPayload(r record.Record) string {
MsgType: "markdown",
Markdown: dingMarkdown{
Title: "New Connection",
- Text: "**New Connection**\n" +
- formatRecordField(r, "> **%s: **%v\n"),
+ Text: "**New Connection**\n" +
+ formatRecordField(r, "> **%s: **%v\n"),
},
At: []dingAt{
{
@@ -54,16 +55,16 @@ func (d *DingTalk) buildPayload(r record.Record) string {
}
func (d *DingTalk) notice(r record.Record) error {
- resp, err := http.DefaultClient.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
+ resp, err := http.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
if err != nil {
- return fmt.Errorf("HTTP request: %v", err)
+ return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
- return fmt.Errorf("read HTTP response body: %v", err)
+ return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}
diff --git a/internal/notice/lark.go b/internal/notice/lark.go
index a2816ea..819a388 100644
--- a/internal/notice/lark.go
+++ b/internal/notice/lark.go
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/li4n0/revsuit/internal/record"
+ "github.com/pkg/errors"
)
var _ Bot = (*Lark)(nil)
@@ -69,16 +70,16 @@ func (d *Lark) buildPayload(r record.Record) string {
}
func (d *Lark) notice(r record.Record) error {
- resp, err := http.DefaultClient.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
+ resp, err := http.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
if err != nil {
- return fmt.Errorf("HTTP request: %v", err)
+ return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
- return fmt.Errorf("read HTTP response body: %v", err)
+ return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}
diff --git a/internal/notice/slack.go b/internal/notice/slack.go
index f296dfa..f38dec1 100644
--- a/internal/notice/slack.go
+++ b/internal/notice/slack.go
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/li4n0/revsuit/internal/record"
+ "github.com/pkg/errors"
)
var _ Bot = (*Slack)(nil)
@@ -67,16 +68,16 @@ func (d *Slack) buildPayload(r record.Record) string {
}
func (d *Slack) notice(r record.Record) error {
- resp, err := http.DefaultClient.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
+ resp, err := http.Post(d.URL, "application/json", strings.NewReader(d.buildPayload(r)))
if err != nil {
- return fmt.Errorf("HTTP request: %v", err)
+ return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
- return fmt.Errorf("read HTTP response body: %v", err)
+ return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}
diff --git a/internal/notice/weixin.go b/internal/notice/weixin.go
index e0182f0..f418e09 100644
--- a/internal/notice/weixin.go
+++ b/internal/notice/weixin.go
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/li4n0/revsuit/internal/record"
+ "github.com/pkg/errors"
)
var _ Bot = (*Weixin)(nil)
@@ -31,7 +32,7 @@ func (w *Weixin) buildPayload(r record.Record) string {
ToUser: "@all",
MsgType: "markdown",
Markdown: weixinMarkdown{
- Content: "New Connection\n" +
+ Content: "New Connection\n" +
formatRecordField(r, `> **%s: **%v`),
},
}
@@ -43,15 +44,15 @@ func (w *Weixin) buildPayload(r record.Record) string {
}
func (w *Weixin) notice(r record.Record) error {
- resp, err := http.DefaultClient.Post(w.URL, "application/json", strings.NewReader(w.buildPayload(r)))
+ resp, err := http.Post(w.URL, "application/json", strings.NewReader(w.buildPayload(r)))
if err != nil {
- return fmt.Errorf("HTTP request: %v", err)
+ return errors.Wrap(err, "HTTP request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
- return fmt.Errorf("read HTTP response body: %v", err)
+ return errors.Wrap(err, "read HTTP response body")
}
return fmt.Errorf("non-success response status code %d with body: %s", resp.StatusCode, data)
}
diff --git a/internal/qqwry/query.go b/internal/qqwry/query.go
index d68d553..be31a48 100644
--- a/internal/qqwry/query.go
+++ b/internal/qqwry/query.go
@@ -2,13 +2,12 @@ package qqwry
// Area return IpArea according to ip
func Area(ip string) string {
- ipData := GetQQWry().SearchByIPv4(ip)
if GetQQWry() == nil {
return ""
}
+ ipData := GetQQWry().SearchByIPv4(ip)
if ipData.Area == " CZ88.NET" {
return ipData.Country
- } else {
- return ipData.Country + " " + ipData.Area
}
+ return ipData.Country + " " + ipData.Area
}
diff --git a/internal/rule/rule.go b/internal/rule/rule.go
index 7365a10..b21e150 100644
--- a/internal/rule/rule.go
+++ b/internal/rule/rule.go
@@ -25,41 +25,29 @@ type BaseRule struct {
Notice bool `gorm:"default:false;not null;" form:"notice" json:"notice"`
}
-func compileCatcher(flagFormat string) (reg *regexp.Regexp, err error) {
- if reg, err := regexp.Compile(flagFormat); err == nil {
- return reg, nil
- } else {
- // * meaning record all connections.
- if flagFormat != "*" {
- return nil, err
- } else {
- return nil, nil
- }
- }
-}
-
func (br BaseRule) Match(s string) (flag, flagGroup string) {
- if br.flagCatcher == nil && br.FlagFormat != "*" {
- // compile rule flags
- catcher, err := compileCatcher(br.FlagFormat)
- if err != nil {
- log.Error("%s(rule:%s)", err.Error(), br.Name)
+ if br.flagCatcher == nil {
+ if br.FlagFormat == "*" {
+ flag = "*"
+ return
+ } else {
+ if catcher, err := regexp.Compile(br.FlagFormat); err != nil {
+ log.Error("%s(rule:%s)", err.Error(), br.Name)
+ return
+ } else {
+ br.flagCatcher = catcher
+ }
}
- br.flagCatcher = catcher
}
- if br.flagCatcher == nil {
- // capture all connection.
- flag = "*"
- } else {
- matched := br.flagCatcher.FindStringSubmatch(s)
- if len(matched) == 0 {
- return
- }
- flag = matched[0]
- if len(matched) > 1 {
- flagGroup = matched[1]
- }
+ matched := br.flagCatcher.FindStringSubmatch(s)
+ if len(matched) == 0 {
+ return
+ }
+
+ flag = matched[0]
+ if len(matched) > 1 {
+ flagGroup = matched[1]
}
return flag, flagGroup
}
diff --git a/pkg/server/router.go b/pkg/server/router.go
index 7fce689..f6c858d 100644
--- a/pkg/server/router.go
+++ b/pkg/server/router.go
@@ -26,7 +26,6 @@ func (revsuit *Revsuit) registerPlatformRouter() {
if !(c.Request.Header.Get("Token") == revsuit.http.Token || err == nil && cookieToken.Value == revsuit.http.Token) {
c.Abort()
c.Status(403)
- return
}
})
revsuit.http.ApiGroup = api