Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Repository Overview

This repository contains a SourcePawn plugin for SourceMod that automatically sets the `net_public_adr` ConVar for game servers behind NAT/DHCP. The plugin provides three methods to obtain the server's public IP address: HTTP endpoint query, hostip conversion, or custom manual setting.
This repository contains a SourcePawn plugin for SourceMod that automatically sets the `net_public_adr` ConVar for game servers behind NAT/DHCP. The plugin provides three methods to obtain the server's public IP address: Steam (SteamWorks) lookup, hostip conversion, or custom manual setting.

**Primary Purpose**: Automatically configure the public IP address for Source engine game servers that are behind NAT/DHCP to ensure proper connectivity for players.

Expand All @@ -21,7 +21,7 @@ addons/sourcemod/scripting/
- **Build Tool**: Native GitHub Actions workflow using `rumblefrog/setup-sp` (spcomp)
- **Dependencies**:
- SourceMod core
- RiPExt extension (for HTTP requests)
- SteamWorks extension ([alliedmodders/SM-SteamWorks](https://github.com/alliedmodders/SM-SteamWorks), for the Steam public IP)
- **Target**: Source engine game servers (CS:GO, CS2, TF2, etc.)

## Code Architecture
Expand All @@ -31,11 +31,10 @@ addons/sourcemod/scripting/
1. **ConVar Management**: Creates and manages configuration variables
- `sm_fixnetpublicaddr_method`: Selection of IP detection method (0-2)
- `sm_fixnetpublicaddr_custom_ip`: Custom IP address setting
- `sm_fixnetpublicaddr_public_ip_endpoint`: HTTP endpoint for IP detection
- `net_public_adr`: Target ConVar that gets set with the detected IP

2. **IP Detection Methods**:
- **Method 0 (Endpoint)**: HTTP request to external service (default: api.ipify.org)
- **Method 0 (Steam)**: Public IP as seen by Steam (`SteamWorks_GetPublicIP`), hostip until the server is logged on to Steam
- **Method 1 (HostIP)**: Convert server's hostip ConVar to public format
- **Method 2 (Custom)**: Use manually specified IP address

Expand All @@ -45,10 +44,10 @@ addons/sourcemod/scripting/

### Building the Plugin

The project uses a native GitHub Actions workflow with `rumblefrog/setup-sp` (spcomp) as the build system. Locally, this equates to cloning the RiPExt dependency and running `spcomp` directly against `FixNetPublicAdr.sp`.
The project uses a native GitHub Actions workflow with `rumblefrog/setup-sp` (spcomp) as the build system. Locally, this equates to cloning the SteamWorks include (alliedmodders/SM-SteamWorks) and running `spcomp` directly against `FixNetPublicAdr.sp`.

**Build Configuration**: See `.github/workflows/ci.yml` for:
- Dependencies (SourceMod via `rumblefrog/setup-sp`, RiPExt extension via `git clone`)
- Dependencies (SourceMod via `rumblefrog/setup-sp`, SteamWorks include via `git clone`)
- Build targets
- Output directory configuration

Expand All @@ -68,7 +67,7 @@ The project uses a native GitHub Actions workflow with `rumblefrog/setup-sp` (sp
- ConVar `net_public_adr` is set correctly
- All three methods work as expected
- ConVar changes trigger updates
- HTTP endpoint failures are handled gracefully
- Method 0 uses hostip until the server is logged on to Steam, then switches to the Steam public IP

## Code Style & Standards

Expand All @@ -81,17 +80,15 @@ The project uses a native GitHub Actions workflow with `rumblefrog/setup-sp` (sp

### This Project's Patterns
- ConVar naming: `g_cv[ConVarName]` pattern
- String buffers: Use appropriate sizes (32 for IPs, 256 for URLs)
- String buffers: Use appropriate sizes (32 for IPs)
- Error handling: Use `LogError()` for user-visible issues
- Memory management: Use `delete` for HTTPRequest cleanup (handled by RiPExt)

## Key Implementation Details

### HTTP Requests (Method 0)
- Uses RiPExt extension's HTTPRequest class
- Expects JSON response with "ip" field
- Asynchronous operation with callback handling
- Default endpoint: https://api.ipify.org?format=json
### Steam Public IP (Method 0)
- Uses `SteamWorks_GetPublicIP()`: no external service, no HTTP request
- Only available once the server is logged on to Steam: until then hostip is used
- `SteamWorks_SteamServersConnected()` sets the Steam IP as soon as the server logs on

### HostIP Conversion (Method 1)
- Reads `hostip` ConVar (32-bit integer)
Expand All @@ -111,11 +108,6 @@ The project uses a native GitHub Actions workflow with `rumblefrog/setup-sp` (sp
3. Update method ConVar bounds and description
4. Test all code paths

### Modifying HTTP Endpoint Behavior
- Modify `OnPublicIPReceived()` for different response formats
- Update default endpoint in ConVar creation
- Ensure error handling for network failures

### Configuration Changes
- New ConVars should follow the `sm_fixnetpublicaddr_*` naming pattern
- Use appropriate FCVAR flags (FCVAR_PROTECTED for sensitive settings)
Expand All @@ -125,12 +117,11 @@ The project uses a native GitHub Actions workflow with `rumblefrog/setup-sp` (sp

- Use `LogError()` for configuration issues that affect functionality
- Check ConVar validity before use
- Handle HTTP response status codes appropriately
- Provide meaningful error messages that help server administrators

## Performance Considerations

- HTTP requests are asynchronous and don't block server tick
- No network request: the Steam public IP is read from SteamWorks
- ConVar changes trigger immediate updates (acceptable for infrequent changes)
- String operations are minimal and use stack-allocated buffers
- No timers or frequent polling - event-driven architecture
Expand All @@ -144,30 +135,27 @@ The project uses a native GitHub Actions workflow with `rumblefrog/setup-sp` (sp

## Security Notes

- HTTP endpoint is configurable (potential for abuse if misconfigured)
- Custom IP input should be validated if expanded
- ConVars are marked FCVAR_PROTECTED where appropriate
- No user input validation beyond SourceMod's built-in ConVar handling

## Dependencies & Compatibility

- **Minimum SourceMod**: 1.12.x (CI compiler baseline; for modern SourcePawn syntax)
- **Required Extensions**: RiPExt (for HTTP functionality)
- **Required Extensions**: SteamWorks (for the Steam public IP)
- **Game Compatibility**: All Source engine games supported by SourceMod
- **Operating Systems**: Linux, Windows (wherever SourceMod runs)

## Debugging Tips

- Use `sm_cvar net_public_adr` to check current setting
- Monitor server console for LogError messages
- Test HTTP endpoint manually: `curl https://api.ipify.org?format=json`
- Check that SteamWorks is loaded: `sm exts list`
- Use `sm_cvar sm_fixnetpublicaddr_method` to switch between methods
- Check `hostip` ConVar value when using method 1

## Future Enhancement Ideas

- Add IPv6 support
- Implement retry logic for failed HTTP requests
- Add validation for custom IP addresses
- Support for multiple fallback endpoints
- Integration with server monitoring systems
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
run: |
set -euo pipefail
mkdir -p addons/sourcemod/scripting/include deps
git clone --depth=1 https://github.com/srcdslab/sm-ext-ripext.git deps/ext-ripext
cp -R deps/ext-ripext/pawn/scripting/* addons/sourcemod/scripting/
git clone --depth=1 https://github.com/alliedmodders/SM-SteamWorks.git deps/sm-steamworks
cp -R deps/sm-steamworks/Pawn/includes/* addons/sourcemod/scripting/include/

- name: Build sourcemod plugin
working-directory: addons/sourcemod/scripting
Expand Down
41 changes: 15 additions & 26 deletions addons/sourcemod/scripting/FixNetPublicAdr.sp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <ripext>
#include <SteamWorks>

#pragma newdecls required

ConVar g_cvNetPublicAddr = null;
ConVar g_cvPublicIPEndpout = null;
ConVar g_cvMethod = null;
ConVar g_cvCustomIP = null;

Expand All @@ -14,15 +13,14 @@ public Plugin myinfo =
name = "FixNetPublicAddr",
author = "maxime1907, .Rushaway",
description = "Add/Edit convar net_public_adr for servers behind NAT/DHCP",
version = "1.1.4",
version = "1.2.0",
url = ""
};

public void OnPluginStart()
{
g_cvMethod = CreateConVar("sm_fixnetpublicaddr_method", "0", "[0 = Endpoint | 1 = Hostip | 2 = Custom]", FCVAR_PROTECTED, true, 0.0, true, 2.0);
g_cvMethod = CreateConVar("sm_fixnetpublicaddr_method", "0", "[0 = Steam | 1 = Hostip | 2 = Custom]", FCVAR_PROTECTED, true, 0.0, true, 2.0);
g_cvCustomIP = CreateConVar("sm_fixnetpublicaddr_custom_ip", "", "Custom IP to set as net_public_adr");
g_cvPublicIPEndpout = CreateConVar("sm_fixnetpublicaddr_public_ip_endpoint", "https://api.ipify.org?format=json", "Endpoint to query the server public ip");

g_cvNetPublicAddr = FindConVar("net_public_adr");
if (g_cvNetPublicAddr == null)
Expand All @@ -32,7 +30,6 @@ public void OnPluginStart()

g_cvMethod.AddChangeHook(OnConVarChanged);
g_cvCustomIP.AddChangeHook(OnConVarChanged);
g_cvPublicIPEndpout.AddChangeHook(OnConVarChanged);
}

public void OnConfigsExecuted()
Expand All @@ -45,12 +42,18 @@ public void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] n
GetServerPublicIP();
}

public void SteamWorks_SteamServersConnected()
{
if (g_cvMethod.IntValue == 0)
GetPublicIPFromSteam();
}

stock void GetServerPublicIP()
{
switch (g_cvMethod.IntValue)
{
case 0:
GetPublicIPFromEndpoint();
GetPublicIPFromSteam();
case 1:
GetPublicIPFromHostIP();
case 2:
Expand All @@ -68,31 +71,17 @@ stock void GetServerPublicIP()
}
}

stock void GetPublicIPFromEndpoint()
{
char sEndpoint[256];
g_cvPublicIPEndpout.GetString(sEndpoint, sizeof(sEndpoint));
HTTPRequest request = new HTTPRequest(sEndpoint);

request.Get(OnPublicIPReceived);
}

void OnPublicIPReceived(HTTPResponse response, any value)
stock void GetPublicIPFromSteam()
{
if (response.Status != HTTPStatus_OK)
return;

JSONObject jsonIP = view_as<JSONObject>(response.Data);
if (jsonIP == null)
int ip[4];
// Not logged on to Steam yet (server start, -insecure, LAN): use hostip until SteamWorks_SteamServersConnected
if (!SteamWorks_GetPublicIP(ip))
{
LogError("Failed to parse JSON response from endpoint, fallback to hostip");
GetPublicIPFromHostIP();
return;
}

jsonIP.GetString("ip", g_sPublicIPAddress, sizeof(g_sPublicIPAddress));
delete jsonIP;

FormatEx(g_sPublicIPAddress, sizeof(g_sPublicIPAddress), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
g_cvNetPublicAddr.SetString(g_sPublicIPAddress, false, true);
}

Expand Down
Loading