Skip to content

Commit

Permalink
feat: use random string instead of sqids
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed Mar 23, 2024
1 parent c7ad517 commit de73bca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/damonto/estkme-rlpa-server

go 1.22.1

require github.com/sqids/sqids-go v0.4.1
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
github.com/sqids/sqids-go v0.4.1 h1:eQKYzmAZbLlRwHeHYPF35QhgxwZHLnlmVj9AkIj/rrw=
github.com/sqids/sqids-go v0.4.1/go.mod h1:EMwHuPQgSNFS0A49jESTfIQS+066XQTVhukrzEPScl8=
22 changes: 12 additions & 10 deletions internal/rlpa/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"math/rand/v2"
"net"
"time"

"github.com/sqids/sqids-go"
)

type Server interface {
Expand Down Expand Up @@ -52,7 +50,7 @@ func (s *server) Listen(address string) error {
}

func (s *server) handleConn(tcpConn *net.TCPConn) {
id := s.id(tcpConn)
id := s.id()
conn := NewConn(id, tcpConn)
s.manager.Add(id, conn)
slog.Info("new connection from", "id", id)
Expand Down Expand Up @@ -82,13 +80,17 @@ func (s *server) handleConn(tcpConn *net.TCPConn) {
}
}

func (s *server) id(tcpConn *net.TCPConn) string {
sqid, _ := sqids.New(sqids.Options{
MinLength: 8,
})
netAddr, _ := net.ResolveTCPAddr("tcp", tcpConn.RemoteAddr().String())
id, _ := sqid.Encode([]uint64{uint64(netAddr.Port), rand.Uint64N(10000)})
return id
func (s *server) id() string {
seeds := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
id := make([]rune, 6)
for i := range id {
id[i] = seeds[rand.IntN(len(seeds))]
}
if _, err := s.manager.Get(string(id)); err.Error() == ErrConnNotFound {
return string(id)
} else {
return s.id()
}
}

func (s *server) Shutdown() error {
Expand Down

0 comments on commit de73bca

Please sign in to comment.