add src files decompiled via jad
[rsa2client.git] / src / mcsClient / McsTcpConnection.java
1 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.kpdus.com/jad.html
3 // Decompiler options: packimports(3) fieldsfirst nonlb space 
4
5 package mcsClient;
6
7 import Acme.Crypto.DesCipher;
8 import java.io.*;
9 import java.net.*;
10 import java.util.Arrays;
11 import javax.swing.JFrame;
12 import javax.swing.JOptionPane;
13
14 public class McsTcpConnection {
15
16     private DataInputStream socketInput;
17     private DataOutputStream socketOutput;
18     private boolean detectedPPP;
19     private int localPort;
20     private InetAddress localAddress;
21     Socket connection;
22     private boolean connected;
23     private boolean encrypt;
24     public DesCipher cipher1;
25     public DesCipher cipher2;
26     private boolean debug;
27
28     public McsTcpConnection(String s, int i, boolean flag, String s1) {
29         socketInput = null;
30         socketOutput = null;
31         detectedPPP = false;
32         connection = null;
33         connected = false;
34         debug = false;
35         Object obj = null;
36         Object obj1 = null;
37         Object obj2 = null;
38         encrypt = flag;
39         try {
40             connection = new Socket(s, i);
41             connection.setTcpNoDelay(true);
42             connection.setReceiveBufferSize(0x20000);
43             NetworkInterface networkinterface = NetworkInterface.getByInetAddress(connection.getLocalAddress());
44             if (networkinterface.getDisplayName().indexOf("PPP") != -1 || networkinterface.getDisplayName().indexOf("ppp") != -1)
45                 detectedPPP = true;
46             localPort = connection.getLocalPort();
47             localAddress = connection.getLocalAddress();
48             java.io.InputStream inputstream = connection.getInputStream();
49             java.io.OutputStream outputstream = connection.getOutputStream();
50             System.out.println("Connected via socket: " + connection);
51             socketInput = new DataInputStream(new BufferedInputStream(inputstream));
52             socketOutput = new DataOutputStream(new BufferedOutputStream(outputstream));
53             connected = true;
54             if (flag) {
55                 byte abyte0[] = new byte[8];
56                 byte abyte1[] = {
57                     34, 17, 31, 76, 113, 30, 91, 115
58                 };
59                 for (int j = 0; j < 8; j++) {
60                     abyte0[j] = (byte)s1.toCharArray()[j];
61                     abyte1[j] = (byte)(abyte1[j] ^ (byte)s1.toCharArray()[j]);
62                 }
63
64                 if (debug)
65                     System.out.println("mcsTcpConnection: accessCode = " + s1);
66                 cipher1 = new DesCipher(abyte0);
67                 cipher2 = new DesCipher(abyte1);
68             }
69         }
70         catch (UnknownHostException unknownhostexception) {
71             close();
72             System.err.println(unknownhostexception);
73             JOptionPane.showMessageDialog(new JFrame(), "Unknown host: " + s, "Remote Console Connection Error", 0);
74         }
75         catch (ConnectException connectexception) {
76             close();
77             System.err.println(connectexception);
78             JOptionPane.showMessageDialog(new JFrame(), "Unable to connect to to " + s + ":" + i + ".", "Remote Console Connection Error", 0);
79         }
80         catch (IOException ioexception) {
81             close();
82             System.err.println(ioexception);
83             JOptionPane.showMessageDialog(new JFrame(), "Connection to " + s + " lost.", "Remote Console Connection Error", 0);
84         }
85     }
86
87     boolean getDetectedPPP() {
88         return detectedPPP;
89     }
90
91     DataInputStream getSocketInput() {
92         return socketInput;
93     }
94
95     int getLocalPort() {
96         return localPort;
97     }
98
99     InetAddress getLocalAddress() {
100         return localAddress;
101     }
102
103     public synchronized void sendMessage(int ai[], boolean flag) throws IOException {
104         if (encrypt) {
105             int l = ai.length * 4;
106             byte abyte0[] = new byte[l];
107             int i1 = 0;
108             for (int i = 0; i < ai.length; i++) {
109                 int k = i * 4;
110                 abyte0[k] = (byte)((ai[i] & 0xff000000) >> 24);
111                 abyte0[k + 1] = (byte)((ai[i] & 0xff0000) >> 16);
112                 abyte0[k + 2] = (byte)((ai[i] & 0xff00) >> 8);
113                 abyte0[k + 3] = (byte)(ai[i] & 0xff);
114             }
115
116             if (l % 8 > 0)
117                 i1 = 8 - l % 8;
118             int j1 = (l + i1) / 8;
119             byte abyte1[] = new byte[j1 * 8];
120             System.arraycopy(abyte0, 0, abyte1, 0, l);
121             if (i1 > 0) {
122                 abyte1[abyte1.length - 1] = (byte)i1;
123                 if (i1 > 1)
124                     Arrays.fill(abyte1, abyte1.length - i1, abyte1.length - 2, (byte)0);
125             }
126             try {
127                 socketOutput.write(encrypt(abyte1, abyte1.length), 0, abyte1.length);
128                 if (flag)
129                     socketOutput.flush();
130             }
131             catch (IOException ioexception1) {
132                 close();
133                 throw ioexception1;
134             }
135         } else {
136             try {
137                 for (int j = 0; j < ai.length; j++)
138                     socketOutput.writeInt(ai[j]);
139
140                 if (flag)
141                     socketOutput.flush();
142             }
143             catch (IOException ioexception) {
144                 close();
145                 throw ioexception;
146             }
147         }
148     }
149
150     public synchronized void sendMessage(int ai[]) throws IOException {
151         sendMessage(ai, true);
152     }
153
154     public void close() {
155         connected = false;
156         try {
157             if (connection != null) {
158                 connection.close();
159                 connection = null;
160             }
161         }
162         catch (IOException ioexception) {
163             System.err.println("Socket already closed.");
164         }
165     }
166
167     public boolean connected() {
168         return connected;
169     }
170
171     public boolean videoEncrypted() {
172         return encrypt;
173     }
174
175     public byte[] decrypt(byte abyte0[], int i, int j) {
176         for (int k = j; k < i + j; k += 8) {
177             cipher1.decrypt(abyte0, k, abyte0, k);
178             cipher2.encrypt(abyte0, k, abyte0, k);
179             cipher1.decrypt(abyte0, k, abyte0, k);
180         }
181
182         return abyte0;
183     }
184
185     public byte[] encrypt(byte abyte0[], int i) {
186         for (int j = 0; j < i; j += 8) {
187             cipher1.encrypt(abyte0, j, abyte0, j);
188             cipher2.decrypt(abyte0, j, abyte0, j);
189             cipher1.encrypt(abyte0, j, abyte0, j);
190         }
191
192         return abyte0;
193     }
194 }