diff --git a/src/main/java/net/lamgc/cgj/Main.java b/src/main/java/net/lamgc/cgj/Main.java index a1d1c7c..3f0028f 100644 --- a/src/main/java/net/lamgc/cgj/Main.java +++ b/src/main/java/net/lamgc/cgj/Main.java @@ -15,9 +15,7 @@ import net.lamgc.cgj.bot.framework.mirai.MiraiMain; import net.lamgc.cgj.pixiv.PixivDownload; import net.lamgc.cgj.pixiv.PixivSearchBuilder; import net.lamgc.cgj.pixiv.PixivURL; -import net.lamgc.cgj.util.PropertiesUtils; import net.lamgc.plps.PixivLoginProxyServer; -import net.lamgc.utils.base.ArgumentsProperties; import net.lamgc.utils.base.runner.Argument; import net.lamgc.utils.base.runner.ArgumentsRunner; import net.lamgc.utils.base.runner.Command; @@ -33,7 +31,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.*; -import java.net.URL; import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -46,13 +43,13 @@ import java.util.zip.ZipOutputStream; @SpringBootApplication public class Main { - private final static Logger log = LoggerFactory.getLogger(Main.class.getName()); + private final static Logger log = LoggerFactory.getLogger(Main.class); private final static File storeDir = new File("store/"); - public static CookieStore cookieStore; + private static CookieStore cookieStore; - public static HttpHost proxy; + private static HttpHost proxy; public static void main(String[] args) throws IOException, ClassNotFoundException { log.trace("ContentGrabbingJi 正在启动..."); @@ -61,21 +58,8 @@ public class Main { ApplicationBoot.initialApplication(args); log.debug("botDataDir: {}", System.getProperty("cgj.botDataDir")); - ArgumentsProperties argsProp = new ArgumentsProperties(args); - - if(!PropertiesUtils.getSettingToSysProp(argsProp, "proxy", null)) { - PropertiesUtils.getEnvSettingToSysProp("CGJ_PROXY", "proxy", null); - } - - String proxyAddress = System.getProperty("cgj.proxy"); - if(!Strings.isNullOrEmpty(proxyAddress)) { - URL proxyUrl = new URL(proxyAddress); - proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort()); - log.info("已启用Http协议代理:{}", proxy.toHostString()); - } else { - proxy = null; - } + proxy = BotGlobal.getGlobal().getProxy(); File cookieStoreFile = new File(BotGlobal.getGlobal().getDataStoreDir(), "cookies.store"); if(!cookieStoreFile.exists()) { log.warn("未找到cookies.store文件, 是否启动PixivLoginProxyServer? (yes/no)"); @@ -91,6 +75,7 @@ public class Main { } ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cookieStoreFile)); cookieStore = (CookieStore) ois.readObject(); + BotGlobal.getGlobal().setCookieStore(cookieStore); ois.close(); log.info("已载入CookieStore"); diff --git a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java index 3dc6e29..6c4dfa2 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java @@ -4,7 +4,6 @@ import com.google.common.base.Strings; import com.google.common.base.Throwables; import com.google.gson.*; import io.netty.handler.codec.http.HttpHeaderNames; -import net.lamgc.cgj.Main; import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.cache.*; import net.lamgc.cgj.bot.event.BufferMessageEvent; @@ -37,9 +36,10 @@ import java.util.concurrent.atomic.AtomicInteger; @SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter", "SameParameterValue"}) public class BotCommandProcess { - private final static PixivDownload pixivDownload = new PixivDownload(Main.cookieStore, Main.proxy); + private final static PixivDownload pixivDownload = + new PixivDownload(BotGlobal.getGlobal().getCookieStore(), BotGlobal.getGlobal().getProxy()); - private final static Logger log = LoggerFactory.getLogger(BotCommandProcess.class.getName()); + private final static Logger log = LoggerFactory.getLogger(BotCommandProcess.class); private final static File imageStoreDir = new File(BotGlobal.getGlobal().getDataStoreDir(), "data/image/cgj/"); private final static Gson gson = new GsonBuilder() diff --git a/src/main/java/net/lamgc/cgj/bot/boot/ApplicationBoot.java b/src/main/java/net/lamgc/cgj/bot/boot/ApplicationBoot.java index 78d06a2..7d6e73b 100644 --- a/src/main/java/net/lamgc/cgj/bot/boot/ApplicationBoot.java +++ b/src/main/java/net/lamgc/cgj/bot/boot/ApplicationBoot.java @@ -18,6 +18,9 @@ public final class ApplicationBoot { */ public static void initialApplication(String[] args) { ArgumentsProperties argsProp = new ArgumentsProperties(args); + if(!PropertiesUtils.getSettingToSysProp(argsProp, "proxy", null)) { + PropertiesUtils.getEnvSettingToSysProp("CGJ_PROXY", "proxy", null); + } if(!PropertiesUtils.getSettingToSysProp(argsProp, "botDataDir", "./") && !PropertiesUtils.getEnvSettingToSysProp("CGJ_BOT_DATA_DIR", "botDataDir", "./")) { log.warn("未设置botDataDir, 当前运行目录将作为酷Q机器人所在目录."); diff --git a/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java b/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java index 87f28ae..224fa1c 100644 --- a/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java +++ b/src/main/java/net/lamgc/cgj/bot/boot/BotGlobal.java @@ -1,11 +1,16 @@ package net.lamgc.cgj.bot.boot; +import com.google.common.base.Strings; +import org.apache.http.HttpHost; +import org.apache.http.client.CookieStore; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.JedisPool; import java.io.File; +import java.net.MalformedURLException; import java.net.URI; +import java.net.URL; public final class BotGlobal { @@ -29,6 +34,10 @@ public final class BotGlobal { private final File dataStoreDir; + private final HttpHost proxy; + + private CookieStore cookieStore; + private BotGlobal() { this.redisUri = URI.create("redis://" + System.getProperty("cgj.redisAddress")); this.redisServer = new JedisPool( @@ -37,6 +46,19 @@ public final class BotGlobal { String dataStoreDirPath = System.getProperty("cgj.botDataDir"); this.dataStoreDir = new File((!dataStoreDirPath.endsWith("/") || !dataStoreDirPath.endsWith("\\")) ? dataStoreDirPath + System.getProperty("file.separator") : dataStoreDirPath); + + String proxyAddress = System.getProperty("cgj.proxy"); + HttpHost temp = null; + if(!Strings.isNullOrEmpty(proxyAddress)) { + try { + URL proxyUrl = new URL(proxyAddress); + temp = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort()); + log.info("已启用Http协议代理:{}", temp.toHostString()); + } catch (MalformedURLException e) { + log.error("Proxy地址解析失败, 代理将不会启用.", e); + } + } + this.proxy = temp; } public URI getRedisUri() { @@ -53,4 +75,17 @@ public final class BotGlobal { public JedisPool getRedisServer() { return redisServer; } + + public HttpHost getProxy() { + return proxy; + } + + + public CookieStore getCookieStore() { + return cookieStore; + } + + public void setCookieStore(CookieStore cookieStore) { + this.cookieStore = cookieStore; + } } diff --git a/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java index 7017521..36ef31c 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java @@ -1,6 +1,6 @@ package net.lamgc.cgj.bot.cache; -import net.lamgc.cgj.Main; +import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.cache.exception.HttpRequestException; import net.lamgc.cgj.pixiv.PixivURL; import net.lamgc.cgj.util.URLs; @@ -22,9 +22,11 @@ import java.util.Set; public class ImageCacheHandler implements EventHandler { - private final static Logger log = LoggerFactory.getLogger(ImageCacheHandler.class.getName()); + private final static Logger log = LoggerFactory.getLogger(ImageCacheHandler.class); - private final static HttpClient httpClient = HttpClientBuilder.create().setProxy(Main.proxy).build(); + private final static HttpClient httpClient = HttpClientBuilder.create() + .setProxy(BotGlobal.getGlobal().getProxy()) + .build(); private final static Set cacheQueue = Collections.synchronizedSet(new HashSet<>());