|
|
@@ -8,16 +8,16 @@ import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.http.Method;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.util.GoogleGenerator;
|
|
|
import com.cyksj.common.util.IoKit;
|
|
|
import com.cyksj.dto.RedisKey;
|
|
|
+import com.cyksj.dto.Result;
|
|
|
import com.cyksj.mapper.AccountMapper;
|
|
|
import com.cyksj.model.entity.Account;
|
|
|
import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.service.chatgpt.ChatGptAuthService;
|
|
|
-import com.ejlchina.searcher.BeanSearcher;
|
|
|
-import com.ejlchina.searcher.util.MapUtils;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -41,8 +41,6 @@ public class ChatGptAuthServiceImpl implements ChatGptAuthService {
|
|
|
|
|
|
private final AccountMapper accountMapper;
|
|
|
|
|
|
- private final BeanSearcher beanSearcher;
|
|
|
-
|
|
|
private final RedisService redisService;
|
|
|
|
|
|
String user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";
|
|
|
@@ -108,6 +106,26 @@ public class ChatGptAuthServiceImpl implements ChatGptAuthService {
|
|
|
return refresh_token;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getAccessToken(String username) {
|
|
|
+ Account account = accountMapper.selectOne(Wrappers.lambdaQuery(Account.class).eq(Account::getAccount, username));
|
|
|
+ if(account == null){
|
|
|
+ throw BusinessRuntimeException.getInstance("账号不存在.");
|
|
|
+ }
|
|
|
+ if(StringUtils.isBlank(account.getGptRefreshToken())){
|
|
|
+ throw BusinessRuntimeException.getInstance("该账号未获取refreshToken");
|
|
|
+ }
|
|
|
+ String accessToken = redisService.getStr(RedisKey.CHATGPT_ACCESS_TOKEN + username);
|
|
|
+ if (StringUtils.isBlank(accessToken)) {
|
|
|
+ accessToken = getAccessTokenByRefreshToken(account.getGptRefreshToken());
|
|
|
+ redisService.set(RedisKey.CHATGPT_ACCESS_TOKEN + account.getAccount(), accessToken, 1209300L);
|
|
|
+ return accessToken;
|
|
|
+ }else {
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
private String one(String codeVerifier, String state, String username, String password) {
|
|
|
String url = "https://auth0.openai.com/u/login/identifier?state=" + state;
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
@@ -176,8 +194,7 @@ public class ChatGptAuthServiceImpl implements ChatGptAuthService {
|
|
|
if (execute.getStatus() == 302) {
|
|
|
String Location = execute.header("Location");
|
|
|
if (Location.startsWith("/u/mfa-otp-challenge?")) {
|
|
|
- Account account = beanSearcher.searchFirst(Account.class, MapUtils.builder()
|
|
|
- .field(Account::getAccount, username).build());
|
|
|
+ Account account = accountMapper.selectOne(Wrappers.lambdaQuery(Account.class).eq(Account::getAccount,username));
|
|
|
if (account == null) {
|
|
|
throw BusinessRuntimeException.getInstance("账号不存在.");
|
|
|
}
|
|
|
@@ -279,4 +296,36 @@ public class ChatGptAuthServiceImpl implements ChatGptAuthService {
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ public String getAccessTokenByRefreshToken(String refreshToken){
|
|
|
+ String result = HttpUtil.get("https://dvapi.doveproxy.net/cmapi.php?rq=distribute&user=1031494735&token=ckc1MUFURnk3bi80MXVxeUFTZUh0Zz09&auth=0&geo=us&agreement=0&timeout=10&num=1&type_ip=1&repeat=0");
|
|
|
+ log.info("付费ip 请求返回 res:{}",result);
|
|
|
+ JSONObject resObj = JSONUtil.parseObj(result);
|
|
|
+ if (200 == resObj.getInt("errno")) {
|
|
|
+ JSONObject data = resObj.getJSONObject("data");
|
|
|
+
|
|
|
+ Proxy proxy = new Proxy(Proxy.Type.SOCKS,
|
|
|
+ new InetSocketAddress(data.getStr("ip"), data.getInt("port")));
|
|
|
+
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.putOpt("redirect_uri","com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback");
|
|
|
+ params.putOpt("grant_type","refresh_token");
|
|
|
+ params.putOpt("client_id","pdlLIX2Y72MIl2rhLhTE9VV9bN905kBh");
|
|
|
+ params.putOpt("refresh_token",refreshToken);
|
|
|
+ HttpRequest request = HttpRequest.post("https://auth0.openai.com/oauth/token").setProxy(proxy)
|
|
|
+ .body(params.toString());
|
|
|
+ HttpResponse execute = request.execute();
|
|
|
+ if (execute.getStatus() == 200) {
|
|
|
+ JSONObject res = new JSONObject(execute.body());
|
|
|
+ return res.getStr("access_token");
|
|
|
+ }else {
|
|
|
+ throw BusinessRuntimeException.getInstance("获取token失败.");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ log.error("----------获取 付费IP 错误 ----------");
|
|
|
+ throw BusinessRuntimeException.getInstance("获取ip错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|