Add some stuff

This commit is contained in:
qowevisa 2024-06-12 23:03:42 +03:00
parent 61f80b82c7
commit 36c7f93c0c
2 changed files with 88 additions and 1 deletions

61
cmd/cgui/conUsers.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"log"
"sync"
)
var (
userCenter UserCenter
)
type UserCenter struct {
UsersSTOI map[string]uint16
UsersITOS map[uint16]string
Mu sync.Mutex
}
func (u *UserCenter) Init() {
u.UsersSTOI = make(map[string]uint16)
u.UsersITOS = make(map[uint16]string)
}
func (u *UserCenter) AddUser(name string, id uint16) error {
u.Mu.Lock()
defer u.Mu.Unlock()
_, alreadyHave := u.UsersSTOI[name]
if alreadyHave {
return ERROR_ALREADY_HAVE
}
log.Printf("Users: add %s with %d id\n", name, id)
u.UsersITOS[id] = name
u.UsersSTOI[name] = id
return nil
}
func (u *UserCenter) DeleteIfHaveOne(id uint16) {
name, found := u.UsersITOS[id]
if !found {
log.Printf("User with %d id is not found; Can not delete\n", id)
return
}
delete(u.UsersITOS, id)
delete(u.UsersSTOI, name)
log.Printf("User with %s name and %d id was found; User is deleted\n", name, id)
}
func (u *UserCenter) GetID(name string) (uint16, error) {
id, have := u.UsersSTOI[name]
if !have {
return 0, ERROR_DONT_HAVE
}
return id, nil
}
func (u *UserCenter) GetName(id uint16) (string, error) {
name, have := u.UsersITOS[id]
if !have {
return "", ERROR_DONT_HAVE
}
return name, nil
}

View File

@ -18,6 +18,7 @@ import (
func main() { func main() {
tlepCenter.Init() tlepCenter.Init()
userCenter.Init()
loadingFileName := env.ServerFullchainFileName loadingFileName := env.ServerFullchainFileName
cert, err := os.ReadFile(loadingFileName) cert, err := os.ReadFile(loadingFileName)
if err != nil { if err != nil {
@ -120,7 +121,12 @@ func readFromServer(conn net.Conn, ws *websocket.Conn) {
log.Printf("ERROR: tlep: ECDHApplyOtherKeyBytes: %v\n", err) log.Printf("ERROR: tlep: ECDHApplyOtherKeyBytes: %v\n", err)
continue continue
} }
msg.Data = []byte{} fromName, err := userCenter.GetName(msg.FromID)
if err != nil {
log.Printf("ERROR: userCenter: GetName: %v\n", err)
} else {
msg.Data = []byte(fromName)
}
case com.ID_CLIENT_SEND_CLIENT_CBES_SPECS: case com.ID_CLIENT_SEND_CLIENT_CBES_SPECS:
t, err := tlepCenter.GetTLEP(msg.FromID) t, err := tlepCenter.GetTLEP(msg.FromID)
if err != nil { if err != nil {
@ -137,6 +143,12 @@ func readFromServer(conn net.Conn, ws *websocket.Conn) {
log.Printf("ERROR: tlep: CBESSetFromBytes: %v\n", err) log.Printf("ERROR: tlep: CBESSetFromBytes: %v\n", err)
continue continue
} }
fromName, err := userCenter.GetName(msg.FromID)
if err != nil {
log.Printf("ERROR: userCenter: GetName: %v\n", err)
} else {
msg.Data = []byte(fromName)
}
// message // message
case com.ID_CLIENT_SEND_CLIENT_MESSAGE: case com.ID_CLIENT_SEND_CLIENT_MESSAGE:
t, err := tlepCenter.GetTLEP(msg.FromID) t, err := tlepCenter.GetTLEP(msg.FromID)
@ -152,6 +164,12 @@ func readFromServer(conn net.Conn, ws *websocket.Conn) {
msg.Data = decrypedMsg msg.Data = decrypedMsg
// switch // switch
} }
// user stuff
switch msg.ID {
case com.ID_CLIENT_ASK_CLIENT_HANDSHAKE,
com.ID_CLIENT_APPROVE_CLIENT_HANDSHAKE:
userCenter.AddUser(string(msg.Data), msg.FromID)
}
log.Printf("client: readServer: sending message to websocket: %v", *msg) log.Printf("client: readServer: sending message to websocket: %v", *msg)
ws.WriteJSON(*msg) ws.WriteJSON(*msg)
} }
@ -202,6 +220,14 @@ func readFromWebSocket(conn net.Conn, ws *websocket.Conn) {
msg.FromID = r.ID msg.FromID = r.ID
// switch // switch
} }
// user stuff
switch msg.ID {
case com.ID_CLIENT_ASK_CLIENT_HANDSHAKE,
com.ID_CLIENT_APPROVE_CLIENT_HANDSHAKE:
if r.IsRegistered {
msg.Data = []byte(r.Name)
}
}
// Crypto stuff // Crypto stuff
switch msg.ID { switch msg.ID {
case com.ID_CLIENT_ASK_CLIENT_HANDSHAKE, case com.ID_CLIENT_ASK_CLIENT_HANDSHAKE,