mirror of
https://github.com/originalmk/archat-server.git
synced 2025-01-18 08:19:17 +00:00
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:
parent
b23e8c00a6
commit
559a33eaa7
@ -35,14 +35,16 @@ type Context struct {
|
|||||||
rToServer chan cm.RFrame
|
rToServer chan cm.RFrame
|
||||||
initiations []InitiationInfo
|
initiations []InitiationInfo
|
||||||
initiationsLock sync.RWMutex
|
initiationsLock sync.RWMutex
|
||||||
|
settings common.ClientSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientContext(conn *websocket.Conn) *Context {
|
func NewClientContext(conn *websocket.Conn, settings common.ClientSettings) *Context {
|
||||||
return &Context{
|
return &Context{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
resFromServer: make(chan cm.RFrame),
|
resFromServer: make(chan cm.RFrame),
|
||||||
reqFromServer: make(chan cm.RFrame),
|
reqFromServer: make(chan cm.RFrame),
|
||||||
rToServer: 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",
|
logger.Infof("servers wants to be punched, got start chat d request for %s with code %s",
|
||||||
startChatDReq.Nickname, startChatDReq.PunchCode)
|
startChatDReq.Nickname, startChatDReq.PunchCode)
|
||||||
logger.Warn("handleStartChatD not implemented yet")
|
|
||||||
|
|
||||||
idx := slices.IndexFunc(cliCtx.initiations, func(i InitiationInfo) bool {
|
idx := slices.IndexFunc(cliCtx.initiations, func(i InitiationInfo) bool {
|
||||||
return i.otherSideNick == startChatDReq.Nickname
|
return i.otherSideNick == startChatDReq.Nickname
|
||||||
})
|
})
|
||||||
@ -144,7 +144,7 @@ func (cliCtx *Context) handleStartChatD(reqFrame cm.RFrame) (res cm.Response, er
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := net.Dial("udp", ":8081")
|
conn, err := net.Dial("udp", cliCtx.settings.UdpAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("error udp dialing for punch", err)
|
logger.Error("error udp dialing for punch", err)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -247,7 +247,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendAuth(ctx *Context, nick, pass string) {
|
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})
|
err := ctx.sendRequest(cm.AuthRequest{Nickname: nick, Password: pass})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -344,8 +344,8 @@ func sendStartChatC(ctx *Context, nick string) {
|
|||||||
logger.Debug("request sent, no wait for response")
|
logger.Debug("request sent, no wait for response")
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunClient() {
|
func RunClient(settings common.ClientSettings) {
|
||||||
u := url.URL{Scheme: "ws", Host: ":8080", Path: "/wsapi"}
|
u := url.URL{Scheme: "ws", Host: settings.WsapiAddr, Path: "/wsapi"}
|
||||||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -353,7 +353,7 @@ func RunClient() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cliCtx := NewClientContext(c)
|
cliCtx := NewClientContext(c, settings)
|
||||||
errGroup, syncCtx := errgroup.WithContext(context.Background())
|
errGroup, syncCtx := errgroup.WithContext(context.Background())
|
||||||
|
|
||||||
errGroup.Go(func() error {
|
errGroup.Go(func() error {
|
||||||
|
@ -215,3 +215,16 @@ type PunchRequest struct {
|
|||||||
type PunchResponse struct {
|
type PunchResponse struct {
|
||||||
IPAddr int32 `json:"ipAddr"`
|
IPAddr int32 `json:"ipAddr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Settings struct {
|
||||||
|
WsapiAddr string
|
||||||
|
UdpAddr string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServerSettings struct {
|
||||||
|
Settings
|
||||||
|
}
|
||||||
|
|
||||||
|
type ClientSettings struct {
|
||||||
|
Settings
|
||||||
|
}
|
||||||
|
33
main.go
33
main.go
@ -1,28 +1,37 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
|
|
||||||
"krzyzanowski.dev/archat/client"
|
"krzyzanowski.dev/archat/client"
|
||||||
|
"krzyzanowski.dev/archat/common"
|
||||||
"krzyzanowski.dev/archat/server"
|
"krzyzanowski.dev/archat/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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 {
|
flag.Parse()
|
||||||
log.Fatalln("You must provide only one argument which is type of " +
|
|
||||||
"application: 'server' or 'client'")
|
commonSettings := common.Settings{
|
||||||
|
WsapiAddr: *wsapiAddr,
|
||||||
|
UdpAddr: *udpAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
runType := args[0]
|
if *runType == "client" {
|
||||||
|
settings := common.ClientSettings{
|
||||||
if runType == "client" {
|
Settings: commonSettings,
|
||||||
client.RunClient()
|
}
|
||||||
} else if runType == "server" {
|
client.RunClient(settings)
|
||||||
server.RunServer()
|
} else if *runType == "server" {
|
||||||
|
settings := common.ServerSettings{
|
||||||
|
Settings: commonSettings,
|
||||||
|
}
|
||||||
|
server.RunServer(settings)
|
||||||
} else {
|
} else {
|
||||||
log.Fatalf("Unknown run type %s\n", runType)
|
log.Fatalf("Unknown run type %s\n", *runType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -698,7 +698,7 @@ func (srvCtx *Context) handleUDP(data []byte, addr net.Addr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunServer() {
|
func RunServer(settings common.ServerSettings) {
|
||||||
srvCtx := NewContext()
|
srvCtx := NewContext()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -709,16 +709,16 @@ func RunServer() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
http.HandleFunc("/wsapi", srvCtx.wsapiHandler)
|
http.HandleFunc("/wsapi", srvCtx.wsapiHandler)
|
||||||
logger.Info("Starting websocket server...")
|
logger.Infof("Starting websocket server on %s...", settings.WsapiAddr)
|
||||||
go func() {
|
go func() {
|
||||||
err := http.ListenAndServe(":8080", nil)
|
err := http.ListenAndServe(settings.WsapiAddr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
logger.Info("Starting punching server...")
|
logger.Infof("Starting punching server on %s...", settings.UdpAddr)
|
||||||
listener, err := net.ListenPacket("udp", ":8081")
|
listener, err := net.ListenPacket("udp", settings.UdpAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("could not create listener for punching server", err)
|
logger.Error("could not create listener for punching server", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user