-
Notifications
You must be signed in to change notification settings - Fork 20
/
listen.go
58 lines (46 loc) · 1.38 KB
/
listen.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2015 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
// Use of this source code is governed by the MIT-license that can be
// found in the LICENSE file.
package telnet
import (
"bufio"
"fmt"
"net"
"strings"
)
type Conn struct {
net.Conn
remoteCall string
}
func (conn Conn) RemoteCall() string { return conn.remoteCall }
type listener struct{ net.Listener }
// Starts a new net.Listener listening for incoming connections.
//
// The Listener takes care of the special Winlink telnet login.
func Listen(addr string) (ln net.Listener, err error) {
ln, err = net.Listen("tcp", addr)
return listener{ln}, err
}
// Accept waits for and returns the next connection to the listener.
//
// The returned net.Conn is a *Conn that holds the remote stations
// call sign. When Accept returns, the caller is logged in and the
// connection can be used directly in a B2F exchange.
//
// BUG(martinhpedersen): Password is discarded and not supported yet.
func (ln listener) Accept() (net.Conn, error) {
conn, err := ln.Listener.Accept()
if err != nil {
return conn, err
}
reader := bufio.NewReader(conn)
fmt.Fprintf(conn, "Callsign :\r")
remoteCall, err := reader.ReadString('\r')
if err != nil {
return conn, err
}
remoteCall = strings.TrimSpace(remoteCall)
fmt.Fprintf(conn, "Password :\r")
_, err = reader.ReadString('\r') //TODO
return &Conn{conn, remoteCall}, err
}