aboutsummaryrefslogtreecommitdiff
path: root/handle.go
blob: 92301bef93dc28b4bb4a5c8b0738649f85248eb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import (
	"fmt"
	"log"
	"net"
	"time"
)

type empty struct{}
type protoHandler func(net.Conn, time.Duration)

func protocolHandler(proto string) (protoHandler, uint16, error) {
	switch proto {
	case "ssh":
		return sshHandler, 22, nil
	case "http":
		return httpHandler, 80, nil
	default:
		return nil, 0, fmt.Errorf("unknown protocol '%s'", proto)
	}
}

func connHandler(proto string, handler protoHandler, conn net.Conn, delay time.Duration) {
	defer conn.Close()

	start := time.Now()
	log.Printf("%s handling %s", conn.RemoteAddr().String(), proto)
	handler(conn, delay)
	duration := time.Since(start)
	log.Printf("%s ended %s lasted for %s", conn.RemoteAddr().String(), proto, duration)
}