Ejemplo n.º 1
0
func main() {
	installQuitHandler()
	path, _ = filepath.Abs(".")

	// ssh
	config := sshServer.Config()
	conn := sshServer.Open(config)

	// Channel for messages that are passed around
	msgchan := make(chan text.Message)
	// Channel for clients that are added
	addchan := make(chan Client)
	// Channel for connections that are removed
	rmchan := make(chan net.Conn)

	go handleMessages(msgchan, addchan, rmchan)

	fmt.Println(text.Clear)
	fmt.Println(text.Yellow("Voyage started"))

	for {
		log.Println("Accepting incoming connections")
		sConn, err := conn.Accept()
		if err != nil {
			log.Println("Failed to accept incoming connection")
			continue
		}
		if err := sConn.Handshake(); err != nil {
			log.Println("Failed to handshake")
			continue
		}
		go handleSshConnection(sConn, msgchan, addchan, rmchan)
	}
}
Ejemplo n.º 2
0
func installQuitHandler() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		<-c
		fmt.Println(text.Yellow("Voyage ended"))
		os.Exit(1)
	}()
}
Ejemplo n.º 3
0
func getNickname(socket Socket) (string, bool) {
	Write(socket, "What is your nick? ")
	nick, err := socket.ReadLine()
	if err != nil {
		return "", false
	}

	nickname := string(nick)
	if nickname == "" {
		return "", false
	}

	Writeln(socket, "Welcome, "+text.Yellow(nickname)+"!")
	return nickname, true
}
Ejemplo n.º 4
0
func handleClientRead(socket Socket, client *Client, fromClient chan<- text.Message) {
	defer close(fromClient)
	Writeln(socket, "Welcome to the Chat Server!\n")

	nickname := ""
	for ok := false; !ok; {
		nickname, ok = getNickname(socket)
	}
	client.username = nickname
	client.term.SetPrompt("say: ")

	t := text.LightGreen("New user ") + text.Yellow(nickname) +
		text.LightGreen(" has joined the chat.")
	fromClient <- text.Message{Msg: t, Sender: nickname}

	for {
		line, err := socket.ReadLine()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Println("handleClient readLine err:", err)
			continue
		}
		// TODO: Needs to be blocking so response can be synchronized
		fromClient <- text.Message{
			Msg:     string(line),
			Sender:  nickname,
			MsgType: "chat",
		}
	}

	t = text.LightGreen("User ") + text.Yellow(nickname) +
		text.LightGreen(" left the chat.")
	fromClient <- text.Message{Msg: t, Sender: nickname}
}