|
|
|
|
@ -93,12 +93,14 @@ func (um *UserManagerTCP) GetUserListTCP() string { |
|
|
|
|
} |
|
|
|
|
return userList |
|
|
|
|
} |
|
|
|
|
func (um *UserManagerTCP) UserMatchTCP(user string, encrypted []byte, sender net.Conn) { |
|
|
|
|
func (um *UserManagerTCP) UserMatchTCP(user string, encrypted []byte, sender net.Conn, message string) { |
|
|
|
|
um.mutex.Lock() |
|
|
|
|
defer um.mutex.Unlock() |
|
|
|
|
for clientCon, client := range um.clients { |
|
|
|
|
if user == client.username { |
|
|
|
|
clientCon.Write(encrypted) |
|
|
|
|
} else if clientCon == sender { |
|
|
|
|
sayTCP(clientCon, message, "@YOU-Said(In Private)") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -225,22 +227,22 @@ func loadConfig(filename string) error { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func sayTCP(conn net.Conn, text string) { |
|
|
|
|
encrypted, err := sayServer(text) |
|
|
|
|
func sayTCP(conn net.Conn, text string, who string) { |
|
|
|
|
encrypted, err := sayServer(text, who) |
|
|
|
|
if err != nil { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
conn.Write(encrypted) |
|
|
|
|
} |
|
|
|
|
func sayUDP(conn *net.UDPConn, addr *net.UDPAddr, text string) { |
|
|
|
|
encrypted, err := sayServer(text) |
|
|
|
|
func sayUDP(conn *net.UDPConn, addr *net.UDPAddr, text string, who string) { |
|
|
|
|
encrypted, err := sayServer(text, who) |
|
|
|
|
if err != nil { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
conn.WriteToUDP(encrypted, addr) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func sayServer(text string) ([]byte, error) { |
|
|
|
|
func sayServer(text string, who string) ([]byte, error) { |
|
|
|
|
// Load timezone
|
|
|
|
|
timezone, err := time.LoadLocation(config.Server.Timezone) |
|
|
|
|
if err != nil { |
|
|
|
|
@ -249,7 +251,7 @@ func sayServer(text string) ([]byte, error) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
msg := Message{ |
|
|
|
|
Username: "@SERVER", |
|
|
|
|
Username: who, |
|
|
|
|
Text: text, |
|
|
|
|
Time: time.Now().In(timezone).Format("2006-01-02 03:04:05 PM"), |
|
|
|
|
} |
|
|
|
|
@ -299,7 +301,7 @@ func handleTCPClient(conn net.Conn) { |
|
|
|
|
fmt.Printf("%s connected\n", username) |
|
|
|
|
|
|
|
|
|
// Say Hello to all, but self
|
|
|
|
|
broadcastTCP(decrypted, conn) |
|
|
|
|
broadcastTCP(decrypted, conn, "") |
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
buf := make([]byte, 1024) |
|
|
|
|
@ -319,14 +321,14 @@ func handleTCPClient(conn net.Conn) { |
|
|
|
|
if message == "users!" { |
|
|
|
|
// Handle the 'users' command
|
|
|
|
|
userList := userManagerTCP.GetUserListTCP() |
|
|
|
|
sayTCP(conn, "User's Online: "+userList) |
|
|
|
|
sayTCP(conn, "User's Online: "+userList, "@SERVER") |
|
|
|
|
} else {
|
|
|
|
|
privateUser, text := parseInput(message) |
|
|
|
|
if privateUser != "" && text != "" { |
|
|
|
|
privateTCP(privateUser, decrypted, conn) |
|
|
|
|
privateTCP(privateUser, decrypted, conn, message) |
|
|
|
|
} else { |
|
|
|
|
// Broadcast the message to all other clients
|
|
|
|
|
broadcastTCP(decrypted, conn) |
|
|
|
|
broadcastTCP(decrypted, conn, message) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -403,7 +405,7 @@ func startUDPServer() error { |
|
|
|
|
if message == "users!" { |
|
|
|
|
// Handle the 'users' command
|
|
|
|
|
userList := userManagerUDP.GetUserListUDP() |
|
|
|
|
sayUDP(conn, addr, "User's Online: "+userList) |
|
|
|
|
sayUDP(conn, addr, "User's Online: "+userList, "@SERVER") |
|
|
|
|
} else { |
|
|
|
|
// Broadcast the message to all other clients
|
|
|
|
|
broadcastUDP(conn, decrypted, addr) |
|
|
|
|
@ -411,16 +413,16 @@ func startUDPServer() error { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func privateTCP(user string, data []byte, sender net.Conn) { |
|
|
|
|
func privateTCP(user string, data []byte, sender net.Conn, message string) { |
|
|
|
|
encrypted, err := encryptor.Encrypt(data) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Printf("Encryption error: %v\n", err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
userManagerTCP.UserMatchTCP(user, encrypted, sender) |
|
|
|
|
userManagerTCP.UserMatchTCP(user, encrypted, sender, message) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func broadcastTCP(data []byte, sender net.Conn) { |
|
|
|
|
func broadcastTCP(data []byte, sender net.Conn, message string) { |
|
|
|
|
encrypted, err := encryptor.Encrypt(data) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Printf("Encryption error: %v\n", err) |
|
|
|
|
@ -432,7 +434,10 @@ func broadcastTCP(data []byte, sender net.Conn) { |
|
|
|
|
for client := range userManagerTCP.clients { |
|
|
|
|
if client != sender { |
|
|
|
|
client.Write(encrypted) |
|
|
|
|
} else if message != "" { |
|
|
|
|
sayTCP(client, message, "@YOU-Said") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|