Flag specification of WS API addr and UDP API addr

From now on flags --waddr for providing WebSocket API address
(for server to run or client to connect to) and --uaddr for providing
address of UDP hole punching listener (also server and client would use it)
This commit is contained in:
Maciej Krzyżanowski 2024-05-27 20:10:31 +02:00
parent b23e8c00a6
commit 559a33eaa7
4 changed files with 47 additions and 25 deletions

View File

@ -35,14 +35,16 @@ type Context struct {
rToServer chan cm.RFrame
initiations []InitiationInfo
initiationsLock sync.RWMutex
settings common.ClientSettings
}
func NewClientContext(conn *websocket.Conn) *Context {
func NewClientContext(conn *websocket.Conn, settings common.ClientSettings) *Context {
return &Context{
conn: conn,
resFromServer: make(chan cm.RFrame),
reqFromServer: make(chan cm.RFrame),
rToServer: make(chan cm.RFrame),
settings: settings,
}
}
@ -132,8 +134,6 @@ func (cliCtx *Context) handleStartChatD(reqFrame cm.RFrame) (res cm.Response, er
logger.Infof("servers wants to be punched, got start chat d request for %s with code %s",
startChatDReq.Nickname, startChatDReq.PunchCode)
logger.Warn("handleStartChatD not implemented yet")
idx := slices.IndexFunc(cliCtx.initiations, func(i InitiationInfo) bool {
return i.otherSideNick == startChatDReq.Nickname
})
@ -144,7 +144,7 @@ func (cliCtx *Context) handleStartChatD(reqFrame cm.RFrame) (res cm.Response, er
return nil, nil
}
conn, err := net.Dial("udp", ":8081")
conn, err := net.Dial("udp", cliCtx.settings.UdpAddr)
if err != nil {
logger.Error("error udp dialing for punch", err)
return nil, nil
@ -247,7 +247,7 @@ func init() {
}
func sendAuth(ctx *Context, nick, pass string) {
logger.Info("trying to authenticate as krzmaciek...")
logger.Info("trying to authenticate...", "nick", nick)
err := ctx.sendRequest(cm.AuthRequest{Nickname: nick, Password: pass})
if err != nil {
@ -344,8 +344,8 @@ func sendStartChatC(ctx *Context, nick string) {
logger.Debug("request sent, no wait for response")
}
func RunClient() {
u := url.URL{Scheme: "ws", Host: ":8080", Path: "/wsapi"}
func RunClient(settings common.ClientSettings) {
u := url.URL{Scheme: "ws", Host: settings.WsapiAddr, Path: "/wsapi"}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
@ -353,7 +353,7 @@ func RunClient() {
return
}
cliCtx := NewClientContext(c)
cliCtx := NewClientContext(c, settings)
errGroup, syncCtx := errgroup.WithContext(context.Background())
errGroup.Go(func() error {

View File

@ -215,3 +215,16 @@ type PunchRequest struct {
type PunchResponse struct {
IPAddr int32 `json:"ipAddr"`
}
type Settings struct {
WsapiAddr string
UdpAddr string
}
type ServerSettings struct {
Settings
}
type ClientSettings struct {
Settings
}

33
main.go
View File

@ -1,28 +1,37 @@
package main
import (
"flag"
"log"
"os"
"krzyzanowski.dev/archat/client"
"krzyzanowski.dev/archat/common"
"krzyzanowski.dev/archat/server"
)
func main() {
args := os.Args[1:]
wsapiAddr := flag.String("waddr", ":8080", "An IP address of the websocket API endpoint")
udpAddr := flag.String("uaddr", ":8081", "An IP address of the UDP hole-punching listener")
runType := flag.String("run", "client", "Either one of 'client' or 'server'")
if len(args) != 1 {
log.Fatalln("You must provide only one argument which is type of " +
"application: 'server' or 'client'")
flag.Parse()
commonSettings := common.Settings{
WsapiAddr: *wsapiAddr,
UdpAddr: *udpAddr,
}
runType := args[0]
if runType == "client" {
client.RunClient()
} else if runType == "server" {
server.RunServer()
if *runType == "client" {
settings := common.ClientSettings{
Settings: commonSettings,
}
client.RunClient(settings)
} else if *runType == "server" {
settings := common.ServerSettings{
Settings: commonSettings,
}
server.RunServer(settings)
} else {
log.Fatalf("Unknown run type %s\n", runType)
log.Fatalf("Unknown run type %s\n", *runType)
}
}

View File

@ -698,7 +698,7 @@ func (srvCtx *Context) handleUDP(data []byte, addr net.Addr) {
}
}
func RunServer() {
func RunServer(settings common.ServerSettings) {
srvCtx := NewContext()
go func() {
@ -709,16 +709,16 @@ func RunServer() {
}()
http.HandleFunc("/wsapi", srvCtx.wsapiHandler)
logger.Info("Starting websocket server...")
logger.Infof("Starting websocket server on %s...", settings.WsapiAddr)
go func() {
err := http.ListenAndServe(":8080", nil)
err := http.ListenAndServe(settings.WsapiAddr, nil)
if err != nil {
logger.Error(err)
}
}()
logger.Info("Starting punching server...")
listener, err := net.ListenPacket("udp", ":8081")
logger.Infof("Starting punching server on %s...", settings.UdpAddr)
listener, err := net.ListenPacket("udp", settings.UdpAddr)
if err != nil {
logger.Error("could not create listener for punching server", err)
}