Documentation ¶
Index ¶
- func GenAuthChallenge(pubKey *rsa.PublicKey) ([]byte, []byte)
- func ValidateCreateChatRoom(ws *websocket.Conn, msg *websock.CreateChatRoomMessage) bool
- func ValidateRegisterUser(ws *websocket.Conn, msg *websock.RegisterUserMessage) bool
- type Config
- type Server
- func (s *Server) AddClient(ws *websocket.Conn, user *User)
- func (s *Server) AddMessageToDB(username, chatName string, timestamp int64, encryptedContent map[string][]byte)
- func (s *Server) AuthedHandler(ws *websocket.Conn, pongCount *int64)
- func (s *Server) ClientJoinedChat(ws *websocket.Conn, user *User, chatName string)
- func (s *Server) ClientLeftChat(ws *websocket.Conn)
- func (s *Server) CreateChatRoom(ws *websocket.Conn, msg *websock.CreateChatRoomMessage)
- func (s *Server) FindMessagesForUser(username, chatName string) []*mdb.Message
- func (s *Server) GetChatRooms(ws *websocket.Conn)
- func (s *Server) JoinChat(ws *websocket.Conn, msg *websock.JoinChatMessage)
- func (s *Server) LoginUser(ws *websocket.Conn, username string) bool
- func (s *Server) NoAuthHandler(ws *websocket.Conn, pongCount *int64) bool
- func (s *Server) NotifyChatMessage(sender string, chatName string, timestamp int64, ...)
- func (s *Server) NotifyUserJoined(user *User, chatName string)
- func (s *Server) NotifyUserLeft(username, chatName string)
- func (s *Server) Pinger(ws *websocket.Conn) (*time.Ticker, *int64)
- func (s *Server) ReceiveChatMessage(ws *websocket.Conn, msg *websock.SendChatMessage)
- func (s *Server) RegisterUser(ws *websocket.Conn, msg *websock.RegisterUserMessage)
- func (s *Server) RemoveClient(ws *websocket.Conn)
- func (s *Server) WebsockHandler(ws *websocket.Conn)
- type User
- type Users
- func (users *Users) ForEach(f func(*websocket.Conn, *User))
- func (users *Users) ForEachInChat(chatName string, f func(*websocket.Conn, *User))
- func (users *Users) Get(ws *websocket.Conn) (user *User, ok bool)
- func (users *Users) Insert(ws *websocket.Conn, user *User) bool
- func (users *Users) Len() int
- func (users *Users) LenInChat(chatName string) (amount int)
- func (users *Users) Remove(ws *websocket.Conn) (user *User, ok bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenAuthChallenge ¶
GenAuthChallenge generates a random authentication key, and encrypts it with the given public key returns the encrypted and the original auth key
func ValidateCreateChatRoom ¶
func ValidateCreateChatRoom(ws *websocket.Conn, msg *websock.CreateChatRoomMessage) bool
ValidateCreateChatRoom validates the content of a request from a client to create a new chat room. the name of the chat room is validated. If the chat room has a password, this is also validated.
func ValidateRegisterUser ¶
func ValidateRegisterUser(ws *websocket.Conn, msg *websock.RegisterUserMessage) bool
ValidateRegisterUser validates the contents of a request from a client to register a new user. The length of the username and the public key bit-length is validated.
Types ¶
type Config ¶
Config describes the server configuration, where the listening port, name of the mongoDB database used by the server, and the mongoDB address
type Server ¶
Server contains the context of the chat engine server
func CreateServer ¶
CreateServer creates a new instance of the server using the config
func (*Server) AddMessageToDB ¶
func (s *Server) AddMessageToDB(username, chatName string, timestamp int64, encryptedContent map[string][]byte)
AddMessageToDB inserts a chat message into the database
func (*Server) AuthedHandler ¶
AuthedHandler handles websocket messages from authenticated clients
func (*Server) ClientJoinedChat ¶
ClientJoinedChat is called when a client joins a chat room, it adds the username of the client to the map of chat rooms and the chat room name to the User object, to be able to keep track of this Then info about the chat room, and messages for this user is sent to the client
func (*Server) ClientLeftChat ¶
ClientLeftChat is called when a client leaves a chat room, it removes the username of the client from the map of chat rooms and the chat room name from the User object. Other clients in the chat will be notfied that this user left the chat as well
func (*Server) CreateChatRoom ¶
func (s *Server) CreateChatRoom(ws *websocket.Conn, msg *websock.CreateChatRoomMessage)
CreateChatRoom creates a new chat room, and adds it to the database
func (*Server) FindMessagesForUser ¶
FindMessagesForUser finds all chat messages with a specific user as recipient in a specific chat room
func (*Server) GetChatRooms ¶
GetChatRooms returns all non-hidden chat rooms to the websocket client
func (*Server) JoinChat ¶
func (s *Server) JoinChat(ws *websocket.Conn, msg *websock.JoinChatMessage)
JoinChat assigns a client to a chat room
func (*Server) LoginUser ¶
LoginUser authenticates a user using a randomly generated authentication token This token is encrypted with the public key of the username the client is trying to log in as The client is then expected to respond with the correct decrypted token TODO check if user is already logged in
func (*Server) NoAuthHandler ¶
NoAuthHandler handles websocket messages from an unauthenticated client This function returns true if the client was authenticated, or false if the client disconnected without authenticating as a user
func (*Server) NotifyChatMessage ¶
func (s *Server) NotifyChatMessage(sender string, chatName string, timestamp int64, encryptedContent map[string][]byte)
NotifyChatMessage notifies all clients in a chat room about a new chat message
func (*Server) NotifyUserJoined ¶
NotifyUserJoined notifies all clients in a chat room that a new user has joined the chat room
func (*Server) NotifyUserLeft ¶
NotifyUserLeft notifies all clients in a chat room that a user left the chat room
func (*Server) Pinger ¶
Pinger sends a ping message to the client in the interval specified in Keepalive in the ServerConfig If no pongs were received during the elapsed time, the server will close the client connection.
func (*Server) ReceiveChatMessage ¶
func (s *Server) ReceiveChatMessage(ws *websocket.Conn, msg *websock.SendChatMessage)
ReceiveChatMessage is called when the server receives a chat message from a client that is in a chat room
func (*Server) RegisterUser ¶
func (s *Server) RegisterUser(ws *websocket.Conn, msg *websock.RegisterUserMessage)
RegisterUser registers a new user, and adds it to the database
func (*Server) RemoveClient ¶
RemoveClient removes a client from the ConnectedClients map
func (*Server) WebsockHandler ¶
WebsockHandler is the handler for the server websocket when a client initially connects. It handles messages from an unauthenticated client.
type User ¶
type User struct { sync.Mutex Username string AuthKey []byte PublicKey *rsa.PublicKey ChatRoom string }
User contains user data and a mutex to enable threadsafe access without copying
The mutex must be held when accessing or modifying fields
func NewUser ¶
NewUser creates a new user object for a connected client, with the username, generated (temporary) authentication key and the encrypted version of the key. A random byte slice is generated and encrypted with the users public key, the user is expected to send in response the decrypted string
func (*User) KeyMatches ¶
KeyMatches checks that an authentication key matches the one for this user
type Users ¶
Users is a threadsafe connection between a websocket connection and a user
The mutex must be held when accessing or modifying the map
func (*Users) ForEachInChat ¶
ForEachInChat performs the given function for every user which is in the given chat
func (*Users) Get ¶
Get gets the User of a connected websocket client
Returns true on success and false on missing user
func (*Users) Insert ¶
Insert adds the given User to the collection indexed by the websocket connection
Returns true on success and false on already existing association between socket and user