forked from hengyunabc/xdiamond
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加crash,用于实现web console,并实现了websocket 的权限控制
- Loading branch information
1 parent
db4eaed
commit 6b90e54
Showing
46 changed files
with
11,044 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
xdiamond-server/src/main/java/io/github/xdiamond/web/api/controller/CrashController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.github.xdiamond.web.api.controller; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.github.xdiamond.web.RestResult; | ||
import io.github.xdiamond.web.shiro.PermissionHelper; | ||
|
||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
import com.codahale.metrics.annotation.Timed; | ||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
|
||
@Controller | ||
@RequestMapping(value = "/api") | ||
public class CrashController { | ||
|
||
static Cache<Object, Object> crashTokenCache = CacheBuilder.newBuilder() | ||
.expireAfterWrite(30 * 1000, TimeUnit.MILLISECONDS).maximumSize(1000).build(); | ||
|
||
/** | ||
* 获取crash websocket的访问token | ||
* | ||
* @return | ||
*/ | ||
@RequestMapping(value = "/crash/token", method = RequestMethod.GET) | ||
@Timed | ||
public ResponseEntity<RestResult> token() { | ||
PermissionHelper.checkAdmin(); | ||
|
||
String token = RandomStringUtils.randomAlphanumeric(16); | ||
crashTokenCache.put(token, new Object()); | ||
return RestResult.success().withResult("token", token).build(); | ||
} | ||
|
||
/** | ||
* 检查token是否存在,如果存在,即用这个token来连接的websocket的连接是合法的 | ||
* | ||
* @param token | ||
* @return | ||
*/ | ||
static public boolean checkToken(String token) { | ||
return crashTokenCache.getIfPresent(token) != null; | ||
} | ||
} |
238 changes: 238 additions & 0 deletions
238
xdiamond-server/src/main/java/io/github/xdiamond/web/crash/CRaSHConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
/* | ||
* Copyright (C) 2012 eXo Platform SAS. | ||
* | ||
* This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser | ||
* General Public License as published by the Free Software Foundation; either version 2.1 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This software 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with this | ||
* software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, | ||
* Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package io.github.xdiamond.web.crash; | ||
|
||
import io.github.xdiamond.web.api.controller.CrashController; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.security.Principal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.logging.Logger; | ||
|
||
import javax.websocket.OnClose; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.OnOpen; | ||
import javax.websocket.Session; | ||
import javax.websocket.server.ServerEndpoint; | ||
|
||
import org.crsh.cli.impl.Delimiter; | ||
import org.crsh.cli.impl.completion.CompletionMatch; | ||
import org.crsh.cli.spi.Completion; | ||
import org.crsh.keyboard.KeyType; | ||
import org.crsh.plugin.PluginContext; | ||
import org.crsh.plugin.WebPluginLifeCycle; | ||
import org.crsh.shell.Shell; | ||
import org.crsh.shell.ShellFactory; | ||
import org.crsh.shell.ShellProcess; | ||
import org.crsh.util.Utils; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
/** | ||
* @author Julien Viet | ||
* @author hengyunabc | ||
* | ||
* 增加Token权限检验功能 | ||
* */ | ||
@ServerEndpoint(value = "/crash", configurator = Configurator.class) | ||
public class CRaSHConnector { | ||
|
||
/** . */ | ||
static final Logger log = Logger.getLogger(CRaSHConnector.class.getName()); | ||
|
||
/** . */ | ||
private final ConcurrentHashMap<String, CRaSHSession> sessions = | ||
new ConcurrentHashMap<String, CRaSHSession>(); | ||
|
||
/** . */ | ||
private static final ThreadLocal<Session> current = new ThreadLocal<Session>(); | ||
|
||
/** | ||
* @return the current session crash id (CRASHID) or null if none is associated with the request | ||
*/ | ||
public static String getHttpSessionId() { | ||
Session session = current.get(); | ||
if (session != null) { | ||
return (String) session.getUserProperties().get("CRASHID"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@OnOpen | ||
public void start(Session wsSession) { | ||
// 检查token是否合法 | ||
String token = null; | ||
List<String> tokenList = wsSession.getRequestParameterMap().get("token"); | ||
if (tokenList != null && !tokenList.isEmpty()) { | ||
token = tokenList.get(0); | ||
} | ||
if (token == null || !CrashController.checkToken(token)) { | ||
throw new RuntimeException("token is invalid"); | ||
} | ||
|
||
current.set(wsSession); | ||
try { | ||
URI uri = wsSession.getRequestURI(); | ||
String path = uri.getPath(); | ||
log.fine("Establishing session for " + path); | ||
String contextPath = path.substring(0, path.lastIndexOf('/')); | ||
PluginContext context = WebPluginLifeCycle.getPluginContext(contextPath); | ||
if (context != null) { | ||
Boolean enabled = context.getProperty(WebPlugin.ENABLED); | ||
enabled = true; | ||
if (enabled != null && enabled) { | ||
log.fine("Using shell " + context); | ||
ShellFactory factory = context.getPlugin(ShellFactory.class); | ||
Principal user = wsSession.getUserPrincipal(); | ||
Shell shell = factory.create(user); | ||
CRaSHSession session = new CRaSHSession(wsSession, shell); | ||
sessions.put(wsSession.getId(), session); | ||
log.fine("Established session " + wsSession.getId()); | ||
} else { | ||
log.fine("Web plugin disabled"); | ||
} | ||
} else { | ||
log.fine("No shell found"); | ||
} | ||
} finally { | ||
current.set(null); | ||
} | ||
} | ||
|
||
@OnClose | ||
public void end(Session wsSession) { | ||
current.set(wsSession); | ||
try { | ||
CRaSHSession session = sessions.remove(wsSession.getId()); | ||
if (session != null) { | ||
log.fine("Destroying session " + wsSession.getId()); | ||
WSProcessContext current = session.current.getAndSet(null); | ||
if (current != null) { | ||
log.fine("Cancelling on going command " + current.command + " for " + wsSession.getId()); | ||
current.process.cancel(); | ||
} | ||
} else { | ||
log.fine("No shell session found"); | ||
} | ||
} finally { | ||
current.set(null); | ||
} | ||
} | ||
|
||
@OnMessage | ||
public void incoming(String message, Session wsSession) { | ||
String key = wsSession.getId(); | ||
log.fine("Received message " + message + " from session " + key); | ||
current.set(wsSession); | ||
try { | ||
CRaSHSession session = sessions.get(key); | ||
if (session != null) { | ||
JsonParser parser = new JsonParser(); | ||
JsonElement json = parser.parse(message); | ||
if (json instanceof JsonObject) { | ||
JsonObject event = (JsonObject) json; | ||
JsonElement type = event.get("type"); | ||
if (type.getAsString().equals("welcome")) { | ||
log.fine("Sending welcome + prompt"); | ||
session.send("print", session.shell.getWelcome()); | ||
session.send("prompt", session.shell.getPrompt()); | ||
} else if (type.getAsString().equals("execute")) { | ||
String command = event.get("command").getAsString(); | ||
int width = event.get("width").getAsInt(); | ||
int height = event.get("height").getAsInt(); | ||
ShellProcess process = session.shell.createProcess(command); | ||
WSProcessContext context = | ||
new WSProcessContext(session, process, command, width, height); | ||
if (session.current.getAndSet(context) == null) { | ||
log.fine("Executing \"" + command + "\""); | ||
process.execute(context); | ||
} else { | ||
log.fine("Could not execute \"" + command + "\""); | ||
} | ||
} else if (type.getAsString().equals("cancel")) { | ||
WSProcessContext current = session.current.getAndSet(null); | ||
if (current != null) { | ||
log.fine("Cancelling command \"" + current.command + "\""); | ||
current.process.cancel(); | ||
} else { | ||
log.fine("No process to cancel"); | ||
} | ||
} else if (type.getAsString().equals("key")) { | ||
WSProcessContext current = session.current.get(); | ||
if (current != null) { | ||
String _keyType = event.get("keyType").getAsString(); | ||
KeyType keyType = KeyType.valueOf(_keyType.toUpperCase()); | ||
if (keyType == KeyType.CHARACTER) { | ||
int code = event.get("keyCode").getAsInt(); | ||
if (code >= 32) { | ||
current.handle(KeyType.CHARACTER, new int[] {code}); | ||
} | ||
} else { | ||
current.handle(keyType, new int[0]); | ||
} | ||
} else { | ||
log.fine("No process can handle the key event"); | ||
} | ||
} else if (type.getAsString().equals("complete")) { | ||
String prefix = event.get("prefix").getAsString(); | ||
CompletionMatch completion = session.shell.complete(prefix); | ||
Completion completions = completion.getValue(); | ||
Delimiter delimiter = completion.getDelimiter(); | ||
StringBuilder sb = new StringBuilder(); | ||
List<String> values = new ArrayList<String>(); | ||
try { | ||
if (completions.getSize() == 1) { | ||
String value = completions.getValues().iterator().next(); | ||
delimiter.escape(value, sb); | ||
if (completions.get(value)) { | ||
sb.append(delimiter.getValue()); | ||
} | ||
values.add(sb.toString()); | ||
} else { | ||
String commonCompletion = Utils.findLongestCommonPrefix(completions.getValues()); | ||
if (commonCompletion.length() > 0) { | ||
delimiter.escape(commonCompletion, sb); | ||
values.add(sb.toString()); | ||
} else { | ||
for (Map.Entry<String, Boolean> entry : completions) { | ||
delimiter.escape(entry.getKey(), sb); | ||
values.add(sb.toString()); | ||
sb.setLength(0); | ||
} | ||
} | ||
} | ||
} catch (IOException ignore) { | ||
// Should not happen | ||
} | ||
log.fine("Completing \"" + prefix + "\" with " + values); | ||
session.send("complete", values); | ||
} | ||
} | ||
} else { | ||
log.fine("No shell session found"); | ||
} | ||
} finally { | ||
current.set(null); | ||
} | ||
} | ||
} |
Oops, something went wrong.