From 7ae3719b8264469570b90ba9fbd9a8e5777f48cd Mon Sep 17 00:00:00 2001 From: fatedier Date: Thu, 22 Feb 2024 21:04:21 +0800 Subject: [PATCH] cleanup code (#4019) --- pkg/config/types/types.go | 9 ------ pkg/msg/ctl.go | 4 --- pkg/util/net/http.go | 24 --------------- pkg/util/net/websocket.go | 10 ------ pkg/util/util/util.go | 10 ------ pkg/util/util/util_test.go | 42 ------------------------- pkg/util/version/version.go | 26 ---------------- pkg/util/version/version_test.go | 53 -------------------------------- pkg/util/wait/backoff.go | 19 ------------ 9 files changed, 197 deletions(-) delete mode 100644 pkg/util/version/version_test.go diff --git a/pkg/config/types/types.go b/pkg/config/types/types.go index fac29d7..a6cd2e7 100644 --- a/pkg/config/types/types.go +++ b/pkg/config/types/types.go @@ -45,15 +45,6 @@ func NewBandwidthQuantity(s string) (BandwidthQuantity, error) { return q, nil } -func MustBandwidthQuantity(s string) BandwidthQuantity { - q := BandwidthQuantity{} - err := q.UnmarshalString(s) - if err != nil { - panic(err) - } - return q -} - func (q *BandwidthQuantity) Equal(u *BandwidthQuantity) bool { if q == nil && u == nil { return true diff --git a/pkg/msg/ctl.go b/pkg/msg/ctl.go index 5ccee4a..bf0c71a 100644 --- a/pkg/msg/ctl.go +++ b/pkg/msg/ctl.go @@ -42,7 +42,3 @@ func ReadMsgInto(c io.Reader, msg Message) (err error) { func WriteMsg(c io.Writer, msg interface{}) (err error) { return msgCtl.WriteMsg(c, msg) } - -func Pack(msg interface{}) (data []byte, err error) { - return msgCtl.Pack(msg) -} diff --git a/pkg/util/net/http.go b/pkg/util/net/http.go index 642d159..e9fc526 100644 --- a/pkg/util/net/http.go +++ b/pkg/util/net/http.go @@ -24,30 +24,6 @@ import ( "github.com/fatedier/frp/pkg/util/util" ) -type HTTPAuthWrapper struct { - h http.Handler - user string - passwd string -} - -func NewHTTPBasicAuthWrapper(h http.Handler, user, passwd string) http.Handler { - return &HTTPAuthWrapper{ - h: h, - user: user, - passwd: passwd, - } -} - -func (aw *HTTPAuthWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) { - user, passwd, hasAuth := r.BasicAuth() - if (aw.user == "" && aw.passwd == "") || (hasAuth && user == aw.user && passwd == aw.passwd) { - aw.h.ServeHTTP(w, r) - } else { - w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) - http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) - } -} - type HTTPAuthMiddleware struct { user string passwd string diff --git a/pkg/util/net/websocket.go b/pkg/util/net/websocket.go index 6a8d369..e642be0 100644 --- a/pkg/util/net/websocket.go +++ b/pkg/util/net/websocket.go @@ -4,7 +4,6 @@ import ( "errors" "net" "net/http" - "strconv" "golang.org/x/net/websocket" ) @@ -50,15 +49,6 @@ func NewWebsocketListener(ln net.Listener) (wl *WebsocketListener) { return } -func ListenWebsocket(bindAddr string, bindPort int) (*WebsocketListener, error) { - tcpLn, err := net.Listen("tcp", net.JoinHostPort(bindAddr, strconv.Itoa(bindPort))) - if err != nil { - return nil, err - } - l := NewWebsocketListener(tcpLn) - return l, nil -} - func (p *WebsocketListener) Accept() (net.Conn, error) { c, ok := <-p.acceptCh if !ok { diff --git a/pkg/util/util/util.go b/pkg/util/util/util.go index d2562b0..c7fd997 100644 --- a/pkg/util/util/util.go +++ b/pkg/util/util/util.go @@ -47,16 +47,6 @@ func RandIDWithLen(idLen int) (id string, err error) { return id[:idLen], nil } -// RandIDWithRandLen return a rand string with length between [start, end). -func RandIDWithRandLen(start, end int) (id string, err error) { - if start >= end { - err = fmt.Errorf("start should be less than end") - return - } - idLen := mathrand.Intn(end-start) + start - return RandIDWithLen(idLen) -} - func GetAuthKey(token string, timestamp int64) (key string) { md5Ctx := md5.New() md5Ctx.Write([]byte(token)) diff --git a/pkg/util/util/util_test.go b/pkg/util/util/util_test.go index cf76485..0061861 100644 --- a/pkg/util/util/util_test.go +++ b/pkg/util/util/util_test.go @@ -14,48 +14,6 @@ func TestRandId(t *testing.T) { assert.Equal(16, len(id)) } -func TestRandIDWithRandLen(t *testing.T) { - tests := []struct { - name string - start int - end int - expectErr bool - }{ - { - name: "start and end are equal", - start: 5, - end: 5, - expectErr: true, - }, - { - name: "start is less than end", - start: 5, - end: 10, - expectErr: false, - }, - { - name: "start is greater than end", - start: 10, - end: 5, - expectErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert := assert.New(t) - id, err := RandIDWithRandLen(tt.start, tt.end) - if tt.expectErr { - assert.Error(err) - } else { - assert.NoError(err) - assert.GreaterOrEqual(len(id), tt.start) - assert.Less(len(id), tt.end) - } - }) - } -} - func TestGetAuthKey(t *testing.T) { assert := assert.New(t) key := GetAuthKey("1234", 1488720000) diff --git a/pkg/util/version/version.go b/pkg/util/version/version.go index 1dc5904..27ed32e 100644 --- a/pkg/util/version/version.go +++ b/pkg/util/version/version.go @@ -14,34 +14,8 @@ package version -import ( - "strconv" - "strings" -) - var version = "0.54.0" func Full() string { return version } - -func getSubVersion(v string, position int) int64 { - arr := strings.Split(v, ".") - if len(arr) < 3 { - return 0 - } - res, _ := strconv.ParseInt(arr[position], 10, 64) - return res -} - -func Proto(v string) int64 { - return getSubVersion(v, 0) -} - -func Major(v string) int64 { - return getSubVersion(v, 1) -} - -func Minor(v string) int64 { - return getSubVersion(v, 2) -} diff --git a/pkg/util/version/version_test.go b/pkg/util/version/version_test.go deleted file mode 100644 index 2b4077c..0000000 --- a/pkg/util/version/version_test.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2016 fatedier, fatedier@gmail.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package version - -import ( - "fmt" - "strconv" - "strings" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestFull(t *testing.T) { - assert := assert.New(t) - version := Full() - arr := strings.Split(version, ".") - assert.Equal(3, len(arr)) - - proto, err := strconv.ParseInt(arr[0], 10, 64) - assert.NoError(err) - assert.True(proto >= 0) - - major, err := strconv.ParseInt(arr[1], 10, 64) - assert.NoError(err) - assert.True(major >= 0) - - minor, err := strconv.ParseInt(arr[2], 10, 64) - assert.NoError(err) - assert.True(minor >= 0) -} - -func TestVersion(t *testing.T) { - assert := assert.New(t) - proto := Proto(Full()) - major := Major(Full()) - minor := Minor(Full()) - parseVersion := fmt.Sprintf("%d.%d.%d", proto, major, minor) - version := Full() - assert.Equal(parseVersion, version) -} diff --git a/pkg/util/wait/backoff.go b/pkg/util/wait/backoff.go index e07c531..16d9dd6 100644 --- a/pkg/util/wait/backoff.go +++ b/pkg/util/wait/backoff.go @@ -16,7 +16,6 @@ package wait import ( "math/rand" - "sync" "time" "github.com/fatedier/frp/pkg/util/util" @@ -174,21 +173,3 @@ func Until(f func(), period time.Duration, stopCh <-chan struct{}) { return period }), true, stopCh) } - -func MergeAndCloseOnAnyStopChannel[T any](upstreams ...<-chan T) <-chan T { - out := make(chan T) - closeOnce := sync.Once{} - for _, upstream := range upstreams { - ch := upstream - go func() { - select { - case <-ch: - closeOnce.Do(func() { - close(out) - }) - case <-out: - } - }() - } - return out -}