Skip to content

Commit

Permalink
feat: openapi add client ip
Browse files Browse the repository at this point in the history
  • Loading branch information
youngzil committed Oct 11, 2024
1 parent 6405daa commit b400539
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import com.ctrip.framework.apollo.openapi.client.exception.ApolloOpenApiException;
import com.ctrip.framework.apollo.openapi.client.url.OpenApiPathBuilder;
import com.ctrip.framework.foundation.Foundation;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.net.HttpHeaders;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -74,6 +77,9 @@ private CloseableHttpResponse execute(HttpEntityEnclosingRequestBase requestBase
}

private CloseableHttpResponse execute(HttpUriRequest request) throws IOException {

addClientIP(request);

CloseableHttpResponse response = client.execute(request);

checkHttpResponseStatus(response);
Expand All @@ -97,6 +103,12 @@ private void checkHttpResponseStatus(HttpResponse response) {
throw new ApolloOpenApiException(status.getStatusCode(), status.getReasonPhrase(), message);
}

private void addClientIP(HttpUriRequest request) {
ProtocolVersion protocolVersion = request.getProtocolVersion();
request.addHeader(HttpHeaders.FORWARDED, String.format("for=%s; proto=%s", Foundation.net().getHostAddress(), protocolVersion.getProtocol()));
request.addHeader(HttpHeaders.X_FORWARDED_FOR, Foundation.net().getHostAddress());
}

protected void checkNotNull(Object value, String name) {
Preconditions.checkArgument(null != value, name + " should not be null");
}
Expand Down

0 comments on commit b400539

Please sign in to comment.