Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log X-Real-IP header #631

Merged
merged 6 commits into from
Mar 24, 2023
Merged

Conversation

Rektyfikowany
Copy link
Contributor

What does this PR do?

日志中显示nginx发送的RealIP地址

Motivation

#630

Additional Notes

@VergilGao
Copy link

VergilGao commented Mar 18, 2023

经过测试发现,日志输出的IP没有变化
image
image
我没有通过nginx反代测试,而是自己构造了请求头

@Rektyfikowany
Copy link
Contributor Author

我没复现出来,控制台测试

await fetch("",{headers:{'x-real-ip':'10.0.0.1','x-forwarded-for':'10.0.0.2','authorization':''}})

能记录Real-IP:10.0.0.1; Forwarded-For:10.0.0.2;
有空可以测试test分支输出完整header是否包含X-Real-Ip

@jeessy2
Copy link
Owner

jeessy2 commented Mar 21, 2023

x-forwarded-for -> X-Forwarded-For
x-real-ip -> X-Real-IP
按标准来测试,首字母应该大写

@Rektyfikowany
Copy link
Contributor Author

按标准来测试,首字母应该大写

Header.Get默认大小写无关的(至少我测试的环境中是),x-real-ipX-Real-IP都转换为X-Real-Ip且输出正确

@jeessy2
Copy link
Owner

jeessy2 commented Mar 21, 2023

Header.Get默认大小写无关的(至少我测试的环境中是),x-real-ipX-Real-IP都转换为X-Real-Ip且输出正确

对的,没注意看。那是他没获取到版本吧,后面都没打印日志

@jeessy2
Copy link
Owner

jeessy2 commented Mar 21, 2023

抽一个公共的方法(util包中),能用的地方都可以调用

@Rektyfikowany
Copy link
Contributor Author

抽一个公共的方法(util包中),能用的地方都可以调用

已移动到util.GetRealIP方法

@jeessy2
Copy link
Owner

jeessy2 commented Mar 22, 2023

打印r.RemoteAddr都需要处理下,不然看到的日志就有点奇怪

2023/03/22 09:23:59 192.168.1.1:55946 登陆失败!
2023/03/22 09:23:59 192.168.2.1 请求登陆! Remote:192.168.1.1:55946; RealIP:192.168.2.1; 

可以返回一个,而不是多个。如下看到也有点奇怪

192.168.2.1 请求登陆! Remote:192.168.1.1:55946; RealIP:192.168.2.1; 

@Rektyfikowany
Copy link
Contributor Author

内网验证保留了RemoteAddr以避免伪造header攻击

@jeessy2 jeessy2 merged commit 9f7ee01 into jeessy2:master Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants