Skip to content

Commit 3ac8866

Browse files
committed
v1.2(development beta 1, patch 9)
1. Changed some structure's usage. 2. Fixed potiential issues after released the allocated memory. 3. Moved "runmode" config option to a command line option. 4. Replaced network library, for future support of IPv6. 5. Merged messages which previously splited, to fix potiential issues of log's mess.
1 parent f55e8d4 commit 3ac8866

19 files changed

+428
-492
lines changed

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# mcrelay
2-
Minecraft Relay Server<br/>
3-
A minecraft reverse proxy server with server address rewrite.<br/>
4-
Supports Minecraft Servers and Clients with version 12w04a or later. (Basically means release 1.2.1 and later.)<br/>
5-
Minecraft Versions before 12w04a are **NOT SUPPORTED**!<br/>
2+
Minecraft Relay Server
3+
4+
A minecraft reverse proxy server with server address rewrite.
5+
6+
Supports Minecraft Servers and Clients with version 12w04a or later. (Basically means release 1.2.1 and later.)
7+
8+
Minecraft Versions before 12w04a are **NOT SUPPORTED**!
69

710
## Features
811
* Support reverse proxy for Minecraft servers by server address in the handshake packet which client send to.
@@ -13,7 +16,8 @@ Minecraft Versions before 12w04a are **NOT SUPPORTED**!<br/>
1316
* libresolv.so
1417

1518
## Compatibility
16-
**Due to Minecraft Handshake restrictions, this server supports:**<br/>
19+
**Due to Minecraft Handshake restrictions, this server supports:**
20+
1721
* Game relay on server & client with version 12w04a and later, except version 12w17a, 13w41a and 13w41b.
1822
* MOTD relay / MOTD status notice on server & client with version 1.6.1 and later, except version 13w41a and 13w41b.
1923

@@ -40,10 +44,13 @@ make
4044
mcrelay < arguments | config_file >
4145
</pre>
4246

47+
The program will run as a non-exit-style program by default.
48+
49+
When using "-f" or "--forking" option, the program will become daemonized, and store its main process' PID into /tmp/mcrelay.pid.
50+
4351
## Config
4452
### Format
4553
<pre>
46-
runmode run_mode
4754
log logfile_path
4855
loglevel loglvl
4956
bind bind_object
@@ -54,8 +61,6 @@ proxy_pass proxy_type
5461
default destination_object
5562
</pre>
5663
### Explanation
57-
* runmode: set program's runmode.
58-
>* run_mode: type of program's runmode, "simple" for a normal, non-exit program, "forking" for a daemonized program. In forking, it will store the PID in /tmp/mcrelay.pid.
5964
* log: set log file.
6065
>* logfile_path: path of the file which logs saved to.
6166
* loglevel: set max message level in logging message.
@@ -69,15 +74,17 @@ default destination_object
6974
>* ident_name: name of destination identification. Usually a Fully Qualified Domain Name(FQDN) by CNAME to your server.
7075
>* destination_object: (format: "address_d[:port]")
7176
>>* address_d: the address you wish to connect. Both FQDN or x.x.x.x allowed.
72-
>>* port: optional, the port you wish to connect. Valid range: 1-65535.<br/>
73-
If not set, the server will detect SRV record first(defined in address_d).<br/>
74-
If SRV record resolve failed, it will fallback to normal address resolve, also connect to this address with port 25565.<br/>
77+
>>* port: optional, the port you wish to connect. Valid range: 1-65535.
78+
79+
If not set, the server will detect SRV record first(defined in address_d).
80+
81+
If SRV record resolve failed, it will fallback to normal address resolve, also connect to this address with port 25565.
82+
7583
**For rewrite enabled relay, it will use actual connect configuration to rewrite.**
7684
>>* path: the socket file you wish to connect.
7785
* default: optional, set a default server to connect when client don't match any valid virtual host. (Intentionally not support rewrite here.) (Security suggestion: Don't use this feature unless you know what you are doing.)
7886
### Example
7987
<pre>
80-
runmode forking
8188
log /var/log/mcrelay/mcrelay.log
8289
bind 0.0.0.0:25565
8390
proxy_pass rewrite
@@ -90,8 +97,10 @@ default 192.168.1.254:25565
9097
</pre>
9198

9299
## Instruction of using a DNS-based redirection(SRV)
93-
If you are using a SRV record to provide your service, you should follow the instructions below.<br/>
94-
Otherwise, your user will see the message of using a wrong address to connect.<br/>
100+
If you are using a SRV record to provide your service, you should follow the instructions below.
101+
102+
Otherwise, your user will see the message of using a wrong address to connect.
103+
95104
For example, your SRV record should be like this:
96105
<pre>
97106
_minecraft._tcp.srvrecord.example.com. => PRIVORITY WEIGHT PORT host.example.com

mcrelay.conf.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
runmode run_mode
21
log logfile_path
32
loglevel loglvl
43
bind bind_object

mcrelay.service.forking.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Group=nogroup
99
Restart=on-failure
1010
RestartSec=5s
1111
PID=/tmp/mcrelay.pid
12-
ExecStart=/usr/bin/mcrelay /etc/mcrelay/mcrelay.conf
12+
ExecStart=/usr/bin/mcrelay -f /etc/mcrelay/mcrelay.conf
1313
ExecStop=/usr/bin/mcrelay -t
1414
ExecReload=/usr/bin/mcrelay -r
1515

mod/config.h

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,37 @@
1414
#include "basic.h"
1515
#ifdef linux
1616
#define CONF_EOPENFILE 1
17-
#define CONF_EBADRUNMODE 2
18-
#define CONF_ENOLOGFILE 3
19-
#define CONF_ENOLOGLEVEL 4
20-
#define CONF_EINVALIDBIND 5
21-
#define CONF_EPROXYNOFIND 6
22-
#define CONF_EPROXYDUP 7
23-
#define CONF_EDEFPROXYDUP 8
24-
struct conf_bind;
25-
struct conf_map;
26-
struct conf;
27-
struct conf_map * getproxyinfo(struct conf * source, unsigned char * proxyname);
28-
void config_dump(struct conf * source);
29-
int config_load(char * filename, struct conf * result);
17+
#define CONF_ENOLOGFILE 2
18+
#define CONF_ENOLOGLEVEL 3
19+
#define CONF_EINVALIDBIND 4
20+
#define CONF_EPROXYNOFIND 5
21+
#define CONF_EPROXYDUP 6
22+
#define CONF_EDEFPROXYDUP 7
23+
typedef struct
24+
{
25+
char inet_addr[BUFSIZ];
26+
unsigned short inet_port;
27+
} conf_bind;
28+
typedef struct
29+
{
30+
unsigned short enable_rewrite;
31+
char from[512],to_inet_addr[512];
32+
unsigned short to_inet_port;
33+
unsigned short to_inet_hybridmode;
34+
} conf_map;
35+
typedef struct
36+
{
37+
char log[512];
38+
unsigned short loglevel;
39+
conf_bind bind;
40+
int relay_count;
41+
conf_map relay[128];
42+
int enable_default;
43+
conf_map relay_default;
44+
} conf;
45+
conf_map * getproxyinfo(conf * source, unsigned char * proxyname);
46+
void config_dump(conf * source);
47+
int config_load(char * filename, conf * result);
3048
#include "linux/config.c"
3149
#endif
3250
#endif

mod/linux/basic.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ size_t freadall(unsigned char * filename, unsigned char ** dest)
189189
if(result==NULL)
190190
{
191191
free(buffer);
192+
buffer=NULL;
192193
errno=FREADALL_ECALLOC;
193194
return 0;
194195
}
@@ -197,14 +198,17 @@ size_t freadall(unsigned char * filename, unsigned char ** dest)
197198
if((total_size+read_size)>max_size)
198199
{
199200
free(buffer);
201+
buffer=NULL;
200202
free(result);
203+
result=NULL;
201204
errno=FREADALL_ELARGE;
202205
return 0;
203206
}
204207
unsigned char * result_pre=realloc(result,total_size+read_size+1);
205208
if(result_pre==NULL)
206209
{
207210
free(buffer);
211+
buffer=NULL;
208212
errno=FREADALL_EREALLOC;
209213
return 0;
210214
}
@@ -218,6 +222,7 @@ size_t freadall(unsigned char * filename, unsigned char ** dest)
218222
}
219223
}
220224
free(buffer);
225+
buffer=NULL;
221226
*dest=result;
222227
return total_size;
223228
}
@@ -246,6 +251,7 @@ unsigned char * base64_encode(unsigned char * source, size_t source_size)
246251
if(target==NULL)
247252
{
248253
free(new_source);
254+
new_source=NULL;
249255
return NULL;
250256
}
251257
memset(target,0,target_size+1);
@@ -261,6 +267,7 @@ unsigned char * base64_encode(unsigned char * source, size_t source_size)
261267
offset_target=offset_target+4;
262268
}
263269
free(new_source);
270+
new_source=NULL;
264271
if(endfix!=0)
265272
{
266273
memset(&(target[target_size-endfix]),'=',endfix);

mod/linux/config.c

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,9 @@
1414
#include <stdio.h>
1515
#include <stdlib.h>
1616
#include <string.h>
17-
struct conf_bind
17+
conf_map * getproxyinfo(conf * source, unsigned char * proxyname)
1818
{
19-
char inet_addr[BUFSIZ];
20-
unsigned short inet_port;
21-
};
22-
struct conf_map
23-
{
24-
unsigned short enable_rewrite;
25-
char from[512],to_inet_addr[512];
26-
unsigned short to_inet_port;
27-
unsigned short to_inet_hybridmode;
28-
};
29-
struct conf
30-
{
31-
unsigned short runmode;
32-
char log[512];
33-
unsigned short loglevel;
34-
struct conf_bind bind;
35-
int relay_count;
36-
struct conf_map relay[128];
37-
int enable_default;
38-
struct conf_map relay_default;
39-
};
40-
struct conf_map * getproxyinfo(struct conf * source, unsigned char * proxyname)
41-
{
42-
struct conf_map * result=NULL;
19+
conf_map * result=NULL;
4320
int proxyname_length=strlen(proxyname);
4421
int detected_length=proxyname_length;
4522
while(proxyname[detected_length-1]=='.')
@@ -66,18 +43,10 @@ struct conf_map * getproxyinfo(struct conf * source, unsigned char * proxyname)
6643
real_proxyname=NULL;
6744
return result;
6845
}
69-
void config_dump(struct conf * source)
46+
void config_dump(conf * source)
7047
{
7148
printf("Config Detail:\n");
7249
printf("\n[COMMON]\n");
73-
switch(source->runmode)
74-
{
75-
case 1:
76-
printf("Runmode\t\tSimple\n");
77-
break;
78-
case 2:
79-
printf("Runmode\t\tForking\n");
80-
}
8150
printf("Log file\t%s\n",source->log);
8251
printf("Log level\t%d\n",source->loglevel);
8352
printf("Relay Count\t%d\n",source->relay_count);
@@ -135,7 +104,7 @@ void config_dump(struct conf * source)
135104
}
136105
printf("\n");
137106
}
138-
int config_load(char * filename, struct conf * result)
107+
int config_load(char * filename, conf * result)
139108
{
140109
char rec_char,buffer[128][BUFSIZ],tmp_buffer[BUFSIZ],key[512],value[512],key2[512],value2[512];
141110
char * tmpptr;
@@ -185,18 +154,7 @@ int config_load(char * filename, struct conf * result)
185154
}
186155
tmpptr=strsplit(tmpptr,' ',key);
187156
tmpptr=strsplit(tmpptr,' ',value);
188-
if(strcmp(key,"runmode")==0)
189-
{
190-
if(strcmp(value,"simple")==0)
191-
{
192-
result->runmode=1;
193-
}
194-
else if(strcmp(value,"forking")==0)
195-
{
196-
result->runmode=2;
197-
}
198-
}
199-
else if(strcmp(key,"log")==0)
157+
if(strcmp(key,"log")==0)
200158
{
201159
strcpy(result->log,value);
202160
}
@@ -286,7 +244,7 @@ int config_load(char * filename, struct conf * result)
286244
}
287245
else if(strcmp(key,"default")==0)
288246
{
289-
struct conf_map relay_default;
247+
conf_map relay_default;
290248
bzero(&relay_default,sizeof(relay_default));
291249
tmpptr=value;
292250
if(result->enable_default==1)
@@ -326,10 +284,6 @@ int config_load(char * filename, struct conf * result)
326284
result->enable_default=1;
327285
}
328286
}
329-
if((result->runmode!=1)&&(result->runmode!=2))
330-
{
331-
return CONF_EBADRUNMODE;
332-
}
333287
if(strcmp(result->log,"")==0)
334288
{
335289
return CONF_ENOLOGFILE;

0 commit comments

Comments
 (0)