Skip to content

Commit

Permalink
Update server for proxy-proto
Browse files Browse the repository at this point in the history
  • Loading branch information
diptadas committed Nov 10, 2017
1 parent 81a01a3 commit ebddc8d
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/test-server/server.go
@@ -1,6 +1,8 @@
package main

import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
Expand All @@ -10,6 +12,8 @@ import (
"os/signal"
"syscall"
"time"

pp "github.com/pires/go-proxyproto"
)

type Response struct {
Expand Down Expand Up @@ -83,12 +87,27 @@ type TCPServerHandler struct {
}

func (h TCPServerHandler) ServeTCP(con net.Conn) {
b := make([]byte, 4096)
if _, err := con.Read(b); err != nil {
fmt.Println(err)
}
header, err := pp.Read(bufio.NewReader(bytes.NewReader(b)))
if err != nil {
fmt.Println(err)
}

fmt.Println("request on", con.LocalAddr().String())
resp := &Response{
Type: "tcp",
Host: con.LocalAddr().String(),
ServerPort: h.port,
}

// set proxy-proto header to response body
if header != nil {
resp.Body = fmt.Sprintf("%+v", *header)
}

json.NewEncoder(con).Encode(resp)
}

Expand Down

0 comments on commit ebddc8d

Please sign in to comment.