github.com/la5nta/wl2k-go@v0.11.8/transport/telnet/listen.go (about)

     1  // Copyright 2015 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
     2  // Use of this source code is governed by the MIT-license that can be
     3  // found in the LICENSE file.
     4  
     5  package telnet
     6  
     7  import (
     8  	"bufio"
     9  	"fmt"
    10  	"net"
    11  	"strings"
    12  )
    13  
    14  type Conn struct {
    15  	net.Conn
    16  	remoteCall string
    17  }
    18  
    19  func (conn Conn) RemoteCall() string { return conn.remoteCall }
    20  
    21  type listener struct{ net.Listener }
    22  
    23  // Starts a new net.Listener listening for incoming connections.
    24  //
    25  // The Listener takes care of the special Winlink telnet login.
    26  func Listen(addr string) (ln net.Listener, err error) {
    27  	ln, err = net.Listen("tcp", addr)
    28  	return listener{ln}, err
    29  }
    30  
    31  // Accept waits for and returns the next connection to the listener.
    32  //
    33  // The returned net.Conn is a *Conn that holds the remote stations
    34  // call sign. When Accept returns, the caller is logged in and the
    35  // connection can be used directly in a B2F exchange.
    36  //
    37  // BUG(martinhpedersen): Password is discarded and not supported yet.
    38  func (ln listener) Accept() (net.Conn, error) {
    39  	conn, err := ln.Listener.Accept()
    40  	if err != nil {
    41  		return conn, err
    42  	}
    43  
    44  	reader := bufio.NewReader(conn)
    45  
    46  	fmt.Fprintf(conn, "Callsign :\r")
    47  	remoteCall, err := reader.ReadString('\r')
    48  	if err != nil {
    49  		return conn, err
    50  	}
    51  
    52  	remoteCall = strings.TrimSpace(remoteCall)
    53  
    54  	fmt.Fprintf(conn, "Password :\r")
    55  	_, err = reader.ReadString('\r') //TODO
    56  
    57  	return &Conn{conn, remoteCall}, err
    58  }