2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
6 * This server application requires steamclient.so to be present in the
7 * executable directory. This is not provided by vg system, it must be
8 * downloaded via steamcmd. It will likely be somewhere in ~/.steam/ ...
11 #define _DEFAULT_SOURCE
16 volatile sig_atomic_t sig_stop
;
18 void inthandler( int signum
)
25 #include "vg/vg_steam.h"
26 #include "vg/vg_steam_networking.h"
27 #include "vg/vg_steam_http.h"
28 #include "vg/vg_steam_auth.h"
29 #include "network_msg.h"
30 #include "highscores.h"
32 static const u64 k_connection_unauthorized
= 0xffffffffffffffff;
34 static void *hSteamHTTP
,
35 *hSteamNetworkingSockets
;
37 static u8 steam_symetric_key
[ k_nSteamEncryptedAppTicketSymmetricKeyLen
];
38 static HSteamNetPollGroup client_pollgroup
;
41 static void recieve_http( void *callresult
, void *context
)
43 HTTPRequestCompleted_t
*result
= callresult
;
45 HTTPRequestHandle request
= result
->m_hRequest
;
48 SteamAPI_ISteamHTTP_GetHTTPResponseBodySize( hSteamHTTP
, request
, &size
);
50 u8
*buffer
= vg_alloc( size
);
51 SteamAPI_ISteamHTTP_GetHTTPResponseBodyData(
52 hSteamHTTP
, request
, buffer
, size
);
54 buffer
[size
-1] = '\0';
55 vg_info( "%s\n", (char *)buffer
);
58 SteamAPI_ISteamHTTP_ReleaseHTTPRequest( hSteamHTTP
, result
->m_hRequest
);
62 static u64_steamid
get_connection_authsteamid( SteamNetworkingMessage_t
*msg
)
64 i64 userdata
= SteamAPI_ISteamNetworkingSockets_GetConnectionUserData(
65 hSteamNetworkingSockets
, msg
->m_conn
);
67 return *((u64_steamid
*)&userdata
);
70 static void set_connection_authsteamid(HSteamNetConnection con
, u64_steamid id
)
72 i64 userdata
= *((i64
*)&id
);
74 SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
75 hSteamNetworkingSockets
, con
, userdata
);
78 static void new_client_connecting( HSteamNetConnection client
)
80 EResult accept_status
= SteamAPI_ISteamNetworkingSockets_AcceptConnection(
81 hSteamNetworkingSockets
, client
);
83 if( accept_status
== k_EResultOK
)
85 vg_success( "Accepted client (id: %u)\n", client
);
86 SteamAPI_ISteamNetworkingSockets_SetConnectionPollGroup(
87 hSteamNetworkingSockets
,
88 client
, client_pollgroup
);
91 set_connection_authsteamid( client
, -1 );
95 vg_warn( "Error accepting client (id: %u)\n", client
);
99 static void on_auth_status( CallbackMsg_t
*msg
)
101 SteamNetAuthenticationStatus_t
*info
= (void *)msg
->m_pubParam
;
102 vg_info( " Authentication availibility: %s\n",
103 string_ESteamNetworkingAvailability(info
->m_eAvail
) );
104 vg_info( " %s\n", info
->m_debugMsg
);
107 static void on_connect_status( CallbackMsg_t
*msg
)
109 SteamNetConnectionStatusChangedCallback_t
*info
= (void *)msg
->m_pubParam
;
110 vg_info( " Connection status changed for %lu\n", info
->m_hConn
);
112 vg_info( " %s -> %s\n",
113 string_ESteamNetworkingConnectionState(info
->m_eOldState
),
114 string_ESteamNetworkingConnectionState(info
->m_info
.m_eState
) );
116 if( info
->m_info
.m_eState
==k_ESteamNetworkingConnectionState_Connecting
)
118 new_client_connecting( info
->m_hConn
);
122 static void on_inet_auth( SteamNetworkingMessage_t
*msg
)
124 if( get_connection_authsteamid( msg
) != k_connection_unauthorized
)
126 vg_warn( "Already authorized this user but app ticket was sent"
127 " again (%u)\n", msg
->m_conn
);
131 vg_low( "Attempting to verify user\n" );
133 if( msg
->m_cbSize
< sizeof(netmsg_auth
) )
135 vg_error( "Malformed auth ticket, too small (%u)\n", msg
->m_conn
);
139 netmsg_auth
*auth
= msg
->m_pData
;
141 if( msg
->m_cbSize
< sizeof(netmsg_auth
)+auth
->ticket_length
||
142 auth
->ticket_length
> 1024 )
144 vg_error( "Malformed auth ticket, ticket_length incorrect (%u)\n",
145 auth
->ticket_length
);
150 u32 ticket_len
= 1024;
152 int success
= SteamEncryptedAppTicket_BDecryptTicket(
153 auth
->ticket
, auth
->ticket_length
, decrypted
,
154 &ticket_len
, steam_symetric_key
,
155 k_nSteamEncryptedAppTicketSymmetricKeyLen
);
159 vg_error( "Failed to decrypt users ticket (client %u)\n", msg
->m_conn
);
160 vg_error( " ticket length: %u\n", auth
->ticket_length
);
162 SteamAPI_ISteamNetworkingSockets_CloseConnection(
163 hSteamNetworkingSockets
,
164 msg
->m_conn
, 0, NULL
, 1 );
168 if( SteamEncryptedAppTicket_GetTicketIssueTime( decrypted
, ticket_len
))
170 RTime32 ctime
= time(NULL
),
171 tickettime
= SteamEncryptedAppTicket_GetTicketIssueTime(
172 decrypted
, ticket_len
),
173 expiretime
= tickettime
+ 24*3*60*60;
175 if( ctime
> expiretime
)
177 vg_error( "Ticket expired (client %u)\n", msg
->m_conn
);
179 /* TODO: Send expired information */
180 SteamAPI_ISteamNetworkingSockets_CloseConnection(
181 hSteamNetworkingSockets
,
182 msg
->m_conn
, 0, NULL
, 1 );
188 SteamEncryptedAppTicket_GetTicketSteamID( decrypted
, ticket_len
, &steamid
);
189 vg_success( "User is authenticated! steamid %lu (%u)\n",
190 steamid
.m_unAll64Bits
, msg
->m_conn
);
192 set_connection_authsteamid( msg
->m_conn
, steamid
.m_unAll64Bits
);
195 static int inet_require_auth( SteamNetworkingMessage_t
*msg
)
197 if( get_connection_authsteamid( msg
) == k_connection_unauthorized
)
199 vg_warn( "Unauthorized request! Disconnecting client: %u\n",
202 SteamAPI_ISteamNetworkingSockets_CloseConnection(
203 hSteamNetworkingSockets
,
204 msg
->m_conn
, 0, NULL
, 1 );
211 static void on_inet_score_request( SteamNetworkingMessage_t
*msg
)
213 if( !inet_require_auth(msg
) ) return;
215 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
216 hSteamNetworkingSockets
, msg
->m_conn
,
217 &scoreboard_client_data
, sizeof(netmsg_scoreboard
),
218 k_nSteamNetworkingSend_Reliable
, NULL
);
221 static void on_inet_set_nickname( SteamNetworkingMessage_t
*msg
)
223 if(!inet_require_auth(msg
)) return;
225 u64_steamid steamid
= get_connection_authsteamid(msg
);
226 netmsg_set_nickname
*setnick
= msg
->m_pData
;
227 if( msg
->m_cbSize
< sizeof(netmsg_set_nickname
) )
229 vg_warn( "Invalid nickname request from client: %u, steamid: %lu\n",
230 msg
->m_conn
, steamid
);
234 highscore_set_user_nickname( steamid
, setnick
->nickname
);
237 static void on_inet_set_score( SteamNetworkingMessage_t
*msg
)
239 if(!inet_require_auth(msg
)) return;
241 u64_steamid steamid
= get_connection_authsteamid(msg
);
243 if( msg
->m_cbSize
< sizeof(netmsg_set_score
) )
245 vg_warn( "Invalid set score post from client: %u, steamid: %lu\n",
246 msg
->m_conn
, steamid
);
250 netmsg_set_score
*info
= msg
->m_pData
;
252 if( msg
->m_cbSize
< sizeof(netmsg_set_score
) +
253 sizeof(struct netmsg_score_record
)*info
->record_count
)
255 vg_warn( "Malformed set score post from client: %u, steamid: %lu\n",
256 msg
->m_conn
, steamid
);
260 for( int i
=0; i
<info
->record_count
; i
++ )
262 highscore_record temp
;
263 temp
.trackid
= info
->records
[i
].trackid
;
264 temp
.datetime
= time(NULL
);
265 temp
.playerid
= steamid
;
266 temp
.points
= info
->records
[i
].points
;
267 temp
.time
= info
->records
[i
].time
;
269 highscores_push_record( &temp
);
273 static void poll_connections(void)
275 SteamNetworkingMessage_t
*messages
[32];
280 len
= SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup(
281 hSteamNetworkingSockets
,
282 client_pollgroup
, messages
, vg_list_size(messages
) );
287 for( int i
=0; i
<len
; i
++ )
289 SteamNetworkingMessage_t
*msg
= messages
[i
];
291 if( msg
->m_cbSize
< sizeof(netmsg_blank
) )
293 vg_warn( "Discarding message (too small: %d)\n",
298 netmsg_blank
*tmp
= msg
->m_pData
;
300 if( tmp
->inetmsg_id
== k_inetmsg_auth
)
302 else if( tmp
->inetmsg_id
== k_inetmsg_scores_request
)
303 on_inet_score_request( msg
);
304 else if( tmp
->inetmsg_id
== k_inetmsg_set_nickname
)
305 on_inet_set_nickname( msg
);
306 else if( tmp
->inetmsg_id
== k_inetmsg_set_score
)
307 on_inet_set_score( msg
);
309 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
314 static u64
seconds_to_server_ticks( double s
)
319 static void generate_boards(void)
321 FILE *fp
= fopen( "www/html/srhighscores.txt", "w" );
323 for( int i
=0; i
<vg_list_size(track_infos
); i
++ )
325 struct netmsg_board
*board
= &scoreboard_client_data
.boards
[i
];
327 highscores_board_generate( board
->data
, i
, 10 );
328 highscores_board_printf( fp
, board
->data
, 10 );
334 int main( int argc
, char *argv
[] )
336 signal( SIGINT
, inthandler
);
337 signal( SIGQUIT
, inthandler
);
339 /* TODO: Options to override, ammend, remove etc */
341 vg_set_mem_quota( 80*1024*1024 );
344 highscores_init( 250000, 10000 );
346 if( !highscores_read() )
347 highscores_create_db();
349 steamworks_ensure_txt( "2103940" );
350 if( !vg_load_steam_symetric_key( "application_key", steam_symetric_key
) )
353 if( !SteamGameServer_Init( 0, 27400, 27401, eServerModeAuthentication
,
356 vg_error( "SteamGameServer_Init failed\n" );
360 void *hSteamGameServer
= SteamAPI_SteamGameServer();
361 SteamAPI_ISteamGameServer_LogOnAnonymous( hSteamGameServer
);
363 SteamAPI_ManualDispatch_Init();
364 HSteamPipe hsteampipe
= SteamGameServer_GetHSteamPipe();
366 //hSteamHTTP = SteamAPI_SteamGameServerHTTP();
367 hSteamNetworkingSockets
=
368 SteamAPI_SteamGameServerNetworkingSockets_SteamAPI();
374 steam_register_callback( k_iSteamNetAuthenticationStatus
, on_auth_status
);
375 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack
,
378 vg_success( "Steamworks API running\n" );
379 steamworks_event_loop( hsteampipe
);
385 HSteamListenSocket listener
;
386 SteamNetworkingIPAddr localAddr
;
387 SteamAPI_SteamNetworkingIPAddr_Clear( &localAddr
);
388 localAddr
.m_port
= 27402;
390 listener
= SteamAPI_ISteamNetworkingSockets_CreateListenSocketIP(
391 hSteamNetworkingSockets
, &localAddr
, 0, NULL
);
392 client_pollgroup
= SteamAPI_ISteamNetworkingSockets_CreatePollGroup(
393 hSteamNetworkingSockets
);
396 HTTPRequestHandle test_req
= SteamAPI_ISteamHTTP_CreateHTTPRequest(
397 hSteamHTTP
, k_EHTTPMethodGET
,
398 "https://www.harrygodden.com/hello.txt" );
400 steam_async
*call1
= steam_new_async();
402 call1
->p_handler
= recieve_http
;
403 SteamAPI_ISteamHTTP_SendHTTPRequest( hSteamHTTP
, test_req
, &call1
->id
);
406 u64 server_ticks
= 8000,
407 last_record_save
= 8000,
408 last_scoreboard_gen
= 0;
414 steamworks_event_loop( hsteampipe
);
420 if(server_ticks
> last_scoreboard_gen
+ seconds_to_server_ticks(1.0*60.0))
422 last_scoreboard_gen
= server_ticks
;
426 if(server_ticks
> last_record_save
+ seconds_to_server_ticks( 10.0*60.0 ))
428 last_record_save
= server_ticks
;
429 highscores_serialize_all();
433 highscores_serialize_all();
435 SteamAPI_ISteamNetworkingSockets_DestroyPollGroup( hSteamNetworkingSockets
,
437 SteamAPI_ISteamNetworkingSockets_CloseListenSocket(
438 hSteamNetworkingSockets
, listener
);
440 vg_info( "Shutting down\n..." );
441 SteamGameServer_Shutdown();