aboutsummaryrefslogtreecommitdiff
path: root/handle.go
blob: c153b7d2c9f68063c927c5ec45e3420027985964 (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
33
34
package main

import (
	"fmt"
	"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 logConn(conn net.Conn, msg string) {
	now := time.Now().UTC()
	fmt.Printf("%s, %s, %s\n", now.String(), conn.RemoteAddr().String(), msg)
}

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

	logConn(conn, "handling")
	handler(conn, delay)
	logConn(conn, "closing")
}