mirror of
https://github.com/zr-hebo/sniffer-agent.git
synced 2025-09-20 02:33:28 +08:00
从proxy获取client ip地址
This commit is contained in:
69
vendor/github.com/pires/go-proxyproto/README.md
generated
vendored
Normal file
69
vendor/github.com/pires/go-proxyproto/README.md
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
# go-proxyproto
|
||||
|
||||
[](https://github.com/pires/go-proxyproto/actions)
|
||||
[](https://coveralls.io/github/pires/go-proxyproto?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/pires/go-proxyproto)
|
||||
[](http://godoc.org/github.com/pires/go-proxyproto)
|
||||
|
||||
|
||||
A Go library implementation of the [PROXY protocol, versions 1 and 2](http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt),
|
||||
which provides, as per specification:
|
||||
> (...) a convenient way to safely transport connection
|
||||
> information such as a client's address across multiple layers of NAT or TCP
|
||||
> proxies. It is designed to require little changes to existing components and
|
||||
> to limit the performance impact caused by the processing of the transported
|
||||
> information.
|
||||
|
||||
This library is to be used in one of or both proxy clients and proxy servers that need to support said protocol.
|
||||
Both protocol versions, 1 (text-based) and 2 (binary-based) are supported.
|
||||
|
||||
## Installation
|
||||
|
||||
```shell
|
||||
$ go get -u github.com/pires/go-proxyproto
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Client (TODO)
|
||||
|
||||
### Server
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
|
||||
proxyproto "github.com/pires/go-proxyproto"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a listener
|
||||
addr := "localhost:9876"
|
||||
list, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
log.Fatalf("couldn't listen to %q: %q\n", addr, err.Error())
|
||||
}
|
||||
|
||||
// Wrap listener in a proxyproto listener
|
||||
proxyListener := &proxyproto.Listener{Listener: list}
|
||||
defer proxyListener.Close()
|
||||
|
||||
// Wait for a connection and accept it
|
||||
conn, err := proxyListener.Accept()
|
||||
defer conn.Close()
|
||||
|
||||
// Print connection details
|
||||
if conn.LocalAddr() == nil {
|
||||
log.Fatal("couldn't retrieve local address")
|
||||
}
|
||||
log.Printf("local address: %q", conn.LocalAddr().String())
|
||||
|
||||
if conn.RemoteAddr() == nil {
|
||||
log.Fatal("couldn't retrieve remote address")
|
||||
}
|
||||
log.Printf("remote address: %q", conn.RemoteAddr().String())
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user