/************************************************************************
* JavaProxy 0.1 *
* SFSU 1999 *
* Shaggy *
* *
* File: Console.java *
* Copyright 1999, Charles Hall
*
*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.
*
*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., 59 Temple Place, Suite 330, Boston, *MA 02111-1307 USA
*
* *
************************************************************************/
import Relay;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.awt.*;
public class Console extends Thread{
int BaseSocket;
int port;
int que;
public Console(int p, int q){
BaseSocket=5500;
port = p;
que = q;
}
public void run()
{
ServerSocket server;
ConSocket prox;
try {
server = new ServerSocket(port, que);
while(true){
yield();
prox = new ConSocket(server.accept(), BaseSocket);
prox.start();
}//while
}catch(IOException e){}
}//run
}//ProxServer
class ConSocket extends Thread{
int basox;
Socket connection;
DataOutputStream out;
DataInputStream in;
DataOutputStream shout;
DataInputStream shin;
int c;
int d;
int i;
int cont;
char buffer[];
String comm;
public ConSocket(Socket s, int base){
basox =base ;
connection = s;
c=0;
d=0;
i=0;
cont=1;
buffer = new char[1024];
}
public void run(){
try{
cont=1;
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(connection.getOutputStream());
out.writeBytes("Welcome to the Gateway\n");
out.writeByte(0x0d);
out.writeBytes("Shaggy SFSU 1999\n");
out.writeByte(0x0d);
out.writeBytes("type 'help' for command list\n");
out.writeByte(0x0d);
//This is the main loop
while(cont!=0){
yield();
i=0;
comm=new String();
buffer=new char[1024];
out.writeBytes("research gateway> ");
while(((c=in.read())!= (int) '\n')&&(i<1024))
{
buffer[i] = (char) c;
i++;
}//while
comm=new String(buffer);
switch(comm.charAt(0)){
case 'h':
out.writeBytes("Command Summary\n");
out.writeByte(0x0d);
out.writeBytes("help: this message\n");
out.writeByte(0x0d);
out.writeBytes("ping \n");
out.writeByte(0x0d);
out.writeBytes("telnet \n");
out.writeByte(0x0d);
out.writeBytes("traceroute \n");
out.writeByte(0x0d);
break;
case 'q':
out.writeBytes("Good-Bye\n");
out.writeByte(0x0d);
connection.close();
cont =0;
break;
case 't':
Relay inny,outy;
StringTokenizer toker=new StringTokenizer(comm);
toker.nextToken();
Socket cli;
InetAddress iadd=InetAddress.getByName(toker.nextToken());
InetAddress ladd=InetAddress.getLocalHost();
try{
cli=new Socket(iadd,23,ladd,++basox);
DataInputStream clin = new DataInputStream(cli.getInputStream());
DataOutputStream clout = new DataOutputStream(cli.getOutputStream());
inny=new Relay();
outy=new Relay();
outy.Setup(this,clin,out);
inny.Setup(this,in,clout);
outy.start();
inny.start();
//wait
this.suspend();
//clean up
inny.stop();
outy.stop();
clin.close();
clout.close();
cli.close();
--basox;
}
catch(IOException e){System.out.println("Telnet Error\n");e.printStackTrace();}
connection.close();
cont = 0;
break;
case 'p':
StringTokenizer tokens=new StringTokenizer(comm);
tokens.nextToken();
DatagramSocket client;
InetAddress ia = InetAddress.getByName
(tokens.nextToken());
byte msg[] = ("hi" +"\0").getBytes();
int retry=4;
try{
client=new DatagramSocket();
DatagramPacket respond=new DatagramPacket
(new byte[100],100);
DatagramPacket send=new DatagramPacket
(msg,msg.length,ia,7);
client.setSoTimeout(3000);
for(int j=1;j<=retry;j++){
client.send(send);
client.receive(respond);
out.writeBytes(respond.getAddress().
getHostName());
out.writeBytes(" said: ");
out.writeBytes(new String
(respond.getData(),0,
respond.getLength()));
out.writeBytes("\n");
out.writeByte(0x0d);
}//for
client.close();
}
catch(SocketException e){e.printStackTrace();}
catch(InterruptedIOException e){out.writeUTF("TimeOut\n");}
catch(IOException e){out.writeUTF("Error\n");}
break;
default:
out.writeChars("Not a command, try help\n");
out.writeByte(0x0d);
break;
}//switch
}//while
}//try
catch(IOException e){
System.out.println("X");e.printStackTrace();}
}//run
}//ConSocket
               (
geocities.com/siliconvalley/way)                   (
geocities.com/siliconvalley)