1 | package com.bowman.cardserv;
|
---|
2 |
|
---|
3 | import com.bowman.cardserv.crypto.DESUtil;
|
---|
4 | import com.bowman.cardserv.util.ProxyLogger;
|
---|
5 | import com.bowman.cardserv.interfaces.CamdConstants;
|
---|
6 |
|
---|
7 | import java.io.*;
|
---|
8 | import java.net.*;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Created by IntelliJ IDEA.
|
---|
12 | * User: bowman
|
---|
13 | * Date: Oct 15, 2005
|
---|
14 | * Time: 1:35:22 PM
|
---|
15 | */
|
---|
16 | public class NewcamdConnection {
|
---|
17 |
|
---|
18 | private int sequenceNr = 0, maxSize = CamdConstants.CWS_NETMSGSIZE;
|
---|
19 | private Socket conn;
|
---|
20 | private DataInputStream is;
|
---|
21 | private BufferedOutputStream os;
|
---|
22 | private byte[] desKey16;
|
---|
23 | private long lastTrafficTimeStamp = System.currentTimeMillis();
|
---|
24 |
|
---|
25 | private CamdNetMessage lastSent;
|
---|
26 | private CamdNetMessage lastRead;
|
---|
27 |
|
---|
28 | private boolean noEncrypt = false;
|
---|
29 | private boolean logDupes = false;
|
---|
30 |
|
---|
31 | private ProxyLogger logger;
|
---|
32 | private String traceLabel;
|
---|
33 |
|
---|
34 | public NewcamdConnection(Socket conn, boolean noEncrypt, boolean logDupes) {
|
---|
35 | this(conn, noEncrypt, logDupes, null);
|
---|
36 | }
|
---|
37 |
|
---|
38 | private NewcamdConnection(Socket conn, boolean noEncrypt, boolean logDupes, String traceLabel) {
|
---|
39 | this.conn = conn;
|
---|
40 | this.noEncrypt = noEncrypt;
|
---|
41 | this.logDupes = logDupes;
|
---|
42 | this.traceLabel = traceLabel;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void setDesKey16(byte[] desKey16) {
|
---|
46 | this.desKey16 = desKey16;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void init(ProxyLogger logger) throws IOException {
|
---|
50 | this.logger = logger;
|
---|
51 | if(noEncrypt) logger.warning("Newcamd protocol encryption is disabled.");
|
---|
52 | is = new DataInputStream(conn.getInputStream());
|
---|
53 | os = new BufferedOutputStream(conn.getOutputStream());
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void clientHandshake(int timeout, byte[] configKey14) throws IOException {
|
---|
57 | os.write(generateLoginKey(configKey14));
|
---|
58 | os.flush();
|
---|
59 | conn.setSoTimeout(timeout);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void serverHandshake(byte[] configKey14) throws IOException {
|
---|
63 | byte[] random14 = new byte[14];
|
---|
64 | is.readFully(random14);
|
---|
65 | desKey16 = DESUtil.desKeySpread((DESUtil.xorKey(configKey14, random14))); // loginKey
|
---|
66 | logger.finest("Read random 14: " + DESUtil.bytesToString(random14));
|
---|
67 | logger.finest("Des key spread: " + DESUtil.bytesToString(desKey16));
|
---|
68 | }
|
---|
69 |
|
---|
70 | private byte[] generateLoginKey(byte[] configKey14) {
|
---|
71 | byte[] random = DESUtil.getRandomBytes(14);
|
---|
72 | desKey16 = DESUtil.desKeySpread(DESUtil.xorKey(configKey14, random)); // make the key
|
---|
73 | return random; // return the random 14 bytes to be sent back to the client
|
---|
74 | }
|
---|
75 |
|
---|
76 | public CamdNetMessage readMessage() throws IOException {
|
---|
77 | if(is == null) return null;
|
---|
78 | int len = is.read();
|
---|
79 | if(len == -1) {
|
---|
80 | close();
|
---|
81 | return null;
|
---|
82 | } else {
|
---|
83 | len *= 256;
|
---|
84 | len += is.read();
|
---|
85 | logger.finest("Incoming message length: " + len + " bytes");
|
---|
86 |
|
---|
87 | if(len < 0 || len > maxSize) throw new IOException("Bad length: " + len);
|
---|
88 | byte[] data = new byte[len];
|
---|
89 | is.readFully(data);
|
---|
90 |
|
---|
91 | byte[] decrypted;
|
---|
92 | if(noEncrypt) decrypted = data;
|
---|
93 | else decrypted = DESUtil.desDecrypt(data, len, desKey16);
|
---|
94 |
|
---|
95 | if(decrypted == null) throw new IOException("Decryption failed, bad checksum.");
|
---|
96 | else {
|
---|
97 | CamdNetMessage msg = CamdNetMessage.parseNewcamd(decrypted, getRemoteAddress());
|
---|
98 | logger.finer("Received message: " + msg.toString());
|
---|
99 | logger.finest("Decrypted message payload (" + decrypted.length + " bytes) -> " + DESUtil.bytesToString(decrypted));
|
---|
100 | if(traceLabel != null) System.out.println("Recv [" + traceLabel + "] " + DESUtil.bytesToString(decrypted));
|
---|
101 | lastTrafficTimeStamp = System.currentTimeMillis();
|
---|
102 |
|
---|
103 | if(logDupes) {
|
---|
104 | // check for duplicate ecm replies, newcs bug?
|
---|
105 | if(lastRead != null && lastRead.getSequenceNr() != 0 && lastRead.getSequenceNr() == msg.getSequenceNr()) {
|
---|
106 | if(lastRead.isEcm() && msg.isEcm()) {
|
---|
107 | long time = msg.getTimeStamp() - lastRead.getTimeStamp();
|
---|
108 | logger.warning("Duplicate newcamd message received.\n " + time + "\tPrevious: " + lastRead + "\n\tCurrent: " + msg);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | lastRead = msg;
|
---|
114 | return msg;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public int sendMessage(CamdNetMessage msg) {
|
---|
120 | return sendMessage(msg, -1, true);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public synchronized int sendMessage(CamdNetMessage msg, int seq, boolean flush) {
|
---|
124 | if(logger == null || desKey16 == null) return -1; // connection not yet initialized
|
---|
125 | if(seq == -1) seq = sequenceNr++;
|
---|
126 | msg.setSequenceNr(seq);
|
---|
127 |
|
---|
128 | logger.finer("Sending message " + msg.hashCodeStr() + ": " + msg.toString());
|
---|
129 | if(sequenceNr > 0xFFFF) sequenceNr = 0;
|
---|
130 |
|
---|
131 | byte[] fixedData = msg.getFixedData();
|
---|
132 | byte[] customData = msg.getCustomData();
|
---|
133 | byte[] buffer = new byte[13 + customData.length];
|
---|
134 |
|
---|
135 | System.arraycopy(fixedData, 0, buffer, 0, 10);
|
---|
136 | buffer[10] = (byte)msg.getCommandTag();
|
---|
137 | buffer[11] = (byte)(customData.length >> 8);
|
---|
138 | buffer[11] |= msg.getUpperBits();
|
---|
139 | buffer[12] = (byte)(customData.length & 0xFF);
|
---|
140 | System.arraycopy(customData, 0, buffer, 13, customData.length);
|
---|
141 |
|
---|
142 | logger.finest("Assembled message (" + buffer.length + " bytes) -> " + DESUtil.bytesToString(buffer));
|
---|
143 | if(traceLabel != null) System.out.println("Sent [" + traceLabel + "] " + DESUtil.bytesToString(buffer));
|
---|
144 | byte[] encrypted;
|
---|
145 | if(noEncrypt) encrypted = buffer;
|
---|
146 | else {
|
---|
147 | encrypted = DESUtil.desEncrypt(buffer, buffer.length, desKey16, maxSize);
|
---|
148 | logger.finest("Encrypted message (" + encrypted.length + " bytes) -> " + DESUtil.bytesToString(encrypted));
|
---|
149 | }
|
---|
150 |
|
---|
151 | try {
|
---|
152 | os.write(encrypted.length >> 8);
|
---|
153 | os.write(encrypted.length & 0xFF);
|
---|
154 | os.write(encrypted);
|
---|
155 | if(flush) {
|
---|
156 | os.flush();
|
---|
157 | lastTrafficTimeStamp = System.currentTimeMillis();
|
---|
158 | }
|
---|
159 | msg.setSent(getRemoteAddress(), buffer, "Newcamd");
|
---|
160 |
|
---|
161 | if(logDupes) {
|
---|
162 | // check for sent ecm/dcw duplicates
|
---|
163 | if(lastSent != null && lastSent.getSequenceNr() != 0 && lastSent.getSequenceNr() == seq) {
|
---|
164 | if(lastSent.isEcm() && msg.isEcm()) {
|
---|
165 | long time = msg.getTimeStamp() - lastSent.getTimeStamp();
|
---|
166 | logger.warning("Duplicate newcamd message sent.\n " + time + "\tPrevious: " + lastSent + "\n\tCurrent: " + msg);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | lastSent = msg;
|
---|
172 | return seq;
|
---|
173 | } catch(IOException e) {
|
---|
174 | logger.throwing("Connection closed while sending", e);
|
---|
175 | close();
|
---|
176 | }
|
---|
177 | return -1;
|
---|
178 | }
|
---|
179 |
|
---|
180 | public int sendMessage(CamdNetMessage msg, int seq, byte[] desKey16) { // send and switch key afterwards
|
---|
181 | seq = sendMessage(msg, seq, false);
|
---|
182 | this.desKey16 = desKey16;
|
---|
183 | try {
|
---|
184 | os.flush();
|
---|
185 | lastTrafficTimeStamp = System.currentTimeMillis();
|
---|
186 | return seq;
|
---|
187 | } catch(IOException e) {
|
---|
188 | logger.throwing("Connection closed while sending", e);
|
---|
189 | close();
|
---|
190 | }
|
---|
191 | return -1;
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void close() {
|
---|
195 | try {
|
---|
196 | if(conn != null) conn.close();
|
---|
197 | conn = null;
|
---|
198 | } catch(IOException e) {
|
---|
199 | if(logger != null) logger.throwing("Exception while closing", e);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | public boolean isConnected() {
|
---|
204 | return conn != null;
|
---|
205 | }
|
---|
206 |
|
---|
207 | public boolean isInitialized() {
|
---|
208 | return logger != null;
|
---|
209 | }
|
---|
210 |
|
---|
211 | public long getLastTrafficTimeStamp() {
|
---|
212 | return lastTrafficTimeStamp;
|
---|
213 | }
|
---|
214 |
|
---|
215 | public synchronized String getRemoteAddress() {
|
---|
216 | if(isConnected()) return conn.getInetAddress().getHostAddress();
|
---|
217 | else return "0.0.0.0";
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void setSoTimeout(int soTimeout) throws SocketException {
|
---|
221 | if(isConnected()) conn.setSoTimeout(soTimeout);
|
---|
222 | }
|
---|
223 |
|
---|
224 | public void setSequenceNr(int sequenceNr) {
|
---|
225 | this.sequenceNr = sequenceNr;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public void setLogger(ProxyLogger logger) {
|
---|
229 | this.logger = logger;
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void setMaxSize(int maxSize) {
|
---|
233 | this.maxSize = maxSize;
|
---|
234 | }
|
---|
235 |
|
---|
236 | public void setTraceLabel(String traceLabel) {
|
---|
237 | this.traceLabel = traceLabel;
|
---|
238 | }
|
---|
239 | }
|
---|