diff --git a/cmd/goforward/root.go b/cmd/goforward/root.go index b347298..00c48a6 100644 --- a/cmd/goforward/root.go +++ b/cmd/goforward/root.go @@ -63,7 +63,7 @@ var rootCmd = &cobra.Command{ if err != nil { return err } - err = runServer(*cfg) + err = runServer(cfg) if err != nil { fmt.Println(err) os.Exit(1) @@ -79,7 +79,7 @@ func Execute() { } } -func runServer(cfg config.Config) (err error) { +func runServer(cfg *config.Config) (err error) { log.InitLog(cfg.LogWay, cfg.LogFile, cfg.LogLevel, cfg.LogMaxDays, cfg.DisableLogColor) if cfgFile != "" { diff --git a/internal/api/forward/add.go b/internal/api/forward/add.go index 62f2006..badba1e 100644 --- a/internal/api/forward/add.go +++ b/internal/api/forward/add.go @@ -16,7 +16,7 @@ type AddForwardRequest struct { func Add(c *gin.Context) { var request AddForwardRequest c.ShouldBindJSON(&request) - if len(request.ForwardLink) == 0 { + if request.ForwardLink == "" { c.JSON(400, gin.H{"error": "forward link is empty"}) return } diff --git a/internal/api/forward/del.go b/internal/api/forward/del.go index 596406a..8e3d6c4 100644 --- a/internal/api/forward/del.go +++ b/internal/api/forward/del.go @@ -11,7 +11,7 @@ import ( func Del(c *gin.Context) { m := manager.GetInstance() listenAddr := c.Param("listen_addr") - if len(listenAddr) == 0 { + if listenAddr == "" { c.JSON(400, gin.H{"error": "listen_addr is empty"}) return } diff --git a/internal/constants/version.go b/internal/constants/version.go index cbdb547..3af3010 100644 --- a/internal/constants/version.go +++ b/internal/constants/version.go @@ -1,3 +1,3 @@ package constants -const VERSION = "0.1.0" +const VERSION = "0.1.1" diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 760080b..5b96783 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -11,10 +11,10 @@ type Manager struct { ForwardMap map[string]*forward.Forward } -func (m Manager) AddForward(ForwardLinks models.ForwardLinks, override bool) error { - for _, i := range ForwardLinks { +func (m Manager) AddForward(forwardLinks models.ForwardLinks, override bool) error { + for _, i := range forwardLinks { key := i.ListenAddr.String() - forward := forward.NewForward(i) + forwardIns := forward.NewForward(i) if f, ok := m.ForwardMap[key]; ok { if override { err := f.Stop() @@ -25,11 +25,11 @@ func (m Manager) AddForward(ForwardLinks models.ForwardLinks, override bool) err return fmt.Errorf("forward %s already exists, if want to override, set param override to true", key) } } - err := forward.Start() + err := forwardIns.Start() if err != nil { return fmt.Errorf("forward %s start err: %s", key, err) } - m.ForwardMap[key] = forward + m.ForwardMap[key] = forwardIns } return nil } diff --git a/internal/pkg/forward/forward.go b/internal/pkg/forward/forward.go index 36bb554..cb1b925 100644 --- a/internal/pkg/forward/forward.go +++ b/internal/pkg/forward/forward.go @@ -111,9 +111,9 @@ func (f *Forward) MarshalJSON() ([]byte, error) { }) } -func NewForward(ForwardLink models.ForwardLink) *Forward { +func NewForward(forwardLink models.ForwardLink) *Forward { return &Forward{ - Link: ForwardLink, + Link: forwardLink, quit: make(chan bool, 1), } } diff --git a/internal/pkg/log/log.go b/internal/pkg/log/log.go index 631ed47..c3321da 100644 --- a/internal/pkg/log/log.go +++ b/internal/pkg/log/log.go @@ -16,14 +16,14 @@ func init() { Log.SetLogFuncCallDepth(Log.GetLogFuncCallDepth() + 1) } -func InitLog(logWay string, logFile string, logLevel string, maxdays int64, disableLogColor bool) { +func InitLog(logWay, logFile, logLevel string, maxdays int64, disableLogColor bool) { SetLogFile(logWay, logFile, maxdays, disableLogColor) SetLogLevel(logLevel) } // SetLogFile to configure log params // logWay: file or console -func SetLogFile(logWay string, logFile string, maxdays int64, disableLogColor bool) { +func SetLogFile(logWay, logFile string, maxdays int64, disableLogColor bool) { if logWay == "console" { params := "" if disableLogColor { @@ -31,7 +31,7 @@ func SetLogFile(logWay string, logFile string, maxdays int64, disableLogColor bo } Log.SetLogger("console", params) } else { - params := fmt.Sprintf(`{"filename": "%s", "maxdays": %d}`, logFile, maxdays) + params := fmt.Sprintf(`{"filename": "%q", "maxdays": %d}`, logFile, maxdays) Log.SetLogger("file", params) } } diff --git a/internal/util/util.go b/internal/util/util.go deleted file mode 100644 index 6cf1e72..0000000 --- a/internal/util/util.go +++ /dev/null @@ -1,52 +0,0 @@ -package util - -import ( - "fmt" - "strconv" - "strings" -) - -func ParseRangeNumbers(rangeStr string) (numbers []int64, err error) { - rangeStr = strings.TrimSpace(rangeStr) - numbers = make([]int64, 0) - // e.g. 1000-2000,2001,2002,3000-4000 - numRanges := strings.Split(rangeStr, ",") - for _, numRangeStr := range numRanges { - // 1000-2000 or 2001 - numArray := strings.Split(numRangeStr, "-") - // length: only 1 or 2 is correct - rangeType := len(numArray) - if rangeType == 1 { - // single number - singleNum, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64) - if errRet != nil { - err = fmt.Errorf("range number is invalid, %v", errRet) - return - } - numbers = append(numbers, singleNum) - } else if rangeType == 2 { - // range numbers - min, errRet := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64) - if errRet != nil { - err = fmt.Errorf("range number is invalid, %v", errRet) - return - } - max, errRet := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64) - if errRet != nil { - err = fmt.Errorf("range number is invalid, %v", errRet) - return - } - if max < min { - err = fmt.Errorf("range number is invalid") - return - } - for i := min; i <= max; i++ { - numbers = append(numbers, i) - } - } else { - err = fmt.Errorf("range number is invalid") - return - } - } - return -} diff --git a/internal/util/util_test.go b/internal/util/util_test.go deleted file mode 100644 index 2254d51..0000000 --- a/internal/util/util_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package util - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestParseRangeNumbers(t *testing.T) { - assert := assert.New(t) - numbers, err := ParseRangeNumbers("2-5") - if assert.NoError(err) { - assert.Equal([]int64{2, 3, 4, 5}, numbers) - } - - numbers, err = ParseRangeNumbers("1") - if assert.NoError(err) { - assert.Equal([]int64{1}, numbers) - } - - numbers, err = ParseRangeNumbers("3-5,8") - if assert.NoError(err) { - assert.Equal([]int64{3, 4, 5, 8}, numbers) - } - - numbers, err = ParseRangeNumbers(" 3-5,8, 10-12 ") - if assert.NoError(err) { - assert.Equal([]int64{3, 4, 5, 8, 10, 11, 12}, numbers) - } - - _, err = ParseRangeNumbers("3-a") - assert.Error(err) -}