Skip to content

Commit

Permalink
replace config.Address with net.JoinHostPort
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mk6i committed May 18, 2024
1 parent a075fb5 commit b162041
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 39 deletions.
16 changes: 5 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 2 additions & 1 deletion foodgroup/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"net"

"github.com/mk6i/retro-aim-server/config"
"github.com/mk6i/retro-aim-server/state"
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions foodgroup/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions foodgroup/oservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log/slog"
"net"
"time"

"github.com/mk6i/retro-aim-server/config"
Expand Down Expand Up @@ -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, ""),
Expand All @@ -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, ""),
Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions foodgroup/oservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions server/http/mgmt_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion server/oscar/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion server/oscar/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion server/oscar/bos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion server/oscar/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion server/oscar/chat_nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit b162041

Please sign in to comment.