我正在尝试在Golang中实现一个HTTP服务器,该服务器接收来自使用代理协议的Amazon ELB的请求。现在我想知道原始IP地址是什么,所以我在考虑使用this包。
现在this包谈论原始HTTP据我所知,但我的服务器使用路由器实现更高级别的HTTP服务器。
我在翻译之间遇到了麻烦。 我的问题是:如何使用this库并仍使用像gorilla / mux这样的路由器?现在,this包没有什么特别之处,它只是在比HTTP更低的层次上进行说明。
示例:
// Listen on TCP port 2000 on all interfaces.
l, err := net.Listen("tcp", ":2000")
// proxyproxy is the library that maintains the source ip address for me
proxyList := &proxyproto.Listener{Listener: list}
if err != nil {
log.Fatal(err)
}
defer proxyList.Close()
for {
// Wait for a connection.
conn, err := proxyList.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c net.Conn) {
// how do I connect my router
}(conn)
}
答案 0 :(得分:1)
通过HTTP查找实际客户端IP的常用方法是使用一些HTTP头,例如:
var json = {
"departments":[
{
"id":0,
"name":"Staff"
},
{
"id":1,
"name":"Sales"
},
{
"id":2,
"name":"Development"
}
],
"custom_fields":[
{
"id":1,
"name":"Company"
},
{
"id":2,
"name":"Job"
}
],
"users":[
{
"id":1,
"email":"email@domain.com",
"fname":"User",
"lname":"One",
"department":2,
"custom_fields":[
{
"id":1,
"value":"ABC Company"
},
{
"id":2,
"value":"Designer"
}
]
},
{
"id":2,
"email":"email2@domain.com",
"fname":"User",
"lname":"Two",
"department":3,
"custom_fields":[
{
"id":1,
"value":"ABC Company"
},
{
"id":2,
"value":"President"
}
]
}
]
};
var users = json.users;
users.map(user => {
var departmentName = json.departments.find(d => d.id == user.department);
if (departmentName) {
user.department_name = departmentName.name;
}
//Asume that custom_fields array are sorted by Id
user.custom_fields = user.custom_fields.map((custom, idx) => {
if (custom.id == json.custom_fields[idx].id) {
custom.name = json.custom_fields[idx].name;
}
return custom;
});
return user;
})
console.log(users);
X-Forwarded-For
实际上,Amazon ELB似乎支持X-Forwarded-For标头:
http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html
如果您正在使用Gorilla,他们会有一个中间件,似乎可以为您解决这个问题: