Changes to client and server

This commit is contained in:
qowevisa 2024-06-07 09:48:13 +03:00
parent 455f178b53
commit 74787c17a9
3 changed files with 150 additions and 26 deletions

View File

@ -1,15 +1,23 @@
package main package main
import ( import (
"context"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io"
"log" "log"
"net"
"os" "os"
"sync"
"time"
"git.qowevisa.me/Qowevisa/gotell/communication"
"git.qowevisa.me/Qowevisa/gotell/env" "git.qowevisa.me/Qowevisa/gotell/env"
) )
var messageChannel chan ([]byte)
func main() { func main() {
host, err := env.GetHost() host, err := env.GetHost()
if err != nil { if err != nil {
@ -42,22 +50,49 @@ func main() {
defer conn.Close() defer conn.Close()
log.Println("client: connected to: ", conn.RemoteAddr()) log.Println("client: connected to: ", conn.RemoteAddr())
ctx, cancel := context.WithCancel(context.Background())
message := "Hello secure Server\n" wg := &sync.WaitGroup{}
n, err := conn.Write([]byte(message)) wg.Add(1)
go func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done() // Mark this goroutine as done when it exits
buf := make([]byte, 1024)
for {
select {
case <-ctx.Done(): // Check if context cancellation has been requested
return
default:
timeoutDuration := 5 * time.Second
err := conn.SetReadDeadline(time.Now().Add(timeoutDuration))
if err != nil { if err != nil {
log.Fatalf("client: write: %s", err) panic(err)
} }
n, err := conn.Read(buf)
log.Printf("client: wrote %q (%d bytes)", message, n)
reply := make([]byte, 256)
n, err = conn.Read(reply)
if err != nil { if err != nil {
log.Fatalf("client: read: %s", err) if err != io.EOF {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
continue
} }
panic(err)
}
return
}
messageChannel <- buf[:n]
}
}
}(ctx, wg)
go func() {
for message := range messageChannel {
msg, err := communication.Decode(message)
if err != nil {
log.Printf("ERROR: %v\n", err)
}
switch msg.From {
case communication.FROM_SERVER:
log.Printf("client: read %q (%d bytes)", string(reply[:n]), n) case communication.FROM_CLIENT:
}
log.Print("client: exiting") }
}()
cancel()
wg.Wait()
} }

View File

@ -3,16 +3,18 @@ package main
import ( import (
"crypto/rand" "crypto/rand"
"crypto/tls" "crypto/tls"
"encoding/binary"
"fmt" "fmt"
"io" "io"
"log" "log"
"net" "net"
"git.qowevisa.me/Qowevisa/gotell/communication" com "git.qowevisa.me/Qowevisa/gotell/communication"
"git.qowevisa.me/Qowevisa/gotell/env" "git.qowevisa.me/Qowevisa/gotell/env"
) )
func main() { func main() {
userCenter.Init()
host, err := env.GetHost() host, err := env.GetHost()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -53,7 +55,7 @@ func main() {
func handleClient(conn net.Conn) { func handleClient(conn net.Conn) {
defer conn.Close() defer conn.Close()
buf := make([]byte, 512) buf := make([]byte, 512)
ask, err := communication.AskClientNickname() ask, err := com.ServerAskClientAboutNickname()
if err != nil { if err != nil {
log.Printf("ERROR: %#v\n", err) log.Printf("ERROR: %#v\n", err)
} else { } else {
@ -72,18 +74,41 @@ func handleClient(conn net.Conn) {
} }
break break
} }
answer := append([]byte("Hello! I see your message:"), buf[:n]...) msg, err := com.Decode(buf[:n])
msg, err := communication.JustGetMessage(answer)
if err != nil { if err != nil {
log.Printf("ERROR: %#v\n", err) log.Printf("ERROR: %#v\n", err)
continue continue
} }
log.Printf("server: conn: sending %#v\n", msg) if msg == nil {
_, err = conn.Write(msg) log.Printf("ERROR MSG IS NIL\n")
if err != nil { continue
log.Printf("server: conn: write: %s", err)
break
} }
log.Printf("server: conn: receive %#v\n", *msg)
// Handle
switch msg.ID {
case com.ID_CLIENT_SEND_SERVER_NICKNAME:
id, err := userCenter.AddUser(string(msg.Data))
if err != nil {
answ, err := com.ServerSendClientDecline()
if err != nil {
log.Printf("ERROR: %v\n", err)
continue
}
conn.Write(answ)
} else {
idBytes := make([]byte, 4)
binary.BigEndian.PutUint32(idBytes, uint32(id))
answ, err := com.ServerSendClientHisID(idBytes)
if err != nil {
log.Printf("ERROR: %v\n", err)
continue
}
conn.Write(answ)
}
case com.ID_CLIENT_SEND_SERVER_LINK:
default:
}
// Handle
} }
log.Println("server: conn: closed") log.Println("server: conn: closed")
} }

64
cmd/server/users.go Normal file
View File

@ -0,0 +1,64 @@
package main
import (
"errors"
"math/rand"
"sync"
)
var (
userCenter UserCenter
)
type UserCenter struct {
UsersSTOI map[string]int32
UsersITOS map[int32]string
Mu sync.Mutex
}
func (u *UserCenter) Init() {
u.UsersSTOI = make(map[string]int32)
u.UsersITOS = make(map[int32]string)
}
var (
ERROR_ALREADY_HAVE = errors.New("Username is already taken")
ERROR_DONT_HAVE = errors.New("Username was not found")
)
func (u *UserCenter) AddUser(name string) (int32, error) {
u.Mu.Lock()
defer u.Mu.Unlock()
_, alreadyHave := u.UsersSTOI[name]
if alreadyHave {
return -1, ERROR_ALREADY_HAVE
}
var ret int32
for {
rInt := rand.Int31()
_, taken := u.UsersITOS[rInt]
if !taken {
ret = rInt
break
}
}
u.UsersITOS[ret] = name
u.UsersSTOI[name] = ret
return ret, nil
}
func (u *UserCenter) GetID(name string) (int32, error) {
id, have := u.UsersSTOI[name]
if !have {
return -1, ERROR_DONT_HAVE
}
return id, nil
}
func (u *UserCenter) GetName(id int32) (string, error) {
name, have := u.UsersITOS[id]
if !have {
return "", ERROR_DONT_HAVE
}
return name, nil
}