1
13693261870
2022-09-16 fee60c3e25fac0982f3b8cb8feea7225c4ed22f8
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.terra.proxy.schedule;
 
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.terra.proxy.bean.ResActionRecord;
import com.terra.proxy.bean.VistorBean;
import com.terra.proxy.properties.TerraProperties;
import com.terra.proxy.service.Impl.LogServiceImpl;
import com.terra.proxy.util.HttpUtils;
import com.terra.proxy.util.JedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
 
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Component
@Configuration
@EnableScheduling
public class schduletask {
    private static Logger log = LoggerFactory.getLogger(schduletask.class);
 
    @Autowired
    private LogServiceImpl logService;
 
    @Autowired
    private TerraProperties properties;
 
    @Value("${sys.jwt.expire}")
    private Long expireSeconds;
 
    /**
     * 每隔一段时间(1 hour),将redis内的资源访问日志写入数据库
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void logwrite() {
        int countNum = 5000;
        Jedis jedis = JedisUtils.getJedis();
        try {
            Set<String> set = jedis.smembers("visitlog");
            List<Map<String, Object>> list = new ArrayList<>();
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String objstr = it.next();
                JSONObject json = JSONUtil.parseObj(objstr);
                Map<String, Object> map = json.toBean(Map.class);
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                try {
                    date = sd.parse(map.get("date").toString());
                } catch (ParseException e) {
                    log.error("日期转换错误");
                    e.printStackTrace();
                }
                map.put("timestamp", date);
                list.add(map);
                if (list.size() > countNum) {
                    logService.batchsavelog(list);
                    list.clear();
                }
            }
            if (!list.isEmpty()) {
                logService.batchsavelog(list);
            }
            jedis.del("visitlog");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
 
    }
 
    @Scheduled(cron = "0 0 2 * * ?")
    public void jgptlogwrite() {
        Jedis jedis = JedisUtils.getJedis();
        try {
            Set<String> set = jedis.smembers("TerraResActionRecordForJGPT");
            List<Map<String, Object>> list = new ArrayList<>();
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String objstr = it.next();
                JSONObject json = JSONUtil.parseObj(objstr);
                ResActionRecord map = json.toBean(ResActionRecord.class);
                String url = properties.getProxy().getLogapipath() + "/actionrecord/adduseinfo";
                Map<String, Object> params = new HashMap<>();
                params.put("resourceid", map.getResourceid());
                params.put("appid", map.getAppid());
                params.put("userid", map.getUserid());
                params.put("ip", map.getIp());
                try {
                    HttpUtils.post(url, params);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            jedis.del("TerraResActionRecordForJGPT");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
 
    }
 
 
    @Scheduled(cron = "0 0 0 * * ?")
    public void writeRecord() {
        Jedis jedis = JedisUtils.getJedis();
        try {
            Set<String> set = jedis.smembers("TerraResActionRecord");
            List<ResActionRecord> list = new ArrayList<>();
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String objstr = it.next();
                JSONObject json = JSONUtil.parseObj(objstr);
                ResActionRecord map = json.toBean(ResActionRecord.class);
                list.add(map);
            }
            if (!list.isEmpty()) {
                logService.batchsaveResRecord(list);
            }
            jedis.del("TerraResActionRecord");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
 
    }
 
    /* 对于自动续约且到期时间不足一天的token进行续约
     *续约时间
     *每天凌晨0点完成token的自动续约
     *
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void autoToken() {
        Jedis jedis = JedisUtils.getJedis();
        try {
            Set<String> autoTokens = jedis.smembers("autoToken");
            log.info("开始Token自动续约");
            for (String autoToken : autoTokens) {
                log.info("本次Token自动续约,共" + autoTokens.size() + "个数据");
                String token = autoToken;
                String value = jedis.get(token);
                Integer ttl = Math.toIntExact(jedis.ttl(token));
                if (ttl < expireSeconds) {
                    Integer integer = Integer.valueOf(value);
                    if (integer < expireSeconds) integer = Math.toIntExact(expireSeconds);
                    jedis.setex(token, ttl + integer, value);
                }
            }
            log.info("Token自动续约完成");
        } catch (Exception e) {
            log.error("Token自动续约失败,请检查是否存在需要自动续约Token");
        } finally {
            jedis.close();
        }
    }
 
 
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("date = " + date);
 
    }
 
    /**
     * 每15分钟清理一次黑名单异常数据 移除黑名单但是统计数仍为1000的数据
     */
    @Scheduled(cron = "0 */15 * * * ?")
    public void releaseBlackList() {
        Jedis jedis = JedisUtils.getJedis();
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("status","2");
            List<VistorBean> list = logService.queryBlackLists(map);
            for (VistorBean vistorBean : list) {
                String requestip = vistorBean.getRequestip();
                String s = jedis.get(requestip);
                if(StringUtils.isNotEmpty(s)){
                    jedis.del(requestip);
                    log.info("本次清除异常ip为:"+requestip);
                }
            }
            //15分钟释放一次特殊IP
            String tempAllowBlackList = properties.getProxy().getTempAllowBlackList();
            String[] split = tempAllowBlackList.split(";");
            for (String s : split) {
                jedis.del(s);
                log.info("本次释放ip为:"+s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
 
}