-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from unmarshal/read_header_timeout
Add support for ReadHeaderTimeout
- Loading branch information
Showing
6 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net" | ||
|
||
proxyproto "github.com/pires/go-proxyproto" | ||
) | ||
|
||
func chkErr(err error) { | ||
if err != nil { | ||
log.Fatalf("Error: %s", err.Error()) | ||
} | ||
} | ||
|
||
func main() { | ||
// Dial some proxy listener e.g. https://github.com/mailgun/proxyproto | ||
target, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9876") | ||
chkErr(err) | ||
|
||
conn, err := net.DialTCP("tcp", nil, target) | ||
chkErr(err) | ||
|
||
defer conn.Close() | ||
|
||
// Create a proxyprotocol header or use HeaderProxyFromAddrs() if you | ||
// have two conn's | ||
header := &proxyproto.Header{ | ||
Version: 1, | ||
Command: proxyproto.PROXY, | ||
TransportProtocol: proxyproto.TCPv4, | ||
SourceAddr: &net.TCPAddr{ | ||
IP: net.ParseIP("10.1.1.1"), | ||
Port: 1000, | ||
}, | ||
DestinationAddr: &net.TCPAddr{ | ||
IP: net.ParseIP("20.2.2.2"), | ||
Port: 2000, | ||
}, | ||
} | ||
// After the connection was created write the proxy headers first | ||
_, err = header.WriteTo(conn) | ||
chkErr(err) | ||
// Then your data... e.g.: | ||
_, err = io.WriteString(conn, "HELO") | ||
chkErr(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pires/go-proxyproto" | ||
) | ||
|
||
// TODO: add httpclient example | ||
|
||
func main() { | ||
server := http.Server{ | ||
Addr: ":8080", | ||
ConnState: func(c net.Conn, s http.ConnState) { | ||
if s == http.StateNew { | ||
log.Printf("[ConnState] %s -> %s", c.LocalAddr().String(), c.RemoteAddr().String()) | ||
} | ||
}, | ||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
log.Printf("[Handler] remote ip %q", r.RemoteAddr) | ||
}), | ||
} | ||
|
||
ln, err := net.Listen("tcp", server.Addr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
proxyListener := &proxyproto.Listener{ | ||
Listener: ln, | ||
ReadHeaderTimeout: 10 * time.Second, | ||
} | ||
defer proxyListener.Close() | ||
|
||
server.Serve(proxyListener) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters