📄 telnetsession.java
字号:
/* * Copyright (c) 2003, KNOPFLERFISH project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * - Neither the name of the KNOPFLERFISH project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */package org.knopflerfish.bundle.consoletelnet;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;import java.util.Dictionary;import org.knopflerfish.service.console.ConsoleService;import org.knopflerfish.service.console.Session;import org.knopflerfish.service.log.LogRef;import org.knopflerfish.service.um.useradmin.ContextualAuthorization;import org.knopflerfish.service.um.useradmin.PasswdAuthenticator;import org.knopflerfish.service.um.useradmin.PasswdSession;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;import org.osgi.service.useradmin.Authorization;/** * * This is the session object that binds the socket, * the input and the * output streams together. * * Here is where the telnet commands are executed * and * where they affect the ConsoleService session. * */public class TelnetSession implements Runnable, org.knopflerfish.service.console.SessionListener { private Socket socket; private TelnetConfig telnetConfig; private ConsoleService consoleService; private Session s; private TelnetServer tserv; private TelnetCommand[] telnetCommands = new TelnetCommand[256]; private InputStream is; private OutputStream os; private TelnetInputStream telnetInputStream; private TelnetOutputStream telnetOutputStream; private TelnetReader reader; private TelnetLogin telnetLogin = null; private PrintWriter printWriter; private char mask = '\177'; // Normal 7 bit mask private boolean enableEcho = true; private LogRef log; private BundleContext bc; // private String DEFAULT_USER_NAME = "admin"; // private String DEFAULT_PASSWORD = "admin"; public TelnetSession(Socket socket, TelnetConfig telnetConfig, ConsoleService consoleService, LogRef log, BundleContext bc, TelnetServer tserv) { this.telnetConfig = telnetConfig; this.socket = socket; this.consoleService = consoleService; this.log = log; this.bc = bc; this.tserv = tserv; try { is = socket.getInputStream(); os = socket.getOutputStream(); } catch (IOException iox) { log.error("Session socket opening exception " + iox.toString()); } telnetInputStream = new TelnetInputStream(is, this); telnetOutputStream = new TelnetOutputStream(os, this); printWriter = new PrintWriter(telnetOutputStream); reader = new TelnetReader(telnetInputStream, this); // Instantiate the supported options, with default state // ts code, do show telnetCommands[TCC.ECHO] = new TelnetCommandEcho(this, TCC.ECHO, false, false); telnetCommands[TCC.SUPGA] = new TelnetCommandSupga(this, TCC.SUPGA, false, true); telnetCommands[TCC.STATUS] = new TelnetCommandStatus(this, TCC.STATUS, false, true); } public void run() { try { // Telnet initial option negotiaion initialNegotiation(reader, printWriter, telnetOutputStream); // Platform login processing printWriter.println("Knopflerfish OSGi console"); telnetLogin = login(reader, printWriter, telnetOutputStream); // System.out.println("telnetLogin state: " + // telnetLogin.isPermitted()); if (telnetLogin.isPermitted() == true) { Authorization authorization = telnetLogin.getAuthorization(); s = consoleService.runSession("telnet session", reader, printWriter); Dictionary props = s.getProperties(); if (authorization != null) { props.put(Session.PROPERTY_AUTHORIZATION, authorization); } printWriter.println("'quit' to end session"); s.addSessionListener(this); } else { printWriter.println("Login incorrect"); printWriter.flush(); printWriter.close(); reader.close(); telnetInputStream.close(); telnetOutputStream.close(); is.close(); os.close(); socket.close(); tserv.removeSession(this); } } catch (IOException ioe) { log.error("Session exception " + ioe.toString()); ioe.printStackTrace(); tserv.removeSession(this); } } public void sessionEnd(Session s) { // called if the console session // terminates try { close(); } catch (Exception iox) { log.error("Session end exception " + iox.toString()); } } public void close() { try { s.close(); printWriter.close(); reader.close(); telnetInputStream.close(); telnetOutputStream.close(); is.close(); os.close(); socket.close(); log.info("User " + telnetLogin.getUser() + " logged out"); tserv.removeSession(this); s = null; } catch (IOException iox) { log.error("Session close exception " + iox.toString()); } } /** * * Get the character mask * This is to support binary mode, * that is 7 or * 8 bit data in the output data stream */ public char getMask() { return mask; } /** * * Get all instantiated commands in this session */ public TelnetCommand[] getCommands() { return telnetCommands; } /** * * Get the TelnetOutputStream */ public TelnetOutputStream getTelnetOutputStream() { return telnetOutputStream; } /** * * Method to do echo to the output stream * This also looks at the * enableEcho flag */ public void echoChar(int character) { TelnetCommand tc = telnetCommands[TCC.ECHO]; // System.out.println("echo=" + character + ", enable=" + enableEcho); if (tc != null) { if (tc.getDoStatus() && enableEcho) { printWriter.print((char) character); printWriter.flush(); } } } public void execSB() { } public void execGA() { } public void execEL() { } public void execEC() { } public void execAYT() { try { telnetOutputStream.writeCommand("[" + socket.getLocalAddress().getHostName() + ": yes]"); } catch (IOException ex) { log.error("Command AYT exception " + ex.toString()); } } public void execAO() { s.abortCommand(); } public void execIP() { s.abortCommand(); } public void execBRK() { s.abortCommand(); } public void execDM() { } public void execNOP() { } /** * * Execution of standard telnet negotiation commmands * DO, DONT, WILL and * WONT and also * the execution of the command via SB code .... SE * option * command structure. * */ /** * * DONT * * * * @parameter code, the optional command code */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -