启用Apache的httpd-CGI能力,并通过参数传入执行脚本。
启用httpd的cgi能力。
安装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
添加其他路径:
/etc/httpd/conf/httpd.conf
ScriptAlias /api/ "/var/www/api/" <Directory "/var/www/api/"> AllowOverride None Options None Require all granted </Directory>
示例脚本:
/var/www/api/Rcon.sh
#!/bin/bash
# Rcon.sh
# Rcon公开调用接口
# 传入参数 1=ip 2=port 3=passwd 4=cmd
# 返回参数 steam时长,为空则返回null
# 返回时长为分钟数
echo "Content-type: text/plain"
echo ""
# 获取QUERY_STRING环境变量,并用Python进行URL解码
decode_query_string=$(python3 -c "import urllib.parse; print(urllib.parse.unquote('$QUERY_STRING'))")
# 将解码后的字符串分割成数组
IFS='&' read -r -a params <<< "$decode_query_string"
# 声明关联数组来存储每个参数
declare -A query_params
# 遍历数组,分离键值对
for param in "${params[@]}"; do
IFS='=' read -r -a kv <<< "$param"
key=${kv[0]}
value=${kv[1]}
query_params[$key]="$value"
done
# 从关联数组中获取参数值
ip=${query_params[ip]}
port=${query_params[port]}
passwd=${query_params[passwd]}
cmd=${query_params[cmd]}
if [ -z "$port" ]; then
port=21114
fi
if [ -z "$cmd" ]; then
cmd=showserverinfo
fi
if [ -z "$ip" ] || [ -z "$port" ] || [ -z "$passwd" ] || [ -z "$cmd" ]; then
echo "缺少必要的参数。用法:"
echo "http://api.bctc-squad.cn:8088/api/Rcon.sh?ip=IP地址+port=端口号+passwd=密码+cmd=命令"
exit 1
fi
# 打印解码后的参数,用于调试
echo "IP: $ip"
echo "Port: $port"
echo "Password: $passwd"
echo "Command: $cmd"
# 后续其他代码逻辑...请求的URL:
http://api.bctc-squad.cn:8088/api/Rcon.sh?ip=127.0.0.1&port=21114&passwd=xxxx&cmd=xxxx%20xx
这里的 %20 是空格符。
一份完整的配置文件示例(清除注释)
vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"
Listen 8088
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
ScriptAlias /api/ "/var/www/api/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<Directory "/var/www/api/">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
发表评论