Commit e398eab0 authored by Administrator's avatar Administrator

Merge branch 'staging' into 'master'

Staging

See merge request !146
parents 2fb169b3 89b62bef
package com.edgec.browserbackend;
import com.mongodb.MongoClient;
import net.javacrumbs.shedlock.core.LockProvider;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
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.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
import org.springframework.web.cors.CorsConfiguration;
......@@ -35,10 +24,8 @@ import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@SpringBootApplication
@EnableGlobalMethodSecurity(prePostEnabled = true)
......@@ -134,7 +121,7 @@ public class BrowserBackendApplication {
return new MongoCustomConversions(converters);
}
enum LocalDateToStringConverter implements Converter<LocalDate, String> {
enum LocalDateToStringConverter implements Converter<LocalDate, String> {
INSTANCE;
......@@ -144,7 +131,7 @@ public class BrowserBackendApplication {
}
}
enum StringToLocalDateConverter implements Converter<String, LocalDate> {
enum StringToLocalDateConverter implements Converter<String, LocalDate> {
INSTANCE;
......@@ -174,7 +161,7 @@ public class BrowserBackendApplication {
}
}
}
}
}
......@@ -2,16 +2,15 @@ package com.edgec.browserbackend.account.controller;
import com.edgec.browserbackend.account.domain.*;
import com.edgec.browserbackend.account.dto.*;
import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.account.service.EmailService;
import com.edgec.browserbackend.account.service.PaymentService;
import com.edgec.browserbackend.alipay.AlipayConfig;
import com.edgec.browserbackend.alipay.AlipaySubmit;
import com.edgec.browserbackend.alipay.VpsAlipayConfig;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.common.auth.Securitys;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import com.edgec.browserbackend.common.commons.utils.DateConverter;
import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.account.service.EmailService;
import com.edgec.browserbackend.account.service.PaymentService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
......@@ -27,10 +26,6 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.security.Principal;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
......@@ -38,7 +33,7 @@ import java.time.format.DateTimeFormatter;
import java.util.*;
@RestController
@RequestMapping("user")
@RequestMapping("/user")
public class AccountController {
public static final String WECHAT_PAY_CALLBACK_URL = "https://cloudam.cn/accounts/0xwxpaycallback/";
......@@ -123,6 +118,7 @@ public class AccountController {
accountService.sendSmsOtp(mobile.getMobile());
resultDto.setStatus(0);
} catch (ClientRequestException e) {
logger.error("fail to send sms", e);
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
......@@ -140,7 +136,7 @@ public class AccountController {
if (!StringUtils.isEmpty(user.getEmail()))
account = accountService.create(user);
else
account = accountService.createWithSms(user);
account = accountService.createWithSms(user);
resultDto.setStatus(0);
resultDto.setData(account);
} catch (ClientRequestException e) {
......@@ -206,14 +202,6 @@ public class AccountController {
return accountService.findOverviewByYearAndMonth(principal.getName(), year, month, zoneId, service);
}
public static void main(String[] args) {
String intStr = "08";
int month = Integer.parseInt(intStr);
System.out.println(month);
}
@RequestMapping(path = "/current/updatetoken", method = RequestMethod.POST)
void updateUserToken(@RequestParam("username") String username, @RequestParam("token") String token) {
accountService.updateUserToken(username, token);
......@@ -225,7 +213,7 @@ public class AccountController {
try {
accountService.resetPassword(principal.getName(), user);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
......@@ -246,7 +234,7 @@ public class AccountController {
try {
accountService.resetPasswordWithOtp(user);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
......@@ -273,7 +261,7 @@ public class AccountController {
@RequestMapping(path = "/wxpay/checkstatus/{tradno}/{chargeType}", method = RequestMethod.GET)
public UserPaymentDto wechatPayCheckStatus(@PathVariable String tradno, @PathVariable int chargeType) {
return paymentService.wxCheckOrderStatus(tradno, chargeType);
return paymentService.wxCheckOrderStatus(tradno, chargeType);
}
@RequestMapping(path = "/0xalipaycallback/{tradno}", method = RequestMethod.GET)
......@@ -356,8 +344,8 @@ public class AccountController {
Pageable pageable = PageRequest.of(page, size);
String[] datas = strDate.split("-");
int year = Integer.valueOf(datas[0]);
int month = Integer.valueOf(datas[1]);
int year = Integer.parseInt(datas[0]);
int month = Integer.parseInt(datas[1]);
int day = 0;
if (datas.length > 2) {
day = Integer.valueOf(datas[2]);
......@@ -399,7 +387,7 @@ public class AccountController {
}
@RequestMapping(path = "/preorder/delete/{username}", method = RequestMethod.POST)
public String deletePreOrder( @PathVariable("username") String username) {
public String deletePreOrder(@PathVariable("username") String username) {
return accountService.deletePreOrder(username);
}
......@@ -439,7 +427,7 @@ public class AccountController {
}
accountService.authorizeCompany(principal.getName(), companyAuthorizeDto);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
......@@ -450,14 +438,14 @@ public class AccountController {
}
@RequestMapping(path = "/ali/login", method = RequestMethod.GET)
public String save(HttpServletRequest request,HttpServletResponse response) {
public String save(HttpServletRequest request, HttpServletResponse response) {
//页面回调地址 必须与应用中的设置一样
String return_url = "http://https://www.fangguanlian.cn/ali/withdraw";
//回调地址必须经encode
return_url = java.net.URLEncoder.encode(return_url);
//重定向到授权页面
AlipayConfig alipayConfig = new VpsAlipayConfig();
return "redirect:"+ VpsAlipayConfig.ALIPAY_AUTH_URL + "?app_id=" + alipayConfig.getAPPID() + "&scope=auth_user&redirect_uri=" + return_url;
return "redirect:" + VpsAlipayConfig.ALIPAY_AUTH_URL + "?app_id=" + alipayConfig.getAPPID() + "&scope=auth_user&redirect_uri=" + return_url;
}
@RequestMapping(path = "/ali/withdraw", method = RequestMethod.PUT)
......
......@@ -7,37 +7,37 @@ public enum AccountErrorCode implements ErrorCode {
/* client errors */
UNKNOWN(ErrorCode.COMMON_UNKNOWN,"unknown"),
NAMEEXIST(ACCOUNT_BASE+100, "Username exists"),
NAMENOTEXIST(ACCOUNT_BASE+102, "Username does not exist"),
EMAILEXIST(ACCOUNT_BASE+101, "Email exists"),
EMAILNOTEXIST(ACCOUNT_BASE+103, "Email does not exist"),
NAMEOREMAILNOTEXIST(ACCOUNT_BASE+104, "Username or Email does not exist"),
NOTALLOWEDTOCREATESUBUSER(ACCOUNT_BASE+105, "The account isn't allowed to create sub user"),
PHONEEXIST(ACCOUNT_BASE+106, "Phone number exists"),
OTPWRONG(ACCOUNT_BASE+107, "otp invalid or expires"),
OTPSENDFAILS(ACCOUNT_BASE+108, "otp sends failure"),
PHONENOTEXIST(ACCOUNT_BASE+109, "Can not find account with phone number"),
UNKNOWN(ErrorCode.COMMON_UNKNOWN, "unknown"),
NAMEEXIST(ACCOUNT_BASE + 100, "Username exists"),
NAMENOTEXIST(ACCOUNT_BASE + 102, "Username does not exist"),
EMAILEXIST(ACCOUNT_BASE + 101, "Email exists"),
EMAILNOTEXIST(ACCOUNT_BASE + 103, "Email does not exist"),
NAMEOREMAILNOTEXIST(ACCOUNT_BASE + 104, "Username or Email does not exist"),
NOTALLOWEDTOCREATESUBUSER(ACCOUNT_BASE + 105, "The account isn't allowed to create sub user"),
PHONEEXIST(ACCOUNT_BASE + 106, "Phone number exists"),
OTPWRONG(ACCOUNT_BASE + 107, "otp invalid or expires"),
OTPSENDFAILS(ACCOUNT_BASE + 108, "otp sends failure"),
PHONENOTEXIST(ACCOUNT_BASE + 109, "Can not find account with phone number"),
INVALIDTOKEN(ErrorCode.COMMON_INVALID_TOKEN, "Invalid token"),
UNAUTHORIZED(ErrorCode.COMMON_UNAUTHRORIZED, "Unauthorized operation"),
NAMEEMPTY(ACCOUNT_BASE+110, "Username connot be empty"),
EMAILEMPTY(ACCOUNT_BASE+111, "Email connot be empty"),
PHONEEMPTY(ACCOUNT_BASE+112, "PhoneNumber connot be empty"),
AGENCYNOEXIST(ACCOUNT_BASE+113, "uuid dose not exists"),
NAMEEMPTY(ACCOUNT_BASE + 110, "Username connot be empty"),
EMAILEMPTY(ACCOUNT_BASE + 111, "Email connot be empty"),
PHONEEMPTY(ACCOUNT_BASE + 112, "PhoneNumber connot be empty"),
AGENCYNOEXIST(ACCOUNT_BASE + 113, "uuid dose not exists"),
CHILDSCALINGSTATEUNABATED(ACCOUNT_BASE+114, "Can't delete child whose still have scaling group "),
CHILDSCALINGSTATEUNABATED(ACCOUNT_BASE + 114, "Can't delete child whose still have scaling group "),
BINDORDERERROR_NOTEXIST(ACCOUNT_BASE+115,"This order is not exist"),
BINDORDERERROR_NOTEXIST(ACCOUNT_BASE + 115, "This order is not exist"),
BINDORDERERROR_BINDBYOTHERS(ACCOUNT_BASE+116,"This order has been bind by others"),
BINDORDERERROR_BINDBYOTHERS(ACCOUNT_BASE + 116, "This order has been bind by others"),
BINDORDERERROR_NOTQUALIFIED(ACCOUNT_BASE+117,"You don't have free qualified"),
BINDORDERERROR_NOTQUALIFIED(ACCOUNT_BASE + 117, "You don't have free qualified"),
BINDORDERERROR_HAVEORDER(ACCOUNT_BASE+118,"You have an unpaid order"),
BINDORDERERROR_HAVEORDER(ACCOUNT_BASE + 118, "You have an unpaid order"),
BINDORDERERROR_NOTKNOW(ACCOUNT_BASE+119,"Order generate error,please try again"),
BINDORDERERROR_NOTKNOW(ACCOUNT_BASE + 119, "Order generate error,please try again"),
/* http request error */
OTHERS(ErrorCode.COMMON_OTHERS, "http request error"),
......@@ -45,19 +45,22 @@ public enum AccountErrorCode implements ErrorCode {
/* server errors */
EMAILSENTERROR(ErrorCode.COMMON_SERVER_ERROR, "email sending error"),
/** wechat errors */
WECHATERROR(ACCOUNT_BASE+120, "Wechat order generation error"),
/**
* wechat errors
*/
WECHATERROR(ACCOUNT_BASE + 120, "Wechat order generation error"),
/** alipay errors */
ALIPAYERROR(ACCOUNT_BASE+130, "Alipay order generation error"),
/**
* alipay errors
*/
ALIPAYERROR(ACCOUNT_BASE + 130, "Alipay order generation error"),
NOPERMISSION(ACCOUNT_BASE+140, "You have no right to do this operarion"),
GROUPMAX(ACCOUNT_BASE+141, "You can not have more groups"),
SHOPMAX(ACCOUNT_BASE+142, "You can not have more shops"),
CHILDMAX(ACCOUNT_BASE+143, "You can not have more subUsers"),
NOTENOUGHBALANCE(ErrorCode.ACCOUNT_BASE+150, "Your balance is not enough");
NOPERMISSION(ACCOUNT_BASE + 140, "You have no right to do this operarion"),
GROUPMAX(ACCOUNT_BASE + 141, "You can not have more groups"),
SHOPMAX(ACCOUNT_BASE + 142, "You can not have more shops"),
CHILDMAX(ACCOUNT_BASE + 143, "You can not have more subUsers"),
NOTENOUGHBALANCE(ErrorCode.ACCOUNT_BASE + 150, "Your balance is not enough");
private final int code;
......@@ -68,10 +71,12 @@ public enum AccountErrorCode implements ErrorCode {
this.reason = reasonPhrase;
}
@Override
public int value() {
return this.code;
}
@Override
@JsonValue
public int getCode() {
return code;
......@@ -82,6 +87,7 @@ public enum AccountErrorCode implements ErrorCode {
return Integer.toString(this.code);
}
@Override
public String getReason() {
return reason;
}
......
package com.edgec.browserbackend.account.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.edgec.browserbackend.account.dto.*;
import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.account.service.PaymentService;
import com.edgec.browserbackend.account.domain.*;
import com.edgec.browserbackend.account.dto.*;
import com.edgec.browserbackend.account.exception.AccountErrorCode;
import com.edgec.browserbackend.account.repository.*;
import com.edgec.browserbackend.account.service.AccountService;
import com.edgec.browserbackend.account.service.EmailService;
import com.edgec.browserbackend.account.service.PaymentService;
import com.edgec.browserbackend.account.service.SmsUtils;
import com.edgec.browserbackend.account.utils.AccountServicePool;
import com.edgec.browserbackend.auth.exception.AuthErrorCode;
......@@ -33,7 +33,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.thymeleaf.util.StringUtils;
import java.io.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.YearMonth;
......@@ -322,7 +322,8 @@ public class AccountServiceImpl implements AccountService {
@Override
public ResultDto getAccountByName(String name) {
Assert.hasLength(name);
Assert.hasLength(name, "账户名不能为空");
ResultDto resultDto = new ResultDto();
try {
Account account = this.findByName(name);
......@@ -398,6 +399,7 @@ public class AccountServiceImpl implements AccountService {
return resultDto;
}
@Override
public void deleteByName(String name) {
userService.deleteUser(name);
repository.deleteById(name);
......@@ -411,6 +413,7 @@ public class AccountServiceImpl implements AccountService {
return create(user, null);
}
@Override
public void deleteSub(String parent, String child) {
Assert.hasText(parent);
Account childAccount = repository.findByName(child);
......@@ -1177,8 +1180,7 @@ public class AccountServiceImpl implements AccountService {
CompanyAuthorize companyAuthorize = companyAuthorizeRepository.findByUsername(username);
account.setCompanyName(companyAuthorize.getCompanyName());
repository.save(account);
}
else {
} else {
account.setAuthorized(3);
account.setCompanyName(null);
repository.save(account);
......
......@@ -30,16 +30,17 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.csrf().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers( "/user/authCode", "/user/signUp",
"/user/forgot**", "/0xadministrator/getconfig**");
.antMatchers("/user/authCode", "/user/signUp",
"/user/forgot**", "/0xadministrator/getconfig**");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
.passwordEncoder(new BCryptPasswordEncoder());
}
@Override
......
......@@ -8,16 +8,16 @@ public enum AuthErrorCode implements ErrorCode {
/* http request error */
OTHERS(ErrorCode.COMMON_OTHERS, "http request error"),
AUTHENTICATION_ERROR(ErrorCode.AUTH_BASE+100, "authentication failure"),
AUTHORIZATION_ERROR(ErrorCode.AUTH_BASE+200, "authorization failure"),
AUTHENTICATION_ERROR(ErrorCode.AUTH_BASE + 100, "authentication failure"),
AUTHORIZATION_ERROR(ErrorCode.AUTH_BASE + 200, "authorization failure"),
NAMEEXIST(AUTH_BASE+106, "Username exists"),
NAMENOTEXIST(AUTH_BASE+102, "Username does not exist"),
EMAILEXIST(AUTH_BASE+101, "Email exists"),
EMAILNOTEXIST(AUTH_BASE+103, "Email does not exist"),
NAMEOREMAILNOTEXIST(AUTH_BASE+104, "Username or Email does not exist"),
WRONGEMAILCODE(AUTH_BASE+105, "Wrong verification code"),
LOGINTIMESEXCEEDED(AUTH_BASE+107, "Login times exceeded"),
NAMEEXIST(AUTH_BASE + 106, "Username exists"),
NAMENOTEXIST(AUTH_BASE + 102, "Username does not exist"),
EMAILEXIST(AUTH_BASE + 101, "Email exists"),
EMAILNOTEXIST(AUTH_BASE + 103, "Email does not exist"),
NAMEOREMAILNOTEXIST(AUTH_BASE + 104, "Username or Email does not exist"),
WRONGEMAILCODE(AUTH_BASE + 105, "Wrong verification code"),
LOGINTIMESEXCEEDED(AUTH_BASE + 107, "Login times exceeded"),
/* server errors */
INTERNALSERVERERROR(ErrorCode.COMMON_SERVER_ERROR, "internal server error");
......@@ -31,11 +31,13 @@ public enum AuthErrorCode implements ErrorCode {
this.reason = reasonPhrase;
}
@Override
public int value() {
return this.code;
}
@JsonValue
@Override
public int getCode() {
return code;
}
......@@ -45,6 +47,7 @@ public enum AuthErrorCode implements ErrorCode {
return Integer.toString(this.code);
}
@Override
public String getReason() {
return reason;
}
......
......@@ -12,8 +12,6 @@ import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {
......@@ -30,9 +28,9 @@ public class AuthenticationSuccessEventListener implements ApplicationListener<A
User user = userRepository.findById(username).orElse(null);
if (user != null) {
UserAttemptsLogin userAttempts = userAttemptsLoginRepository.findById(username).orElse(null);
if(userAttempts != null) {
if (userAttempts != null) {
if (userAttempts.getAttempts() >= 20) {
if (userAttempts.getDate().getTime() >= (new Date().getTime() - 600000))
if (userAttempts.getDate().getTime() >= (System.currentTimeMillis() - 600000))
throw new ClientRequestException(AuthErrorCode.LOGINTIMESEXCEEDED, "Login times exceeded");
} else if (userAttempts.getAttempts() != 0) {
saveUserAttemptsLogin(username, 0);
......
......@@ -7,20 +7,20 @@ public enum BrowserErrorCode implements ErrorCode {
/* client errors */
UNKNOWN(ErrorCode.COMMON_UNKNOWN,"unknown"),
INFORMATIONNOTCOMPELETE(BROWSER_BASE+100, "The information about shop does not complete"),
SHOPNOTEXIST(BROWSER_BASE+101, "The shop does not exist"),
UNKNOWN(ErrorCode.COMMON_UNKNOWN, "unknown"),
INFORMATIONNOTCOMPELETE(BROWSER_BASE + 100, "The information about shop does not complete"),
SHOPNOTEXIST(BROWSER_BASE + 101, "The shop does not exist"),
SHOP_BINDED(BROWSER_BASE + 102, "The shop has bind with ip"),
IPNOTEXIST(BROWSER_BASE+201, "The ip do not exist"),
IPNOTBINDTOSHOP(BROWSER_BASE+202, "The ip does not bind this shop."),
IPNOTEXIST(BROWSER_BASE + 201, "The ip do not exist"),
IPNOTBINDTOSHOP(BROWSER_BASE + 202, "The ip does not bind this shop."),
GROUPNOTEXIST(BROWSER_BASE+301, "The group does not exist"),
GROUPNOTEXIST(BROWSER_BASE + 301, "The group does not exist"),
IPTRANSACTIONNOTEXIST(BROWSER_BASE+401, "The ipTransaction does not exist"),
COMPANYAUTHORIZEEXIST(BROWSER_BASE+501, "The authority exists"),
COMPANYAUTHORIZENOTEXIST(BROWSER_BASE+502, "The authority dose not exist");
IPTRANSACTIONNOTEXIST(BROWSER_BASE + 401, "The ipTransaction does not exist"),
COMPANYAUTHORIZEEXIST(BROWSER_BASE + 501, "The authority exists"),
COMPANYAUTHORIZENOTEXIST(BROWSER_BASE + 502, "The authority dose not exist");
private final int code;
......@@ -31,11 +31,13 @@ public enum BrowserErrorCode implements ErrorCode {
this.reason = reasonPhrase;
}
@Override
public int value() {
return this.code;
}
@JsonValue
@Override
public int getCode() {
return code;
}
......@@ -45,6 +47,7 @@ public enum BrowserErrorCode implements ErrorCode {
return Integer.toString(this.code);
}
@Override
public String getReason() {
return reason;
}
......
package com.edgec.browserbackend.browser.controller;
import com.edgec.browserbackend.account.dto.ResultDto;
import com.edgec.browserbackend.browser.domain.OperationHistory;
import com.edgec.browserbackend.browser.dto.HistoryListRequestDto;
import com.edgec.browserbackend.browser.dto.IpResourceRequestDto;
import com.edgec.browserbackend.browser.dto.LoginHistoryDto;
import com.edgec.browserbackend.browser.dto.OperationHistoryDto;
import com.edgec.browserbackend.browser.service.HistoryService;
......@@ -32,11 +30,7 @@ public class HistoryController {
historyService.addLoginHistory(principal.getName(), loginHistoryDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -48,11 +42,7 @@ public class HistoryController {
historyService.addOperationHistory(principal.getName(), operationHistoryDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -64,11 +54,7 @@ public class HistoryController {
resultDto.setData(historyService.getLoginHistories(principal.getName(), historyListRequestDto));
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
......@@ -80,12 +66,16 @@ public class HistoryController {
resultDto.setData(historyService.getOperationHistories(principal.getName(), historyListRequestDto));
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
dealClientRequestException(resultDto, e);
}
return resultDto;
}
private void dealClientRequestException(ResultDto resultDto, ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
}
package com.edgec.browserbackend.browser.controller;
import com.edgec.browserbackend.account.dto.ResultDto;
import com.edgec.browserbackend.browser.dto.*;
import com.edgec.browserbackend.browser.dto.IpListRequestDto;
import com.edgec.browserbackend.browser.dto.IpPageResultDto;
import com.edgec.browserbackend.browser.dto.IpResourceRequestDto;
import com.edgec.browserbackend.browser.dto.IpResourceUpdateDto;
import com.edgec.browserbackend.browser.service.IpResourceService;
import com.edgec.browserbackend.common.auth.Securitys;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
......@@ -25,7 +28,7 @@ public class IpControlloer {
private IpResourceService ipResourceService;
@RequestMapping(value = "/buy", method = RequestMethod.POST)
public ResultDto buyIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto){
public ResultDto buyIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto) {
ResultDto resultDto = new ResultDto();
try {
List<String> ipResourceDto = ipResourceService.buyIp(principal.getName(), ipResourceRequestDto);
......@@ -93,7 +96,7 @@ public class IpControlloer {
ipListRequestDto.getAmount(), ipListRequestDto.getFilter());
resultDto.setData(ipResourceDto);
resultDto.setStatus(0);
}catch (ClientRequestException e) {
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
......@@ -110,6 +113,12 @@ public class IpControlloer {
try {
ipResourceService.updateIp(principal.getName(), ipResourceUpdateDto);
resultDto.setStatus(0);
} catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode().getCode());
statusInfo.put("message", e.getErrorCode().getReason());
resultDto.setStatusInfo(statusInfo);
} catch (Exception e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
......
package com.edgec.browserbackend.browser.domain;
import com.edgec.browserbackend.browser.dto.Interval;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Document(collection = "ipresource")
@JsonIgnoreProperties(ignoreUnknown = true)
public class IpResource implements Serializable {
@Id
private String id;
private String addr;
......@@ -311,4 +311,22 @@ public class IpResource implements Serializable {
public void setShopId(String shopId) {
this.shopId = shopId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof IpResource)) return false;
IpResource that = (IpResource) o;
return Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
return Objects.hash(getId());
}
}
package com.edgec.browserbackend.browser.dto;
import com.alibaba.fastjson.JSONObject;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.domain.Shop;
import com.edgec.browserbackend.browser.domain.TransferStatus;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.gson.JsonObject;
import org.elasticsearch.client.license.LicensesStatus;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ShopResultDto {
......
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.domain.IpType;
import com.edgec.browserbackend.browser.domain.Shop;
import com.google.gson.internal.$Gson$Preconditions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
......@@ -13,27 +9,45 @@ import java.util.List;
public interface IpResourceRepository extends MongoRepository<IpResource, String>, IpResourceRepositoryCustom {
IpResource findByAddr(String addr);
IpResource findByAddrAndIsDeletedAndIsLocked(String addr, boolean isDeleted, boolean isLocked);
IpResource findByAddrAndIsDeleted(String addr, boolean isDeleted);
IpResource findByIdAndIsDeletedAndIsLocked(String id, boolean isDeleted, boolean isLocked);
IpResource findByIdAndIsDeleted(String id, boolean isDeleted);
Page<IpResource> findByIdInAndIsDeletedOrderByPurchasedTimeDesc(List<String> ipIds, boolean isDeleted, Pageable pageable);
List<IpResource> findByOwnerAndIsDeletedAndBind(String owner, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerAndStatusIsNotInAndIsDeletedAndBind(String owner, List<Integer> status, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerAndStatusInAndIsDeletedAndBind(String owner, List<Integer> status, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerAndStatusAndIsDeletedAndBind(String owner, int status, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerInAndIsDeletedAndBind(List<String> owner, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerInAndStatusIsNotInAndIsDeletedAndBind(List<String> owner, List<Integer> status, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerInAndStatusInAndIsDeletedAndBind(List<String> owner, List<Integer> status, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerInAndStatusAndIsDeletedAndBind(List<String> owner, int status, boolean isDeleted, boolean bind);
Page<IpResource> findByIsDeletedAndIdInAndAddrLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String addr, Pageable pageable);
Page<IpResource> findByIsDeletedAndIdInAndVendorCnLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String vendorCn, Pageable pageable);
Page<IpResource> findByIsDeletedAndIdInAndVendorLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String vendor, Pageable pageable);
Page<IpResource> findByIsDeletedAndIdInAndRegionCnLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String region, Pageable pageable);
List<IpResource> findByIsDeleted(boolean isDeleted);
List<IpResource> findByOwnerInAndSpecialLine(List<String> owners, boolean specialLine);
List<IpResource> findBySpecialLineAndIsDeleted(boolean specialLine, boolean isDeleted);
List<IpResource> findByRegionInAndIsDeleted(List<String> regions, boolean isDeleted);
......@@ -49,6 +63,8 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String
long countAllByIsDeletedAndValidTimeGreaterThan(boolean isDeleted, long time);
long countAllByOwnerAndIsDeletedAndValidTimeGreaterThan(String username, boolean isDeleted, long time);
long countAllByOwnerInAndIsDeletedAndValidTimeGreaterThan(List<String> username, boolean isDeleted, long time);
long countAllByOwner(String username);
}
......@@ -19,6 +19,8 @@ public interface IpResourceRepositoryCustom {
List<IpResource> sampleTasks(List<Integer> status);
List<IpResource> findIds();
boolean addShopId(String ipId, String shopId);
boolean deleteShopId(String ipId, String shopId, BindHistory bindHistory);
......
......@@ -3,17 +3,13 @@ package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.BindHistory;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.dto.IpResourceUnwindResultDto;
import com.edgec.browserbackend.common.utils.JsonUtils;
import com.mongodb.client.result.UpdateResult;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Update;
import java.time.Instant;
......@@ -95,13 +91,13 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
@Override
public List<IpResource> sampleTasks(int status, long timestamp) {
Criteria matchCriteria = new Criteria();
Criteria matchCriteria = new Criteria();
matchCriteria.orOperator(where("status").is(status).and("isLocked").is(false).and("isDeleted").is(false),
where("status").is(status).and("isLocked").is(true).and("lockTimestamp").lte(timestamp).and("isDeleted").is(false));
MatchOperation match = Aggregation.match(matchCriteria);
SampleOperation sample = Aggregation.sample(20);
SampleOperation sample = Aggregation.sample(100);
AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class);
List<IpResource> mappedResults = results.getMappedResults();
......@@ -110,8 +106,8 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
@Override
public List<IpResource> sampleTasks(List<Integer> status) {
Criteria matchCriteria = new Criteria();
matchCriteria.orOperator(where("status").in(status).and("isLocked").is(false).and("validTime").gt(Instant.now().toEpochMilli()).and("isDeleted").is(false).and("healthLockTimestamp").lte(Instant.now().minusSeconds(60*30).toEpochMilli()),
Criteria matchCriteria = new Criteria();
matchCriteria.orOperator(where("status").in(status).and("isLocked").is(false).and("validTime").gt(Instant.now().toEpochMilli()).and("isDeleted").is(false).and("healthLockTimestamp").lte(Instant.now().minusSeconds(60 * 30).toEpochMilli()),
where("status").in(status).and("isLocked").is(true).and("healthLockTimestamp").lte(Instant.now().minusSeconds(600).toEpochMilli()).and("isDeleted").is(false));
MatchOperation match = Aggregation.match(matchCriteria);
......@@ -194,7 +190,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
if (ipResourceUnwindResultDtos.isEmpty()) {
return new ArrayList<>();
}
HashMap<String ,IpResource> ipResourceHashMap = new HashMap<>();
HashMap<String, IpResource> ipResourceHashMap = new HashMap<>();
ipResourceUnwindResultDtos.forEach(x -> {
if (ipResourceHashMap.containsKey(x.getId())) {
ipResourceHashMap.get(x.getId()).getShopIds().add(x.getShopId());
......@@ -203,8 +199,8 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
}
});
List<IpResource> result = new ArrayList<>();
Set<Map.Entry<String,IpResource>> entry = ipResourceHashMap.entrySet();
for(Map.Entry<String,IpResource> e:entry){
Set<Map.Entry<String, IpResource>> entry = ipResourceHashMap.entrySet();
for (Map.Entry<String, IpResource> e : entry) {
result.add(e.getValue());
}
return result;
......@@ -220,7 +216,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
if (ipResourceUnwindResultDtos.isEmpty()) {
return new ArrayList<>();
}
HashMap<String ,IpResource> ipResourceHashMap = new HashMap<>();
HashMap<String, IpResource> ipResourceHashMap = new HashMap<>();
ipResourceUnwindResultDtos.forEach(x -> {
if (ipResourceHashMap.containsKey(x.getId())) {
ipResourceHashMap.get(x.getId()).getShopIds().add(x.getShopId());
......@@ -229,8 +225,8 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
}
});
List<IpResource> result = new ArrayList<>();
Set<Map.Entry<String,IpResource>> entry = ipResourceHashMap.entrySet();
for(Map.Entry<String,IpResource> e:entry){
Set<Map.Entry<String, IpResource>> entry = ipResourceHashMap.entrySet();
for (Map.Entry<String, IpResource> e : entry) {
result.add(e.getValue());
}
return result;
......@@ -246,7 +242,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
if (ipResourceUnwindResultDtos.isEmpty()) {
return new ArrayList<>();
}
HashMap<String ,IpResource> ipResourceHashMap = new HashMap<>();
HashMap<String, IpResource> ipResourceHashMap = new HashMap<>();
ipResourceUnwindResultDtos.forEach(x -> {
if (ipResourceHashMap.containsKey(x.getId())) {
ipResourceHashMap.get(x.getId()).getShopIds().add(x.getShopId());
......@@ -255,10 +251,19 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
}
});
List<IpResource> result = new ArrayList<>();
Set<Map.Entry<String,IpResource>> entry = ipResourceHashMap.entrySet();
for(Map.Entry<String,IpResource> e:entry){
Set<Map.Entry<String, IpResource>> entry = ipResourceHashMap.entrySet();
for (Map.Entry<String, IpResource> e : entry) {
result.add(e.getValue());
}
return result;
}
@Override
public List<IpResource> findIds() {
BasicQuery basicQuery = new BasicQuery(new Document());
basicQuery.fields().include("id");
return mongoTemplate.find(basicQuery, IpResource.class);
}
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.account.repository.FileRepository;
import com.edgec.browserbackend.account.repository.FileRepositoryImpl;
import com.edgec.browserbackend.browser.domain.ProxyConfig;
import com.edgec.browserbackend.browser.domain.UserShop;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Update;
import java.io.File;
import static org.springframework.data.mongodb.core.query.Criteria.where;
public class ProxyConfigRepositoryCustomImpl implements ProxyConfigRepositoryCustom {
@Autowired
......@@ -33,10 +27,8 @@ public class ProxyConfigRepositoryCustomImpl implements ProxyConfigRepositoryCus
Update update = new Update();
update.set("configFileId", fileId).set("timestamp", time);
UpdateResult operation = mongoTemplate.upsert(basicQuery, update, ProxyConfig.class);
if (operation.getModifiedCount() < 1)
return false;
else
return true;
return operation.getModifiedCount() >= 1;
}
}
......@@ -36,39 +36,44 @@ public class HistoryServiceImpl implements HistoryService {
@Override
public void addLoginHistory(String username, LoginHistoryDto loginHistoryDto) {
Account account = accountRepository.findById(username).orElse(null);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
Account account = accountRepository.findById(username)
.orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
int num = loginHistoryRepository.countByUsername(username);
if (num > 10000)
if (num > 10000) {
loginHistoryRepository.delete(loginHistoryRepository.findFirstByUsername(username));
}
LoginHistory loginHistory = new LoginHistory(loginHistoryDto);
loginHistory.setLoginTime(Instant.now().toEpochMilli());
loginHistory.setUsername(username);
if (account.getParent() == null)
if (account.getParent() == null) {
loginHistory.setAdministrator(username);
else
} else {
loginHistory.setAdministrator(account.getParent());
}
loginHistory.setNickname(account.getNickname());
loginHistoryRepository.save(loginHistory);
}
@Override
public void addOperationHistory(String username, OperationHistoryDto operationHistoryDto) {
Account account = accountRepository.findById(username).orElse(null);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
Account account = accountRepository.findById(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
int num = operationHistoryRepository.countByUsername(username);
if (num > 10000)
if (num > 10000) {
operationHistoryRepository.delete(operationHistoryRepository.findFirstByUsername(username));
}
OperationHistory operationHistory = new OperationHistory(operationHistoryDto);
operationHistory.setOperationTime(Instant.now().toEpochMilli());
operationHistory.setUsername(username);
operationHistory.setNickname(account.getNickname());
if (account.getParent() == null)
if (account.getParent() == null) {
operationHistory.setAdministrator(username);
else
} else {
operationHistory.setAdministrator(account.getParent());
}
operationHistoryRepository.save(operationHistory);
}
......@@ -99,7 +104,7 @@ public class HistoryServiceImpl implements HistoryService {
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(loginHistoryPage.getNumber());
pageInfo.setTotalPages(loginHistoryPage.getTotalPages());
pageInfo.setTotalItems((int)loginHistoryPage.getTotalElements());
pageInfo.setTotalItems((int) loginHistoryPage.getTotalElements());
historyPageResultDto.setHistoryPage(pageInfo);
return historyPageResultDto;
}
......@@ -131,7 +136,7 @@ public class HistoryServiceImpl implements HistoryService {
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(operationHistories.getNumber());
pageInfo.setTotalPages(operationHistories.getTotalPages());
pageInfo.setTotalItems((int)operationHistories.getTotalElements());
pageInfo.setTotalItems((int) operationHistories.getTotalElements());
historyPageResultDto.setHistoryPage(pageInfo);
return historyPageResultDto;
}
......
......@@ -43,18 +43,22 @@ public class IpAndShopServiceImpl implements IpAndShopService {
@Override
public void bindShop(String username, ShopRequestDto shopRequestDto) {
String shopId = shopRequestDto.getShopId();
// 1. 根据id 来获取账户
Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
String shopId = shopRequestDto.getShopId();
// 2.获取当前账户下指定的商铺
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (account.getPermission() < 4 || userShop == null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
Shop shop = shopRepository.findById(shopId).orElse(null);
if (shop == null)
throw new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST);
// 3.根据shopId获取商铺的详情
Shop shop = shopRepository.findById(shopId).orElseThrow(() -> new ClientRequestException(BrowserErrorCode.SHOPNOTEXIST));
// 4.根据传入的id或者addr查询是否有指定的 ip 资源
IpResource ipResource = null;
if (StringUtils.isNotBlank(shopRequestDto.getIpAddr())) {
ipResource = ipResourceRepository.findByAddrAndIsDeleted(shopRequestDto.getIpAddr(), false);
......@@ -62,17 +66,21 @@ public class IpAndShopServiceImpl implements IpAndShopService {
ipResource = ipResourceRepository.findByIdAndIsDeleted(shopRequestDto.getIpId(), false);
}
if (ipResource == null)
if (ipResource == null) {
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
}
// 5.根据shopId来查询指定 isDeleted 为 false 的 ip资源
IpResource bind = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(shopId, false);
if (bind != null) {
return;
throw new ClientRequestException(BrowserErrorCode.SHOP_BINDED);
}
try {
// 6.绑定ip,将shopId添加到ip资源中,且将 bind 状态设置为 true
ipResourceRepository.addShopId(ipResource.getId(), shopId);
} catch (Exception e) {
logger.error("fail to bind shop and ip", e.getMessage());
logger.error("fail to bind shop and ip", e);
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
......
......@@ -26,12 +26,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.time.Instant;
......@@ -83,15 +79,14 @@ public class BrowserTask {
return headers;
}
public HttpHeaders buildPostHeader() {
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
if (profiles.equals("dev") || profiles.equals("staging")) {
header.setBearerAuth("oq5tg3gMsflHcK5iZ2741G5R30XYd9blyOqH9qeBItKtrzfTsGIoy8AsxqqNXdcm");
} else if (profiles.equals("prod")) {
header.setBearerAuth("tKWsuHzcngf0RQPMss70f9jgymDIwgQ9zbLfESJdcou3pZSNWl7lNTzto8VQgwaO");
}
return header;
public Map<String, String> buildPostHeader() {
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");
return headers;
}
private IpChargeRequestDto buildIpChargeRequestDto(IpResource request, int chargeType, int payMethod) {
......@@ -111,14 +106,15 @@ public class BrowserTask {
String URL = (profiles.equals("dev") || profiles.equals("staging")) ? TESTURL : CLOUDAMURL;
long time = Instant.now().minusSeconds(300).toEpochMilli();
List<IpResource> ipResources = ipResourceRepository.sampleTasks(6, time);
log.error("buyIpTasks sample {} tasks", ipResources.size());
List<CompletableFuture> futureList = new ArrayList<>();
for (IpResource ipResource : ipResources) {
long start = System.currentTimeMillis();
CompletableFuture.runAsync(() -> {
CompletableFuture future = CompletableFuture.runAsync(() -> {
if (ipResourceRepository.lockTask(ipResource)) {
try {
boolean result = false;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders header = buildPostHeader();
Map<String, String> header = buildPostHeader();
HashMap<String, Object> map = new HashMap<>();
map.put("name", ipResource.getUsername());
map.put("region", ipResource.getRegion());
......@@ -130,10 +126,11 @@ public class BrowserTask {
map.put("startscript", "");
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;
try {
ipBuyResultDto = restTemplate.postForObject(URL + "/intelligroup/ipresources?accountId=browser", httpEntity, IpBuyResultDto.class);
String requestResult = HttpClientutils.doPost(URL + "/intelligroup/ipresources?accountId=browser", header, JSONObject.toJSONString(map));
ipBuyResultDto = JSONObject.parseObject(requestResult, IpBuyResultDto.class);
if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode())) {
log.error("fail to buy ip");
log.error(ipBuyResultDto.getErrorCode());
......@@ -186,7 +183,9 @@ public class BrowserTask {
}
}
}, ThreadPoolUtils.buyIpTasksPool);
futureList.add(future);
}
CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0])).join();
}
@Scheduled(cron = "0 0/1 * * * ?")
......@@ -358,6 +357,20 @@ public class BrowserTask {
return responseBody.string();
}
public static String doPost(String url, Map<String, String> headers, String body) throws IOException {
Headers httpHeaders = Headers.of(headers);
Request request = new Request.Builder()
.post(RequestBody.create(body, okhttp3.MediaType.get("application/json")))
.url(url)
.headers(httpHeaders)
.build();
Call call = client.newCall(request);
Response response = call.execute();
ResponseBody responseBody = response.body();
return responseBody.string();
}
}
......
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,8 +19,10 @@ 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 {
......@@ -42,22 +44,21 @@ public class Set3proxyTask {
@Autowired
private ProxyConfigRepository proxyConfigRepository;
@Scheduled(cron = "0 0/10 * * * ?")
@Scheduled(cron = "0 0/2 * * * ?")
@SchedulerLock(name = "proxyTask", lockAtLeastFor = 60 * 1000 * 5, lockAtMostFor = 60 * 1000 * 9)
public void set3proxy() {
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);
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);
File file = new File("3proxy_" + Instant.now().toEpochMilli());
try {
try {
file.delete();
file.createNewFile();
} catch (Exception t1) {
log.error(t1.getMessage(), t1);
}
file.delete();
file.createNewFile();
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fileWriter);
......@@ -66,53 +67,55 @@ public class Set3proxyTask {
bw.write("config /root/3proxy.cfg\n");
bw.write("monitor /root/3proxy.cfg\n");
for (IpResource ipResource : ipResources) {
if (StringUtils.isNotBlank(ipResource.getAddr()))
bw.write("users \"" + ipResource.getProxyUsername() + ":CL:" + ipResource.getProxyPassword() + "\"\n");
ipResource.setUsingSpecialLine(true);
// 写入 ipResources 相关信息
if (ipResources.size() != 0) {
bwWriteIpResources(ipResources, bw);
}
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.error("成功写入文件");
log.info("成功写入文件");
if (proxyConfigRepository.count() > 0) {
ProxyConfig proxyConfig = proxyConfigRepository.findAll().get(0);
if (nowtime < proxyConfig.getTimestamp()) {
file.delete();
return;
} else {
proxyConfigRepository.updateProxy(file, nowtime);
file.delete();
}
long nowtime = Instant.now().toEpochMilli();
long proxyConfigTimestamp = proxyConfigRepository.findAll().get(0).getTimestamp();
if (proxyConfigRepository.count() > 0 && nowtime < proxyConfigTimestamp) {
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");
}
}
}
}
}
......@@ -26,7 +26,7 @@ public class ClientRequestException extends IllegalArgumentException {
this.errorCode = errorCode;
}
public ClientRequestException(int errorCode, String message){
public ClientRequestException(int errorCode, String message) {
super(message);
}
......
......@@ -12,7 +12,6 @@ public interface ErrorCode {
public static final int COMMON_UNAUTHRORIZED = 100004;
public static final int ACCOUNT_BASE = 400000;
public static final int AUTH_BASE = 500000;
......@@ -20,9 +19,11 @@ public interface ErrorCode {
public static final int BROWSER_BASE = 600000;
int value() ;
int value();
@JsonValue
int getCode() ;
int getCode();
String getReason();
}
......@@ -3,12 +3,9 @@ package com.edgec.browserbackend.common.utils;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
......@@ -29,12 +26,12 @@ public class FileUtil {
String line = null;
StringBuffer buf = new StringBuffer();
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(f), charset);
BufferedReader br = new BufferedReader(reader);){
BufferedReader br = new BufferedReader(reader);) {
while ((line = br.readLine()) != null) {
buf.append(line+System.getProperty("line.separator"));
buf.append(line + System.getProperty("line.separator"));
}
} catch (Exception e) {
log.error("[" + f.getName() + "]文件读出失败{}",e);
log.error("[" + f.getName() + "]文件读出失败{}", e);
return null;
}
log.debug("[" + f.getName() + "]文件读出成功");
......@@ -46,14 +43,14 @@ public class FileUtil {
List<String> lineList = new ArrayList<String>();
StringBuffer buf = new StringBuffer();
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(f), charset);
BufferedReader br = new BufferedReader(reader);){
BufferedReader br = new BufferedReader(reader);) {
while ((line = br.readLine()) != null) {
lineList.add(line);
}
br.close();
reader.close();
} catch (Exception e) {
log.error("[" + f.getName() + "]文件读出失败{}",e);
log.error("[" + f.getName() + "]文件读出失败{}", e);
return null;
}
log.debug("[" + f.getName() + "]文件读出成功");
......@@ -61,10 +58,10 @@ public class FileUtil {
}
public static boolean write(String content, File file) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))){
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
bw.write(content);
} catch (Exception e) {
log.error("[" + file.getName() + "]写入失败{}",e);
log.error("[" + file.getName() + "]写入失败{}", e);
return false;
}
log.debug("[" + file.getName() + "]文件写入成功");
......@@ -72,7 +69,7 @@ public class FileUtil {
}
//读取excel
public static List<List<Object>> readExcel(InputStream inputStream){
public static List<List<Object>> readExcel(InputStream inputStream) {
List<List<Object>> list = new ArrayList<>();
Workbook workbook = null;
try {
......@@ -89,7 +86,8 @@ public class FileUtil {
//总列数
int colLength = row.getLastCellNum();
//得到指定的单元格
Cell cell = row.getCell(0);;
Cell cell = row.getCell(0);
;
int size = 1;
boolean first = false;
for (int i = 1; i < rowLength; i++) {
......@@ -99,7 +97,7 @@ public class FileUtil {
for (int j = 0; j < colLength; j++) {
cell = row.getCell(j);
// System.out.println(cell);
if (cell!=null) {
if (cell != null) {
Object cellValue = getCellFormatValue(cell);
if (cellValue.toString().contains("店铺名称")) {
first = true;
......@@ -109,8 +107,7 @@ public class FileUtil {
if (StringUtils.isNotBlank(cellValue.toString())) {
size++;
}
}
else
} else
rowvalue.add("");
}
if (first) {
......@@ -127,37 +124,37 @@ public class FileUtil {
log.error("parse excel file error :", e);
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
}
return list ;
return list;
}
public static Object getCellFormatValue(Cell cell){
public static Object getCellFormatValue(Cell cell) {
Object cellValue = null;
if(cell!=null){
if (cell != null) {
//判断cell类型
switch(cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC:{
cellValue = String.valueOf((int)cell.getNumericCellValue());
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: {
cellValue = String.valueOf((int) cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA:{
case Cell.CELL_TYPE_FORMULA: {
//判断cell是否为日期格式
if(DateUtil.isCellDateFormatted(cell)){
if (DateUtil.isCellDateFormatted(cell)) {
//转换为日期格式YYYY-mm-dd
cellValue = cell.getDateCellValue();
}else{
} else {
//数字
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
case Cell.CELL_TYPE_STRING:{
case Cell.CELL_TYPE_STRING: {
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}else{
} else {
cellValue = "";
}
return cellValue;
......@@ -165,25 +162,26 @@ public class FileUtil {
/**
* 方法说明将files打包放到一个zip中。
*
* @return
* @throws IOException
*/
public static File putBatchFilesInZip(List<File> filePaths,File tempFile) throws IOException {
public static File putBatchFilesInZip(List<File> filePaths, File tempFile) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempFile));
for(File inputFile : filePaths) {
try(FileInputStream fis = new FileInputStream(inputFile);){
for (File inputFile : filePaths) {
try (FileInputStream fis = new FileInputStream(inputFile);) {
//压缩文件中写入名称
ZipEntry entry = new ZipEntry(inputFile.getName());
zos.putNextEntry(entry);
// 向压缩文件中输出数据
int len = 0;
byte[] bt = new byte[5*1024];
byte[] bt = new byte[5 * 1024];
while ((len = fis.read(bt)) != -1) {
//压缩文件中写入真正的文件流
zos.write(bt, 0, len);
}
}catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
......
package com.edgec.browserbackend.common.utils;
import org.apache.commons.io.Charsets;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
......@@ -12,12 +13,11 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Trans {
private final Logger logger = LoggerFactory.getLogger(Trans.class);
......@@ -50,7 +50,7 @@ public class Trans {
*/
public static void main(String[] args) {
String url = "http://free.ipwhois.io/json/";
Trans trans = new Trans("23.105.2.118", "uE5LkzM81UhUaA1");
Trans trans = new Trans("112.74.47.217", 20001, "fangguanlianbrowser", "5D753F36QKqIaA1");
String rs = trans.get(url);
System.out.println(rs);
}
......@@ -84,17 +84,7 @@ public class Trans {
HttpResponse response = client.execute(httpGet);
HttpEntity entry = response.getEntity();
if (entry != null) {
InputStreamReader is = new InputStreamReader(entry.getContent());
BufferedReader br = new BufferedReader(is);
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str.trim());
}
br.close();
}
return sb.toString();
return EntityUtils.toString(entry, Charsets.UTF_8);
} catch (ClientProtocolException e) {
logger.error(e.getMessage(), e);
return "";
......@@ -106,6 +96,11 @@ public class Trans {
return "";
} finally {
httpGet.releaseConnection();
try {
client.close();
} catch (IOException e) {
logger.error("fail to close connection", e);
}
}
}
}
\ No newline at end of file
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