Forum

> > CS2D > Scripts > Encoding of the dedicated server/game
Forums overviewCS2D overview Scripts overviewLog in to reply

English Encoding of the dedicated server/game

7 replies
To the start Previous 1 Next To the start

old Encoding of the dedicated server/game

Marcell
Super User Off Offline

Quote
Hi,

I am creating a quick RCon Terminal in PHP and kind of it works fine. But I met some issues with the colors because of the encoding. (Even though the PHP/HTML file has the right encoding set)

The output can be seen here which is not good:

IMG:https://media.discordapp.net/attachments/641771891421347890/793208660284538970/unknown.png


Wonder if this related to the encoding or the RCon PHP itself.

Any suggestions?

old Re: Encoding of the dedicated server/game

Hajt
User Off Offline

Quote
I wrote simple function to parse colors for You, enjoy.

1
2
3
4
5
6
7
8
9
10
11
12
function color($string)
{
	$string = iconv('ASCII', 'UTF-8//IGNORE', $string);
	$pattern = '/([0-9]{3})([0-9]{3})([0-9]{3})/i';
	$replacement = '<span style="color: rgb(${1}, ${2}, ${3});">';
	$html = preg_replace($pattern, $replacement, $string);
	if (!$html) {
		return $string;
	} else {
		return $html . '</span>';
	}
}

old Re: Encoding of the dedicated server/game

Marcell
Super User Off Offline

Quote
Hajt, you are a god, thanks a lot. I promsie not to forget this. For some reason it does not work tho. It appears to be still something with the encoding, I believe.

The cron PHP

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
function send_commands($addr, $rcon, $cmds)
{
     $pieces = explode(":", $addr);
     $ip = $pieces[0];
     $port = $pieces[1];

     $fp = fsockopen("udp://$ip", $port, $errno, $errstr);

     $string = chr(1).chr(0).chr(242).chr(strlen($rcon)).$rcon.pack("S", strlen($cmds)).$cmds;
     fwrite($fp, $string);
     stream_set_timeout($fp, 1);
     $buf = fread($fp, 1024);
     fclose($fp);

     if (bin2hex($buf) == "0100f201340000") {
          return "RCon: Wrong rcon password.";
     }

     if (bin2hex($buf) == "0100f201330000") {
          return "RCon: Too many remote control attempts with wrong password!";
     }

     $buf = preg_split("/\x{F0}\x{00}\x{03}/", $buf);

     unset($buf[0]);

    
     return implode("<br>", $buf); 
     

    }

The form with the action:

1
2
3
4
5
6
7
8
9
<?php  
    if(isset($_POST['command'])) {
      $cmds = $_POST['command'];
      //do action
      $sentcommand = send_commands($addr, $rcon, $cmds);
      $output = color($sentcommand);  
      echo "<span class='text-white'>localhost@</span>devtest:~$<br>" . $output;
     } 
?>

https://imgur.com/a/dCm4Ged
edited 2×, last 03.01.21 07:30:30 pm

old Re: Encoding of the dedicated server/game

Hajt
User Off Offline

Quote
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
<?php
function color($string)
{
	$string = iconv('ASCII', 'UTF-8//IGNORE', $string);
	$pattern = '/([0-9]{3})([0-9]{3})([0-9]{3})/i';
	$replacement = '<span style="color: rgb(${1}, ${2}, ${3});">';
	$html = preg_replace($pattern, $replacement, $string);
	if (!$html) {
		return $string;
	} else {
		return $html . '</span>';
	}
}

function readByteFast()
{
	global $d;
	global $dOS;
	$byte = ord(substr($d, $dOS, 1));
	$dOS++;
	return $byte;
}

function send_commands($addr, $rcon, $cmds)
{
	global $d;
	global $dOS;

	$output = "";
	$pieces = explode(":", $addr);
	$ip = $pieces[0];
	$port = $pieces[1];

	// Send multiple copies of the request packet, because cs2d likes to just ignore them randomly
	for ($i = 0; $i < 3; $i++) {
		$fp = fsockopen("udp://$ip", $port, $errno, $errstr);
		$string = chr(1) . chr(0) . chr(242) . chr(strlen($rcon)) . $rcon . pack("S", strlen($cmds)) . $cmds;
		fwrite($fp, $string);
		stream_set_timeout($fp, 1);
		$d = fread($fp, 4096);
		$dOS = 0;
		fclose($fp);

		if (!empty($d)) {
			break;
		}
	}

	readByteFast(); // 1
	readByteFast(); // 0
	readByteFast(); // 240
	readByteFast(); // 0
	readByteFast(); // 3
	$size = readByteFast();

	while ($size >= 1) {
		readByteFast();
		$arr[] = readByteFast();
		if ($size == 1) {
			$string = implode(array_map("chr", $arr));
			$output .= color($string) . "<br>";
			unset($arr);
			readByteFast();
			readByteFast();
			readByteFast();
			readByteFast();
			$size = readByteFast();
			continue;
		}
		$size--;
	}
	return $output;
}
?>
<!DOCTYPE html>
<html>
<body>

<?php
if (isset($_POST['cmds']) && !empty($_POST['cmds'])) {
	$addr = $_POST['addr'];
	$rcon = $_POST['rcon'];
	$cmds = $_POST['cmds'];

	print(send_commands($addr, $rcon, $cmds));
}
?>

<form method="POST">
	<label for="addr">addr:</label><br>
	<input type="text" id="addr" name="addr"><br>
	<label for="rcon">rcon:</label><br>
	<input type="text" id="rcon" name="rcon"><br>
	<label for="rcon">cmds:</label><br>
	<input type="text" id="cmds" name="cmds"><br><br>
	<input type="submit" value="Submit">
</form>

</body>
</html>
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview