Skip to content

Commit

Permalink
cleanup code (fatedier#4019)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier authored Feb 22, 2024
1 parent 80cfd09 commit 7ae3719
Show file tree
Hide file tree
Showing 9 changed files with 0 additions and 197 deletions.
9 changes: 0 additions & 9 deletions pkg/config/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions pkg/msg/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 0 additions & 24 deletions pkg/util/net/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions pkg/util/net/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"net"
"net/http"
"strconv"

"golang.org/x/net/websocket"
)
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 0 additions & 10 deletions pkg/util/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
42 changes: 0 additions & 42 deletions pkg/util/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 0 additions & 26 deletions pkg/util/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
53 changes: 0 additions & 53 deletions pkg/util/version/version_test.go

This file was deleted.

19 changes: 0 additions & 19 deletions pkg/util/wait/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package wait

import (
"math/rand"
"sync"
"time"

"github.com/fatedier/frp/pkg/util/util"
Expand Down Expand Up @@ -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
}

0 comments on commit 7ae3719

Please sign in to comment.