Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Create Record, lower ReadTimeout=10s

  • Loading branch information...
commit c24a8edf8ea40aad2e1bbe35b6e665854fa41518 1 parent 529927d
@temoto authored
Showing with 50 additions and 10 deletions.
  1. +1 −1  oakmole-radar/main.go
  2. +49 −9 oakmole-radar/net.go
View
2  oakmole-radar/main.go
@@ -12,7 +12,7 @@ import (
)
func main() {
- bind := flag.String("bind", "", "Listen on address:port")
+ bind := flag.String("bind", ":80", "Listen on address:port")
cpuprofile := flag.String("cpuprofile", "", "Write CPU profile to file")
memprofile := flag.String("memprofile", "", "Write memory profile to file")
flag.Parse()
View
58 oakmole-radar/net.go
@@ -1,13 +1,16 @@
package main
import (
+ "bytes"
+ "github.com/temoto/oakmole/oakmole"
+ "io"
"log"
"net"
"runtime"
"time"
)
-const IOTimeout time.Duration = 30 * time.Second
+const IOTimeout time.Duration = 10 * time.Second
const ReadBufferSize = 16 << 10
func acceptLoop(listen *net.TCPListener) {
@@ -25,24 +28,47 @@ func acceptLoop(listen *net.TCPListener) {
func connectionHandler(conn *net.TCPConn) {
defer conn.Close()
+ timeBegin := time.Now()
conn.SetKeepAlive(false)
conn.SetLinger(0)
- conn.SetReadDeadline(time.Now().Add(IOTimeout))
+ conn.SetReadDeadline(timeBegin.Add(IOTimeout))
- // addrLocal := conn.LocalAddr()
- addrRemote := conn.RemoteAddr()
+ addrLocal := conn.LocalAddr().(*net.TCPAddr)
+ addrRemote := conn.RemoteAddr().(*net.TCPAddr)
log.Println("New connection from", addrRemote)
// TODO: reuse existing buffers
buffer := make([]byte, ReadBufferSize)
- size, err := conn.Read(buffer)
- if err != nil {
- log.Println("Read error from", addrRemote, err)
- return
+ totalSize := 0
+ for i := 1; i <= 3 && totalSize < ReadBufferSize; i++ {
+ size, err := conn.Read(buffer[totalSize:])
+ totalSize += size
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ // Only log IO timeout on first Read().
+ // Later it just means that client already sent everything.
+ if i == 1 {
+ log.Println("Read: try:", i, "local:", addrLocal, "remote:", addrRemote, "error:", err)
+ return
+ } else if netErr, ok := err.(net.Error); !ok || !netErr.Timeout() {
+ log.Println("Read: try:", i, "local:", addrLocal, "remote:", addrRemote, "error:", err)
+ }
+ break
+ }
}
+ buffer = buffer[:totalSize]
- log.Println("Read success from", addrRemote, "size", size, "first bytes:", string(buffer[:20]))
+ record := &oakmole.Record{
+ Timestamp: uint64(timeBegin.UnixNano()),
+ LocalIP: addrLocal.IP,
+ RemoteIP: addrRemote.IP,
+ HttpHost: readHost(buffer),
+ Body: buffer,
+ }
+ log.Println("Read: success local:", addrLocal, "remote:", addrRemote, "size:", totalSize, "first bytes:", string(buffer[:20]))
// TODO: save
}
@@ -63,3 +89,17 @@ func startListener(bind *string) *net.TCPListener {
}
return listen
}
+
+func readHost(b []byte) []byte {
+ beginBytes := []byte("\nHost: ")
+ const beginBytesLength = 7
+ indexBegin := bytes.Index(b, beginBytes)
+ if indexBegin == -1 {
+ return nil
+ }
+ indexEnd := bytes.IndexByte(b[indexBegin+beginBytesLength:], '\r')
+ if indexEnd == -1 {
+ return nil
+ }
+ return b[indexBegin+beginBytesLength : indexBegin+beginBytesLength+indexEnd]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.