1
1
#include <sourcemod>
2
2
#include <system2>
3
- #include <SteamWorks>
4
3
5
4
#define PLUGIN_VERSION " 0.4.1"
5
+ #define PUBLIC_IP_API_ADDRESS " https://api.ipify.org"
6
6
7
7
ConVar tf2pickupOrgApiAddress = null ;
8
8
ConVar tf2pickupOrgSecret = null ;
9
9
ConVar tf2pickupOrgPriority = null ;
10
10
ConVar tf2pickupOrgOverrideInternalAddress = null ;
11
11
Handle timer = null ;
12
+ char publicIpAddress [64 ];
12
13
13
14
public Plugin myinfo =
14
15
{
@@ -33,13 +34,39 @@ public void OnPluginStart()
33
34
tf2pickupOrgOverrideInternalAddress .AddChangeHook (OnApiAddressOrSecretChange );
34
35
35
36
RegServerCmd (" sm_tf2pickuporg_heartbeat" , CommandHeartbeat );
37
+ ResolvePublicIpAddress ();
36
38
}
37
39
38
40
public void OnPluginEnd ()
39
41
{
40
42
41
43
}
42
44
45
+ public void ResolvePublicIpAddress ()
46
+ {
47
+ System2HTTPRequest request = new System2HTTPRequest (PublicIpCallback , PUBLIC_IP_API_ADDRESS );
48
+ request .SetUserAgent (" tf2pickup.org connector plugin/%s " , PLUGIN_VERSION );
49
+ request .GET ();
50
+ delete request ;
51
+
52
+ PrintToServer (" Querying https://api.ipify.org..." );
53
+ }
54
+
55
+ public void PublicIpCallback (bool success , const char [] error , System2HTTPRequest request , System2HTTPResponse response , HTTPRequestMethod method )
56
+ {
57
+ if (! success ) {
58
+ char url [128 ];
59
+ request .GetURL (url , sizeof (url ));
60
+ PrintToServer (" ERROR: %s failed: %s " , url , error );
61
+ return ;
62
+ }
63
+
64
+ publicIpAddress [0 ] = ' \0 ' ;
65
+ response .GetContent (publicIpAddress , sizeof (publicIpAddress ));
66
+ TrimString (publicIpAddress );
67
+ PrintToServer (" Gameserver public IP address: %s " , publicIpAddress );
68
+ }
69
+
43
70
public void OnApiAddressOrSecretChange (ConVar convar , char [] oldValue , char [] newValue )
44
71
{
45
72
if (timer != null ) {
@@ -65,6 +92,11 @@ public Action CommandHeartbeat(int args)
65
92
66
93
public Action HeartbeatGameServer (Handle timerHandle )
67
94
{
95
+ if (strlen (publicIpAddress ) == 0 ) {
96
+ PrintToServer (" Gameserver public IP address unknown; heartbeat impossible" );
97
+ return Plugin_Stop ;
98
+ }
99
+
68
100
char apiAddress [128 ];
69
101
tf2pickupOrgApiAddress .GetString (apiAddress , sizeof (apiAddress ));
70
102
@@ -79,12 +111,8 @@ public Action HeartbeatGameServer(Handle timerHandle)
79
111
request .SetHeader (" Authorization" , " secret %s " , secret );
80
112
request .SetHeader (" Content-Type" , " application/x-www-form-urlencoded" );
81
113
82
- int ipAddr [4 ];
83
- SteamWorks_GetPublicIP (ipAddr );
84
-
85
114
char address [64 ];
86
- Format (address , sizeof (address ), " %d .%d .%d .%d " , ipAddr [0 ], ipAddr [1 ], ipAddr [2 ], ipAddr [3 ]);
87
- System2_URLEncode (address , sizeof (address ), address );
115
+ System2_URLEncode (address , sizeof (address ), publicIpAddress );
88
116
89
117
char port [6 ];
90
118
GetConVarString (FindConVar (" hostport" ), port , sizeof (port ));
0 commit comments