您的位置:首页 > 其它

给服务器添加端口映射服务

2016-04-30 22:39 274 查看
用于校区实验室服务器上的自助端口映射脚本。

portmgr.c

#include <stdio.h>

#define IPTABLES        "/sbin/iptables"

int main(int argc, char *argv[])
{
char *iptables_argv[512] = {
IPTABLES, "-tnat"
};
int i = 2;
if (argc >= 2) {
switch(*argv[1]) {
case 'a': iptables_argv[i++] = "-A"; break;
case 'd': iptables_argv[i++] = "-D"; break;
default:  iptables_argv[i++] = "--line-numbers"; iptables_argv[i++] = "-nvL";
}
iptables_argv[i++] = "prerouting_port_forwarding";
int j;
for(j = 2; j<argc; ++j)
iptables_argv[i++] = argv[j];
return execv(IPTABLES, iptables_argv);
}
return 0;
}


portmgr.php

switch($_SERVER["REQUEST_METHOD"]) {
case "GET":
echo "<body><pre>";
passthru("./portmgr l");
echo "</pre></body>";
exit;
case "POST":
switch($_POST["cmd"]) {
case "a":
if ($_POST["dport"] && !empty($_POST["to"])) {
//if ($_POST["dport"] === "80" || $_POST["dport"] === "http") {
//      $msg = "不能为80(http)端口";
//      break;
//} else
$proto = " -p tcp --dport " . $_POST["dport"];
if (!empty($_POST["dst"])) $dst = "-d ". $_POST["dst"];
else $dst = "";
passthru("./portmgr a " . $dst . $proto . " -j DNAT --to " . $_POST["to"], $ret);
if ($ret === 0) $msg = "操作成功";
}
break;
case "d":
if (!empty($_POST["id"])) {
passthru("./portmgr d " . $_POST["id"], $ret);
if ($ret === 0) $msg = "操作成功";
}
break;
}
break;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: