Read server IP from the config file

This commit is contained in:
TheLordScruffy 2023-10-19 23:15:23 -04:00
parent 5a5373afdc
commit 70a1765a9f
10 changed files with 99 additions and 90 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.xml

View File

@ -1,6 +1,6 @@
<Config>
<!-- The address which HTTP will bind to -->
<address>127.0.0.1:8080</address>
<!-- The address the server will bind to -->
<address>127.0.0.1</address>
<!-- Database Credentials -->
<username>username</username>
@ -9,4 +9,4 @@
<!-- Database information-->
<databaseAddress>127.0.0.1</databaseAddress>
<databaseName>wwfc</databaseName>
</Config>
</Config>

View File

@ -3,6 +3,7 @@ package database
import (
"context"
"database/sql"
"strconv"
"wwfc/common"
"wwfc/logging"
@ -105,11 +106,15 @@ func LoginUserToGPCM(pool *pgxpool.Pool, ctx context.Context, authToken string)
if !exists {
// Create the GPCM account
user.CreateUser(pool, ctx)
logging.Notice("DATABASE", "Created new GPCM user:", aurora.Cyan(strconv.FormatInt(user.UserId, 10)).String(), aurora.Cyan(user.GsbrCode).String(), "-", aurora.Cyan(strconv.FormatInt(user.ProfileId, 10)).String())
} else {
err := pool.QueryRow(ctx, GetUserProfileID, userId, gsbrcd).Scan(&user.ProfileId)
if err != nil {
panic(err)
}
logging.Notice("DATABASE", "Log in GPCM user:", aurora.Cyan(strconv.FormatInt(user.UserId, 10)).String(), aurora.Cyan(user.GsbrCode).String(), "-", aurora.Cyan(strconv.FormatInt(user.ProfileId, 10)).String())
}
return user, true

View File

@ -5,16 +5,13 @@ import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/logrusorgru/aurora/v3"
"io"
"log"
"net"
"os"
"time"
"wwfc/common"
"wwfc/logging"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/logrusorgru/aurora/v3"
)
var (
@ -23,12 +20,6 @@ var (
userId int64
)
func checkError(err error) {
if err != nil {
log.Fatalf("GCSP server has encountered a fatal error! Reason: %v\n", err)
}
}
func StartServer() {
// Get config
config := common.GetConfig()
@ -36,24 +27,30 @@ func StartServer() {
// Start SQL
dbString := fmt.Sprintf("postgres://%s:%s@%s/%s", config.Username, config.Password, config.DatabaseAddress, config.DatabaseName)
dbConf, err := pgxpool.ParseConfig(dbString)
checkError(err)
pool, err = pgxpool.ConnectConfig(ctx, dbConf)
checkError(err)
l, err := net.Listen("tcp", "127.0.0.1:29901")
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
panic(err)
}
pool, err = pgxpool.ConnectConfig(ctx, dbConf)
if err != nil {
panic(err)
}
address := config.Address + ":29901"
l, err := net.Listen("tcp", address)
if err != nil {
panic(err)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on " + "127.0.0.1:29901")
logging.Notice("GCSP", "Listening on", address)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
panic(err)
}
// Handle connections in a new goroutine.
@ -90,7 +87,7 @@ func handleRequest(conn net.Conn) {
commands, err := common.ParseGameSpyMessage(string(buffer))
if err != nil {
log.Fatal(err)
panic(err)
}
for _, command := range commands {

View File

@ -5,13 +5,12 @@ import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"github.com/jackc/pgx/v4/pgxpool"
"log"
"strconv"
"strings"
"wwfc/common"
"wwfc/database"
"github.com/jackc/pgx/v4/pgxpool"
)
func generateResponse(gpcmChallenge, nasChallenge, authToken, clientChallenge string) string {

View File

@ -5,16 +5,13 @@ import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/logrusorgru/aurora/v3"
"io"
"log"
"net"
"os"
"time"
"wwfc/common"
"wwfc/logging"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/logrusorgru/aurora/v3"
)
var (
@ -23,12 +20,6 @@ var (
userId int64
)
func checkError(err error) {
if err != nil {
log.Fatalf("GPCM server has encountered a fatal error! Reason: %v\n", err)
}
}
func StartServer() {
// Get config
config := common.GetConfig()
@ -36,24 +27,30 @@ func StartServer() {
// Start SQL
dbString := fmt.Sprintf("postgres://%s:%s@%s/%s", config.Username, config.Password, config.DatabaseAddress, config.DatabaseName)
dbConf, err := pgxpool.ParseConfig(dbString)
checkError(err)
pool, err = pgxpool.ConnectConfig(ctx, dbConf)
checkError(err)
l, err := net.Listen("tcp", "127.0.0.1:29900")
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
panic(err)
}
pool, err = pgxpool.ConnectConfig(ctx, dbConf)
if err != nil {
panic(err)
}
address := config.Address + ":29900"
l, err := net.Listen("tcp", address)
if err != nil {
panic(err)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on " + "127.0.0.1:29900")
logging.Notice("GPCM", "Listening on", address)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
panic(err)
}
// Handle connections in a new goroutine.

View File

@ -4,26 +4,32 @@ import (
"fmt"
"net"
"os"
"wwfc/common"
"wwfc/logging"
)
func StartServer() {
l, err := net.Listen("tcp", "127.0.0.1:27900")
// Get config
config := common.GetConfig()
address := config.Address + ":27900"
l, err := net.Listen("tcp", address)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
panic(err)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on " + "127.0.0.1:29901")
logging.Notice("GPSP", "Listening on", address)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
panic(err)
}
// Handle connections in a new goroutine.
fmt.Println("aaaa")
go handleRequest(conn)
}
}
@ -35,10 +41,9 @@ func handleRequest(conn net.Conn) {
// Read the incoming connection into the buffer.
reqLen, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading:", err.Error())
panic(err)
}
fmt.Println(reqLen)
fmt.Println(string(buf))
// Send a response back to person contacting us.
conn.Write([]byte(`\ka\\final\`))
// Close the connection when you're done with it.

View File

@ -2,12 +2,11 @@ package master
import (
"encoding/binary"
"log"
"github.com/logrusorgru/aurora/v3"
"net"
"sync"
"wwfc/common"
"wwfc/logging"
"github.com/logrusorgru/aurora/v3"
)
var (
@ -31,12 +30,18 @@ const (
)
func StartServer() {
conn, err := net.ListenPacket("udp", ":27900")
// Get config
config := common.GetConfig()
address := config.Address + ":27900"
conn, err := net.ListenPacket("udp", address)
if err != nil {
log.Fatal(err)
panic(err)
}
// Close the listener when the application closes.
defer conn.Close()
logging.Notice("MASTER", "Listening on", address)
for {
buf := make([]byte, 1024)

View File

@ -5,16 +5,14 @@ import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/logrusorgru/aurora/v3"
"io"
"log"
"net"
"os"
"time"
"wwfc/common"
"wwfc/logging"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/logrusorgru/aurora/v3"
)
var (
@ -28,12 +26,6 @@ const (
ModuleName = "MATCHMAKING"
)
func checkError(err error) {
if err != nil {
log.Fatalf("GCSP server has encountered a fatal error! Reason: %v\n", err)
}
}
func StartServer() {
// Get config
config := common.GetConfig()
@ -41,18 +33,25 @@ func StartServer() {
// Start SQL
dbString := fmt.Sprintf("postgres://%s:%s@%s/%s", config.Username, config.Password, config.DatabaseAddress, config.DatabaseName)
dbConf, err := pgxpool.ParseConfig(dbString)
checkError(err)
pool, err = pgxpool.ConnectConfig(ctx, dbConf)
checkError(err)
l, err := net.Listen("tcp", "127.0.0.1:28910")
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
panic(err)
}
pool, err = pgxpool.ConnectConfig(ctx, dbConf)
if err != nil {
panic(err)
}
address := config.Address + ":28910"
l, err := net.Listen("tcp", address)
if err != nil {
panic(err)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on " + "127.0.0.1:28910")
logging.Notice("MATCHMAKING", "Listening on", address)
for {
// Listen for an incoming connection.
conn, err := l.Accept()

View File

@ -6,6 +6,7 @@ import (
"github.com/jackc/pgx/v4/pgxpool"
"log"
"wwfc/common"
"wwfc/logging"
"wwfc/nhttp"
)
@ -14,12 +15,6 @@ var (
pool *pgxpool.Pool
)
func checkError(err error) {
if err != nil {
log.Fatalf("NAS server has encountered a fatal error! Reason: %v\n", err)
}
}
func StartServer() {
// Get config
config := common.GetConfig()
@ -27,12 +22,17 @@ func StartServer() {
// Start SQL
dbString := fmt.Sprintf("postgres://%s:%s@%s/%s", config.Username, config.Password, config.DatabaseAddress, config.DatabaseName)
dbConf, err := pgxpool.ParseConfig(dbString)
checkError(err)
if err != nil {
panic(err)
}
pool, err = pgxpool.ConnectConfig(ctx, dbConf)
checkError(err)
if err != nil {
panic(err)
}
// Finally, initialize the HTTP server
fmt.Printf("Starting HTTP connection (%s)...\nNot using the usual port for HTTP?\nBe sure to use a proxy, otherwise the Wii can't connect!\n", "[::1]")
address := config.Address + ":80"
r := NewRoute()
ac := r.HandleGroup("ac")
{
@ -40,5 +40,6 @@ func StartServer() {
ac.HandleAction("login", login)
}
logging.Notice("NAS", "Starting HTTP server on", address)
log.Fatal(nhttp.ListenAndServe("0.0.0.0:80", r.Handle()))
}