From 9306091d3bf9369d3866e3539f78e5e164a38280 Mon Sep 17 00:00:00 2001 From: zayac Date: Thu, 31 Aug 2023 12:13:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8F=AF=E7=94=A8=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 33 ++++ pom.xml | 77 ++++++++ .../changeurl/ChangeUrlApiApplication.java | 18 ++ .../com/zayac/changeurl/api/ChangeURLApi.java | 23 +++ .../changeurl/config/SecurityConfig.java | 33 ++++ .../zayac/changeurl/entity/Msg2JSEntity.java | 7 + .../changeurl/service/ChangeURLService.java | 9 + .../service/ChangeURLServiceImpl.java | 171 ++++++++++++++++++ .../java/com/zayac/changeurl/util/util.java | 8 + src/main/resources/application-ky.yml | 38 ++++ src/main/resources/application.yml | 39 ++++ src/main/resources/link.js.template | 38 ++++ 12 files changed, 494 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/zayac/changeurl/ChangeUrlApiApplication.java create mode 100644 src/main/java/com/zayac/changeurl/api/ChangeURLApi.java create mode 100644 src/main/java/com/zayac/changeurl/config/SecurityConfig.java create mode 100644 src/main/java/com/zayac/changeurl/entity/Msg2JSEntity.java create mode 100644 src/main/java/com/zayac/changeurl/service/ChangeURLService.java create mode 100644 src/main/java/com/zayac/changeurl/service/ChangeURLServiceImpl.java create mode 100644 src/main/java/com/zayac/changeurl/util/util.java create mode 100644 src/main/resources/application-ky.yml create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/link.js.template diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6bb05b1 --- /dev/null +++ b/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.1.3 + + + com.zayac + changeURL + 0.0.1-SNAPSHOT + changeURL + 通过http请求修改指定模板内指定关键词的小工具 + + 17 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-cache + + + org.springframework.boot + spring-boot-starter-security + + + com.github.ben-manes.caffeine + caffeine + 3.0.0 + + + cn.hutool + hutool-all + 5.8.21 + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/com/zayac/changeurl/ChangeUrlApiApplication.java b/src/main/java/com/zayac/changeurl/ChangeUrlApiApplication.java new file mode 100644 index 0000000..8d636fc --- /dev/null +++ b/src/main/java/com/zayac/changeurl/ChangeUrlApiApplication.java @@ -0,0 +1,18 @@ +package com.zayac.changeurl; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +/** + * @author zayac + */ +@SpringBootApplication +@EnableCaching +public class ChangeUrlApiApplication { + + public static void main(String[] args) { + SpringApplication.run(ChangeUrlApiApplication.class, args); + } + +} diff --git a/src/main/java/com/zayac/changeurl/api/ChangeURLApi.java b/src/main/java/com/zayac/changeurl/api/ChangeURLApi.java new file mode 100644 index 0000000..fff9aba --- /dev/null +++ b/src/main/java/com/zayac/changeurl/api/ChangeURLApi.java @@ -0,0 +1,23 @@ +package com.zayac.changeurl.api; + +import com.zayac.changeurl.service.ChangeURLService; +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author zayac + * @since 2023-08-30 12:07 + */ +@RestController +@RequestMapping("/api/v1") +public class ChangeURLApi { + @Resource + private ChangeURLService changeURLService; + + @GetMapping("/change") + public Boolean changeURL(String text) { + return changeURLService.change(text); + } +} diff --git a/src/main/java/com/zayac/changeurl/config/SecurityConfig.java b/src/main/java/com/zayac/changeurl/config/SecurityConfig.java new file mode 100644 index 0000000..faf26f6 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/config/SecurityConfig.java @@ -0,0 +1,33 @@ +package com.zayac.changeurl.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authorization.AuthorizationDecision; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.util.matcher.IpAddressMatcher; + +/** + * @author zayac + */ +@Configuration +public class SecurityConfig { + @Value("${ip.white}") + private String ip; + + @Bean + public SecurityFilterChain filterChain(HttpSecurity security) throws Exception { + IpAddressMatcher hasIpAddress = new IpAddressMatcher(ip); + return security + .authorizeHttpRequests(conf -> conf + .requestMatchers("/api/**") + .access((authentication, context) -> + new AuthorizationDecision(hasIpAddress.matches(context.getRequest())) + ).anyRequest().denyAll()) + .csrf(AbstractHttpConfigurer::disable) + .formLogin(AbstractHttpConfigurer::disable) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/zayac/changeurl/entity/Msg2JSEntity.java b/src/main/java/com/zayac/changeurl/entity/Msg2JSEntity.java new file mode 100644 index 0000000..1e06d26 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/entity/Msg2JSEntity.java @@ -0,0 +1,7 @@ +package com.zayac.changeurl.entity; + +/** + * @author zayac + * @since 2023-08-30 15:19 + */ +public record Msg2JSEntity(String original, String target) { } diff --git a/src/main/java/com/zayac/changeurl/service/ChangeURLService.java b/src/main/java/com/zayac/changeurl/service/ChangeURLService.java new file mode 100644 index 0000000..7723ded --- /dev/null +++ b/src/main/java/com/zayac/changeurl/service/ChangeURLService.java @@ -0,0 +1,9 @@ +package com.zayac.changeurl.service; + +/** + * @author zayac + * @since 2023-08-30 12:07 + */ +public interface ChangeURLService { + Boolean change(String text); +} diff --git a/src/main/java/com/zayac/changeurl/service/ChangeURLServiceImpl.java b/src/main/java/com/zayac/changeurl/service/ChangeURLServiceImpl.java new file mode 100644 index 0000000..2e45199 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/service/ChangeURLServiceImpl.java @@ -0,0 +1,171 @@ +package com.zayac.changeurl.service; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.ReUtil; +import cn.hutool.core.util.StrUtil; +import com.zayac.changeurl.entity.Msg2JSEntity; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * @author zayac + * @since 2023-08-30 12:08 + */ +@Service +public class ChangeURLServiceImpl implements ChangeURLService { + + public static final String URL_PATTERN = "(https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6})(:[0-9]{1,5})?"; + private static final String PREFER = "新"; + @Value("${js.path.targets}") + //开云唯一词 + private String[] targets; + + @Value("${js.path.template}") + //开云唯一词 + private String templatePath; + + @Value("${msg.ky.startWith.web}") + private String msgKyWeb; + + @Value("${msg.ky.startWith.h5}") + private String msgKyH5; + + @Value("${msg.ky.startWith.app}") + private String msgKyApp; + + + @Value("${msg.hth.startWith.web}") + private String msgHthWeb; + @Value("${msg.hth.startWith.h5}") + private String msgHthH5; + + @Value("${msg.hth.startWith.app1}") + private String msgHthApp1; + + @Value("${msg.hth.startWith.app2}") + private String msgHthApp2; + + //js修改部分 + @Value("${js.ky.kyweb1}") + private String jsKyWeb1; + + @Value("${js.ky.kyweb2}") + private String jsKyWeb2; + + @Value("${js.ky.kyh51}") + private String jsKyH51; + + @Value("${js.ky.kyh52}") + private String jsKyH52; + + @Value("${js.ky.kyApp1}") + private String jsKyApp1; + + @Value("${js.ky.kyApp2}") + private String jsKyApp2; + + @Value("${js.hth.hthweb}") + private String jsHthWeb; + + @Value("${js.hth.hthh5}") + private String jsHthH5; + + @Value("${js.hth.hthApp1}") + private String jsHthApp1; + + @Value("${js.hth.hthApp2}") + private String jsHthApp2; + + @Override + public Boolean change(String text) { + //如果传入参数为空,返回修改失败 + if (StrUtil.isEmpty(text)) { + return false; + } else { + text = StrUtil.subBefore(text, "SEO防拦截域名", false); + } + + //处理传入参数 + String[] strings = StrUtil.splitToArray(text, "\n"); + + List msgKyWebList = getByStartWith(strings, msgKyWeb, URL_PATTERN, PREFER); + List msgKyH5List = getByStartWith(strings, msgKyH5, URL_PATTERN, PREFER); + List msgKyAppList = getByStartWith(strings, msgKyApp, URL_PATTERN, PREFER); + String template = loadTemplate(templatePath); + List listEntity = toListEntity(msgKyWebList, msgKyH5List, msgKyAppList); + for (Msg2JSEntity msg2JSEntity : listEntity) { + template = template.replace(msg2JSEntity.original(), msg2JSEntity.target()); + } + for (String target : targets) { + if (FileUtil.exist(target)) { + FileUtil.del(target); + } else { + FileUtil.newFile(target); + } + FileUtil.writeString(template, target, "UTF-8"); + } + + return true; + } + + /** + * 从模板文件中读取文件 + * + * @param path 文件路径 + * @return js文件模板中行List + */ + @Cacheable("template") + public String loadTemplate(String path) { + if (FileUtil.exist(path)) { + return FileUtil.readUtf8String(path); + } else { + throw new RuntimeException("模板文件不存在"); + } + } + + /** + * 处理传入飞机信息 + * + * @param arr 字符数组 + * @param startWith 开始标识 + * @param pattern url正则字符 + * @param prefer 优先 + * @return List + */ + public List getByStartWith(String[] arr, String startWith, String pattern, String prefer) { + List list = Arrays.stream(arr) + .filter(item -> item.startsWith(startWith)) + .map(x -> ReUtil.getGroup1(pattern, x)) + .sorted(Comparator.comparing(str -> str.contains(prefer))) + .collect(Collectors.toList()); + if (list.size() == 0) { + throw new RuntimeException("飞机信息处理错误"); + } + return list; + } + + public List toListEntity(List msgKyWebList, + List msgKyH5List, + List msgKyAppList) { + ArrayList msg2JSEntities = new ArrayList<>(); + msg2JSEntities.add(build(jsKyWeb1, msgKyWebList.get(0))); + msg2JSEntities.add(build(jsKyWeb2, msgKyWebList.get(1))); + msg2JSEntities.add(build(jsKyH51, msgKyH5List.get(0))); + msg2JSEntities.add(build(jsKyH52, msgKyH5List.get(1))); + msg2JSEntities.add(build(jsKyApp1, msgKyAppList.get(0))); + msg2JSEntities.add(build(jsKyApp2, msgKyAppList.get(1))); + return msg2JSEntities.stream().filter(Objects::nonNull).collect(Collectors.toList()); + } + + public static Msg2JSEntity build(String js, String msg) { + if (StrUtil.isNotEmpty(js) && StrUtil.isNotEmpty(msg)) { + return new Msg2JSEntity("{{" + js + "}}", msg); + } + return null; + } + +} diff --git a/src/main/java/com/zayac/changeurl/util/util.java b/src/main/java/com/zayac/changeurl/util/util.java new file mode 100644 index 0000000..2a619e0 --- /dev/null +++ b/src/main/java/com/zayac/changeurl/util/util.java @@ -0,0 +1,8 @@ +package com.zayac.changeurl.util; + +/** + * @author zayac + * @since 2023-08-30 13:12 + */ +public class util { +} diff --git a/src/main/resources/application-ky.yml b/src/main/resources/application-ky.yml new file mode 100644 index 0000000..2f58285 --- /dev/null +++ b/src/main/resources/application-ky.yml @@ -0,0 +1,38 @@ +msg: + ky: + startWith: + web: "WEB" + h5: "H5" + app: "全站" + hth: + startWith: + web: "" + h5: "" + app1: "" + app2: "" +js: + ky: + kyweb1: "kyPc" + kyweb2: "" + kyh51: "kyH5" + kyh52: "" + kyApp1: "kyApp" + kyApp2: "" + hth: + hthweb: "" + hthh5: "" + hthApp1: "" + hthApp2: "" + path: + template: "link.js.template" + targets: "link.js" + +server: + port: 8888 +spring: + cache: + type: CAFFEINE + caffeine: + spec: maximumSize=100,expireAfterWrite=10m +ip: + white: "127.0.0.1" \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..b511090 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,39 @@ + +msg: + ky: + startWith: + web: "WEB" + h5: "H5" + app: "全站" + hth: + startWith: + web: "web" + h5: "h5" + app1: "全站APP" + app2: "体育APP" +js: + ky: + kyweb1: "kyweb1" + kyweb2: "kyweb2" + kyh51: "kyh51" + kyh52: "kyh52" + kyApp1: "kyApp" + kyApp2: "kyApp2" + hth: + hthweb: "hthweb" + hthh5: "hthh5" + hthApp1: "hthApp1" + hthApp2: "hthApp2" + path: + template: "link.js.template" + targets: "link.js" + +server: + port: 8888 +spring: + cache: + type: CAFFEINE + caffeine: + spec: maximumSize=100,expireAfterWrite=10m + profiles: + active: ky \ No newline at end of file diff --git a/src/main/resources/link.js.template b/src/main/resources/link.js.template new file mode 100644 index 0000000..351cfed --- /dev/null +++ b/src/main/resources/link.js.template @@ -0,0 +1,38 @@ +let code = '97238304', + ybty_link = { + kyApp: '{{kyApp}}/?i_code=' + code,// 开云全站app + kyPc: '{{kyPc}}/register/?i_code=' + code,//·开云体育电脑端 + kyH5: '{{kyH5}}/entry/register/?i_code=' + code,//·开云体育手机端 + } + +function ybty_visit(key) { + window.location.href = ybty_link[key]; +} + +function ybty_visit_newopen(key) { + window.open(ybty_link[key]) +} + +function ybty_kf(url) { + window.open(url) +} + +function getMyUrl(key) { + return ybty_link[key]; +} + +function deviceYBRegister(code) { + if (window.innerWidth < 768) { + window.open(ybty_link['ybH5']); + } else { + window.open(ybty_link['ybPc']); + } +} + +function deviceLYRegister(code) { + if (window.innerWidth < 768) { + window.open(ybty_link['ybH55']); + } else { + window.open(ybty_link['ybPc']); + } +} \ No newline at end of file