summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorJustSomePwner <crotchyalt@gmail.com>2018-08-30 14:01:54 +0200
committerJustSomePwner <crotchyalt@gmail.com>2018-08-30 14:01:54 +0200
commit7ccb819f867493f8ec202ea3b39c94c198c64584 (patch)
tree94622e61af0ff359e3d6689cf274d74f60b2492a /server
parent564d979b79e8a5aaa5014eba0ecd36c61575934f (diff)
first
Diffstat (limited to 'server')
-rw-r--r--server/client.cpp66
-rw-r--r--server/client.hpp58
-rw-r--r--server/message.hpp8
-rw-r--r--server/server.cpp47
-rw-r--r--server/server.hpp31
-rw-r--r--server/server.vcxproj161
-rw-r--r--server/server.vcxproj.filters42
-rw-r--r--server/server_windows.cpp39
-rw-r--r--server/util.hpp25
9 files changed, 477 insertions, 0 deletions
diff --git a/server/client.cpp b/server/client.cpp
new file mode 100644
index 0000000..37f20a2
--- /dev/null
+++ b/server/client.cpp
@@ -0,0 +1,66 @@
+#include "client.hpp"
+
+std::vector< byte > server::c_client::receive_message( ) {
+ std::vector< uint8_t > ret;
+ char buffer[ BUFFER_SIZE ];
+ int received = 0;
+
+ while( true ) {
+ received = recv( m_socket, buffer, BUFFER_SIZE, 0 );
+ if( received < 0 )
+ break;
+
+ for( int i{ }; i < received; ++i )
+ ret.push_back( buffer[ i ] );
+
+ if( received < BUFFER_SIZE )
+ break;
+ }
+
+ if( ret.size( ) ) {
+ decode_buffer( ret.data( ), ret.size( ) );
+ ret.push_back( 0 );
+ ret.erase( ret.begin( ) );
+ }
+
+ return ret;
+}
+
+bool server::c_client::send_message( byte* msg, size_t length ) {
+ auto new_buffer = ( uint8_t* )( malloc( length + 1 ) );
+ auto key = util::random_number( 0, 255 ) & 0xff;
+
+ new_buffer[ 0 ] = key;
+ memcpy( new_buffer + 1,
+ msg,
+ length );
+
+ for( size_t i = 1; i < length + 1; ++i ) {
+ new_buffer[ i ] ^= key;
+ }
+
+ int result = send( m_socket, ( char* )new_buffer, length + 1, 0 );
+ if( result == -1 ) {
+ printf( "error sending message to %s: %d\n",
+ get_ip( ), WSAGetLastError( ) );
+
+ free( new_buffer );
+ return false;
+ }
+
+ free( new_buffer );
+ return true;
+}
+
+bool server::c_client::handle( ) {
+ char msg[ ] = "hello client";
+ if( !send_message( ( byte* )( msg ), sizeof( msg ) ) )
+ return false;
+
+ auto buf = receive_message( );
+ if( buf.size( ) ) {
+ printf( "message received: %s\n", buf.data( ) );
+ }
+
+ return true;
+} \ No newline at end of file
diff --git a/server/client.hpp b/server/client.hpp
new file mode 100644
index 0000000..a3dcd78
--- /dev/null
+++ b/server/client.hpp
@@ -0,0 +1,58 @@
+#pragma once
+
+#ifdef WIN32
+#include <Windows.h>
+#pragma comment(lib, "ws2_32.lib")
+#else
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#endif
+
+#include <cstdio>
+#include <cstdlib>
+#include <string>
+#include <fstream>
+
+#include "util.hpp"
+
+constexpr auto PORT_NUM = 6969;
+constexpr auto BUFFER_SIZE = 255;
+
+namespace server
+{
+ class c_client {
+ SOCKET m_socket{ };
+ in_addr m_address{ };
+ ulong_t m_hwid{ };
+
+ public:
+ c_client( SOCKET socket, in_addr& address ) :
+ m_socket( socket ),
+ m_address( address ) { }
+
+ ~c_client( ) {
+ closesocket( m_socket );
+ }
+
+ void decode_buffer( uint8_t* buf, size_t length ) {
+ auto key = buf[ 0 ];
+ for( size_t i{ 1 }; i < length; ++i )
+ buf[ i ] ^= key;
+ }
+
+ auto get_ip( ) {
+ return inet_ntoa( m_address );
+ }
+
+ std::vector< byte > receive_message( );
+ bool send_message( byte* msg, size_t length );
+
+ //handles messages, hwid etc
+ void handle_buffer( byte* msg );
+ virtual bool handle( );
+ };
+} \ No newline at end of file
diff --git a/server/message.hpp b/server/message.hpp
new file mode 100644
index 0000000..bf67389
--- /dev/null
+++ b/server/message.hpp
@@ -0,0 +1,8 @@
+#pragma once
+
+enum MessageType_t {
+ MSG_HELLO = 'h',
+ MSG_READY = 'r',
+ MSG_BAN = 'q',
+ MSG_IMPORT = 'i'
+}; \ No newline at end of file
diff --git a/server/server.cpp b/server/server.cpp
new file mode 100644
index 0000000..805239c
--- /dev/null
+++ b/server/server.cpp
@@ -0,0 +1,47 @@
+#include "server.hpp"
+
+int server::c_server::init( ) {
+ if( WSAStartup( MAKEWORD( 2, 2 ), &m_sock_data ) )// windows
+ return 1;
+
+ m_socket = socket( AF_INET, SOCK_STREAM, 0 );
+ if( m_socket == INVALID_SOCKET )
+ return 2;
+
+ sockaddr_in server_address{ };
+ server_address.sin_addr.s_addr = INADDR_ANY;
+ server_address.sin_port = htons( PORT_NUM );
+ server_address.sin_family = AF_INET;
+
+ auto result = ::bind( m_socket, ( sockaddr* )( &server_address ), sizeof( server_address ) );
+ return result != -1 ? 0 : 3;
+}
+
+void server::c_server::listen( ) {
+ ::listen( m_socket, 5 );
+
+ sockaddr_in client_address{ };
+ client_address.sin_port = htons( PORT_NUM );
+ int len = sizeof( client_address );
+
+ SOCKET result = accept( m_socket, ( sockaddr* )( &client_address ), &len );
+ if( result == INVALID_SOCKET ) {
+ printf( "socket error accepting a connection\n" );
+ return;
+ }
+
+ printf( "incoming connection from: %s\n", inet_ntoa( client_address.sin_addr ) );
+ m_clients.push_back( std::make_shared< c_client >( result, client_address.sin_addr ) );
+}
+
+void server::c_server::client_loop( ) {
+ if( m_clients.size( ) ) {
+ for( auto it = m_clients.begin( ); it != m_clients.end( ); ++it ) {
+ if( !( *it )->handle( ) ) {
+ printf( "%s disconnected\n", ( *it )->get_ip( ) );
+ m_clients.erase( it );
+ break;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/server/server.hpp b/server/server.hpp
new file mode 100644
index 0000000..8bd017e
--- /dev/null
+++ b/server/server.hpp
@@ -0,0 +1,31 @@
+#pragma once
+#include <vector>
+#include <memory>
+
+#include "client.hpp"
+
+
+//since this will be running on our vps we dont need string encryption or protection for anything
+//which is cool, i guess
+
+namespace server
+{
+ class c_server {
+ std::vector< std::shared_ptr< c_client > > m_clients;
+
+ WSADATA m_sock_data{ };
+ SOCKET m_socket{ };
+
+ public:
+ ~c_server( ) {
+ if( m_socket )
+ closesocket( m_socket );
+ }
+
+ int init( );
+ void listen( );
+ void client_loop( );
+ };
+}
+
+extern server::c_server g_server; \ No newline at end of file
diff --git a/server/server.vcxproj b/server/server.vcxproj
new file mode 100644
index 0000000..0070058
--- /dev/null
+++ b/server/server.vcxproj
@@ -0,0 +1,161 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|x64">
+ <Configuration>Debug</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|x64">
+ <Configuration>Release</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <VCProjectVersion>15.0</VCProjectVersion>
+ <ProjectGuid>{F0038E32-6DE8-47B7-BC86-8A2274B24406}</ProjectGuid>
+ <Keyword>Win32Proj</Keyword>
+ <RootNamespace>server</RootNamespace>
+ <WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <PlatformToolset>v141</PlatformToolset>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="Shared">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <LinkIncremental>true</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <LinkIncremental>true</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <LinkIncremental>false</LinkIncremental>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <SDLCheck>true</SDLCheck>
+ <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <ConformanceMode>true</ConformanceMode>
+ <LanguageStandard>stdcpp17</LanguageStandard>
+ </ClCompile>
+ <Link>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <SubSystem>Console</SubSystem>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <SDLCheck>true</SDLCheck>
+ <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <ConformanceMode>true</ConformanceMode>
+ </ClCompile>
+ <Link>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <SubSystem>Console</SubSystem>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <SDLCheck>true</SDLCheck>
+ <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <ConformanceMode>true</ConformanceMode>
+ <LanguageStandard>stdcpp17</LanguageStandard>
+ </ClCompile>
+ <Link>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <SubSystem>Console</SubSystem>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <SDLCheck>true</SDLCheck>
+ <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <ConformanceMode>true</ConformanceMode>
+ </ClCompile>
+ <Link>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <SubSystem>Console</SubSystem>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="client.cpp" />
+ <ClCompile Include="server.cpp" />
+ <ClCompile Include="server_windows.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="client.hpp" />
+ <ClInclude Include="message.hpp" />
+ <ClInclude Include="server.hpp" />
+ <ClInclude Include="util.hpp" />
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project> \ No newline at end of file
diff --git a/server/server.vcxproj.filters b/server/server.vcxproj.filters
new file mode 100644
index 0000000..d732209
--- /dev/null
+++ b/server/server.vcxproj.filters
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <Filter Include="Source Files">
+ <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+ <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+ </Filter>
+ <Filter Include="Header Files">
+ <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+ <Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
+ </Filter>
+ <Filter Include="Resource Files">
+ <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
+ <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
+ </Filter>
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="server_windows.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ <ClCompile Include="client.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ <ClCompile Include="server.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="server.hpp">
+ <Filter>Source Files</Filter>
+ </ClInclude>
+ <ClInclude Include="client.hpp">
+ <Filter>Source Files</Filter>
+ </ClInclude>
+ <ClInclude Include="util.hpp">
+ <Filter>Source Files</Filter>
+ </ClInclude>
+ <ClInclude Include="message.hpp">
+ <Filter>Source Files</Filter>
+ </ClInclude>
+ </ItemGroup>
+</Project> \ No newline at end of file
diff --git a/server/server_windows.cpp b/server/server_windows.cpp
new file mode 100644
index 0000000..a593d1b
--- /dev/null
+++ b/server/server_windows.cpp
@@ -0,0 +1,39 @@
+#ifdef WIN32
+#include <Windows.h>
+#pragma comment(lib, "ws2_32.lib")
+#else
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#endif
+
+#include <cstdio>
+#include <cstdlib>
+#include <string>
+#include <fstream>
+#include <thread>
+#include "server.hpp"
+
+server::c_server g_server;
+
+int main( ) {
+ std::thread listen_thread;
+
+ int result = g_server.init( );
+ if( !result ) {
+ listen_thread = std::thread( [ ]( ) { while( 1 ) { g_server.listen( ); } } );
+ listen_thread.detach( );
+
+ while( 1 ) {
+ g_server.client_loop( );
+ }
+ }
+ else
+ printf( "server init error (%d)\n", result );
+
+ system( "pause" );
+ return 0;
+}
diff --git a/server/util.hpp b/server/util.hpp
new file mode 100644
index 0000000..1d408e0
--- /dev/null
+++ b/server/util.hpp
@@ -0,0 +1,25 @@
+#pragma once
+#include <random>
+
+using ulong_t = unsigned long;
+
+namespace util
+{
+ namespace {
+ //make a random generator and seed it with a p random number
+ static std::random_device rd;
+ static std::mt19937 gen( rd( ) );
+ }
+
+ template < typename t >
+ __forceinline t random_number( t min, t max ) {
+ if constexpr( !std::is_integral_v< t > ) {
+ std::uniform_real_distribution< t > dist( min, max );
+ return dist( gen );
+ }
+ else {
+ std::uniform_int_distribution< t > dist( min, max );
+ return dist( gen );
+ }
+ }
+} \ No newline at end of file