Skip to content

Commit

Permalink
Refactor tryToGetClientIp method (#4514)
Browse files Browse the repository at this point in the history
  • Loading branch information
klboke authored Aug 15, 2022
1 parent 2ab923f commit b1f78b4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.common.utils;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import javax.servlet.http.HttpServletRequest;

/**
* @author kl (http://kailing.pub)
* @since 2022/8/12
*/
public final class WebUtils {

private static final Splitter X_FORWARDED_FOR_SPLITTER = Splitter.on(",").omitEmptyStrings()
.trimResults();

private WebUtils(){}

public static String tryToGetClientIp(HttpServletRequest request) {
String forwardedFor = request.getHeader("X-FORWARDED-FOR");
if (!Strings.isNullOrEmpty(forwardedFor)) {
return X_FORWARDED_FOR_SPLITTER.splitToList(forwardedFor).get(0);
}
return request.getRemoteAddr();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.common.utils;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletRequest;

/**
* @author kl (http://kailing.pub)
* @since 2022/8/12
*/
@RunWith(MockitoJUnitRunner.class)
public class WebUtilsTest {

@Test
public void testTryToGetClientIp() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("X-FORWARDED-FOR", "172.1.1.1,172.1.1.2");
request.setRemoteAddr("172.1.1.3");
String ip = WebUtils.tryToGetClientIp(request);
assertThat(ip).isEqualTo("172.1.1.1");

request.removeHeader("X-FORWARDED-FOR");
ip = WebUtils.tryToGetClientIp(request);
assertThat(ip).isEqualTo("172.1.1.3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.ctrip.framework.apollo.biz.entity.Release;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.common.utils.WebUtils;
import com.ctrip.framework.apollo.configservice.service.AppNamespaceServiceWithCache;
import com.ctrip.framework.apollo.configservice.service.config.ConfigService;
import com.ctrip.framework.apollo.configservice.util.InstanceConfigAuditUtil;
Expand Down Expand Up @@ -53,8 +54,7 @@
@RestController
@RequestMapping("/configs")
public class ConfigController {
private static final Splitter X_FORWARDED_FOR_SPLITTER = Splitter.on(",").omitEmptyStrings()
.trimResults();

private final ConfigService configService;
private final AppNamespaceServiceWithCache appNamespaceService;
private final NamespaceUtil namespaceUtil;
Expand Down Expand Up @@ -93,7 +93,7 @@ public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String
namespace = namespaceUtil.normalizeNamespace(appId, namespace);

if (Strings.isNullOrEmpty(clientIp)) {
clientIp = tryToGetClientIp(request);
clientIp = WebUtils.tryToGetClientIp(request);
}

ApolloNotificationMessages clientMessages = transformMessages(messagesAsString);
Expand Down Expand Up @@ -222,14 +222,6 @@ private void auditReleases(String appId, String cluster, String dataCenter, Stri
}
}

private String tryToGetClientIp(HttpServletRequest request) {
String forwardedFor = request.getHeader("X-FORWARDED-FOR");
if (!Strings.isNullOrEmpty(forwardedFor)) {
return X_FORWARDED_FOR_SPLITTER.splitToList(forwardedFor).get(0);
}
return request.getRemoteAddr();
}

ApolloNotificationMessages transformMessages(String messagesAsString) {
ApolloNotificationMessages notificationMessages = null;
if (!Strings.isNullOrEmpty(messagesAsString)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolder;
import com.ctrip.framework.apollo.biz.message.ReleaseMessageListener;
import com.ctrip.framework.apollo.biz.message.Topics;
import com.ctrip.framework.apollo.common.utils.WebUtils;
import com.ctrip.framework.apollo.configservice.util.NamespaceUtil;
import com.ctrip.framework.apollo.configservice.util.WatchKeysUtil;
import com.ctrip.framework.apollo.core.ConfigConsts;
Expand Down Expand Up @@ -65,8 +66,6 @@
public class ConfigFileController implements ReleaseMessageListener {
private static final Logger logger = LoggerFactory.getLogger(ConfigFileController.class);
private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR);
private static final Splitter X_FORWARDED_FOR_SPLITTER = Splitter.on(",").omitEmptyStrings()
.trimResults();
private static final long MAX_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
private static final long EXPIRE_AFTER_WRITE = 30;
private final HttpHeaders propertiesResponseHeaders;
Expand Down Expand Up @@ -172,7 +171,7 @@ String queryConfig(ConfigFileOutputFormat outputFormat, String appId, String clu
namespace = namespaceUtil.normalizeNamespace(appId, namespace);

if (Strings.isNullOrEmpty(clientIp)) {
clientIp = tryToGetClientIp(request);
clientIp = WebUtils.tryToGetClientIp(request);
}

//1. check whether this client has gray release rules
Expand Down Expand Up @@ -301,11 +300,4 @@ public String getValue() {
}
}

private String tryToGetClientIp(HttpServletRequest request) {
String forwardedFor = request.getHeader("X-FORWARDED-FOR");
if (!Strings.isNullOrEmpty(forwardedFor)) {
return X_FORWARDED_FOR_SPLITTER.splitToList(forwardedFor).get(0);
}
return request.getRemoteAddr();
}
}

0 comments on commit b1f78b4

Please sign in to comment.