Add some stuff
This commit is contained in:
parent
61f80b82c7
commit
36c7f93c0c
61
cmd/cgui/conUsers.go
Normal file
61
cmd/cgui/conUsers.go
Normal 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
|
||||
}
|
|
@ -18,6 +18,7 @@ import (
|
|||
|
||||
func main() {
|
||||
tlepCenter.Init()
|
||||
userCenter.Init()
|
||||
loadingFileName := env.ServerFullchainFileName
|
||||
cert, err := os.ReadFile(loadingFileName)
|
||||
if err != nil {
|
||||
|
@ -120,7 +121,12 @@ func readFromServer(conn net.Conn, ws *websocket.Conn) {
|
|||
log.Printf("ERROR: tlep: ECDHApplyOtherKeyBytes: %v\n", err)
|
||||
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:
|
||||
t, err := tlepCenter.GetTLEP(msg.FromID)
|
||||
if err != nil {
|
||||
|
@ -137,6 +143,12 @@ func readFromServer(conn net.Conn, ws *websocket.Conn) {
|
|||
log.Printf("ERROR: tlep: CBESSetFromBytes: %v\n", err)
|
||||
continue
|
||||
}
|
||||
fromName, err := userCenter.GetName(msg.FromID)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: userCenter: GetName: %v\n", err)
|
||||
} else {
|
||||
msg.Data = []byte(fromName)
|
||||
}
|
||||
// message
|
||||
case com.ID_CLIENT_SEND_CLIENT_MESSAGE:
|
||||
t, err := tlepCenter.GetTLEP(msg.FromID)
|
||||
|
@ -152,6 +164,12 @@ func readFromServer(conn net.Conn, ws *websocket.Conn) {
|
|||
msg.Data = decrypedMsg
|
||||
// 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)
|
||||
ws.WriteJSON(*msg)
|
||||
}
|
||||
|
@ -202,6 +220,14 @@ func readFromWebSocket(conn net.Conn, ws *websocket.Conn) {
|
|||
msg.FromID = r.ID
|
||||
// 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
|
||||
switch msg.ID {
|
||||
case com.ID_CLIENT_ASK_CLIENT_HANDSHAKE,
|
||||
|
|
Loading…
Reference in New Issue
Block a user