Commit 80bc9bc1 authored by renjie's avatar renjie

Merge branch 'dev-zrj' into 'staging'

Dev zrj

See merge request !76
parents 5a671c49 933d81b6
...@@ -176,6 +176,14 @@ ...@@ -176,6 +176,14 @@
<version>262</version> <version>262</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.6.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -4,6 +4,8 @@ public interface UserShopRepositoryCustom { ...@@ -4,6 +4,8 @@ public interface UserShopRepositoryCustom {
boolean deleteByUsernameAndShopId(String username, String shopId); boolean deleteByUsernameAndShopId(String username, String shopId);
boolean deleteByShopId(String shopId);
void updateGroupId(String groupId_old, String groupId_new); void updateGroupId(String groupId_old, String groupId_new);
boolean deleteByShopIdExceptOwner(String shopId, String owner); boolean deleteByShopIdExceptOwner(String shopId, String owner);
......
...@@ -31,6 +31,18 @@ public class UserShopRepositoryCustomImpl implements UserShopRepositoryCustom { ...@@ -31,6 +31,18 @@ public class UserShopRepositoryCustomImpl implements UserShopRepositoryCustom {
return true; return true;
} }
@Override
public boolean deleteByShopId(String shopId) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("shopId").is(shopId));
DeleteResult operation = mongoTemplate.remove(basicQuery, UserShop.class);
if (operation.getDeletedCount() < 1)
return false;
else
return true;
}
@Override @Override
public void updateGroupId(String groupId_old, String groupId_new) { public void updateGroupId(String groupId_old, String groupId_new) {
Document doc = new Document(); Document doc = new Document();
......
...@@ -182,11 +182,6 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -182,11 +182,6 @@ public class IpResourceServiceImpl implements IpResourceService {
return retStr; return retStr;
} }
public static void main(String[] args) {
String password = genRandom(3, 12);
System.out.println(password);
}
private IpChargeRequestDto buildIpChargeRequestDto(IpResourceRequestDto request, int chargeType, int payMethod) { private IpChargeRequestDto buildIpChargeRequestDto(IpResourceRequestDto request, int chargeType, int payMethod) {
IpChargeRequestDto ipChargeRequestDto = new IpChargeRequestDto(); IpChargeRequestDto ipChargeRequestDto = new IpChargeRequestDto();
ipChargeRequestDto.setAmount(request.getAmount()); ipChargeRequestDto.setAmount(request.getAmount());
......
...@@ -99,6 +99,11 @@ public class ShopServiceImpl implements ShopService { ...@@ -99,6 +99,11 @@ public class ShopServiceImpl implements ShopService {
userShop.setGroupId(shopResultDto.getGroup()); userShop.setGroupId(shopResultDto.getGroup());
} }
userShopRepository.save(userShop); userShopRepository.save(userShop);
if (StringUtils.isNotBlank(account.getParent())) {
userShop.setUsername(account.getParent());
userShop.setGroupId("-1");
userShopRepository.save(userShop);
}
//可以优化 //可以优化
account.setShopCount(account.getShopCount() + 1); account.setShopCount(account.getShopCount() + 1);
accountRepository.save(account); accountRepository.save(account);
...@@ -155,6 +160,11 @@ public class ShopServiceImpl implements ShopService { ...@@ -155,6 +160,11 @@ public class ShopServiceImpl implements ShopService {
userShop.setGroupId(shopResultDto.getGroup()); userShop.setGroupId(shopResultDto.getGroup());
} }
userShopRepository.save(userShop); userShopRepository.save(userShop);
if (StringUtils.isNotBlank(account.getParent())) {
userShop.setUsername(account.getParent());
userShop.setGroupId("-1");
userShopRepository.save(userShop);
}
//可以优化 //可以优化
account.setShopCount(account.getShopCount() + 1); account.setShopCount(account.getShopCount() + 1);
accountRepository.save(account); accountRepository.save(account);
...@@ -215,7 +225,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -215,7 +225,7 @@ public class ShopServiceImpl implements ShopService {
ipResource.setShopId(null); ipResource.setShopId(null);
ipResourceRepository.save(ipResource); ipResourceRepository.save(ipResource);
} }
boolean result = userShopRepository.deleteByUsernameAndShopId(username, shopId); boolean result = userShopRepository.deleteByShopId(shopId);
if (result) { if (result) {
shopRepository.deleteById(shopId); shopRepository.deleteById(shopId);
account.setShopCount(account.getShopCount() - 1); account.setShopCount(account.getShopCount() - 1);
...@@ -402,6 +412,8 @@ public class ShopServiceImpl implements ShopService { ...@@ -402,6 +412,8 @@ public class ShopServiceImpl implements ShopService {
if (shops == null || shops.getNumberOfElements() < 1) if (shops == null || shops.getNumberOfElements() < 1)
return new ShopPageResultDto(); return new ShopPageResultDto();
List<ShopResultDto> shopResultDtos = new ArrayList<>(); List<ShopResultDto> shopResultDtos = new ArrayList<>();
logger.error("shops.size" + shops.getNumberOfElements());
logger.error("shops.content.size " + shops.getContent().size());
shops.getContent().stream().forEach(x -> { shops.getContent().stream().forEach(x -> {
IpResource ipResource = ipResourceRepository.findByShopIdAndIsDeleted(x.getShopId(), false); IpResource ipResource = ipResourceRepository.findByShopIdAndIsDeleted(x.getShopId(), false);
if (ipResource == null) if (ipResource == null)
...@@ -450,7 +462,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -450,7 +462,7 @@ public class ShopServiceImpl implements ShopService {
int willexpired = ipResourceRepository.countByStatusAndIdInAndIsDeleted(2, bind, false); int willexpired = ipResourceRepository.countByStatusAndIdInAndIsDeleted(2, bind, false);
shopSummary.setExpired(expired); shopSummary.setExpired(expired);
shopSummary.setWillExpire(willexpired); shopSummary.setWillExpire(willexpired);
shopSummary.setTotal(userShopRepository.countByUsername(username)); shopSummary.setTotal(shopIds.size());
return shopSummary; return shopSummary;
} }
......
package com.edgec.browserbackend.browser.task; package com.edgec.browserbackend.browser.task;
import com.alibaba.fastjson.JSONObject;
import com.edgec.browserbackend.account.domain.IpChargeRequestDto; import com.edgec.browserbackend.account.domain.IpChargeRequestDto;
import com.edgec.browserbackend.account.service.AccountService; import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.browser.domain.*; import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.dto.*; import com.edgec.browserbackend.browser.dto.IpBuyResultDto;
import com.edgec.browserbackend.browser.dto.IpInfoResultDto;
import com.edgec.browserbackend.browser.dto.ShopRequestDto;
import com.edgec.browserbackend.browser.repository.IpResourceRepository; import com.edgec.browserbackend.browser.repository.IpResourceRepository;
import com.edgec.browserbackend.browser.service.IpAndShopService; import com.edgec.browserbackend.browser.service.IpAndShopService;
import com.edgec.browserbackend.common.commons.utils.NotifyUtils;
import com.edgec.browserbackend.common.utils.ThreadPoolUtils; import com.edgec.browserbackend.common.utils.ThreadPoolUtils;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*; import org.springframework.http.HttpEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import static com.edgec.browserbackend.browser.service.Impl.IpResourceServiceImpl.genRandom; import static com.edgec.browserbackend.browser.service.Impl.IpResourceServiceImpl.genRandom;
...@@ -37,6 +44,7 @@ public class BrowserTask { ...@@ -37,6 +44,7 @@ public class BrowserTask {
private static String CLOUDAMURL = "https://www.cloudam.cn"; private static String CLOUDAMURL = "https://www.cloudam.cn";
private static String TESTURL = "http://112.74.13.2"; private static String TESTURL = "http://112.74.13.2";
@Autowired @Autowired
private IpResourceRepository ipResourceRepository; private IpResourceRepository ipResourceRepository;
...@@ -49,13 +57,13 @@ public class BrowserTask { ...@@ -49,13 +57,13 @@ public class BrowserTask {
@Value("${spring.profiles.active}") @Value("${spring.profiles.active}")
private String profiles; private String profiles;
public HttpHeaders buildGetHeader() { public Map<String, String> buildGetHeader() {
HttpHeaders headers = new HttpHeaders(); Map<String, String> headers = new HashMap<>();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.put("Content-Type", "application/json");
if (profiles.equals("dev") || profiles.equals("staging")) if (profiles.equals("dev") || profiles.equals("staging"))
headers.setBearerAuth("oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm"); headers.put("Authorization", "Bearer oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm");
else if (profiles.equals("prod")) else if (profiles.equals("prod"))
headers.setBearerAuth("tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO"); headers.put("Authorization", "Bearer tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO");
return headers; return headers;
} }
...@@ -105,6 +113,7 @@ public class BrowserTask { ...@@ -105,6 +113,7 @@ public class BrowserTask {
map.put("loginPassword", ipResource.getPassword()); map.put("loginPassword", ipResource.getPassword());
map.put("startscript", ""); map.put("startscript", "");
map.put("ipkeptperiod", 7); map.put("ipkeptperiod", 7);
map.put("persistSystemDiskOnTermination", false);
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header); HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header);
IpBuyResultDto ipBuyResultDto = null; IpBuyResultDto ipBuyResultDto = null;
try { try {
...@@ -174,24 +183,19 @@ public class BrowserTask { ...@@ -174,24 +183,19 @@ public class BrowserTask {
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
if (ipResourceRepository.lockTask(ipResource)) { if (ipResourceRepository.lockTask(ipResource)) {
try { try {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); String url = URL + "/ecc/ipinfo?accountId=browser&ip=" + ipResource.getAddr();
requestFactory.setConnectTimeout(10000);// 设置超时 Map<String, String> header = buildGetHeader();
requestFactory.setReadTimeout(10000); String rs = HttpClientutils.doGet(url, header);
RestTemplate restTemplate = new RestTemplate(requestFactory); IpInfoResultDto ipInfoResultDto = JSONObject.parseObject(rs, IpInfoResultDto.class);
Map<String, Object> params = new HashMap<>();
HttpHeaders headers = buildGetHeader();
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(params, headers);
ResponseEntity<IpInfoResultDto> result = restTemplate.exchange(URL + "/ecc/ipinfo?accountId={accountId}&ip={ip}",
HttpMethod.GET, entity, IpInfoResultDto.class, "browser", ipResource.getAddr());
IpInfoResultDto ipInfoResultDto = result.getBody();
if (ipInfoResultDto != null && StringUtils.isBlank(ipInfoResultDto.getErrorCode())) { if (ipInfoResultDto != null && StringUtils.isBlank(ipInfoResultDto.getErrorCode())) {
if ( StringUtils.isNotEmpty(ipInfoResultDto.getStatus())) { if (StringUtils.isNotEmpty(ipInfoResultDto.getStatus())) {
ipResource.setStatus(0); ipResource.setStatus(0);
ipResourceRepository.save(ipResource); ipResourceRepository.save(ipResource);
} }
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
NotifyUtils.sendMessage("浏览器后端 queryIpTasks() 又炸了,赶紧看啊", e, NotifyUtils.MsgType.WEBHOOK);
} finally { } finally {
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
log.debug("queryIpTask {} execution time is: " + (end - start) / 1000 + "s", ipResource.getId()); log.debug("queryIpTask {} execution time is: " + (end - start) / 1000 + "s", ipResource.getId());
...@@ -209,4 +213,47 @@ public class BrowserTask { ...@@ -209,4 +213,47 @@ public class BrowserTask {
} }
public static class HttpClientutils {
static OkHttpClient.Builder builder = new OkHttpClient.Builder();
static {
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(10, TimeUnit.SECONDS);
}
static OkHttpClient client = new OkHttpClient(builder);
public static String doGet(String url, Map<String, String> headers) throws IOException {
Headers httpHeaders = Headers.of(headers);
Request request = new Request.Builder()
.get()
.url(url)
.headers(httpHeaders)
.build();
Call call = client.newCall(request);
Response response = call.execute();
ResponseBody responseBody = response.body();
return responseBody.string();
}
}
public static void main(String[] args) throws IOException {
String url = CLOUDAMURL + "/ecc/ipinfo?accountId=browser&ip=" + "100.25.101.201";
String profiles = "prod";
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
if (profiles.equals("dev") || profiles.equals("staging"))
headers.put("Authorization", "Bearer oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm");
else if (profiles.equals("prod"))
headers.put("Authorization", "Bearer tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO");
String rs = HttpClientutils.doGet(url, headers);
System.out.println(rs);
IpInfoResultDto ipInfoResultDto = JSONObject.parseObject(rs, IpInfoResultDto.class);
System.out.println(ipInfoResultDto);
}
} }
...@@ -23,7 +23,7 @@ public class NotifyUtils { ...@@ -23,7 +23,7 @@ public class NotifyUtils {
private static final CloseableHttpClient httpClient = HttpClients.createDefault(); private static final CloseableHttpClient httpClient = HttpClients.createDefault();
public enum MsgType { public enum MsgType {
WEBHOOK("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=6bdea516-5321-45b8-bdf4-c666aa35af7c"), WEBHOOK("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7876986a-40f2-47a5-8071-f8aef21a996c"),
; ;
String url; String url;
......
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