🎨 使用客户端 IP 进行请求

This commit is contained in:
ronger 2023-05-26 18:42:36 +08:00
parent e7f9ddc35d
commit fbd1977152
2 changed files with 37 additions and 2 deletions

View File

@ -0,0 +1,29 @@
package com.rymcu.forest.openai.service;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
/**
* OkHttp Interceptor that adds an ip address header
* @author ronger
*/
public class IpAddressInterceptor implements Interceptor {
private final String ip;
IpAddressInterceptor(String ip) {
this.ip = ip;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.header("x-forwarded-for", ip)
.build();
return chain.proceed(request);
}
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.rymcu.forest.util.SpringContextHolder; import com.rymcu.forest.util.SpringContextHolder;
import com.rymcu.forest.util.Utils;
import com.theokanning.openai.DeleteResult; import com.theokanning.openai.DeleteResult;
import com.theokanning.openai.OpenAiApi; import com.theokanning.openai.OpenAiApi;
import com.theokanning.openai.OpenAiError; import com.theokanning.openai.OpenAiError;
@ -30,18 +31,20 @@ import com.theokanning.openai.image.ImageResult;
import com.theokanning.openai.model.Model; import com.theokanning.openai.model.Model;
import com.theokanning.openai.moderation.ModerationRequest; import com.theokanning.openai.moderation.ModerationRequest;
import com.theokanning.openai.moderation.ModerationResult; import com.theokanning.openai.moderation.ModerationResult;
import io.reactivex.BackpressureStrategy; import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.Single; import io.reactivex.Single;
import okhttp3.*; import okhttp3.*;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import retrofit2.Call;
import retrofit2.HttpException; import retrofit2.HttpException;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.Call;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory; import retrofit2.converter.jackson.JacksonConverterFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.time.Duration; import java.time.Duration;
import java.util.List; import java.util.List;
@ -336,8 +339,11 @@ public class OpenAiService {
} }
public static OkHttpClient defaultClient(String token, Duration timeout) { public static OkHttpClient defaultClient(String token, Duration timeout) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = Utils.getIpAddress(request);
return new OkHttpClient.Builder() return new OkHttpClient.Builder()
.addInterceptor(new AuthenticationInterceptor(token)) .addInterceptor(new AuthenticationInterceptor(token))
.addInterceptor(new IpAddressInterceptor(ip))
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS)) .connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
.readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS) .readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS)
.build(); .build();