Commit 4d35f9a8 authored by xuxin's avatar xuxin

Merge remote-tracking branch 'origin/staging' into staging

parents 60eff80f 3d9a0c0f
......@@ -9,7 +9,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.scheduling.TaskScheduler;
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.oauth2.provider.token.AuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
......@@ -49,6 +51,14 @@ public class BrowserBackendApplication {
return new CorsFilter(source);
}
@Bean
public TaskScheduler taskScheduler() {
final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(5);
return scheduler;
}
@Configuration
public class CORSConfiguration {
@Bean
......
......@@ -63,7 +63,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
@Override
public boolean unLockHealth(String id) {
return false;
return true;
}
@Override
......
......@@ -348,8 +348,8 @@ public class BrowserTask {
static OkHttpClient.Builder builder = new OkHttpClient.Builder();
static {
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(10, TimeUnit.SECONDS);
builder.connectTimeout(300, TimeUnit.SECONDS);
builder.readTimeout(300, TimeUnit.SECONDS);
}
static OkHttpClient client = new OkHttpClient(builder);
......@@ -377,7 +377,9 @@ public class BrowserTask {
Call call = client.newCall(request);
Response response = call.execute();
ResponseBody responseBody = response.body();
return responseBody.string();
String rs = responseBody.string();
response.close();
return rs;
}
}
}
package com.edgec.browserbackend.browser.task;
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.browser.domain.IpResource;
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.ProxyConfigRepository;
import com.edgec.browserbackend.browser.repository.SpecialLineRepository;
......@@ -19,10 +19,8 @@ import org.springframework.stereotype.Component;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class Set3proxyTask {
......@@ -45,20 +43,20 @@ public class Set3proxyTask {
private ProxyConfigRepository proxyConfigRepository;
@Scheduled(cron = "0 0/2 * * * ?")
@SchedulerLock(name = "proxyTask", lockAtLeastFor = 60 * 1000 * 5, lockAtMostFor = 60 * 1000 * 9)
public void set3proxy() {
long validTime = Instant.now().minusSeconds(43200).toEpochMilli();
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<IpResource> ipResources = ipResourceRepository.findByOwnerInAndSpecialLine(accountParents, true);
// List<IpResource> ipResources = ipResourceRepository.findBySpecialLineAndIsDeleted(true, false);
long nowtime = Instant.now().toEpochMilli();
// 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<IpResource> ipResources = ipResourceRepository.findByOwnerInAndSpecialLine(accountParents, true);
List<IpResource> ipResources = ipResourceRepository.findBySpecialLineAndIsDeleted(true, false);
File file = new File("3proxy_" + Instant.now().toEpochMilli());
try {
file.delete();
file.createNewFile();
try {
file.delete();
file.createNewFile();
} catch (Exception t1) {
log.error(t1.getMessage(), t1);
}
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fileWriter);
......@@ -67,55 +65,53 @@ public class Set3proxyTask {
bw.write("config /root/3proxy.cfg\n");
bw.write("monitor /root/3proxy.cfg\n");
// 写入 ipResources 相关信息
if (ipResources.size() != 0) {
bwWriteIpResources(ipResources, bw);
for (IpResource ipResource : ipResources) {
if (StringUtils.isNotBlank(ipResource.getAddr()))
bw.write("users \"" + ipResource.getProxyUsername() + ":CL:" + ipResource.getProxyPassword() + "\"\n");
ipResource.setUsingSpecialLine(true);
}
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.flush();
bw.close();
log.info("成功写入文件");
log.error("成功写入文件");
long nowtime = Instant.now().toEpochMilli();
long proxyConfigTimestamp = proxyConfigRepository.findAll().get(0).getTimestamp();
if (proxyConfigRepository.count() > 0 && nowtime < proxyConfigTimestamp) {
file.delete();
if (proxyConfigRepository.count() > 0) {
ProxyConfig proxyConfig = proxyConfigRepository.findAll().get(0);
if (nowtime < proxyConfig.getTimestamp()) {
file.delete();
return;
} else {
proxyConfigRepository.updateProxy(file, nowtime);
file.delete();
}
} else {
proxyConfigRepository.updateProxy(file, nowtime);
file.delete();
}
} catch (Exception e) {
log.error("出错了");
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