利用RCON和CGI实现前后端交互管理
html代码示例:
<div style="color:#4682B4;text-align:center;"> <a name="top" id="top_anchor"> <h1>TDU-简易控制面板</h1> </a></div> <a name="top" id="top_anchor"> </a> <div style="color:#D67B67"><a name="top" id="top_anchor">下方操作点击或提交后报错“Internal Server Error”是正常现象,请勿重复点击</div> <div style="color:#696969"><a name="top" id="top_anchor"> <p> 立即切换地图 </p></a><ul><a name="top" id="top_anchor"> </a><li><a name="top" id="top_anchor"> </a><a href="http://1270.0.1:8088/cgi-bin/Anvil_RAAS_v4.sh" target="">Anvil_RAAS_v4</a> </li> <li> <a href="http://1270.0.1:8088/cgi-bin/BlackCoast_RAAS_v4.sh" target="">BlackCoast_RAAS_v4</a> </li> <li><a href="http" target=""></a></li> </ul> <div style="color:#696969"><a name="top" id="top_anchor"> <p> 自助跳边<ul> <form role="search" method="get" action="http://1270.0.1:8088/cgi-bin/AdminForceTeamChange.sh"><font style="color:#756fe9">用户昵称:</font></label><input type="search" name="username" value="" placeholder="请输入用户昵称" required=""> <input type="submit" value="提交"></ul> </svg></button></div></form> </ul> <p></p> <br> </div>
游戏地图切换后台调用的CGI脚本示例:
#!/bin/bash /usr/local/bin/mcrcon -P 21114 -p paswd "AdminChangeLayer Anvil_RAAS_v4" ok
传参CGI脚本示例:
#!/bin/bash USERNAME=`echo "$QUERY_STRING"|sed 's/username=//'` /usr/local/bin/mcrcon -P 21114 -p paswd "AdminForceTeamChange $USERNAME" ok
利用CGI传参格式为:http://127.0.0.1:8088/cgi-bin/shell/参数1+参数2+参数3+...
解决每次调用CGI都会报错500的问题的shell脚本:[echo "Content-type:text/html;charset=UTF8"]
#!/bin/bash echo "Content-type:text/html;charset=UTF8" echo "" echo "服务器返回内容:<br>" DATA_YMD=`date +%H时%M分` USERNAME=`echo "$QUERY_STRING"|sed 's/username=//'` /usr/local/bin/mcrcon -P 21114 -p paswd "AdminForceTeamChange $USERNAME" ok
以上脚本不支持中文字符,使用以下方法支持中文字符:
#!/bin/bash echo "Content-type:text/html;charset=UTF8" echo "" echo "指令已向服务器下发!<br>" DATA_YMD=`date +%H时%M分` USERNAME=`echo "$QUERY_STRING"|sed 's/username=//'` #USERNAME=`echo "$1"|sed 's/UserName=//g'` DATE=`printf $(echo -n "$USERNAME" | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')"\n"` /usr/local/bin/mcrcon -P 21114 -p ouyangwenguang "AdminForceTeamChange $DATE" ok
其中
DATE=`printf $(echo -n "$USERNAME" | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')"\n"`
是转化URL中的UrlEncode编码。
CGI安装部署:
参考文档:https://blog.csdn.net/weixin_30496751/article/details/99595366
安装httpd:
yum install -y httpd
修改配置:
vim /etc/httpd/conf/httpd.conf
末尾添加内容:
LoadModule cgi_module /usr/lib64/httpd/modules/mod_cgi.so LoadModule cgid_module /usr/lib64/httpd/modules/mod_cgid.so
修改内容:(注意路径)
<Directory "/var/www/cgi-bin"> AllowOverride None Options ExecCGI Order deny,allow Allow from all </Directory>
取消注释:
AddType text/html .shtml AddOutputFilter INCLUDES .shtml AddHandler cgi-script .cgi .sh //在其后添加.sh,使其支持此结尾的脚本
修改默认端口,防止与Nginx冲突:
Listen 8088
启动httpd
systemctl start httpd
在CGI目录下编写脚本:
[root@cq html]# cd /var/www/cgi-bin/
此处需注意权限问题,CGI目录下的脚本调用是使用的apache用户;需要根据自身需求修改权限。
需要了解的是CGI的环境参数,以方便后续在脚本中直接调用:
此处引用:https://blog.csdn.net/JOKER0707/article/details/101569780
echo SERVER_SOFTWARE = $SERVER_SOFTWARE #服务器软件 echo SERVER_NAME = $SERVER_NAME #服务器主机名 echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE #CGI版本 echo SERVER_PROTOCOL = $SERVER_PROTOCOL #通信使用的协议 echo SERVER_PORT = $SERVER_PORT #服务器的端口号 echo REQUEST_METHOD = $REQUEST_METHOD #请求方法(GET/POST/PUT/DELETE..) echo HTTP_ACCEPT = $HTTP_ACCEPT #HTTP定义的浏览器能够接受的数据类型 echo SCRIPT_NAME = $SCRIPT_NAME #当前运行的脚本名称(包含路径) echo QUERY_STRING = $QUERY_STRING #地址栏中传的数据(get方式) echo REMOTE_ADDR = $REMOTE_ADDR #客户端的ip
RCON安装部署:
项目地址:https://github.com/Tiiffi/mcrcon
安装过程:
git clone https://github.com/Tiiffi/mcrcon cd mcrcon make && make install
如git clone失败,请前往github打包下载至本地安装。
发表评论