防火墙使用Linux自带的iptable防火墙
大部分校园内部分两种登录
因为是多账户登录,每次登录都会更改配置文件同时对配置文件的标记文件也进行修改,造成了标志文件每次登录都会覆盖标志文件的问题。被反复覆盖
Shell文件需要记录上次标志文件的版本号,如果版本号发生改变的话,重启配置文件—既标记返回覆盖也无关系
修改配置文件的时候如果同时修改配置文件的话会产生干扰,退出修改,登录新增。产生覆盖问题
方法:配置文件可以分成多份 既防火墙文件 将配置规则放入d、b、c配置文件,但是将d、b、c的文件的引入方式放入a中这样我们只需要调用a文件就能同时引入d、b、c文件。重启的时候只要重启a文件就可以
然后通过指令解析解析配置文件就行
登录之后防火墙允许那些ip登录是需要记录到数据库中,通过管理平台进行管理—(指定那些ip需要登录,那些ip不可以登录)
大概流程:多个用户先进行登录,用户的账户密码来源—管理平台导入进来—,登录完毕b网站之后,修改配置文件,并且修改数据库同意登录用户的ip访问A网站
既修改完配置文件之后还要修改数据库
登录完之后–新增ip和删除ip需要在在表中做记录。假设数据库中有《用户表》《用户和ip对应表》《新增ip表》《删除ip表》《最新时间表》每次登录都需要对用户和ip对应表进行更新。登录之后发现是新增的ip那么需要删除以前对应的ip。删除IP需要记录删除的时间。写一个打成jar包的java文件(定时器)每隔一段时候都会读取最新删除的ip将其以字符串方式存储到配置文件中。例如aaa文件。《最新时间表》记录最新删除ip的时间。例如bbb文件为新增ip文件和aaa文件一样
定时器定时调用脚本,脚本读取aaa文件或者bbb文件,获取最新删除(新增)ip拼接防火墙删除ip的指令----shell脚本读取aaa文件内容根,设定变量字符串分割,循环、字符串拼接、拼接好指令之后,执行指令(指令执行的关键字)
导出含有主方法jar包,由定时器执行,测试是否可以定时向数据集插入数据,java修改配置文件内存
package yaojing.dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
public static void main(String[] args) {
}
private static String driver = "com.mysql.jdbc.Driver";//驱动
private static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
private static String user = "root";
private static String password = "2020";
public Connection conn;
public DBConnection() {
try {
// 加载驱动jar包
Class.forName(driver); // 反射:获取类的信息
conn = (Connection) DriverManager.getConnection(url, user, password);
// if(!conn.isClosed())
// System.out.println("Succeeded connecting to the Database!");
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
this.conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package yaojing.dao;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MysqlUtil {
/**
* 添加
* @param sql insert
* @return
*/
public static int add(String sql) {
System.out.println("sql="+sql);
int i=0;
DBConnection db = new DBConnection();
try {
PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
preStmt.executeUpdate();
preStmt.close();
db.close();
i = 1;
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
/**
*查询
* @param sql
* @param colums
* @return
*/
public static ArrayList showUtil(String sql, String[] colums){
ArrayList result = new ArrayList();
DBConnection db = new DBConnection();
try {
Statement stmt = (Statement) db.conn.createStatement();
ResultSet rs = (ResultSet) stmt.executeQuery(sql);
while(rs.next()){
String[] dataRow = new String[colums.length];
for( int i = 0; i < dataRow.length; i++ ) {
dataRow[i] = rs.getString( colums[i] );
}
result.add(dataRow);
}
rs.close();
db.close();//
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 获取表中数据的数量
* @param sql
* @return
*/
public static int getCount(String sql) {
int sum = 0;
DBConnection db = new DBConnection();
try {
Statement stmt = (Statement) db.conn.createStatement();
ResultSet rs = (ResultSet) stmt.executeQuery(sql);
while (rs.next()) {
sum += rs.getInt(1);
}
rs.close();
db.close();
} catch (Exception e) {
}
return sum;
}
/**
* 通过Sql语句查询并且将值转为JSON
* @param sql
* @param colums
* @return
*/
public static String getJsonBySql( String sql, String[] colums){
System.err.println("sql:" + sql);
ArrayList result = new ArrayList();
DBConnection db = new DBConnection();
try {
Statement stmt = (Statement) db.conn.createStatement();
ResultSet rs = (ResultSet) stmt.executeQuery(sql);
while(rs.next()){
String[] dataRow = new String[colums.length];
for( int i = 0; i < dataRow.length; i++ ) {
dataRow[i] = rs.getString( colums[i] );
}
result.add(dataRow);
}
rs.close();
db.close();//
} catch (SQLException e) {
e.printStackTrace();
}
return listToJson(result,colums);
}
/**
* 更新
* @param sql
* @return
*/
public static int update(String sql) {
int i =0;
DBConnection db = new DBConnection();
try {
PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(sql);
preStmt.executeUpdate();
preStmt.close();
db.close();
i = 1;
System.out.println("sql" + sql);
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
/**
* json
* @param sql select * from 表
* @param params [id,name,sex,age]
* @return
*/
public static String show(String sql, String[] params){
List< Map > listmap = new ArrayList();
DBConnection db = new DBConnection();
ResultSet rs = null;
try {
Statement stmt = (Statement) db.conn.createStatement();
rs = (ResultSet) stmt.executeQuery(sql);
while(rs.next()){
Map map = new HashMap();
for(int i = 0; i < params.length; i++) {
map.put(params[i], rs.getString(params[i]));
}
listmap.add(map);
}
rs.close();
db.close();
} catch (SQLException e) {
e.printStackTrace();
}
return mapToJson(listmap);
}
/**
* 删除
* @param delstr
* @return
*/
public static int del(String delstr) {
int i=0;
DBConnection db = new DBConnection();
try {
PreparedStatement preStmt = (PreparedStatement) db.conn.prepareStatement(delstr);
preStmt.executeUpdate();
preStmt.close();
db.close();
i = 1;
System.out.println("sql" + delstr);
} catch (SQLException e){
e.printStackTrace();
}
return i;
}
/**
* map转JSON
* @param maplist
* @return
*/
public static String mapToJson( List
package yaojing.servlet;
import yaojing.dao.MysqlUtil;
import yaojing.util.IpUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
//获取账户
String account = request.getParameter("account");//获取用户
//获取密码
String password = request.getParameter("password");
System.out.println("账户 "+account + " 密码 " +password);
String json;
if(account==null || account.equals("")||password==null||password.equals("")){
json = "{\"code\":\"200\",\"message\":\"账户或者密码为空\"}";
response.getWriter().append(json);
return;
}
String sql = "select * from user where account = '"+account+"' and password = '"+password+"';";
String[] colums = {"id","account","password"};
ArrayList arrayList = MysqlUtil.showUtil(sql, colums);
System.out.println(arrayList.toArray());
if (arrayList.size() == 0) {
json = "{\"code\":\"200\",\"message\":\"查无此人\"}";
}else {
//获取真实ip
String ip = IpUtil.getIpAddress(request);
json = "{\"code\":\"200\",\"message\":\"登录成功\",\"ip\":\""+ip+"\"}";
//判断有没有绑定其他ip 如果绑定了替换 并且将删除的ip保存起来
// TODO 不像写直接替换掉每次每次重新暴力拉去
//先查找对应表里有没有存储对应关系
//拿到登录人的id
String id = arrayList.get(0)[0];
String showIpSql = "select userid from userip where userid = "+id+" ";
String[] showIpcolums = {"id","userid","ip","logtim"};
ArrayList showIpList = MysqlUtil.showUtil(sql, colums);
//判断有没有登录过
if(showIpList.size()!=0){ //有 修改
System.out.println("有对应的user " +"修改器对应的ip为" + id);
String showIpId = showIpList.get(0)[0];
String updateIpIdSql = "update userip set userip.ip ='"+ip+"' where userid = "+showIpId+"";
MysqlUtil.update(updateIpIdSql);
}else{ //没有添加
String insertUserip = "insert into userip(userid,ip,logtim) values("+id+",'"+ip+"','"+new Date()+"')";
MysqlUtil.add(insertUserip);
}
String UUID = java.util.UUID.randomUUID().toString();
String insertVersion = "update version set version='"+UUID+"' where id = '1'";
MysqlUtil.add(insertVersion);
request.getSession().setAttribute("user",id);//登录成功标记
}
response.getWriter().append(json);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
package yaojing.servlet;
import yaojing.dao.MysqlUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "ExitServlet", value = "/ExitServlet")
public class ExitServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
String id = (String)request.getSession().getAttribute("user");
//直接删除对应的ip
String updateIpIdSql = "update userip set userip.ip ='' where userid = "+id+"";
MysqlUtil.update(updateIpIdSql);
//更新版本号
String UUID = java.util.UUID.randomUUID().toString();
String insertVersion = "update version set version='"+UUID+"' where id = '1'";
MysqlUtil.add(insertVersion);
String json = "{\"code\":\"200\",\"message\":\"退出成功\"}";
response.getWriter().append(json);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
package yaojing.util;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
/**
* 获取用户访问ip地址
*/
public class IpUtil {
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
// 获取到多个ip时取第一个作为客户端真实ip
if (StringUtils.isNotEmpty(ip) && ip.contains(",")) {
String[] ipArray = ip.split(",");
if (ArrayUtils.isNotEmpty(ipArray)) {
ip = ipArray[0];
}
}
return ip;
}
}
Insert title here
用户名:
密码:
Title
import db.DBConnection;
import db.MysqlUtil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main{
//从数据库查询所有ip
public static void main(String[] args) {
char index = args[0].charAt(0);
switch (index){
case '1':
System.out.println(ShowuseVersion());
break;
case '2':
System.out.println(ShowuserIp());
break;
default:
System.out.println("什么都没有啊");
break;
}
}
public static String ShowuseVersion(){
String sql= "select * from version";
String[] coml = new String[]{"id","version"};
List
4.0.0
com.yaojing
VpnTest
1.0-SNAPSHOT
jar
1.8
1.8
UTF-8
UTF-8
UTF-8
mysql
mysql-connector-java
5.1.6
VpnTest
org.apache.maven.plugins
maven-surefire-plugin
true
org.apache.maven.plugins
maven-assembly-plugin
3.0.0
Main
jar-with-dependencies
make-assembly
package
single
我是练习Shell脚本是如何使用的,所以用的Shell实现,但是我不是很推荐使用Shell,现在服务器都自带py推荐使用py
以下Shell脚本按照执行顺序粘贴
只需要定时器crontab定时执行start.sh就可以
ver=$(cat version.txt)
java -Dfile.encoding=utf-8 -jar VpnTest.jar 1 >version.txt
ver2=$(cat version.txt)
if [ $ver -eq $ver2 ]
then
echo 'yes'
else
sh ./loadConfiguration.sh
fi
#!/bin/bash
#获取ip
sh InitAllowedIp.sh
#分割ip文件
sh splitIp.sh $(cat InitAllowedIp.txt) "InitAllowedIp.txt"
#拿到基础设置
cp InitIptable.txt lastIptable.sh
for line in $(cat InitAllowedIp.txt)
do
ipt='iptables -A INPUT -s '${line}' -p tcp --dport 8080 -j ACCEPT';
echo $ipt >> lastIptable.sh ;
done
#保存在配置文件里
iptables -F
service iptables save
echo 'iptables -P INPUT DROP' >> lastIptable.sh
echo 'service iptables save' >> lastIptable.sh
echo 'service iptables restart' >> lastIptable.sh
sh lastIptable.sh
#!/bin/bash
java -jar ./VpnTest.jar 2 >InitAllowedIp.txt
#!/bin/bash
ip=${1};
url=${2};
#清空指定文件
sed -i '1,$d' $url
fun_Host_Isolcpus(){
param=$1
flag=$(echo $param | grep ",")
if [ $flag ]; then
#有逗号,看有几个逗号
fun_Host_Isolcpus_do ${param%%,*}
fun_Host_Isolcpus ${param#*,}
else
#没有逗号,一次就OK
fun_Host_Isolcpus_do $param
fi
}
fun_Host_Isolcpus_do(){
echo $1>>$url;
echo $1;
}
fun_Host_Isolcpus $ip
ptables -A INPUT -i lo -j ACCEPT;
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT;
iptables -A OUTPUT -j ACCEPT;
iptables -A INPUT -p tcp --dport 22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -j ACCEPT;
iptables -A INPUT -p tcp --dport 21 -j ACCEPT;
iptables -A INPUT -p tcp --dport 20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT;
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT;
iptables -A INPUT -s 127.0.0.1 -p tcp --dport 9999 -j ACCEPT
iptables -A INPUT -s 128.0.0.1 -p tcp --dport 9999 -j ACCEPT
service iptables save
service iptables restart
127.0.0.1
128.0.0.1
ptables -A INPUT -i lo -j ACCEPT;
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT;
iptables -A OUTPUT -j ACCEPT;
iptables -A INPUT -p tcp --dport 22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -j ACCEPT;
iptables -A INPUT -p tcp --dport 21 -j ACCEPT;
iptables -A INPUT -p tcp --dport 20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT;
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT;
ptables -A INPUT -i lo -j ACCEPT;
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT;
iptables -A OUTPUT -j ACCEPT;
iptables -A INPUT -p tcp --dport 22 -j ACCEPT;
iptables -A INPUT -p tcp --dport 80 -j ACCEPT;
iptables -A INPUT -p tcp --dport 443 -j ACCEPT;
iptables -A INPUT -p tcp --dport 21 -j ACCEPT;
iptables -A INPUT -p tcp --dport 20 -j ACCEPT;
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT;
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT;
iptables -A INPUT -s 127.0.0.1 -p tcp --dport 9999 -j ACCEPT
iptables -A INPUT -s 128.0.0.1 -p tcp --dport 9999 -j ACCEPT
444