日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA实现telnet代理,对输入命令拦截

發布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA实现telnet代理,对输入命令拦截 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目前只是個簡單版本,有一些難點沒有突破(vi編輯器的處理)

package telnet.server;import jinghai.base.Environment; import telnet.TelnetProxyMain; import telnet.handle.Handle;import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.LinkedList; import java.util.concurrent.CountDownLatch;/*** @author Chun* @create 2021-04-02 10:46**/ public class TelnetProxyServer extends Thread {Socket client;Socket accept;InputStream inServer;OutputStream outClient;CountDownLatch countDownLatch;public volatile static STATUS status;//狀態public static STATUS login; //登錄三個過程private static int index = 0;private static final int temp13 = 13;public TelnetProxyServer(Socket client, Socket accept, InputStream inServer, OutputStream outClient, CountDownLatch countDownLatch) throws IOException {this.client = client;this.accept = accept;this.inServer = inServer;this.outClient = outClient;this.countDownLatch = countDownLatch;}@Overridepublic void run() {LinkedList<Integer> linkedListProxyClient = new LinkedList<>();status = STATUS.CONNECT; //設置狀態login = STATUS.CONNECT; //登錄過程驗證使用while (!accept.isClosed() && !client.isClosed()) {try {if (TelnetProxyMain.readClient == -1 || TelnetProxyMain.readServer == -1) break;TelnetProxyMain.readServer = inServer.read();} catch (IOException e) {e.printStackTrace();}//三個狀態,連接,登錄,交互(登錄成功)if (status.getStatus().equalsIgnoreCase("CONNECT")) { //連接過程直接將獲取的值傳給TelnetServer即可connectStatus(outClient);} else {//處理LOGIN和INTERACTIVE,存在公用部分try {statusPublic(inServer, outClient, linkedListProxyClient);} catch (IOException e) {e.printStackTrace();}}}countDownLatch.countDown();}private void connectStatus(OutputStream outClient) {try {outClient.write(TelnetProxyMain.readServer);//前21字節是連接過程產生的,第21個字節ascii是3表示正文結束(3 => 0x03 => ETX => (end of text) => 正文結束)if (TelnetProxyMain.readServer == 3) {//TODO status狀態轉為Loginstatus = status.nextState(); //狀態后移}} catch (IOException e) {e.printStackTrace();}}//老版,根據interactiveStatus去改private void checkLogin(boolean b, InputStream inServer, OutputStream outClient, LinkedList<Integer> linkedListProxyClient, byte[] bytes) throws IOException {if (b) {outClient.write(TelnetProxyMain.readServer);outClient.write(inServer.read());login = login.nextState();System.out.println("LegalOrder: " + new String(bytes));} else {inServer.read(); //去掉換行10outClient.write(27);outClient.write(27);}linkedListProxyClient.clear();index = 0;}private void statusPublic(InputStream inServer, OutputStream outClient, LinkedList<Integer> linkedListProxyClient) throws IOException {if (TelnetProxyMain.readServer == 10) {// 13表示回車鍵,10表示換行符(windows下是/r/n,linux下/n) ,證明輸入結束,對輸入的命令進行校驗,合法發送給服務器,不合法打回//區分LOGIN和INTERACTIVE,如果用戶名密碼不需要攔截,則不用區分 // if (status.getStatus().equalsIgnoreCase("LOGIN")) { // loginStatus(inServer, outClient, linkedListProxyClient); // } else if (status.getStatus().equalsIgnoreCase("INTERACTIVE")) {// TODO 這里處理命令交互過程interactiveStatus(inServer, outClient, linkedListProxyClient); // }} else if (TelnetProxyMain.readServer == 8) { // 刪除backspaceif (index != 0 && linkedListProxyClient.size() != 0) {linkedListProxyClient.remove(--index);}outClient.write(TelnetProxyMain.readServer);} else if (TelnetProxyMain.readServer == 27) {specialButtons(inServer, outClient, linkedListProxyClient);//特殊按鍵的處理,上下左右,home,end等} else if (TelnetProxyMain.readServer == 127) { //delete按鍵暫時沒處理} else if (TelnetProxyMain.readServer != 3 && TelnetProxyMain.readServer != 13) {linkedListProxyClient.add(index++, TelnetProxyMain.readServer);outClient.write(TelnetProxyMain.readServer);}}private void specialButtons(InputStream inServer, OutputStream outClient, LinkedList<Integer> linkedListProxyClient) throws IOException {ByteArrayOutputStream special = new ByteArrayOutputStream(); //處理特殊字符上下左右,home、end、esc等special.write(TelnetProxyMain.readServer);int read = inServer.read();if (read == 91) {special.write(91);int read1 = inServer.read();//讀一位if (read1 == 68 || read1 == 67) {//左右special.write(read1);outClient.write(special.toByteArray());if (read1 == 68 && index > 0) {index--;}if (read1 == 67 && index < linkedListProxyClient.size()) {index++;}}//其他目前來說直接過濾了else if (read1 == 65 || read1 == 66) {special.write(read1);outClient.write(special.toByteArray());} else if (read1 == 49 || read1 == 52) {special.write(inServer.read());outClient.write(special.toByteArray());}} else if (read == 27) { //escoutClient.write(TelnetProxyMain.readServer);//清空outClient.write(read);linkedListProxyClient.clear();index = 0;}}//三個過程:用戶名,密碼,域名private void loginStatus(InputStream inServer, OutputStream outClient, LinkedList<Integer> linkedListProxyClient) throws IOException {//轉換成byte數組byte[] bytes = listToBytes(linkedListProxyClient);// TODO 這之后對用戶名密碼做攔截if (login.getStatus().equalsIgnoreCase("CONNECT")) { // 用戶名boolean b = Handle.checkUserName(bytes);checkLogin(b, inServer, outClient, linkedListProxyClient, bytes);} else if (login.getStatus().equalsIgnoreCase("LOGIN")) { //密碼boolean b = Handle.checkPassWord(bytes);checkLogin(b, inServer, outClient, linkedListProxyClient, bytes);} else if (login.getStatus().equalsIgnoreCase("INTERACTIVE")) { //域名boolean b = Handle.checkDomainName(bytes);checkLogin(b, inServer, outClient, linkedListProxyClient, bytes);//TODO status狀態轉為INTERACTIVEif (b) status = status.nextState();index = 0;}}private void interactiveStatus(InputStream inServer, OutputStream outClient, LinkedList<Integer> linkedListProxyClient) throws IOException {//轉換成byte數組byte[] bytes = listToBytes(linkedListProxyClient);String command = new String(bytes).trim();// TODO 對鍵入的命令檢查校驗boolean b = Handle.checkCommand(command);if (b) {if (Environment.isWindows) outClient.write(temp13);outClient.write(TelnetProxyMain.readServer);System.out.println("LegalOrder: " + new String(bytes));} else {for (int i = 0; i < bytes.length; i++) {outClient.write(8);}System.out.println(bytes.length + "個8");}linkedListProxyClient.clear();index = 0;}private byte[] listToBytes(LinkedList<Integer> linkedListProxyClient) {Integer[] ints = linkedListProxyClient.toArray(new Integer[0]);int len = ints.length;byte[] bytes = new byte[len];for (int i = 0; i < len; i++) {bytes[i] = (byte) (int) ints[i];}return bytes;}} package telnet.client;import telnet.TelnetProxyMain;import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.concurrent.CountDownLatch;/*** @author Chun* @create 2021-04-02 10:46**/ public class TelnetProxyClient extends Thread {Socket client;Socket accept;InputStream inClient;OutputStream outServer;CountDownLatch countDownLatch;public TelnetProxyClient(Socket client, Socket accept, InputStream inClient, OutputStream outServer, CountDownLatch countDownLatch) {this.client = client;this.accept = accept;this.inClient = inClient;this.outServer = outServer;this.countDownLatch = countDownLatch;}@Overridepublic void run() {while (!accept.isClosed() && !client.isClosed()) {try {if (TelnetProxyMain.readServer == -1 || TelnetProxyMain.readClient == -1) break;TelnetProxyMain.readClient = inClient.read(); // System.out.println("返回="+TelnetProxyMain.readClient);outServer.write(TelnetProxyMain.readClient);} catch (IOException e) {try {outServer.flush();e.printStackTrace();} catch (IOException ioException) {ioException.printStackTrace();}break;}}countDownLatch.countDown();} } package telnet;import telnet.client.TelnetProxyClient; import telnet.server.TelnetProxyServer;import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.CountDownLatch;/*** @author Chun* @create 2021-04-08 8:59**/ public class TelnetProxyMain {private final ServerSocket serverSocket;private final Socket client;private final Socket accept;private final InputStream inServer;private final OutputStream outServer;private final InputStream inClient;private final OutputStream outClient;public volatile static int readServer = 0;public volatile static int readClient = 0;private final CountDownLatch countDownLatch = new CountDownLatch(2);public TelnetProxyMain(String ip, int serverPort) throws IOException {serverSocket = new ServerSocket(55);accept = serverSocket.accept();inServer = accept.getInputStream();outServer = accept.getOutputStream();client = new Socket(ip, serverPort);inClient = client.getInputStream();outClient = client.getOutputStream();}/**** 思路1:(采用)目前實現思路是server接收客戶端的字節,接收到保留并發送給telnetServer,當輸入回車時去校驗命令是否合法* **出現的問題:當我們鍵入↑↓時應該是返回上一次的命令,這里有個問題就是當我們輸入回車后client發送的只有回車并沒有將命令發送過來,* 這樣就導致我們接收到了空,無法對此命令進行攔截** 思路2:在TelnetServer發送給client處也就是我們自己實現的client處進行攔截發送的字節,* 攔截到就拼接,拼接成字符串,在server處仍然監聽回車,敲入回車后校驗命令是否合法。* **出現的問題:這樣會攔截到我們輸入↑↓后server發送給client的命令,但是當我們在vi/vim編輯器里使用↑↓同樣會出現問題。** ** 注:client我們輸入命令后客戶端想要顯示,是需要server將這個字母的字節發送給client的,* 也就是client發送給server一個a(97),server就需要回復client一個a(97)*/public void run() {try {//啟動兩個線程,分別作為代理的client和servernew TelnetProxyServer(client, accept, inServer, outClient, countDownLatch).start();new TelnetProxyClient(client, accept, inClient, outServer, countDownLatch).start();countDownLatch.await(); //等待兩個子線程結束在執行} catch (InterruptedException | IOException e) {e.printStackTrace();} finally {try {client.close();accept.close();serverSocket.close();} catch (IOException e) {e.printStackTrace();}}}public static void main(String[] args) {try {TelnetProxyMain telnetProxyMain = new TelnetProxyMain(args[0],50000);telnetProxyMain.run();} catch (IOException e) {e.printStackTrace();}} }

總結

以上是生活随笔為你收集整理的JAVA实现telnet代理,对输入命令拦截的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。