server

package
v0.1.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2018 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenAuthChallenge

func GenAuthChallenge(pubKey *rsa.PublicKey) ([]byte, []byte)

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

type Config struct {
	DBName    string
	MongoURL  string
	Keepalive int
}

Config describes the server configuration, where the listening port, name of the mongoDB database used by the server, and the mongoDB address

type Server

type Server struct {
	Config
	Db    *mdb.Database
	Users Users
}

Server contains the context of the chat engine server

func CreateServer

func CreateServer(config Config) *Server

CreateServer creates a new instance of the server using the config

func (*Server) AddClient

func (s *Server) AddClient(ws *websocket.Conn, user *User)

AddClient adds a new client to Users

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

func (s *Server) AuthedHandler(ws *websocket.Conn, pongCount *int64)

AuthedHandler handles websocket messages from authenticated clients

func (*Server) ClientJoinedChat

func (s *Server) ClientJoinedChat(ws *websocket.Conn, user *User, chatName string)

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

func (s *Server) ClientLeftChat(ws *websocket.Conn)

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

func (s *Server) FindMessagesForUser(username, chatName string) []*mdb.Message

FindMessagesForUser finds all chat messages with a specific user as recipient in a specific chat room

func (*Server) GetChatRooms

func (s *Server) GetChatRooms(ws *websocket.Conn)

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

func (s *Server) LoginUser(ws *websocket.Conn, username string) bool

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

func (s *Server) NoAuthHandler(ws *websocket.Conn, pongCount *int64) bool

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

func (s *Server) NotifyUserJoined(user *User, chatName string)

NotifyUserJoined notifies all clients in a chat room that a new user has joined the chat room

func (*Server) NotifyUserLeft

func (s *Server) NotifyUserLeft(username, chatName string)

NotifyUserLeft notifies all clients in a chat room that a user left the chat room

func (*Server) Pinger

func (s *Server) Pinger(ws *websocket.Conn) (*time.Ticker, *int64)

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

func (s *Server) RemoveClient(ws *websocket.Conn)

RemoveClient removes a client from the ConnectedClients map

func (*Server) WebsockHandler

func (s *Server) WebsockHandler(ws *websocket.Conn)

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

func NewUser(db *mdb.Database, username string) (*User, []byte, error)

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

func (u *User) KeyMatches(authKey []byte) bool

KeyMatches checks that an authentication key matches the one for this user

type Users

type Users struct {
	sync.Mutex
	// contains filtered or unexported fields
}

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) ForEach

func (users *Users) ForEach(f func(*websocket.Conn, *User))

ForEach performs the given function on all stored users

func (*Users) ForEachInChat

func (users *Users) ForEachInChat(chatName string, f func(*websocket.Conn, *User))

ForEachInChat performs the given function for every user which is in the given chat

func (*Users) Get

func (users *Users) Get(ws *websocket.Conn) (user *User, ok bool)

Get gets the User of a connected websocket client

Returns true on success and false on missing user

func (*Users) Insert

func (users *Users) Insert(ws *websocket.Conn, user *User) bool

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

func (*Users) Len

func (users *Users) Len() int

Len gets the amount of registered users

func (*Users) LenInChat

func (users *Users) LenInChat(chatName string) (amount int)

LenInChat gets the amount of registered users in a given chat

func (*Users) Remove

func (users *Users) Remove(ws *websocket.Conn) (user *User, ok bool)

Remove deletes the connection between a websocket and a user

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL