⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sessioninfo.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.security;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.CoreServlet;


/**
 * Encapsulates everything known about an SSL-Explorer session.
 * 
 * 
 * @author Brett Smith <a href="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
 * @version $Revision: 1.12 $
 */
public class SessionInfo {
    
    final static Log log = LogFactory.getLog(SessionInfo.class);
    
    private static List davUserAgents = null;
    
    /* Logon type */
    
    /**
     * Originated from the user interface
     */
	public final static int UI = 0;
    
    /**
     * Originated from the VPN client
     */
	public final static int VPN_CLIENT = 1;
    
    /**
     * Originated from a DAV client
     */
    public final static int DAV_CLIENT = 2;
    
    /* Navigation context */
    
    /**
     * User console for normal user.
     */
    public static final int USER_CONSOLE_CONTEXT = 1;
    
    /**
     * Management console for super user or users that have been delegated
     * responsibility for management tasks.
     */
    public static final int MANAGEMENT_CONSOLE_CONTEXT = 2;

    /**
     * Setup console that may be accessed from the management console
     * or when the server has been started in setup mode
     */
    public static final int SETUP_CONSOLE_CONTEXT = 4;

    /**
     * Help context works slighly differently in that the sessions current
     * context is not actually changed.
     */
    public static final int HELP_CONTEXT = 8;

    /**
     * Convenience mask for all contexts
     */
    public static final int ALL_CONTEXTS = 255;
    
    /* Private instance variables */
	private User user;
	private InetAddress address;
	private Calendar logonTime;
	private int type;
	private String logonTicket;
    private int navigationContext;
	private HttpSession session;
    private int id;
    private String userAgent;
    
    /* Private static variables */
    
    private static HashMap sessions = new HashMap();
    private static int nextId = 1;
    
    /**
     * Create a new {@link SessionInfo} object, assiging it the next Id.
     * 
     * @param session {@link HttpSession} originator of this session
     * @param logonTicket logon ticket
     * @param user user object
     * @param address address
     * @param type client type
     * @param userAgent user agent if known
     * @return session
     */
    public static SessionInfo nextSession(HttpSession session, String logonTicket, User user,
                    InetAddress address, int type, String userAgent) {
        synchronized(sessions) {
            SessionInfo info = new SessionInfo(nextId, session, logonTicket, user, address, type, userAgent);
            if(CoreServlet.getServlet().getLogonController().isAdministrator(user)) {
                info.setNavigationContext(SessionInfo.MANAGEMENT_CONSOLE_CONTEXT);
            }
            sessions.put(String.valueOf(nextId), info);
            nextId++;
            return info;
        }         
    }

    /**
     * Get a session given its Id.
     * 
     * @param id session id
     * @return session
     */
    public static SessionInfo getSession(int id) {
        return (SessionInfo)sessions.get(String.valueOf(id));
    }
    
    /**
     * Release a session so its ID can be re-used
     */
    public void release() {
        synchronized(sessions) {
            sessions.remove(String.valueOf(id));
            
            // TODO implement a more efficient way of getting the next session id
            
            Map.Entry e;
            int next = 1;
            boolean found;
            while(true) {
                found = false;
                for(Iterator i = sessions.entrySet().iterator(); i.hasNext(); ) {
                    e = (Map.Entry)i.next();
                    if(((SessionInfo)e.getValue()).getId() == next) {
                        found = true;
                        break;
                    }                    
                }
                if(!found) {
                    nextId = next;
                    break;
                }
                next++;
                
            }
        }
    }
    
    /* Private constructor to prevent instantiation */

	private SessionInfo(int id, HttpSession session, String logonTicket, User user,
			InetAddress address, int type, String userAgent) {
		this.user = user;
        this.id = id;
		this.session = session;
		this.logonTicket = logonTicket;
		this.address = address;
        navigationContext = USER_CONSOLE_CONTEXT;
		this.type = type;
        this.userAgent = userAgent;
		logonTime = new GregorianCalendar();
	}
    
    /**
     * Get the sequential ID of this session
     * 
     * @return Id
     */
    public int getId() {
        return id;
    }

    /**
     * Get the {@link HttpSession} that originated this logon session
     * 
     * @return {@link HttpSession} originator
     */
	public HttpSession getHttpSession() {
		return session;
	}

    /**
     * Get the logon ticket for this session
     * 
     * @return logon ticket
     */
	public String getLogonTicket() {
		return logonTicket;
	}

    /**
     * Get the type of session. May be one of {@link #UI}, {@link #VPN_CLIENT}
     * or {@link #DAV_CLIENT}.
     * 
     * 
     * @return session type
     */
	public int getType() {
		return type;
	}

    /**
     * Get the internet address that this session originated from
     * 
     * @return originating internet address
     */
	public InetAddress getAddress() {
		return address;
	}

    /**
     * Get the time this session started
     * 
     * @return logon time
     */
	public Calendar getLogonTime() {
		return logonTime;
	}

    /**
     * Get the user that originated this session
     * 
     * @return user
     */
	public User getUser() {
		return user;
	}

    /**
     * Set the {@link HttpSession} that owns or now owns this session.
     * 
     * @param session session
     */
	public void setSession(HttpSession session) {
		this.session = session;
	}
    
    /**
     * Get the navigation context the user is currently in. May be one 
     * of {@link #USER_CONSOLE_CONTEXT} or {@link #MANAGEMENT_CONSOLE_CONTEXT}.
     * 
     * @return navigation context
     */
    public int getNavigationContext() {
        return navigationContext;
    }
    
    /**
     * Set the navigation context the user is currently in. May be one 
     * of {@link #USER_CONSOLE_CONTEXT} or {@link #MANAGEMENT_CONSOLE_CONTEXT}.
     * 
     * @param navigationContext new navigation context
     */
    public void setNavigationContext(int navigationContext) {
        this.navigationContext = navigationContext;
    }

    /**
     * Get the scheme that was used to logon.
     * 
     * @return logon scheme
     */
    public AuthenticationScheme getCredentials() {
        return (AuthenticationScheme)session.getAttribute(Constants.AUTH_SESSION);
    }
    
    /**
     * Get the user agent.
     * 
     * @return user agent
     */
    public String getUserAgent() {
        return userAgent;
    }

    /**
     * Set the user for this session
     * 
     * @param user user
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * Try to determine what type of client is connecting based on the
     * user agent. If <code>null</code> is supplied then the type will
     * just be returned as {@link SessionInfo#UI}. 
     * <p>
     * When not null, first the user agent is tested to see if it is 
     * the VPN client (agent). If so then {@link SessionInfo#VPN_CLIENT}
     * will be returned.
     * <p>
     * If not the VPN client, then the file <i>conf/dav.agents</i> will be 
     * examined to see if any of the patterns it contains match the 
     * supplied agent string. If so, the the session is of type {@link SessionInfo#DAV_CLIENT}.
     * <p>
     * If none of the conditions are met, then the default of {@link SessionInfo#UI}
     * is returned.
     * 
     * @param userAgent user agent string
     * @return session type 
     * @see SessionInfo#UI 
     * @see SessionInfo#DAV_CLIENT
     * @see SessionInfo#VPN_CLIENT
     */
    public static int getSessionTypeForUserAgent(String userAgent) {
        if(userAgent == null) {
            return UI;
        }
        if(userAgent.equals("SSL-Explorer/Agent")) {
            return VPN_CLIENT;
        }
        if(davUserAgents == null) {
            davUserAgents = new ArrayList();
            File f = new File(ContextHolder.getContext().getConfDirectory(), "dav.agents");
            FileInputStream fin = null;
            try {
                fin = new FileInputStream(f);
                BufferedReader br = new BufferedReader(new InputStreamReader(fin));
                String line = null;
                while( ( line = br.readLine() ) != null) {
                    line = Util.trimBoth(line);
                    if(!line.startsWith("#")) {
                        davUserAgents.add(line);
                    }
                }
            }
            catch(IOException ioe) {
                log.warn("Failed to read " + f.getAbsolutePath() + ". Will not be able to identify DAV clients.");
            }
            finally {
                Util.closeStream(fin);
            }
        }
        for(Iterator i = davUserAgents.iterator(); i.hasNext(); ) {
            String us = (String)i.next();
            if(userAgent.matches(us)) {
                return SessionInfo.DAV_CLIENT;
            }
        }
        return SessionInfo.UI;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -