aboutsummaryrefslogtreecommitdiff
path: root/handle.go
blob: 918f3a8302a2ca2c62381ddea1c0a02acba275a8 (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"
	"net"
	"time"
)

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

func protocolHandler(proto string) (protoHandler, error) {
	switch proto {
	case "ssh":
		return sshHandler, nil
	default:
		return nil, 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")
}