Ver código fonte

replace config.Address with net.JoinHostPort

Replace the config.Address function with the go stdlib function
net.JoinHostPort that does the same exact thing. The only difference
here is that ports are strings instead of ints.
Mike 2 anos atrás
pai
commit
b1620417bd

+ 5 - 11
config/config.go

@@ -1,22 +1,16 @@
 package config
 
-import "fmt"
-
 //go:generate go run github.com/mk6i/retro-aim-server/cmd/config_generator windows settings.bat
 //go:generate go run github.com/mk6i/retro-aim-server/cmd/config_generator unix settings.env
 type Config struct {
-	AlertPort   int    `envconfig:"ALERT_PORT" default:"5194" description:"The port that the Alert service binds to."`
-	AuthPort    int    `envconfig:"AUTH_PORT" default:"5190" description:"The port that the auth service binds to."`
-	BOSPort     int    `envconfig:"BOS_PORT" default:"5191" description:"The port that the BOS service binds to."`
-	ChatNavPort int    `envconfig:"CHAT_NAV_PORT" default:"5193" description:"The port that the chat nav service binds to."`
-	ChatPort    int    `envconfig:"CHAT_PORT" default:"5192" description:"The port that the chat service binds to."`
+	AlertPort   string `envconfig:"ALERT_PORT" default:"5194" description:"The port that the Alert service binds to."`
+	AuthPort    string `envconfig:"AUTH_PORT" default:"5190" description:"The port that the auth service binds to."`
+	BOSPort     string `envconfig:"BOS_PORT" default:"5191" description:"The port that the BOS service binds to."`
+	ChatNavPort string `envconfig:"CHAT_NAV_PORT" default:"5193" description:"The port that the chat nav service binds to."`
+	ChatPort    string `envconfig:"CHAT_PORT" default:"5192" description:"The port that the chat service binds to."`
 	DBPath      string `envconfig:"DB_PATH" default:"oscar.sqlite" description:"The path to the SQLite database file. The file and DB schema are auto-created if they doesn't exist."`
 	DisableAuth bool   `envconfig:"DISABLE_AUTH" default:"true" description:"Disable password check and auto-create new users at login time. Useful for quickly creating new accounts during development without having to register new users via the management API."`
 	FailFast    bool   `envconfig:"FAIL_FAST" default:"false" description:"Crash the server in case it encounters a client message type it doesn't recognize. This makes failures obvious for debugging purposes."`
 	LogLevel    string `envconfig:"LOG_LEVEL" default:"info" description:"Set logging granularity. Possible values: 'trace', 'debug', 'info', 'warn', 'error'."`
 	OSCARHost   string `envconfig:"OSCAR_HOST" default:"127.0.0.1" description:"The hostname that AIM clients connect to in order to reach OSCAR services (auth, BOS, BUCP, etc). Make sure the hostname is reachable by all clients. For local development, the default loopback address should work provided the server and AIM client(s) are running on the same machine. For LAN-only clients, a private IP address (e.g. 192.168..) or hostname should suffice. For clients connecting over the Internet, specify your public IP address and ensure that TCP ports 5190-5194 are open on your firewall."`
 }
-
-func Address(host string, port int) string {
-	return fmt.Sprintf("%s:%d", host, port)
-}

+ 2 - 1
foodgroup/auth.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"context"
 	"errors"
+	"net"
 
 	"github.com/mk6i/retro-aim-server/config"
 	"github.com/mk6i/retro-aim-server/state"
@@ -250,7 +251,7 @@ func (s AuthService) login(
 		return wire.TLVRestBlock{
 			TLVList: []wire.TLV{
 				wire.NewTLV(wire.LoginTLVTagsScreenName, screenName),
-				wire.NewTLV(wire.LoginTLVTagsReconnectHere, config.Address(s.config.OSCARHost, s.config.BOSPort)),
+				wire.NewTLV(wire.LoginTLVTagsReconnectHere, net.JoinHostPort(s.config.OSCARHost, s.config.BOSPort)),
 				wire.NewTLV(wire.LoginTLVTagsAuthorizationCookie, sess.ID()),
 			},
 		}, nil

+ 13 - 13
foodgroup/auth_test.go

@@ -44,7 +44,7 @@ func TestAuthService_BUCPLoginRequest(t *testing.T) {
 			name: "user provides valid credentials and logs in successfully",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				BOSPort:   1234,
+				BOSPort:   "1234",
 			},
 			inputSNAC: wire.SNAC_0x17_0x02_BUCPLoginRequest{
 				TLVRestBlock: wire.TLVRestBlock{
@@ -93,7 +93,7 @@ func TestAuthService_BUCPLoginRequest(t *testing.T) {
 			name: "user logs in with non-existent screen name--account is created and logged in successfully",
 			cfg: config.Config{
 				OSCARHost:   "127.0.0.1",
-				BOSPort:     1234,
+				BOSPort:     "1234",
 				DisableAuth: true,
 			},
 			inputSNAC: wire.SNAC_0x17_0x02_BUCPLoginRequest{
@@ -151,7 +151,7 @@ func TestAuthService_BUCPLoginRequest(t *testing.T) {
 			name: "user logs in with invalid password--account is created and logged in successfully",
 			cfg: config.Config{
 				OSCARHost:   "127.0.0.1",
-				BOSPort:     1234,
+				BOSPort:     "1234",
 				DisableAuth: true,
 			},
 			inputSNAC: wire.SNAC_0x17_0x02_BUCPLoginRequest{
@@ -209,7 +209,7 @@ func TestAuthService_BUCPLoginRequest(t *testing.T) {
 			name: "user logs in with invalid password--account already exists and logged in successfully",
 			cfg: config.Config{
 				OSCARHost:   "127.0.0.1",
-				BOSPort:     1234,
+				BOSPort:     "1234",
 				DisableAuth: true,
 			},
 			inputSNAC: wire.SNAC_0x17_0x02_BUCPLoginRequest{
@@ -330,7 +330,7 @@ func TestAuthService_BUCPLoginRequest(t *testing.T) {
 			name: "user provides invalid password and receives invalid login response",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				BOSPort:   1234,
+				BOSPort:   "1234",
 			},
 			inputSNAC: wire.SNAC_0x17_0x02_BUCPLoginRequest{
 				TLVRestBlock: wire.TLVRestBlock{
@@ -456,7 +456,7 @@ func TestAuthService_FLAPLoginResponse(t *testing.T) {
 			name: "user provides valid credentials and logs in successfully",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				BOSPort:   1234,
+				BOSPort:   "1234",
 			},
 			inputSNAC: wire.FLAPSignonFrame{
 				TLVRestBlock: wire.TLVRestBlock{
@@ -497,7 +497,7 @@ func TestAuthService_FLAPLoginResponse(t *testing.T) {
 			name: "user logs in with non-existent screen name--account is created and logged in successfully",
 			cfg: config.Config{
 				OSCARHost:   "127.0.0.1",
-				BOSPort:     1234,
+				BOSPort:     "1234",
 				DisableAuth: true,
 			},
 			inputSNAC: wire.FLAPSignonFrame{
@@ -547,7 +547,7 @@ func TestAuthService_FLAPLoginResponse(t *testing.T) {
 			name: "user logs in with invalid password--account is created and logged in successfully",
 			cfg: config.Config{
 				OSCARHost:   "127.0.0.1",
-				BOSPort:     1234,
+				BOSPort:     "1234",
 				DisableAuth: true,
 			},
 			inputSNAC: wire.FLAPSignonFrame{
@@ -597,7 +597,7 @@ func TestAuthService_FLAPLoginResponse(t *testing.T) {
 			name: "user logs in with invalid password--account already exists and logged in successfully",
 			cfg: config.Config{
 				OSCARHost:   "127.0.0.1",
-				BOSPort:     1234,
+				BOSPort:     "1234",
 				DisableAuth: true,
 			},
 			inputSNAC: wire.FLAPSignonFrame{
@@ -710,7 +710,7 @@ func TestAuthService_FLAPLoginResponse(t *testing.T) {
 			name: "user provides invalid password and receives invalid login response",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				BOSPort:   1234,
+				BOSPort:   "1234",
 			},
 			inputSNAC: wire.FLAPSignonFrame{
 				TLVRestBlock: wire.TLVRestBlock{
@@ -816,7 +816,7 @@ func TestAuthService_BUCPChallengeRequest(t *testing.T) {
 			name: "login with valid username, expect OK login response",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				BOSPort:   1234,
+				BOSPort:   "1234",
 			},
 			inputSNAC: wire.SNAC_0x17_0x06_BUCPChallengeRequest{
 				TLVRestBlock: wire.TLVRestBlock{
@@ -852,7 +852,7 @@ func TestAuthService_BUCPChallengeRequest(t *testing.T) {
 			name: "login with invalid username, expect OK login response (Cfg.DisableAuth=true)",
 			cfg: config.Config{
 				OSCARHost:   "127.0.0.1",
-				BOSPort:     1234,
+				BOSPort:     "1234",
 				DisableAuth: true,
 			},
 			inputSNAC: wire.SNAC_0x17_0x06_BUCPChallengeRequest{
@@ -886,7 +886,7 @@ func TestAuthService_BUCPChallengeRequest(t *testing.T) {
 			name: "login with invalid username, expect failed login response (Cfg.DisableAuth=false)",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				BOSPort:   1234,
+				BOSPort:   "1234",
 			},
 			inputSNAC: wire.SNAC_0x17_0x06_BUCPChallengeRequest{
 				TLVRestBlock: wire.TLVRestBlock{

+ 4 - 3
foodgroup/oservice.go

@@ -6,6 +6,7 @@ import (
 	"errors"
 	"fmt"
 	"log/slog"
+	"net"
 	"time"
 
 	"github.com/mk6i/retro-aim-server/config"
@@ -529,7 +530,7 @@ func (s OServiceServiceForBOS) ServiceRequest(_ context.Context, sess *state.Ses
 			Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
 				TLVRestBlock: wire.TLVRestBlock{
 					TLVList: wire.TLVList{
-						wire.NewTLV(wire.OServiceTLVTagsReconnectHere, config.Address(s.cfg.OSCARHost, s.cfg.AlertPort)),
+						wire.NewTLV(wire.OServiceTLVTagsReconnectHere, net.JoinHostPort(s.cfg.OSCARHost, s.cfg.AlertPort)),
 						wire.NewTLV(wire.OServiceTLVTagsLoginCookie, sess.ID()),
 						wire.NewTLV(wire.OServiceTLVTagsGroupID, wire.Alert),
 						wire.NewTLV(wire.OServiceTLVTagsSSLCertName, ""),
@@ -548,7 +549,7 @@ func (s OServiceServiceForBOS) ServiceRequest(_ context.Context, sess *state.Ses
 			Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
 				TLVRestBlock: wire.TLVRestBlock{
 					TLVList: wire.TLVList{
-						wire.NewTLV(wire.OServiceTLVTagsReconnectHere, config.Address(s.cfg.OSCARHost, s.cfg.ChatNavPort)),
+						wire.NewTLV(wire.OServiceTLVTagsReconnectHere, net.JoinHostPort(s.cfg.OSCARHost, s.cfg.ChatNavPort)),
 						wire.NewTLV(wire.OServiceTLVTagsLoginCookie, sess.ID()),
 						wire.NewTLV(wire.OServiceTLVTagsGroupID, wire.ChatNav),
 						wire.NewTLV(wire.OServiceTLVTagsSSLCertName, ""),
@@ -584,7 +585,7 @@ func (s OServiceServiceForBOS) ServiceRequest(_ context.Context, sess *state.Ses
 			Body: wire.SNAC_0x01_0x05_OServiceServiceResponse{
 				TLVRestBlock: wire.TLVRestBlock{
 					TLVList: wire.TLVList{
-						wire.NewTLV(wire.OServiceTLVTagsReconnectHere, config.Address(s.cfg.OSCARHost, s.cfg.ChatPort)),
+						wire.NewTLV(wire.OServiceTLVTagsReconnectHere, net.JoinHostPort(s.cfg.OSCARHost, s.cfg.ChatPort)),
 						wire.NewTLV(wire.OServiceTLVTagsLoginCookie, chatLoginCookie{
 							Cookie: room.Cookie,
 							SessID: sess.ID(),

+ 4 - 4
foodgroup/oservice_test.go

@@ -52,7 +52,7 @@ func TestOServiceServiceForBOS_ServiceRequest(t *testing.T) {
 			name: "request info for connecting to chat nav, return chat nav connection metadata",
 			cfg: config.Config{
 				OSCARHost:   "127.0.0.1",
-				ChatNavPort: 1234,
+				ChatNavPort: "1234",
 			},
 			userSession: newTestSession("user_screen_name", sessOptCannedID),
 			inputSNAC: wire.SNACMessage{
@@ -86,7 +86,7 @@ func TestOServiceServiceForBOS_ServiceRequest(t *testing.T) {
 			name: "request info for connecting to alert svc, return alert svc connection metadata",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				AlertPort: 1234,
+				AlertPort: "1234",
 			},
 			userSession: newTestSession("user_screen_name", sessOptCannedID),
 			inputSNAC: wire.SNACMessage{
@@ -120,7 +120,7 @@ func TestOServiceServiceForBOS_ServiceRequest(t *testing.T) {
 			name: "request info for connecting to chat room, return chat service and chat room metadata",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				ChatPort:  1234,
+				ChatPort:  "1234",
 			},
 			chatRoom: &state.ChatRoom{
 				CreateTime:     time.UnixMilli(0),
@@ -174,7 +174,7 @@ func TestOServiceServiceForBOS_ServiceRequest(t *testing.T) {
 			name: "request info for connecting to non-existent chat room, return ErrChatRoomNotFound",
 			cfg: config.Config{
 				OSCARHost: "127.0.0.1",
-				ChatPort:  1234,
+				ChatPort:  "1234",
 			},
 			chatRoom:    nil,
 			userSession: newTestSession("user_screen_name", sessOptCannedID),

+ 2 - 2
server/http/mgmt_api.go

@@ -5,12 +5,12 @@ import (
 	"errors"
 	"fmt"
 	"log/slog"
+	"net"
 	"net/http"
 	"os"
 
 	"github.com/google/uuid"
 
-	"github.com/mk6i/retro-aim-server/config"
 	"github.com/mk6i/retro-aim-server/state"
 )
 
@@ -54,7 +54,7 @@ func StartManagementAPI(userManager UserManager, sessionRetriever SessionRetriev
 	})
 
 	//todo make port configurable
-	addr := config.Address("", 8080)
+	addr := net.JoinHostPort("", "8080")
 	logger.Info("starting management API server", "addr", addr)
 	if err := http.ListenAndServe(addr, mux); err != nil {
 		logger.Error("unable to bind management API address address", "err", err.Error())

+ 1 - 1
server/oscar/alert.go

@@ -28,7 +28,7 @@ type AlertServer struct {
 // authentication handshake sequences are handled by this method. The remaining
 // requests are relayed to Handler.
 func (rt AlertServer) Start() {
-	addr := config.Address("", rt.Config.AlertPort)
+	addr := net.JoinHostPort("", rt.Config.AlertPort)
 	listener, err := net.Listen("tcp", addr)
 	if err != nil {
 		rt.Logger.Error("unable to bind ALERT server address", "err", err.Error())

+ 1 - 1
server/oscar/auth.go

@@ -34,7 +34,7 @@ type AuthServer struct {
 
 // Start starts the authentication server and listens for new connections.
 func (rt AuthServer) Start() {
-	addr := config.Address("", rt.Config.AuthPort)
+	addr := net.JoinHostPort("", rt.Config.AuthPort)
 	listener, err := net.Listen("tcp", addr)
 	if err != nil {
 		rt.Logger.Error("unable to bind auth server address", "err", err.Error())

+ 1 - 1
server/oscar/bos.go

@@ -33,7 +33,7 @@ type BOSServer struct {
 // authentication handshake sequences are handled by this method. The remaining
 // requests are relayed to BOSRouter.
 func (rt BOSServer) Start() {
-	addr := config.Address("", rt.Config.BOSPort)
+	addr := net.JoinHostPort("", rt.Config.BOSPort)
 	listener, err := net.Listen("tcp", addr)
 	if err != nil {
 		rt.Logger.Error("unable to bind BOS server address", "err", err.Error())

+ 1 - 1
server/oscar/chat.go

@@ -25,7 +25,7 @@ type ChatServer struct {
 
 // Start creates a TCP server that implements that chat flow.
 func (rt ChatServer) Start() {
-	addr := config.Address("", rt.Config.ChatPort)
+	addr := net.JoinHostPort("", rt.Config.ChatPort)
 	listener, err := net.Listen("tcp", addr)
 	if err != nil {
 		rt.Logger.Error("unable to bind chat server address", "err", err.Error())

+ 1 - 1
server/oscar/chat_nav.go

@@ -27,7 +27,7 @@ type ChatNavServer struct {
 
 // Start starts a TCP server and listens for ChatNav connections.
 func (rt ChatNavServer) Start() {
-	addr := config.Address("", rt.Config.ChatNavPort)
+	addr := net.JoinHostPort("", rt.Config.ChatNavPort)
 	listener, err := net.Listen("tcp", addr)
 	if err != nil {
 		rt.Logger.Error("unable to bind chat nav server address", "err", err.Error())