📄 controlservice.java
字号:
import java.io.*;import java.util.*;import java.net.*;import dvb.kuanshi.kssms.util.*;import dvb.kuanshi.kssms.server.Server;public class ControlService implements Service { Server server; String password; public ControlService(Server server, String password) { this.server = server; this.password = password; } public void serve(InputStream in, OutputStream out) throws IOException { boolean authorized = false; BufferedReader from_client = new BufferedReader(new InputStreamReader(in)); PrintWriter to_console = new PrintWriter(System.out, true); to_console.println("后台管理服务响应请求!\n"); PrintWriter to_client = new PrintWriter(out); synchronized (this) { if(server.connected) { to_client.println("已经有用户连接,本服务仅允许一个连接!\n"); to_client.close(); return; } else server.setControlFlag(); } to_client.println("Remote Console>"); to_client.flush(); String line; while ((line=from_client.readLine())!=null) { int len = line.indexOf("Remote Console>"); line = line.substring(len+1,line.length()); String printStr; try { StringTokenizer st = new StringTokenizer(line); int count = st.countTokens(); if (!st.hasMoreElements()) continue; String first = st.nextToken().toLowerCase(); if (first.equals("password")) { String pwd = st.nextToken(); if (pwd.equals(this.password)) { to_client.println("OK"); authorized = true; } else to_client.println("密码无效,请重试!\n"); } else if (first.equals("add")) { if(!authorized) to_client.println("请登陆!\n"); else { count--; String servicename; int Port; boolean flag = true; if (count>0) { servicename = st.nextToken(); Port = Integer.parseInt(st.nextToken()); server.addService(loadClass(servicename1), Port); to_client.println("服务" + servicename + "已经加载\n"); flag = false; } if (flag) to_client.println("系统不能启动非法服务:" + servicename); else {to_client.println("请输入服务名!\n");} } } else if (first.equals("remove")) { if(!authorized) to_client.println("请登陆!\n"); else { count--; if (count>0) { int port = Integer.parseInt(st.nextToken()); boolean bl = server.removeService(port); if (bl) to_client.println("端口: " + port +"上的服务已经卸载\n"); else to_client.println("端口: "+ port +"上无任何服务运行,卸载操作失败!\n"); } else to_client.println("请输入端口名!\n"); } } else if(first.equals("status")) { if(!authorized) to_client.println("请登陆!\n"); else server.displayStatus(to_client); } else if(first.equals("help")) { if(!authorized) to_client.println("请登陆!\n"); else printHelp(to_client); } else if(first.equals("quit")) break; else to_client.println("命令不能识别!\n"); } catch(Exception e) {to_client.println("系统后台出错" + e.getMessage() +"\n"); printHelp(to_client); } to_client.println("Remote Console>"); to_client.flush(); } to_client.flush(); authorized = false; server.removeControlFlag(); to_client.close(); from_client.close(); } private void printHelp(PrintWriter out) { out.println("COMMANDS:" + "\tpassword <password>\n" + "\t\tadd <servicename> <port>\n" + "\t\tremove <port>\n" + "\t\tstatus\n" + "\t\thelp\n" + "\t\tquit\n"); } protected Service loadClass(String servicename) { Service s = null; try { Class serviceClass = Class.forName(servicename); s = (Service) serviceClass.newInstance(); } catch (Exception e) { } return s; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -