feat:glados和ddns脚本
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.xiang.service.module.glados.constants;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-05-08 15:27
|
||||
*/
|
||||
public class GladosConstants {
|
||||
|
||||
/**
|
||||
* glados 主域名
|
||||
*/
|
||||
private static final String GLADOS_URL_PREFIX = "https://glados.cloud";
|
||||
|
||||
/**
|
||||
* 签到接口
|
||||
*/
|
||||
public static final String GLADOS_CHECK_IN_URL = GLADOS_URL_PREFIX + "/api/user/checkin";
|
||||
|
||||
/**
|
||||
* 签到请求体
|
||||
*/
|
||||
public static final String GLADOS_CHECK_IN_BODY = "{\"token\":\"glados.cloud\"}";
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.xiang.service.module.glados.manage;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.xiang.common.mapper.GladosRunLogDao;
|
||||
import com.xiang.common.pojo.glados.GladosRunLogDO;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class GladosRunLogManageImpl extends ServiceImpl<GladosRunLogDao, GladosRunLogDO> implements IGladosRunLogManage {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.xiang.service.module.glados.manage;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.xiang.common.mapper.GladosUserDao;
|
||||
import com.xiang.common.pojo.glados.GladosUserDO;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class GladosUserManageImpl extends ServiceImpl<GladosUserDao, GladosUserDO> implements IGladosUserManage {
|
||||
@Override
|
||||
public List<GladosUserDO> listAllUsers() {
|
||||
LambdaQueryWrapper<GladosUserDO> lqw = Wrappers.lambdaQuery();
|
||||
lqw.eq(GladosUserDO::getStatus, 1);
|
||||
return baseMapper.selectList(lqw);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GladosUserDO findByUsername(String username) {
|
||||
LambdaQueryWrapper<GladosUserDO> lqw = Wrappers.lambdaQuery();
|
||||
lqw.eq(GladosUserDO::getStatus, 1);
|
||||
lqw.eq(GladosUserDO::getUser, username);
|
||||
lqw.last("limit 1");
|
||||
return baseMapper.selectOne(lqw);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.xiang.service.module.glados.manage;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.xiang.common.pojo.glados.GladosRunLogDO;
|
||||
|
||||
public interface IGladosRunLogManage extends IService<GladosRunLogDO> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.xiang.service.module.glados.manage;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.xiang.common.pojo.glados.GladosUserDO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IGladosUserManage extends IService<GladosUserDO> {
|
||||
|
||||
List<GladosUserDO> listAllUsers();
|
||||
|
||||
GladosUserDO findByUsername(String username);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.xiang.service.module.glados.schedule;
|
||||
|
||||
import com.xiang.common.enums.ScheduleEnums;
|
||||
import com.xiang.common.factory.schedule.BaseScheduleTaskTemplate;
|
||||
import com.xiang.common.pojo.schedule.TaskResult;
|
||||
import com.xiang.common.service.IScheduleOpeningConfigService;
|
||||
import com.xiang.common.service.IScheduleRunLogService;
|
||||
import com.xiang.service.module.glados.service.IGLaDOSService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-05-08 15:24
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class GladosCheckInTask extends BaseScheduleTaskTemplate {
|
||||
|
||||
private final IGLaDOSService glaDOSService;
|
||||
|
||||
public GladosCheckInTask(IScheduleOpeningConfigService scheduleOpeningConfigService, IScheduleRunLogService scheduleRunLogService, IGLaDOSService glaDOSService) {
|
||||
super(scheduleOpeningConfigService, scheduleRunLogService);
|
||||
this.glaDOSService = glaDOSService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTaskName() {
|
||||
return ScheduleEnums.GLADOS_CHECK_IN_TASK.getTaskName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer getModule() {
|
||||
return ScheduleEnums.GLADOS_CHECK_IN_TASK.getModeleCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getModuleName() {
|
||||
return ScheduleEnums.GLADOS_CHECK_IN_TASK.getModule();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TaskResult doExecute(Object validatedParams) {
|
||||
return glaDOSService.checkIn();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.xiang.service.module.glados.schedule;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
public class GladosSchedule {
|
||||
|
||||
private final GladosCheckInTask gladosCheckInJob;
|
||||
|
||||
@Scheduled(cron = "0 0 7 1/1 * ?")
|
||||
@GetMapping("/test1")
|
||||
public void checkIn() {
|
||||
gladosCheckInJob.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.xiang.service.module.glados.server;
|
||||
|
||||
import com.xiang.service.module.glados.service.IGLaDOSService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/script/glados")
|
||||
@RequiredArgsConstructor
|
||||
public class GladosServer {
|
||||
|
||||
private final IGLaDOSService gladosService;
|
||||
|
||||
@GetMapping("/checkIn")
|
||||
public void checkIn(@RequestParam("username") String username) {
|
||||
try {
|
||||
gladosService.checkIn(username);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.xiang.service.module.glados.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.xiang.common.factory.ScriptDingTalkFactory;
|
||||
import com.xiang.common.pojo.glados.GladosRunLogDO;
|
||||
import com.xiang.common.pojo.glados.GladosUserDO;
|
||||
import com.xiang.common.pojo.glados.resp.CheckInResp;
|
||||
import com.xiang.common.pojo.glados.resp.GLaDOSResponse;
|
||||
import com.xiang.common.pojo.schedule.TaskResult;
|
||||
import com.xiang.common.utils.HttpService;
|
||||
import com.xiang.service.module.glados.constants.GladosConstants;
|
||||
import com.xiang.service.module.glados.manage.IGladosRunLogManage;
|
||||
import com.xiang.service.module.glados.manage.IGladosUserManage;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-05-08 14:38
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class GLaDOSServiceImpl implements IGLaDOSService {
|
||||
|
||||
private final IGladosUserManage gladosUserManage;
|
||||
|
||||
private final ScriptDingTalkFactory dingTalkService;
|
||||
|
||||
|
||||
@Override
|
||||
public TaskResult checkIn() {
|
||||
TaskResult taskResult = new TaskResult();
|
||||
|
||||
|
||||
List<GladosUserDO> users = gladosUserManage.listAllUsers();
|
||||
if (CollectionUtils.isEmpty(users)) {
|
||||
taskResult.setSuccess(false);
|
||||
taskResult.setSummary("无可使用的用户");
|
||||
return taskResult;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (GladosUserDO user : users) {
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (checkInV2(user, sb)) {
|
||||
break;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("线程暂停10s失败");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("签到失败,", e);
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(sb.toString())) {
|
||||
taskResult.setSuccess(false);
|
||||
taskResult.setSummary(sb.toString());
|
||||
return taskResult;
|
||||
}
|
||||
taskResult.setSuccess(true);
|
||||
return taskResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkIn(String username) {
|
||||
GladosUserDO gladosUserDO = gladosUserManage.findByUsername(username);
|
||||
if (Objects.isNull(gladosUserDO)) {
|
||||
log.info("[job] Glados Check In user not exists, username:{}", username);
|
||||
return;
|
||||
}
|
||||
checkInV2(gladosUserDO, new StringBuilder());
|
||||
}
|
||||
|
||||
public boolean checkInV2(GladosUserDO user, StringBuilder sb) {
|
||||
Map<String, String> header = Maps.newHashMap();
|
||||
header.put("Cookie", user.getCookie());
|
||||
|
||||
String response = null;
|
||||
|
||||
try {
|
||||
response = HttpService.doPost(GladosConstants.GLADOS_CHECK_IN_URL, header, GladosConstants.GLADOS_CHECK_IN_BODY);
|
||||
} catch (Exception e) {
|
||||
log.error("http请求异常:{}", user.getEmail());
|
||||
return false;
|
||||
}
|
||||
if (org.apache.commons.lang3.StringUtils.isBlank(response)) {
|
||||
sb.append(user.getUser()).append("请求结果为空!");
|
||||
return false;
|
||||
}
|
||||
|
||||
GLaDOSResponse<CheckInResp> gLaDOSResponse = JSONObject.parseObject(response, new TypeReference<GLaDOSResponse<CheckInResp>>() {
|
||||
});
|
||||
if (Objects.isNull(gLaDOSResponse)) {
|
||||
sb.append(user.getUser()).append("请求结果为空!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (0 == gLaDOSResponse.getCode()) {
|
||||
// 成功请求
|
||||
if (Objects.nonNull(gLaDOSResponse.getPoints()) && 0 != gLaDOSResponse.getPoints()) {
|
||||
// 签到成功
|
||||
dingTalkService.sendMsg("[时间:" + LocalDateTime.now() + "] 用户: " +
|
||||
user.getEmail() + "签到成功,获得积分:" + gLaDOSResponse.getPoints());
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
if (gLaDOSResponse.getMessage().contains("Today's observation logged")) {
|
||||
if (!CollectionUtils.isEmpty(gLaDOSResponse.getList())) {
|
||||
dingTalkService.sendMsg("用户:" + user.getEmail() + "当前已签到。结果:" + gLaDOSResponse.getList().get(0));
|
||||
sb.append(user.getUser()).append("当前已经签到!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (-2 == gLaDOSResponse.getCode()) {
|
||||
log.warn("签到失败,用户:{}, cookie过期:{}", user.getEmail(), gLaDOSResponse.getMessage());
|
||||
String message = "[时间:" + LocalDateTime.now() + "] 用户: " + user.getEmail() + ",签到消息: " +
|
||||
gLaDOSResponse.getMessage() + "手动请求:http://general.xiangtech.xyz:30026/script/glados/checkIn?username=" + user.getUser();
|
||||
try {
|
||||
dingTalkService.sendMsg(message);
|
||||
sb.append(user.getUser()).append("签到结果异常!结果:").append(gLaDOSResponse.getMessage());
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("发送钉钉消息失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 请求异常
|
||||
dingTalkService.sendMsg("用户:" + user.getEmail() + "请求异常,响应结果:" + gLaDOSResponse.getMessage());
|
||||
sb.append(user.getUser()).append("响应结果异常!结果:").append(gLaDOSResponse.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.xiang.service.module.glados.service;
|
||||
|
||||
import com.xiang.common.pojo.glados.GladosUserDO;
|
||||
import com.xiang.common.pojo.schedule.TaskResult;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-05-08 14:38
|
||||
*/
|
||||
public interface IGLaDOSService {
|
||||
|
||||
|
||||
/**
|
||||
* 签到
|
||||
*/
|
||||
TaskResult checkIn();
|
||||
|
||||
/**
|
||||
* 根据用户名签到
|
||||
* @param username
|
||||
*/
|
||||
void checkIn(String username);
|
||||
}
|
||||
Reference in New Issue
Block a user