Skip to content

Commit

Permalink
Merge pull request #6 from shoginyan/master
Browse files Browse the repository at this point in the history
merged auth and secure, updated nex libraries.
  • Loading branch information
binaryoverload authored Feb 22, 2025
2 parents d30c0b2 + 12707c4 commit 7f8fad8
Show file tree
Hide file tree
Showing 42 changed files with 894 additions and 893 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ go.work.sum
build
log
*.pem
*.key
*.key

# macOS nonsense
.DS_Store
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ARG app_dir="/home/go/app"


# * Building the application
FROM golang:1.22-alpine3.20 AS build
FROM golang:1.23-alpine3.20 AS build
ARG app_dir

WORKDIR ${app_dir}
Expand All @@ -15,8 +15,9 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \
go mod download -x

COPY . .
ARG BUILD_STRING=pretendo.WUC.docker
RUN --mount=type=cache,target=/go/pkg/mod/ \
CGO_ENABLED=0 go build -v -o ${app_dir}/build/server
CGO_ENABLED=0 go build -ldflags "-X 'main.serverBuildString=${BUILD_STRING}'" -v -o ${app_dir}/build/server


# * Running the final application
Expand Down
60 changes: 60 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# TODO - Assumes a UNIX-like OS

RED := $(shell tput setaf 1)
BLUE := $(shell tput setaf 4)
CYAN := $(shell tput setaf 14)
ORANGE := $(shell tput setaf 202)
YELLOW := $(shell tput setaf 214)
RESET := $(shell tput sgr0)

ifeq ($(shell which go),)
# TODO - Read contents from .git folder instead?
$(error "$(RED)go command not found. Install go to continue $(BLUE)https://go.dev/doc/install$(RESET)")
endif

ifneq ($(wildcard .git),)
# * .git folder exists, build server build string from repo info
ifeq ($(shell which git),)
# TODO - Read contents from .git folder instead?
$(error "$(RED)git command not found. Install git to continue $(ORANGE)https://git-scm.com/downloads$(RESET)")
endif
$(info "$(CYAN)Building server build string from repository info$(RESET)")
# * Build server build string from repo info
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
REMOTE_ORIGIN := $(shell git config --get remote.origin.url)

# * Handle multiple origin URL formats
HTTPS_PREFIX_CHECK := $(shell echo $(REMOTE_ORIGIN) | head -c 8)
HTTP_PREFIX_CHECK := $(shell echo $(REMOTE_ORIGIN) | head -c 7)
GIT@_PREFIX_CHECK := $(shell echo $(REMOTE_ORIGIN) | head -c 4)

ifeq ($(HTTPS_PREFIX_CHECK), https://)
REMOTE_PATH := $(shell echo $(REMOTE_ORIGIN) | cut -d/ -f4-)
else ifeq ($(HTTP_PREFIX_CHECK), http://)
REMOTE_PATH := $(shell echo $(REMOTE_ORIGIN) | cut -d/ -f4-)
else ifeq ($(GIT@_PREFIX_CHECK), git@)
REMOTE_PATH := $(shell echo $(REMOTE_ORIGIN) | cut -d: -f2-)
else
REMOTE_PATH := $(shell echo $(REMOTE_ORIGIN) | cut -d/ -f2-)
endif

HASH := $(shell git rev-parse --short HEAD)
SERVER_BUILD := $(BRANCH):$(REMOTE_PATH)@$(HASH)

else
# * .git folder not present, assume downloaded from zip file and just use folder name
$(info "$(CYAN)git repository not found. Building server build string from folder name$(RESET)")
SERVER_BUILD := $(notdir $(CURDIR))
endif

# * Final build string
DATE_TIME := $(shell date --iso=seconds)
BUILD_STRING := $(SERVER_BUILD), $(DATE_TIME)

all:
ifeq ($(wildcard .env),)
$(warning "$(YELLOW).env file not found, environment variables may not be populated correctly$(RESET)")
endif
go get -u
go mod tidy
go build -ldflags "-X 'main.serverBuildString=$(BUILD_STRING)'" -o ./build/$(notdir $(CURDIR))
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Wii U Chat replacement server
Includes both the authentication and secure servers

## Compiling

### Setup
Install [Go](https://go.dev/doc/install) and [git](https://git-scm.com/downloads), then clone and enter the repository

```bash
$ git clone https://github.com/PretendoNetwork/wiiu-chat
$ cd wiiu-chat
```

### Compiling using `go`
To compile using Go, `go get` the required modules and then `go build` to your desired location. You may also want to tidy the go modules, though this is optional

```bash
$ go get -u
$ go mod tidy
$ go build -o build/wiiu-chat
```

The server is now built to `build/wiiu-chat`

When compiling with only Go, the authentication servers build string is not automatically set. This should not cause any issues with gameplay, but it means that the server build will not be visible in any packet dumps or logs a title may produce

To compile the servers with the authentication server build string, add `-ldflags "-X 'main.serverBuildString=BUILD_STRING_HERE'"` to the build command, or use `make` to compile the server

### Compiling using `make`
Compiling using `make` will read the local `.git` directory to create a dynamic authentication server build string, based on your repositories remote origin and current commit

Install `make` either through your systems package manager or the [official download](https://www.gnu.org/software/make/). We provide a `default` rule which compiles [using `go`](#compiling-using-go)

To build using `go`

```bash
$ make
```

The server is now built to `build/wiiu-chat`
## Configuration
All configuration options are handled via environment variables

`.env` files are supported

| Name | Description | Required |
| ---- | ----------- | -------- |
| `PN_WUC_POSTGRES_URI` | Fully qualified URI to your Postgres server (Example `postgres://username:password@localhost/wiiuchat?sslmode=disable`) | Yes |
| `PN_WUC_AUTHENTICATION_SERVER_PORT` | Port for the authentication server | Yes |
| `PN_WUC_SECURE_SERVER_HOST` | Host name for the secure server (should point to the same address as the authentication server) | Yes |
| `PN_WUC_SECURE_SERVER_PORT` | Port for the secure server | Yes |
| `PN_WUC_ACCOUNT_GRPC_HOST` | Host name for your account server gRPC service | Yes |
| `PN_WUC_ACCOUNT_GRPC_PORT` | Port for your account server gRPC service | Yes |
| `PN_WUC_ACCOUNT_GRPC_API_KEY` | API key for your account server gRPC service | No (Assumed to be an open gRPC API) |
| `PN_WUC_FRIENDS_GRPC_HOST` | Host name for your friends server gRPC service | Yes |
| `PN_WUC_FRIENDS_GRPC_PORT` | Port for your friends server gRPC service | Yes |
| `PN_WUC_FRIENDS_GRPC_API_KEY` | API key for your friends server gRPC service | No (Assumed to be an open gRPC API) |
23 changes: 0 additions & 23 deletions database/add_new_user.go

This file was deleted.

5 changes: 0 additions & 5 deletions database/connect_all.go

This file was deleted.

41 changes: 0 additions & 41 deletions database/connect_mongo.go

This file was deleted.

25 changes: 25 additions & 0 deletions database/connect_postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package database

import (
"database/sql"
"os"

_ "github.com/lib/pq"

"github.com/PretendoNetwork/wiiu-chat/globals"
)

var Postgres *sql.DB

func ConnectPostgres() {
var err error

Postgres, err = sql.Open("postgres", os.Getenv("PN_WUC_POSTGRES_URI"))
if err != nil {
globals.Logger.Critical(err.Error())
}

globals.Logger.Success("Connected to Postgres!")

InitPostgres()
}
27 changes: 0 additions & 27 deletions database/does_user_exist.go

This file was deleted.

16 changes: 6 additions & 10 deletions database/end_call.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package database

import (
"context"

"go.mongodb.org/mongo-driver/bson"
"github.com/PretendoNetwork/nex-go/v2/types"
"github.com/PretendoNetwork/wiiu-chat/globals"
)

func EndCall(caller uint32) {
filter := bson.D{
{"caller_pid", caller},
}

_, err := callsCollection.DeleteOne(context.TODO(), filter)
func EndCall(caller types.PID) {
_, err := Postgres.Exec(`DELETE FROM ongoingcalls WHERE caller_pid = $1;`, caller)
if err != nil {
panic(err)
globals.Logger.Critical(err.Error())
return
}
}
24 changes: 6 additions & 18 deletions database/end_call_ringing.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
package database

import (
"context"

"go.mongodb.org/mongo-driver/bson"
"github.com/PretendoNetwork/nex-go/v2/types"
"github.com/PretendoNetwork/wiiu-chat/globals"
)

func EndCallRinging(caller uint32) {
filter := bson.D{
{"caller_pid", caller},
}

update := bson.D{
{
"$set", bson.D{
{"ringing", false},
},
},
}

_, err := callsCollection.UpdateOne(context.TODO(), filter, update)
func EndCallRinging(caller types.PID) {
_, err := Postgres.Exec(`UPDATE ongoingcalls SET (ringing = $1) WHERE caller_pid = $2;`, false, caller)
if err != nil {
panic(err)
globals.Logger.Critical(err.Error())
return
}
}
27 changes: 8 additions & 19 deletions database/get_call_info_by_caller.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
package database

import (
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/PretendoNetwork/nex-go/v2/types"
"github.com/PretendoNetwork/wiiu-chat/globals"
)

func GetCallInfoByCaller(caller uint32) (uint32, uint32, bool) { // caller pid, target pid, ringing
var result bson.M
filter := bson.D{
{"caller_pid", caller},
}

err := callsCollection.FindOne(context.TODO(), filter, options.FindOne()).Decode(&result)
func GetCallInfoByCaller(caller types.PID) (caller_pid types.PID, target_pid types.PID, ringing types.Bool) {
row := Postgres.QueryRow(`SELECT (caller_pid, target_pid, ringing) FROM ongoingcalls WHERE caller_pid = $1;`, caller)
err := row.Scan(&caller_pid, &target_pid, &ringing)
if err != nil {
if err == mongo.ErrNoDocuments {
return 0, 0, false
} else {
panic(err)
}
} else {
return uint32(result["caller_pid"].(int64)), uint32(result["target_pid"].(int64)), result["ringing"].(bool)
globals.Logger.Critical(err.Error())
return 0, 0, false
}
return caller_pid, target_pid, ringing
}
Loading

0 comments on commit 7f8fad8

Please sign in to comment.