Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
204 lines (172 loc) · 5.53 KB

File metadata and controls

204 lines (172 loc) · 5.53 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <system.h>
#include "netban.h"
#include "network.h"
#include "main.h"
#include "server.h"
int CServer::NewClientCallback(int ClientID, void *pUser)
{
CServer *pThis = (CServer *)pUser;
char aAddrStr[NETADDR_MAXSTRSIZE];
net_addr_str(pThis->m_Network.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true);
if(pThis->Main()->Config()->m_Verbose)
dbg_msg("server", "Connection accepted. ncid=%d addr=%s'", ClientID, aAddrStr);
pThis->m_aClients[ClientID].m_State = CClient::STATE_CONNECTED;
pThis->m_aClients[ClientID].m_TimeConnected = time_get();
pThis->m_Network.Send(ClientID, "Authentication required:");
return 0;
}
int CServer::DelClientCallback(int ClientID, const char *pReason, void *pUser)
{
CServer *pThis = (CServer *)pUser;
char aAddrStr[NETADDR_MAXSTRSIZE];
net_addr_str(pThis->m_Network.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true);
if(pThis->Main()->Config()->m_Verbose)
dbg_msg("server", "Client dropped. ncid=%d addr=%s reason='%s'", ClientID, aAddrStr, pReason);
if(pThis->m_aClients[ClientID].m_State == CClient::STATE_AUTHED)
pThis->Main()->OnDelClient(ClientID);
pThis->m_aClients[ClientID].m_State = CClient::STATE_EMPTY;
return 0;
}
int CServer::Init(CMain *pMain, const char *Bind, int Port)
{
m_pMain = pMain;
m_NetBan.Init();
for(int i = 0; i < NET_MAX_CLIENTS; i++)
m_aClients[i].m_State = CClient::STATE_EMPTY;
m_Ready = false;
if(Port == 0)
{
dbg_msg("server", "Will not bind to port 0.");
return 1;
}
NETADDR BindAddr;
if(Bind[0] && net_host_lookup(Bind, &BindAddr, NETTYPE_ALL) == 0)
{
// got bindaddr
BindAddr.type = NETTYPE_ALL;
BindAddr.port = Port;
}
else
{
mem_zero(&BindAddr, sizeof(BindAddr));
BindAddr.type = NETTYPE_ALL;
BindAddr.port = Port;
}
if(m_Network.Open(BindAddr, &m_NetBan))
{
m_Network.SetCallbacks(NewClientCallback, DelClientCallback, this);
m_Ready = true;
dbg_msg("server", "Bound to %s:%d", Bind, Port);
return 0;
}
else
dbg_msg("server", "Couldn't open socket. Port (%d) might already be in use.", Port);
return 1;
}
void CServer::Update()
{
if(!m_Ready)
return;
m_NetBan.Update();
m_Network.Update();
char aBuf[NET_MAX_PACKETSIZE];
int ClientID;
while(m_Network.Recv(aBuf, (int)(sizeof(aBuf))-1, &ClientID))
{
dbg_assert(m_aClients[ClientID].m_State != CClient::STATE_EMPTY, "Got message from empty slot.");
if(m_aClients[ClientID].m_State == CClient::STATE_CONNECTED)
{
int ID = -1;
char aUsername[128] = {0};
char aPassword[128] = {0};
const char *pTmp;
if(!(pTmp = str_find(aBuf, ":"))
|| (unsigned)(pTmp - aBuf) > sizeof(aUsername) || (unsigned)(str_length(pTmp) - 1) > sizeof(aPassword))
{
m_Network.NetBan()->BanAddr(m_Network.ClientAddr(ClientID), 60, "You're an idiot, go away.");
m_Network.Drop(ClientID, "Fuck off.");
return;
}
str_copy(aUsername, aBuf, pTmp - aBuf + 1);
str_copy(aPassword, pTmp + 1, sizeof(aPassword));
if(!*aUsername || !*aPassword)
{
m_Network.NetBan()->BanAddr(m_Network.ClientAddr(ClientID), 60, "You're an idiot, go away.");
m_Network.Drop(ClientID, "Username and password must not be blank.");
return;
}
for(int i = 0; i < NET_MAX_CLIENTS; i++)
{
if(!Main()->Client(i)->m_Active)
continue;
if(str_comp(Main()->Client(i)->m_aUsername, aUsername) == 0 && str_comp(Main()->Client(i)->m_aPassword, aPassword) == 0)
ID = i;
}
if(ID == -1)
{
m_Network.NetBan()->BanAddr(m_Network.ClientAddr(ClientID), 60, "Wrong username and/or password.");
m_Network.Drop(ClientID, "Wrong username and/or password.");
}
else if(Main()->Client(ID)->m_ClientNetID != -1)
{
m_Network.Drop(ClientID, "Only one connection per user allowed.");
}
else
{
m_aClients[ClientID].m_State = CClient::STATE_AUTHED;
m_aClients[ClientID].m_LastReceived = time_get();
m_Network.Send(ClientID, "Authentication successful. Access granted.");
if(m_Network.ClientAddr(ClientID)->type == NETTYPE_IPV4)
m_Network.Send(ClientID, "You are connecting via: IPv4");
else if(m_Network.ClientAddr(ClientID)->type == NETTYPE_IPV6)
m_Network.Send(ClientID, "You are connecting via: IPv6");
if(Main()->Config()->m_Verbose)
dbg_msg("server", "ncid=%d authed", ClientID);
Main()->OnNewClient(ClientID, ID);
}
}
else if(m_aClients[ClientID].m_State == CClient::STATE_AUTHED)
{
m_aClients[ClientID].m_LastReceived = time_get();
if(Main()->Config()->m_Verbose)
dbg_msg("server", "ncid=%d cmd='%s'", ClientID, aBuf);
if(str_comp(aBuf, "logout") == 0)
m_Network.Drop(ClientID, "Logout. Bye Bye ~");
else
Main()->HandleMessage(ClientID, aBuf);
}
}
for(int i = 0; i < NET_MAX_CLIENTS; ++i)
{
if(m_aClients[i].m_State == CClient::STATE_CONNECTED &&
time_get() > m_aClients[i].m_TimeConnected + 5 * time_freq())
{
m_Network.NetBan()->BanAddr(m_Network.ClientAddr(i), 30, "Authentication timeout.");
m_Network.Drop(i, "Authentication timeout.");
}
else if(m_aClients[i].m_State == CClient::STATE_AUTHED &&
time_get() > m_aClients[i].m_LastReceived + 15 * time_freq())
m_Network.Drop(i, "Timeout.");
}
}
void CServer::Send(int ClientID, const char *pLine)
{
if(!m_Ready)
return;
if(ClientID == -1)
{
for(int i = 0; i < NET_MAX_CLIENTS; i++)
{
if(m_aClients[i].m_State == CClient::STATE_AUTHED)
m_Network.Send(i, pLine);
}
}
else if(ClientID >= 0 && ClientID < NET_MAX_CLIENTS && m_aClients[ClientID].m_State == CClient::STATE_AUTHED)
m_Network.Send(ClientID, pLine);
}
void CServer::Shutdown()
{
if(!m_Ready)
return;
m_Network.Close();
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.