Commit 3d9a0c0f authored by jim's avatar jim

Merge branch 'master' into staging

# Conflicts:
#	src/main/java/com/edgec/browserbackend/browser/repository/IpResourceRepositoryCustomImpl.java
parents fc8e8d2f 3b419262
...@@ -9,7 +9,9 @@ import org.springframework.context.annotation.Configuration; ...@@ -9,7 +9,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.Jsr310Converters; import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions; import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator; import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator; import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
...@@ -49,6 +51,14 @@ public class BrowserBackendApplication { ...@@ -49,6 +51,14 @@ public class BrowserBackendApplication {
return new CorsFilter(source); return new CorsFilter(source);
} }
@Bean
public TaskScheduler taskScheduler() {
final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(5);
return scheduler;
}
@Configuration @Configuration
public class CORSConfiguration { public class CORSConfiguration {
@Bean @Bean
......
...@@ -63,7 +63,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto ...@@ -63,7 +63,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
@Override @Override
public boolean unLockHealth(String id) { public boolean unLockHealth(String id) {
return false; return true;
} }
@Override @Override
......
...@@ -348,8 +348,8 @@ public class BrowserTask { ...@@ -348,8 +348,8 @@ public class BrowserTask {
static OkHttpClient.Builder builder = new OkHttpClient.Builder(); static OkHttpClient.Builder builder = new OkHttpClient.Builder();
static { static {
builder.connectTimeout(10, TimeUnit.SECONDS); builder.connectTimeout(300, TimeUnit.SECONDS);
builder.readTimeout(10, TimeUnit.SECONDS); builder.readTimeout(300, TimeUnit.SECONDS);
} }
static OkHttpClient client = new OkHttpClient(builder); static OkHttpClient client = new OkHttpClient(builder);
...@@ -377,7 +377,9 @@ public class BrowserTask { ...@@ -377,7 +377,9 @@ public class BrowserTask {
Call call = client.newCall(request); Call call = client.newCall(request);
Response response = call.execute(); Response response = call.execute();
ResponseBody responseBody = response.body(); ResponseBody responseBody = response.body();
return responseBody.string(); String rs = responseBody.string();
response.close();
return rs;
} }
} }
} }
package com.edgec.browserbackend.browser.task; package com.edgec.browserbackend.browser.task;
import com.edgec.browserbackend.account.repository.AccountRepository; import com.edgec.browserbackend.account.repository.AccountRepository;
import com.edgec.browserbackend.auth.domain.mongo.MongoOAuth2AccessToken;
import com.edgec.browserbackend.auth.repository.mongo.MongoOAuth2AccessTokenRepository; import com.edgec.browserbackend.auth.repository.mongo.MongoOAuth2AccessTokenRepository;
import com.edgec.browserbackend.browser.domain.IpResource; import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.domain.IpType; import com.edgec.browserbackend.browser.domain.IpType;
import com.edgec.browserbackend.browser.domain.ProxyConfig;
import com.edgec.browserbackend.browser.repository.IpResourceRepository; import com.edgec.browserbackend.browser.repository.IpResourceRepository;
import com.edgec.browserbackend.browser.repository.ProxyConfigRepository; import com.edgec.browserbackend.browser.repository.ProxyConfigRepository;
import com.edgec.browserbackend.browser.repository.SpecialLineRepository; import com.edgec.browserbackend.browser.repository.SpecialLineRepository;
...@@ -19,10 +19,8 @@ import org.springframework.stereotype.Component; ...@@ -19,10 +19,8 @@ import org.springframework.stereotype.Component;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Component @Component
public class Set3proxyTask { public class Set3proxyTask {
...@@ -45,20 +43,20 @@ public class Set3proxyTask { ...@@ -45,20 +43,20 @@ public class Set3proxyTask {
private ProxyConfigRepository proxyConfigRepository; private ProxyConfigRepository proxyConfigRepository;
@Scheduled(cron = "0 0/2 * * * ?") @Scheduled(cron = "0 0/2 * * * ?")
@SchedulerLock(name = "proxyTask", lockAtLeastFor = 60 * 1000 * 5, lockAtMostFor = 60 * 1000 * 9)
public void set3proxy() { public void set3proxy() {
long validTime = Instant.now().minusSeconds(43200).toEpochMilli(); long nowtime = Instant.now().toEpochMilli();
// List<String> tokenUsernames = mongoOAuth2AccessTokenRepository.findByCreatedAtGreaterThan(validTime).stream().map(MongoOAuth2AccessToken::getUsername).collect(Collectors.toList());
List<String> tokenUsernames = mongoOAuth2AccessTokenRepository.findByCreatedAtGreaterThan(validTime).stream().map(MongoOAuth2AccessToken::getUsername).collect(Collectors.toList()); // List<String> accountParents = accountRepository.findByNameIn(tokenUsernames).stream().map(x -> x.getParent() == null ? x.getName() : x.getParent()).distinct().collect(Collectors.toList());
List<String> accountParents = accountRepository.findByNameIn(tokenUsernames).stream().map(x -> x.getParent() == null ? x.getName() : x.getParent()).distinct().collect(Collectors.toList()); // List<IpResource> ipResources = ipResourceRepository.findByOwnerInAndSpecialLine(accountParents, true);
List<IpResource> ipResources = ipResourceRepository.findByOwnerInAndSpecialLine(accountParents, true); List<IpResource> ipResources = ipResourceRepository.findBySpecialLineAndIsDeleted(true, false);
// List<IpResource> ipResources = ipResourceRepository.findBySpecialLineAndIsDeleted(true, false);
File file = new File("3proxy_" + Instant.now().toEpochMilli()); File file = new File("3proxy_" + Instant.now().toEpochMilli());
try { try {
try {
file.delete(); file.delete();
file.createNewFile(); file.createNewFile();
} catch (Exception t1) {
log.error(t1.getMessage(), t1);
}
FileWriter fileWriter = new FileWriter(file, true); FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fileWriter); BufferedWriter bw = new BufferedWriter(fileWriter);
...@@ -67,55 +65,53 @@ public class Set3proxyTask { ...@@ -67,55 +65,53 @@ public class Set3proxyTask {
bw.write("config /root/3proxy.cfg\n"); bw.write("config /root/3proxy.cfg\n");
bw.write("monitor /root/3proxy.cfg\n"); bw.write("monitor /root/3proxy.cfg\n");
// 写入 ipResources 相关信息 for (IpResource ipResource : ipResources) {
if (ipResources.size() != 0) { if (StringUtils.isNotBlank(ipResource.getAddr()))
bwWriteIpResources(ipResources, bw); bw.write("users \"" + ipResource.getProxyUsername() + ":CL:" + ipResource.getProxyPassword() + "\"\n");
ipResource.setUsingSpecialLine(true);
} }
bw.write("\nauth strong\n"); bw.write("\nauth strong\n");
a:
for (IpResource ipResource : ipResources) {
if (StringUtils.isNotBlank(ipResource.getAddr())) {
if (ipResource.getPort() == null || ipResource.getPort().size() == 0)
continue a;
bw.write("allow " + ipResource.getProxyUsername() + "\n");
if (ipResource.getIpType() != IpType.OWN) {
bw.write("parent 1000 http " + ipResource.getAddr() + " " +
(ipResource.getPort().size() > 1 ? ipResource.getPort().get(1) : ipResource.getPort().get(0)) + " fangguanlianbrowser " + ipResource.getPassword() + "\n");
} else {
bw.write("parent 1000 http " + ipResource.getAddr() + " " +
(ipResource.getPort().size() > 1 ? ipResource.getPort().get(1) : ipResource.getPort().get(0)) + " " + ipResource.getUsername() + " " + ipResource.getPassword() + "\n");
}
}
}
bw.write("\nallow none\nproxy -p20004\nsocks -p20005\n"); bw.write("\nallow none\nproxy -p20004\nsocks -p20005\n");
bw.flush(); bw.flush();
bw.close(); bw.close();
log.info("成功写入文件"); log.error("成功写入文件");
long nowtime = Instant.now().toEpochMilli(); if (proxyConfigRepository.count() > 0) {
long proxyConfigTimestamp = proxyConfigRepository.findAll().get(0).getTimestamp(); ProxyConfig proxyConfig = proxyConfigRepository.findAll().get(0);
if (proxyConfigRepository.count() > 0 && nowtime < proxyConfigTimestamp) { if (nowtime < proxyConfig.getTimestamp()) {
file.delete(); file.delete();
return;
} else {
proxyConfigRepository.updateProxy(file, nowtime);
file.delete();
}
} else { } else {
proxyConfigRepository.updateProxy(file, nowtime); proxyConfigRepository.updateProxy(file, nowtime);
file.delete(); file.delete();
} }
} catch (Exception e) { } catch (Exception e) {
log.error("出错了");
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
} }
private void bwWriteIpResources(List<IpResource> ipResources, BufferedWriter bw) throws IOException {
for (IpResource ipResource : ipResources) {
ipResource.setUsingSpecialLine(true);
if (StringUtils.isNotBlank(ipResource.getAddr())) {
bw.write("users \"" + ipResource.getProxyUsername() + ":CL:" + ipResource.getProxyPassword() + "\"\n");
if (ipResource.getPort() == null || ipResource.getPort().size() == 0) {
continue;
}
bw.write("allow " + ipResource.getProxyUsername() + "\n");
String textStart = "parent 1000 http " + ipResource.getAddr() + " ";
String textMiddle = ipResource.getPort().size() > 1 ? ipResource.getPort().get(1) : ipResource.getPort().get(0);
if (ipResource.getIpType() != IpType.OWN) {
bw.write(textStart + textMiddle
+ " fangguanlianbrowser " + ipResource.getPassword() + "\n");
} else {
bw.write(textStart + textMiddle
+ " " + ipResource.getUsername() + " " + ipResource.getPassword() + "\n");
}
}
}
}
} }
package com.edgec.browserbackend.browser.task;
import com.edgec.browserbackend.account.utils.AccountServicePool;
import com.edgec.browserbackend.common.utils.ThreadPoolUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
import java.util.concurrent.TimeUnit;
@Component
public class TerminateBean {
private static final Logger log = LoggerFactory.getLogger(TerminateBean.class);
@PreDestroy
public void onDestroy() throws Exception {
AccountServicePool.taskPool.shutdown();
ThreadPoolUtils.buyIpTasksPool.shutdown();
ThreadPoolUtils.queryIpHealth.shutdown();
ThreadPoolUtils.queryIpTasksPool.shutdown();
AccountServicePool.taskPool.awaitTermination(5, TimeUnit.MINUTES);
ThreadPoolUtils.buyIpTasksPool.awaitTermination(5, TimeUnit.MINUTES);
ThreadPoolUtils.queryIpHealth.awaitTermination(5, TimeUnit.MINUTES);
ThreadPoolUtils.queryIpTasksPool.awaitTermination(5, TimeUnit.MINUTES);
log.warn("threadpool is destroyed!!");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment