mirror of
https://github.com/PretendoNetwork/friends.git
synced 2026-03-21 18:04:11 -05:00
feat: update config parsing and env var names
This commit is contained in:
parent
6fee83c989
commit
2c79a1228d
28
README.md
28
README.md
|
|
@ -84,16 +84,18 @@ All configuration options are handled via environment variables
|
|||
|
||||
`.env` files are supported
|
||||
|
||||
| Name | Description | Required |
|
||||
| --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------- |
|
||||
| `PN_FRIENDS_CONFIG_DATABASE_URI` | Fully qualified URI to your Postgres server (Example `postgres://username:password@localhost/friends?sslmode=disable`) | Yes |
|
||||
| `PN_FRIENDS_CONFIG_AES_KEY` | AES key used in tokens provided by the account server | Yes |
|
||||
| `PN_FRIENDS_CONFIG_GRPC_API_KEY` | API key for your GRPC server | No (Assumed to be an open gRPC API) |
|
||||
| `PN_FRIENDS_GRPC_SERVER_PORT` | Port for the GRPC server | Yes |
|
||||
| `PN_FRIENDS_AUTHENTICATION_SERVER_PORT` | Port for the authentication server | Yes |
|
||||
| `PN_FRIENDS_SECURE_SERVER_HOST` | Host name for the secure server (should point to the same address as the authentication server) | Yes |
|
||||
| `PN_FRIENDS_SECURE_SERVER_PORT` | Port for the secure server | Yes |
|
||||
| `PN_FRIENDS_ACCOUNT_GRPC_HOST` | Host name for your account server gRPC service | Yes |
|
||||
| `PN_FRIENDS_ACCOUNT_GRPC_PORT` | Port for your account server gRPC service | Yes |
|
||||
| `PN_FRIENDS_ACCOUNT_GRPC_API_KEY` | API key for your account server gRPC service | No (Assumed to be an open gRPC API) |
|
||||
| `PN_FRIENDS_CONFIG_HEALTH_CHECK_PORT` | Port for the basic UDP health check server | No |
|
||||
| Name | Description | Required |
|
||||
| ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------- |
|
||||
| `PN_FRIENDS_CONFIG_POSTGRES_URI` | Fully qualified URI to your Postgres server (Example `postgres://username:password@localhost/friends?sslmode=disable`) | Yes |
|
||||
| `PN_FRIENDS_CONFIG_POSTGRES_MAX_CONNECTIONS` | Postgres server max connections | Yes |
|
||||
| `PN_FRIENDS_CONFIG_AES_KEY` | AES key used in tokens provided by the account server | Yes |
|
||||
| `PN_FRIENDS_CONFIG_GRPC_API_KEY` | API key for your GRPC server | No (Assumed to be an open gRPC API) |
|
||||
| `PN_FRIENDS_CONFIG_GRPC_SERVER_PORT` | Port for the GRPC server | Yes |
|
||||
| `PN_FRIENDS_CONFIG_AUTHENTICATION_SERVER_PORT` | Port for the authentication server | Yes |
|
||||
| `PN_FRIENDS_CONFIG_SECURE_SERVER_HOST` | Host name for the secure server (should point to the same address as the authentication server) | Yes |
|
||||
| `PN_FRIENDS_CONFIG_SECURE_SERVER_PORT` | Port for the secure server | Yes |
|
||||
| `PN_FRIENDS_CONFIG_ACCOUNT_GRPC_HOST` | Host name for your account server gRPC service | Yes |
|
||||
| `PN_FRIENDS_CONFIG_ACCOUNT_GRPC_PORT` | Port for your account server gRPC service | Yes |
|
||||
| `PN_FRIENDS_CONFIG_ACCOUNT_GRPC_API_KEY` | API key for your account server gRPC service | No (Assumed to be an open gRPC API) |
|
||||
| `PN_FRIENDS_CONFIG_HEALTH_CHECK_PORT` | Port for the basic UDP health check server | No |
|
||||
| `PN_FRIENDS_CONFIG_ENABLE_BELLA` | Enables a debug user named "Bella" which is always assigned as your friend | No | No
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
|
||||
"github.com/PretendoNetwork/friends/globals"
|
||||
|
|
@ -14,7 +12,7 @@ var Manager *sqlmanager.SQLManager
|
|||
func ConnectPostgres() {
|
||||
var err error
|
||||
|
||||
Manager, err = sqlmanager.NewSQLManager("postgres", os.Getenv("PN_FRIENDS_CONFIG_DATABASE_URI"), int64(globals.DatabaseMaxConnections))
|
||||
Manager, err = sqlmanager.NewSQLManager("postgres", globals.Config.PostgresURI, globals.Config.PostgresMaxConnections)
|
||||
if err != nil {
|
||||
globals.Logger.Critical(err.Error())
|
||||
}
|
||||
|
|
|
|||
19
globals/config.go
Normal file
19
globals/config.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package globals
|
||||
|
||||
type config struct {
|
||||
PostgresURI string
|
||||
PostgresMaxConnections int64
|
||||
AESKey string
|
||||
GRPCAPIKey string `envconf:"optional"`
|
||||
GRPCServerPort uint16
|
||||
AuthenticationServerPort uint16
|
||||
SecureServerHost string
|
||||
SecureServerPort uint16
|
||||
AccountGRPCHost string
|
||||
AccountGRPCPort uint16
|
||||
AccountGRPCAPIKEY string `envconf:"optional"`
|
||||
HealthCheckPort uint16 `envconf:"optional"`
|
||||
EnableBella bool `envconf:"optional"`
|
||||
}
|
||||
|
||||
var Config *config
|
||||
303
globals/config_parser.go
Normal file
303
globals/config_parser.go
Normal file
|
|
@ -0,0 +1,303 @@
|
|||
package globals
|
||||
|
||||
// * NOTE: THIS IS ALL LIBRARY CODE, INTENDED TO BE REMOVED FROM THIS REPO IN THE FUTURE.
|
||||
// * THIS IS ONLY HERE FOR NOW SO I CAN PLAY AROUND WITH THE IDEA.
|
||||
|
||||
// * MESSING AROUND WITH THIS BECAUSE I DIDN'T REALLY LIKE THE WAY EXISTING CONFIG
|
||||
// * PARSERS WORKED, THEY ALL HAD WEIRD QUIRKS LIKE ODD SEMANTICS FOR STRUCT TAGS,
|
||||
// * COULDN'T HANDLE COMPLEX SLICES AND MAPS CLEANLY, ETC.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type fieldTagOptions struct {
|
||||
optional bool
|
||||
defaultValue string
|
||||
hasDefault bool
|
||||
envNameOverride string
|
||||
}
|
||||
|
||||
func parseFieldTag(tag string) fieldTagOptions {
|
||||
options := fieldTagOptions{}
|
||||
if tag == "" {
|
||||
return options
|
||||
}
|
||||
|
||||
if i := strings.Index(tag, "default:"); i != -1 {
|
||||
options.defaultValue = tag[i+8:]
|
||||
options.hasDefault = true
|
||||
tag = strings.TrimSuffix(tag[:i], ",")
|
||||
}
|
||||
|
||||
for _, part := range strings.Split(tag, ",") {
|
||||
part = strings.TrimSpace(part)
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if part == "optional" {
|
||||
options.optional = true
|
||||
} else if strings.HasPrefix(part, "env:") {
|
||||
options.envNameOverride = strings.TrimPrefix(part, "env:")
|
||||
}
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
type ConfigParser[T any] struct {
|
||||
config T
|
||||
prefix string
|
||||
initialisms map[string]bool
|
||||
allowedPluralInitialisms map[string]bool
|
||||
}
|
||||
|
||||
func (cp *ConfigParser[T]) toPascalCase(str string) string {
|
||||
lowercase := strings.ToLower(str)
|
||||
words := strings.Split(lowercase, "_")
|
||||
|
||||
var pascalCase strings.Builder
|
||||
|
||||
for _, word := range words {
|
||||
pascalCase.WriteString(cp.capitalizeWord(word))
|
||||
}
|
||||
|
||||
return pascalCase.String()
|
||||
}
|
||||
|
||||
func (cp *ConfigParser[T]) capitalizeWord(word string) string {
|
||||
if word == "" {
|
||||
return word
|
||||
}
|
||||
|
||||
upper := strings.ToUpper(word)
|
||||
endsWithS := strings.HasSuffix(upper, "S")
|
||||
withoutS := strings.TrimSuffix(upper, "S")
|
||||
|
||||
if cp.initialisms[withoutS] && endsWithS && cp.allowedPluralInitialisms[withoutS] {
|
||||
return withoutS + "s"
|
||||
} else if cp.initialisms[withoutS] {
|
||||
return upper
|
||||
}
|
||||
|
||||
r, n := utf8.DecodeRuneInString(word)
|
||||
if r == utf8.RuneError && n == 0 {
|
||||
return word
|
||||
}
|
||||
|
||||
return string(unicode.ToUpper(r)) + word[n:]
|
||||
}
|
||||
|
||||
func (cp *ConfigParser[T]) toEnvVarName(fieldName string) string {
|
||||
var result strings.Builder
|
||||
if cp.prefix != "" {
|
||||
result.WriteString(cp.prefix)
|
||||
}
|
||||
|
||||
runes := []rune(fieldName)
|
||||
for i := 0; i < len(runes); i++ {
|
||||
r := runes[i]
|
||||
|
||||
if r == 's' && i > 0 && unicode.IsUpper(runes[i-1]) {
|
||||
result.WriteRune('S')
|
||||
continue
|
||||
}
|
||||
|
||||
if i > 0 && unicode.IsUpper(r) {
|
||||
prevIsLower := unicode.IsLower(runes[i-1])
|
||||
nextIsLower := i+1 < len(runes) && unicode.IsLower(runes[i+1])
|
||||
nextIsPluralS := i+1 < len(runes) && runes[i+1] == 's' && (i+2 >= len(runes) || unicode.IsUpper(runes[i+2]))
|
||||
|
||||
if prevIsLower || (nextIsLower && !nextIsPluralS) {
|
||||
result.WriteRune('_')
|
||||
}
|
||||
}
|
||||
|
||||
result.WriteRune(unicode.ToUpper(r))
|
||||
}
|
||||
|
||||
return result.String()
|
||||
}
|
||||
|
||||
func (cp *ConfigParser[T]) SetPrefix(prefix string) *ConfigParser[T] {
|
||||
cp.prefix = prefix + "_"
|
||||
|
||||
return cp
|
||||
}
|
||||
|
||||
func (cp *ConfigParser[T]) AddInitialisms(initialisms map[string]bool) *ConfigParser[T] {
|
||||
for key, value := range initialisms {
|
||||
cp.initialisms[key] = value
|
||||
}
|
||||
|
||||
return cp
|
||||
}
|
||||
|
||||
func (cp *ConfigParser[T]) ParseFromEnv() T {
|
||||
envAsPascal := make(map[string]string)
|
||||
|
||||
for _, env := range os.Environ() {
|
||||
pair := strings.SplitN(env, "=", 2)
|
||||
key := pair[0]
|
||||
value := strings.TrimSpace(pair[1])
|
||||
|
||||
if !strings.HasPrefix(key, cp.prefix) {
|
||||
continue
|
||||
}
|
||||
|
||||
fieldName := cp.toPascalCase(strings.TrimPrefix(key, cp.prefix))
|
||||
|
||||
envAsPascal[fieldName] = value
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(cp.config).Elem()
|
||||
t := v.Type()
|
||||
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
fieldName := field.Name
|
||||
fieldValue := v.Field(i)
|
||||
fieldOptions := parseFieldTag(field.Tag.Get("envconf"))
|
||||
errors := make([]string, 0)
|
||||
warnings := make([]string, 0)
|
||||
|
||||
envVarName := cp.toEnvVarName(fieldName)
|
||||
envValue, exists := envAsPascal[fieldName]
|
||||
|
||||
if fieldOptions.envNameOverride != "" {
|
||||
envVarName = fieldOptions.envNameOverride
|
||||
envValue, exists = os.LookupEnv(envVarName)
|
||||
}
|
||||
|
||||
if !exists && fieldOptions.hasDefault {
|
||||
envValue = fieldOptions.defaultValue
|
||||
exists = true
|
||||
|
||||
warnings = append(warnings, fmt.Sprintf("Optional field %s does not have a corresponding %s environment variable. Using default value \"%s\"", fieldName, envVarName, envValue))
|
||||
}
|
||||
|
||||
if exists && fieldValue.CanSet() {
|
||||
fieldType := fieldValue.Type()
|
||||
switch fieldValue.Kind() {
|
||||
case reflect.String:
|
||||
fieldValue.SetString(envValue)
|
||||
case reflect.Bool:
|
||||
if boolVal, err := strconv.ParseBool(envValue); err == nil {
|
||||
fieldValue.SetBool(boolVal)
|
||||
}
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
if intVal, err := strconv.ParseInt(envValue, 10, fieldValue.Type().Bits()); err == nil {
|
||||
fieldValue.SetInt(intVal)
|
||||
} else {
|
||||
errors = append(errors, fmt.Sprintf("Error parsing %s: %v", envVarName, err))
|
||||
}
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
if uintVal, err := strconv.ParseUint(envValue, 10, fieldValue.Type().Bits()); err == nil {
|
||||
fieldValue.SetUint(uintVal)
|
||||
} else {
|
||||
errors = append(errors, fmt.Sprintf("Error parsing %s: %v", envVarName, err))
|
||||
}
|
||||
case reflect.Float32, reflect.Float64:
|
||||
if floatVal, err := strconv.ParseFloat(envValue, fieldValue.Type().Bits()); err == nil {
|
||||
fieldValue.SetFloat(floatVal)
|
||||
} else {
|
||||
errors = append(errors, fmt.Sprintf("Error parsing %s: %v", envVarName, err))
|
||||
}
|
||||
case reflect.Slice, reflect.Map:
|
||||
sliceOrMap := reflect.New(fieldType)
|
||||
if err := json.Unmarshal([]byte(envValue), sliceOrMap.Interface()); err != nil {
|
||||
errors = append(errors, fmt.Sprintf("Error parsing %s: %v", envVarName, err))
|
||||
}
|
||||
fieldValue.Set(sliceOrMap.Elem())
|
||||
}
|
||||
} else if !exists {
|
||||
if !fieldOptions.optional {
|
||||
errors = append(errors, fmt.Sprintf("Required field %s does not have a corresponding %s environment variable", fieldName, envVarName))
|
||||
} else if !fieldOptions.hasDefault {
|
||||
warnings = append(warnings, fmt.Sprintf("Optional field %s does not have a corresponding %s environment variable and no default value. Skipping", fieldName, envVarName))
|
||||
}
|
||||
}
|
||||
|
||||
if len(warnings) != 0 {
|
||||
for _, warning := range warnings {
|
||||
fmt.Println(warning)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errors) != 0 {
|
||||
for _, err := range errors {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
return cp.config
|
||||
}
|
||||
|
||||
func NewConfigParser[T any](config T) *ConfigParser[T] {
|
||||
return &ConfigParser[T]{
|
||||
config: config,
|
||||
initialisms: map[string]bool{ // * https://go.googlesource.com/lint/+/818c5a804067/lint.go#767
|
||||
"ACL": true,
|
||||
"API": true,
|
||||
"ASCII": true,
|
||||
"CPU": true,
|
||||
"CSS": true,
|
||||
"DNS": true,
|
||||
"EOF": true,
|
||||
"GUID": true,
|
||||
"HTML": true,
|
||||
"HTTP": true,
|
||||
"HTTPS": true,
|
||||
"ID": true,
|
||||
"IP": true,
|
||||
"JSON": true,
|
||||
"LHS": true,
|
||||
"QPS": true,
|
||||
"RAM": true,
|
||||
"RHS": true,
|
||||
"RPC": true,
|
||||
"SLA": true,
|
||||
"SMTP": true,
|
||||
"SQL": true,
|
||||
"SSH": true,
|
||||
"TCP": true,
|
||||
"TLS": true,
|
||||
"TTL": true,
|
||||
"UDP": true,
|
||||
"UI": true,
|
||||
"UID": true,
|
||||
"UUID": true,
|
||||
"URI": true,
|
||||
"URL": true,
|
||||
"UTF8": true,
|
||||
"VM": true,
|
||||
"XML": true,
|
||||
"XMPP": true,
|
||||
"XSRF": true,
|
||||
"XSS": true,
|
||||
"NEX": true, // * Start of our custom ones
|
||||
"GRPC": true,
|
||||
"AES": true,
|
||||
},
|
||||
allowedPluralInitialisms: map[string]bool{
|
||||
"API": true,
|
||||
"GUID": true,
|
||||
"ID": true,
|
||||
"IP": true,
|
||||
"UUID": true,
|
||||
"URI": true,
|
||||
"URL": true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -23,4 +23,3 @@ var AESKey []byte
|
|||
var GRPCAccountClientConnection *grpc.ClientConn
|
||||
var GRPCAccountClient pb.AccountClient
|
||||
var GRPCAccountCommonMetadata metadata.MD
|
||||
var DatabaseMaxConnections int
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ package grpc
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/PretendoNetwork/friends/globals"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
|
@ -15,7 +15,7 @@ func apiKeyInterceptor(ctx context.Context, req interface{}, info *grpc.UnarySer
|
|||
if ok {
|
||||
apiKeyHeader := md.Get("X-API-Key")
|
||||
|
||||
if len(apiKeyHeader) == 0 || apiKeyHeader[0] != os.Getenv("PN_FRIENDS_CONFIG_GRPC_API_KEY") {
|
||||
if len(apiKeyHeader) == 0 || apiKeyHeader[0] != globals.Config.GRPCAPIKey {
|
||||
return nil, errors.New("Missing or invalid API key")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/PretendoNetwork/friends/globals"
|
||||
pb "github.com/PretendoNetwork/grpc/go/friends"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
|
@ -15,7 +15,7 @@ type gRPCFriendsServer struct {
|
|||
}
|
||||
|
||||
func StartGRPCServer() {
|
||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%s", os.Getenv("PN_FRIENDS_GRPC_SERVER_PORT")))
|
||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", globals.Config.GRPCServerPort))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
|
|
|||
113
init.go
113
init.go
|
|
@ -1,12 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/PretendoNetwork/friends/database"
|
||||
|
|
@ -27,7 +25,6 @@ func init() {
|
|||
globals.Logger = plogger.NewLogger()
|
||||
globals.ConnectedUsers = nex.NewMutexMap[uint32, *types.ConnectedUser]()
|
||||
|
||||
// * Setup RSA private key for token parsing
|
||||
var err error
|
||||
|
||||
err = godotenv.Load()
|
||||
|
|
@ -35,31 +32,7 @@ func init() {
|
|||
globals.Logger.Warningf("Error loading .env file: %s", err.Error())
|
||||
}
|
||||
|
||||
postgresURI := os.Getenv("PN_FRIENDS_CONFIG_DATABASE_URI")
|
||||
databaseMaxConnectionsStr := cmp.Or(os.Getenv("PN_FRIENDS_CONFIG_DATABASE_MAX_CONNECTIONS"), "100")
|
||||
aesKey := os.Getenv("PN_FRIENDS_CONFIG_AES_KEY")
|
||||
grpcAPIKey := os.Getenv("PN_FRIENDS_CONFIG_GRPC_API_KEY")
|
||||
grpcServerPort := os.Getenv("PN_FRIENDS_GRPC_SERVER_PORT")
|
||||
authenticationServerPort := os.Getenv("PN_FRIENDS_AUTHENTICATION_SERVER_PORT")
|
||||
secureServerHost := os.Getenv("PN_FRIENDS_SECURE_SERVER_HOST")
|
||||
secureServerPort := os.Getenv("PN_FRIENDS_SECURE_SERVER_PORT")
|
||||
accountGRPCHost := os.Getenv("PN_FRIENDS_ACCOUNT_GRPC_HOST")
|
||||
accountGRPCPort := os.Getenv("PN_FRIENDS_ACCOUNT_GRPC_PORT")
|
||||
accountGRPCAPIKey := os.Getenv("PN_FRIENDS_ACCOUNT_GRPC_API_KEY")
|
||||
|
||||
if strings.TrimSpace(postgresURI) == "" {
|
||||
globals.Logger.Error("PN_FRIENDS_CONFIG_DATABASE_URI environment variable not set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
databaseMaxConnections, err := strconv.Atoi(databaseMaxConnectionsStr)
|
||||
|
||||
if err != nil {
|
||||
globals.Logger.Errorf("PN_FRIENDS_CONFIG_DATABASE_MAX_CONNECTIONS is not a valid number. Got %s", databaseMaxConnectionsStr)
|
||||
os.Exit(0)
|
||||
} else {
|
||||
globals.DatabaseMaxConnections = databaseMaxConnections
|
||||
}
|
||||
globals.Config = globals.NewConfigParser(globals.Config).SetPrefix("PN_FRIENDS_CONFIG").ParseFromEnv()
|
||||
|
||||
kerberosPassword := make([]byte, 0x10)
|
||||
_, err = rand.Read(kerberosPassword)
|
||||
|
|
@ -73,89 +46,21 @@ func init() {
|
|||
globals.AuthenticationServerAccount = nex.NewAccount(nex_types.NewPID(1), "Quazal Authentication", globals.KerberosPassword, true) // * Is "true" correct here?
|
||||
globals.SecureServerAccount = nex.NewAccount(nex_types.NewPID(2), "Quazal Rendez-Vous", globals.KerberosPassword, true) // * Is "true" correct here?
|
||||
globals.GuestAccount = nex.NewAccount(nex_types.NewPID(100), "guest", "MMQea3n!fsik", true) // * Is "true" correct here? Guest account password is always the same, known to all consoles. Only allow on the friends server
|
||||
|
||||
if strings.TrimSpace(aesKey) == "" {
|
||||
globals.Logger.Error("PN_FRIENDS_CONFIG_AES_KEY environment variable not set")
|
||||
globals.AESKey, err = hex.DecodeString(globals.Config.AESKey)
|
||||
if err != nil {
|
||||
globals.Logger.Criticalf("Failed to decode AES key: %v", err)
|
||||
os.Exit(0)
|
||||
} else {
|
||||
globals.AESKey, err = hex.DecodeString(os.Getenv("PN_FRIENDS_CONFIG_AES_KEY"))
|
||||
if err != nil {
|
||||
globals.Logger.Criticalf("Failed to decode AES key: %v", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.TrimSpace(grpcAPIKey) == "" {
|
||||
if strings.TrimSpace(globals.Config.GRPCAPIKey) == "" {
|
||||
globals.Logger.Warning("Insecure gRPC server detected. PN_FRIENDS_CONFIG_GRPC_API_KEY environment variable not set")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(grpcServerPort) == "" {
|
||||
globals.Logger.Error("PN_FRIENDS_GRPC_SERVER_PORT environment variable not set")
|
||||
os.Exit(0)
|
||||
if strings.TrimSpace(globals.Config.AccountGRPCAPIKEY) == "" {
|
||||
globals.Logger.Warning("Insecure gRPC server detected. PN_FRIENDS_CONFIG_ACCOUNT_GRPC_API_KEY environment variable not set")
|
||||
}
|
||||
|
||||
if port, err := strconv.Atoi(grpcServerPort); err != nil {
|
||||
globals.Logger.Errorf("PN_FRIENDS_GRPC_SERVER_PORT is not a valid port. Expected 0-65535, got %s", grpcServerPort)
|
||||
os.Exit(0)
|
||||
} else if port < 0 || port > 65535 {
|
||||
globals.Logger.Errorf("PN_FRIENDS_GRPC_SERVER_PORT is not a valid port. Expected 0-65535, got %s", grpcServerPort)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(authenticationServerPort) == "" {
|
||||
globals.Logger.Error("PN_FRIENDS_AUTHENTICATION_SERVER_PORT environment variable not set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if port, err := strconv.Atoi(authenticationServerPort); err != nil {
|
||||
globals.Logger.Errorf("PN_FRIENDS_AUTHENTICATION_SERVER_PORT is not a valid port. Expected 0-65535, got %s", authenticationServerPort)
|
||||
os.Exit(0)
|
||||
} else if port < 0 || port > 65535 {
|
||||
globals.Logger.Errorf("PN_FRIENDS_AUTHENTICATION_SERVER_PORT is not a valid port. Expected 0-65535, got %s", authenticationServerPort)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(secureServerHost) == "" {
|
||||
globals.Logger.Error("PN_FRIENDS_SECURE_SERVER_HOST environment variable not set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(secureServerPort) == "" {
|
||||
globals.Logger.Error("PN_FRIENDS_SECURE_SERVER_PORT environment variable not set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if port, err := strconv.Atoi(secureServerPort); err != nil {
|
||||
globals.Logger.Errorf("PN_FRIENDS_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort)
|
||||
os.Exit(0)
|
||||
} else if port < 0 || port > 65535 {
|
||||
globals.Logger.Errorf("PN_FRIENDS_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(accountGRPCHost) == "" {
|
||||
globals.Logger.Error("PN_FRIENDS_ACCOUNT_GRPC_HOST environment variable not set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(accountGRPCPort) == "" {
|
||||
globals.Logger.Error("PN_FRIENDS_ACCOUNT_GRPC_PORT environment variable not set")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if port, err := strconv.Atoi(accountGRPCPort); err != nil {
|
||||
globals.Logger.Errorf("PN_FRIENDS_ACCOUNT_GRPC_PORT is not a valid port. Expected 0-65535, got %s", accountGRPCPort)
|
||||
os.Exit(0)
|
||||
} else if port < 0 || port > 65535 {
|
||||
globals.Logger.Errorf("PN_FRIENDS_ACCOUNT_GRPC_PORT is not a valid port. Expected 0-65535, got %s", accountGRPCPort)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(accountGRPCAPIKey) == "" {
|
||||
globals.Logger.Warning("Insecure gRPC server detected. PN_FRIENDS_ACCOUNT_GRPC_API_KEY environment variable not set")
|
||||
}
|
||||
|
||||
globals.GRPCAccountClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", accountGRPCHost, accountGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
globals.GRPCAccountClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", globals.Config.AccountGRPCHost, globals.Config.AccountGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
globals.Logger.Criticalf("Failed to connect to account gRPC server: %v", err)
|
||||
os.Exit(0)
|
||||
|
|
@ -163,7 +68,7 @@ func init() {
|
|||
|
||||
globals.GRPCAccountClient = pb.NewAccountClient(globals.GRPCAccountClientConnection)
|
||||
globals.GRPCAccountCommonMetadata = metadata.Pairs(
|
||||
"X-API-Key", accountGRPCAPIKey,
|
||||
"X-API-Key", globals.Config.AccountGRPCAPIKEY,
|
||||
)
|
||||
|
||||
database.ConnectPostgres()
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
package nex
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/PretendoNetwork/friends/globals"
|
||||
"github.com/PretendoNetwork/nex-go/v2"
|
||||
)
|
||||
|
|
@ -11,12 +8,9 @@ import (
|
|||
var serverBuildString string
|
||||
|
||||
func StartAuthenticationServer() {
|
||||
prudpPort, _ := strconv.Atoi(os.Getenv("PN_FRIENDS_AUTHENTICATION_SERVER_PORT"))
|
||||
|
||||
healthCheckPortString := os.Getenv("PN_FRIENDS_CONFIG_HEALTH_CHECK_PORT")
|
||||
if healthCheckPortString != "" {
|
||||
healthCheckPort, _ := strconv.Atoi(healthCheckPortString)
|
||||
go nex.EnableBasicUDPHealthCheck(healthCheckPort)
|
||||
if globals.Config.HealthCheckPort != 0 {
|
||||
go nex.EnableBasicUDPHealthCheck(int(globals.Config.HealthCheckPort))
|
||||
}
|
||||
|
||||
globals.AuthenticationServer = nex.NewPRUDPServer()
|
||||
|
|
@ -33,5 +27,5 @@ func StartAuthenticationServer() {
|
|||
globals.AuthenticationServer.SessionKeyLength = 16
|
||||
globals.AuthenticationServer.AccessKey = "ridfebb9"
|
||||
globals.AuthenticationServer.BindPRUDPEndPoint(globals.AuthenticationEndpoint)
|
||||
globals.AuthenticationServer.Listen(prudpPort)
|
||||
globals.AuthenticationServer.Listen(int(globals.Config.AuthenticationServerPort))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
package nex_friends_wiiu
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/PretendoNetwork/friends/database"
|
||||
database_wiiu "github.com/PretendoNetwork/friends/database/wiiu"
|
||||
"github.com/PretendoNetwork/friends/globals"
|
||||
|
|
@ -93,7 +91,7 @@ func UpdateAndGetAllInformation(err error, packet nex.PacketInterface, callID ui
|
|||
|
||||
notifications_wiiu.SendPresenceUpdate(presence)
|
||||
|
||||
if os.Getenv("PN_FRIENDS_CONFIG_ENABLE_BELLA") == "true" {
|
||||
if globals.Config.EnableBella {
|
||||
bella := friends_wiiu_types.NewFriendInfo()
|
||||
|
||||
bella.NNAInfo = friends_wiiu_types.NewNNAInfo()
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
package nex
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/PretendoNetwork/friends/globals"
|
||||
"github.com/PretendoNetwork/nex-go/v2/constants"
|
||||
"github.com/PretendoNetwork/nex-go/v2/types"
|
||||
|
|
@ -15,12 +12,10 @@ func registerCommonAuthenticationServerProtocols() {
|
|||
ticketGrantingProtocol := ticket_granting.NewProtocol()
|
||||
commonTicketGrantingProtocol := common_ticket_granting.NewCommonProtocol(ticketGrantingProtocol)
|
||||
|
||||
port, _ := strconv.Atoi(os.Getenv("PN_FRIENDS_SECURE_SERVER_PORT"))
|
||||
|
||||
secureStationURL := types.NewStationURL("")
|
||||
secureStationURL.SetURLType(constants.StationURLPRUDPS)
|
||||
secureStationURL.SetAddress(os.Getenv("PN_FRIENDS_SECURE_SERVER_HOST"))
|
||||
secureStationURL.SetPortNumber(uint16(port))
|
||||
secureStationURL.SetAddress(globals.Config.SecureServerHost)
|
||||
secureStationURL.SetPortNumber(globals.Config.SecureServerPort)
|
||||
secureStationURL.SetConnectionID(1)
|
||||
secureStationURL.SetPrincipalID(types.NewPID(2))
|
||||
secureStationURL.SetStreamID(1)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
package nex
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
database_3ds "github.com/PretendoNetwork/friends/database/3ds"
|
||||
|
|
@ -17,8 +15,6 @@ import (
|
|||
)
|
||||
|
||||
func StartSecureServer() {
|
||||
port, _ := strconv.Atoi(os.Getenv("PN_FRIENDS_SECURE_SERVER_PORT"))
|
||||
|
||||
globals.SecureServer = nex.NewPRUDPServer()
|
||||
globals.SecureEndpoint = nex.NewPRUDPEndPoint(1)
|
||||
|
||||
|
|
@ -66,5 +62,5 @@ func StartSecureServer() {
|
|||
globals.SecureServer.SessionKeyLength = 16
|
||||
globals.SecureServer.AccessKey = "ridfebb9"
|
||||
globals.SecureServer.BindPRUDPEndPoint(globals.SecureEndpoint)
|
||||
globals.SecureServer.Listen(port)
|
||||
globals.SecureServer.Listen(int(globals.Config.SecureServerPort))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user