Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Oct 29, 2023
1 parent 7c647f2 commit c9090bd
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 53 deletions.
4 changes: 2 additions & 2 deletions cmd/goatak_server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ func getConnHandler(app *App) func(req *air.Request, res *air.Response) error {
app.ForAllClients(func(ch client.ClientHandler) bool {
c := &Connection{
Uids: ch.GetUids(),
User: ch.GetUser(),
User: ch.GetUser().Login,
Ver: ch.GetVersion(),
Addr: ch.GetName(),
Scope: ch.GetScope(),
Scope: ch.GetUser().Scope,
LastSeen: ch.GetLastSeen(),
}
conn = append(conn, c)
Expand Down
2 changes: 1 addition & 1 deletion cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (app *App) RemoveHandlerCb(cl client.ClientHandler) {
}
msg := &cot.CotMessage{
From: cl.GetName(),
Scope: cl.GetScope(),
Scope: cl.GetUser().Scope,
TakMessage: cot.MakeOfflineMsg(uid, ""),
}
app.NewCotMessage(msg)
Expand Down
7 changes: 1 addition & 6 deletions cmd/goatak_server/tcpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,11 @@ func (app *App) listenTls(addr string) error {

st := c1.ConnectionState()
username, serial := getCertUser(&st)
var scope string
if user := app.users.GetUser(username); user != nil {
scope = user.Scope
}

name := "ssl:" + conn.RemoteAddr().String()
h := client.NewConnClientHandler(name, conn, &client.HandlerConfig{
Logger: app.Logger.With(zap.String("user", username), zap.String("addr", name)),
User: username,
Scope: scope,
User: app.users.GetUser(username),
Serial: serial,
MessageCb: app.NewCotMessage,
RemoveCb: app.RemoveHandlerCb,
Expand Down
9 changes: 3 additions & 6 deletions cmd/goatak_server/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/kdudkov/goatak/internal/model"
"strings"
"sync"
"sync/atomic"
Expand All @@ -29,12 +30,8 @@ type WsClientHandler struct {
func (w *WsClientHandler) GetName() string {
return w.name
}
func (w *WsClientHandler) GetUser() string {
return ""
}

func (w *WsClientHandler) GetScope() string {
return ""
func (w *WsClientHandler) GetUser() *model.User {
return nil
}

func (w *WsClientHandler) CanSeeScope(scope string) bool {
Expand Down
12 changes: 6 additions & 6 deletions cmd/userctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import (
"github.com/kdudkov/goatak/internal/model"
)

func read(fn string) []*model.UserInfo {
func read(fn string) []*model.User {
dat, err := os.ReadFile(fn)
if err != nil {
return nil
}

users := make([]*model.UserInfo, 0)
users := make([]*model.User, 0)
if err := yaml.Unmarshal(dat, &users); err != nil {
panic(err.Error())
}

return users
}

func write(fn string, users []*model.UserInfo) error {
func write(fn string, users []*model.User) error {
f, err := os.Create(fn)
if err != nil {
return err
Expand All @@ -50,7 +50,7 @@ func main() {

if *user == "" {
for _, user := range users {
fmt.Printf("%s\t%s\t%s\t%s\n", user.User, user.Callsign, user.Team, user.Role)
fmt.Printf("%s\t%s\t%s\t%s\n", user.Login, user.Callsign, user.Team, user.Role)
}
return
}
Expand All @@ -75,7 +75,7 @@ func main() {
var found bool

for _, u := range users {
if u.User == *user {
if u.Login == *user {
found = true
u.Password = string(bpass)
if *scope != "" {
Expand All @@ -86,7 +86,7 @@ func main() {
}

if !found {
users = append(users, &model.UserInfo{User: *user, Password: string(bpass), Scope: *scope})
users = append(users, &model.User{Login: *user, Password: string(bpass), Scope: *scope})
}

if err := write(*file, users); err != nil {
Expand Down
9 changes: 3 additions & 6 deletions cmd/webclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"flag"
"fmt"
"math/rand"
Expand Down Expand Up @@ -206,12 +207,8 @@ func (app *App) IsConnected() bool {
}

func makeUid(callsign string) string {
h := md5.New()
h.Write([]byte(callsign))
uid := fmt.Sprintf("%x", h.Sum(nil))
uid = uid[len(uid)-14:]

return "ANDROID-" + uid
s := hex.EncodeToString(md5.New().Sum([]byte(callsign)))
return "ANDROID-" + s[:16]
}

func (app *App) myPosSender(ctx context.Context, wg *sync.WaitGroup) {
Expand Down
15 changes: 15 additions & 0 deletions cmd/webclient/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestMakeUid(t *testing.T) {
c := makeUid("s1")
fmt.Println(c)
assert.Equal(t, 24, len(c))
assert.Equal(t, makeUid("string1"), makeUid("string1"))
assert.NotEqual(t, makeUid("s1"), makeUid("s2"))
}
24 changes: 10 additions & 14 deletions internal/client/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/xml"
"fmt"
"github.com/kdudkov/goatak/internal/model"
"github.com/kdudkov/goatak/pkg/cot"
"io"
"net"
Expand All @@ -23,8 +24,7 @@ const (
)

type HandlerConfig struct {
User string
Scope string
User *model.User
Serial string
Uid string
IsClient bool
Expand All @@ -38,8 +38,7 @@ type ClientHandler interface {
GetName() string
HasUid(uid string) bool
GetUids() map[string]string
GetUser() string
GetScope() string
GetUser() *model.User
CanSeeScope(scope string) bool
GetVersion() int32
SendMsg(msg *cotproto.TakMessage) error
Expand All @@ -59,8 +58,7 @@ type ConnClientHandler struct {
closeTimer *time.Timer
sendChan chan []byte
active int32
user string
scope string
user *model.User
serial string
messageCb func(msg *cot.CotMessage)
removeCb func(ch ClientHandler)
Expand All @@ -83,7 +81,6 @@ func NewConnClientHandler(name string, conn net.Conn, config *HandlerConfig) *Co

if config != nil {
c.user = config.User
c.scope = config.Scope
c.serial = config.Serial
c.localUid = config.Uid
if config.Logger != nil {
Expand All @@ -102,16 +99,15 @@ func (h *ConnClientHandler) GetName() string {
return h.addr
}

func (h *ConnClientHandler) GetUser() string {
func (h *ConnClientHandler) GetUser() *model.User {
return h.user
}

func (h *ConnClientHandler) GetScope() string {
return h.scope
}

func (h *ConnClientHandler) CanSeeScope(scope string) bool {
return h.scope == "" || scope == "broadcast" || h.scope == scope
if h.user == nil {
return true
}
return h.user.Scope == "" || scope == "broadcast" || h.user.Scope == scope
}

func (h *ConnClientHandler) GetUids() map[string]string {
Expand Down Expand Up @@ -202,7 +198,7 @@ func (h *ConnClientHandler) handleRead() {

cotmsg := &cot.CotMessage{
From: h.addr,
Scope: h.scope,
Scope: h.user.Scope,
TakMessage: msg,
Detail: d,
}
Expand Down
4 changes: 2 additions & 2 deletions internal/model/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package model

type UserInfo struct {
User string `yaml:"user"`
type User struct {
Login string `yaml:"user"`
Callsign string `yaml:"callsign,omitempty"`
Team string `yaml:"team,omitempty"`
Role string `yaml:"role,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion internal/repository/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type UserRepository interface {
Stop()
CheckUserAuth(user, password string) bool
UserIsValid(user, sn string) bool
GetUser(username string) *int.UserInfo
GetUser(username string) *int.User
}

type ItemsRepository interface {
Expand Down
18 changes: 9 additions & 9 deletions internal/repository/user_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type UserFileRepository struct {
userFile string
logger *zap.SugaredLogger
users map[string]*model.UserInfo
users map[string]*model.User

watcher *fsnotify.Watcher

Expand All @@ -26,7 +26,7 @@ func NewFileUserRepo(logger *zap.SugaredLogger, userFile string) *UserFileReposi
um := &UserFileRepository{
logger: logger.Named("UserManager"),
userFile: userFile,
users: make(map[string]*model.UserInfo),
users: make(map[string]*model.User),
mx: sync.RWMutex{},
}

Expand All @@ -36,8 +36,8 @@ func NewFileUserRepo(logger *zap.SugaredLogger, userFile string) *UserFileReposi
um.logger.Infof("no valid users found - create one")
bytes, _ := bcrypt.GenerateFromPassword([]byte("11111"), 14)

um.users["user"] = &model.UserInfo{
User: "user",
um.users["user"] = &model.User{
Login: "user",
Password: string(bytes),
}
}
Expand Down Expand Up @@ -65,16 +65,16 @@ func (r *UserFileRepository) loadUsersFile() error {
return err
}

users := make([]*model.UserInfo, 0)
users := make([]*model.User, 0)

if err := yaml.Unmarshal(dat, &users); err != nil {
return err
}

r.users = make(map[string]*model.UserInfo)
r.users = make(map[string]*model.User)
for _, user := range users {
if user.User != "" {
r.users[user.User] = user
if user.Login != "" {
r.users[user.Login] = user
}
}

Expand Down Expand Up @@ -141,7 +141,7 @@ func (r *UserFileRepository) UserIsValid(user, sn string) bool {
return ok
}

func (r *UserFileRepository) GetUser(username string) *model.UserInfo {
func (r *UserFileRepository) GetUser(username string) *model.User {
r.mx.RLock()
defer r.mx.RUnlock()
return r.users[username]
Expand Down

0 comments on commit c9090bd

Please sign in to comment.