273
|
1 /*
|
|
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
|
|
3 * Please refer to the LICENSE.txt for licensing details.
|
|
4 */
|
|
5 package ch.ethz.ssh2.channel;
|
|
6
|
|
7 import java.io.IOException;
|
|
8 import java.net.Socket;
|
|
9
|
|
10 import ch.ethz.ssh2.log.Logger;
|
|
11
|
|
12 /**
|
|
13 * RemoteAcceptThread.
|
|
14 *
|
|
15 * @author Christian Plattner
|
|
16 * @version $Id: RemoteAcceptThread.java 119 2014-04-12 20:30:58Z dkocher@sudo.ch $
|
|
17 */
|
307
|
18 public class RemoteAcceptThread extends Thread {
|
|
19 private static final Logger log = Logger.getLogger(RemoteAcceptThread.class);
|
273
|
20
|
307
|
21 Channel c;
|
273
|
22
|
307
|
23 String remoteConnectedAddress;
|
|
24 int remoteConnectedPort;
|
|
25 String remoteOriginatorAddress;
|
|
26 int remoteOriginatorPort;
|
|
27 String targetAddress;
|
|
28 int targetPort;
|
273
|
29
|
307
|
30 Socket s;
|
273
|
31
|
307
|
32 public RemoteAcceptThread(Channel c, String remoteConnectedAddress, int remoteConnectedPort,
|
|
33 String remoteOriginatorAddress, int remoteOriginatorPort, String targetAddress, int targetPort) {
|
|
34 this.c = c;
|
|
35 this.remoteConnectedAddress = remoteConnectedAddress;
|
|
36 this.remoteConnectedPort = remoteConnectedPort;
|
|
37 this.remoteOriginatorAddress = remoteOriginatorAddress;
|
|
38 this.remoteOriginatorPort = remoteOriginatorPort;
|
|
39 this.targetAddress = targetAddress;
|
|
40 this.targetPort = targetPort;
|
|
41 log.debug("RemoteAcceptThread: " + remoteConnectedAddress + "/" + remoteConnectedPort + ", R: "
|
|
42 + remoteOriginatorAddress + "/" + remoteOriginatorPort);
|
|
43 }
|
273
|
44
|
307
|
45 @Override
|
|
46 public void run() {
|
|
47 try {
|
|
48 c.cm.sendOpenConfirmation(c);
|
|
49 s = new Socket(targetAddress, targetPort);
|
|
50 StreamForwarder r2l = new StreamForwarder(c, null, null, c.getStdoutStream(), s.getOutputStream(),
|
|
51 "RemoteToLocal");
|
|
52 StreamForwarder l2r = new StreamForwarder(c, null, null, s.getInputStream(), c.getStdinStream(),
|
|
53 "LocalToRemote");
|
|
54 /* No need to start two threads, one can be executed in the current thread */
|
|
55 r2l.setDaemon(true);
|
|
56 r2l.start();
|
|
57 l2r.run();
|
273
|
58
|
307
|
59 while (r2l.isAlive()) {
|
|
60 try {
|
|
61 r2l.join();
|
|
62 }
|
|
63 catch (InterruptedException ignored) {
|
|
64 }
|
|
65 }
|
273
|
66
|
307
|
67 /* If the channel is already closed, then this is a no-op */
|
|
68 c.cm.closeChannel(c, "EOF on both streams reached.", true);
|
|
69 s.close();
|
|
70 }
|
|
71 catch (IOException e) {
|
|
72 log.warning("IOException in proxy code: " + e.getMessage());
|
273
|
73
|
307
|
74 try {
|
|
75 c.cm.closeChannel(c, e, true);
|
|
76 }
|
|
77 catch (IOException ignored) {
|
|
78 }
|
|
79
|
|
80 try {
|
|
81 if (s != null)
|
|
82 s.close();
|
|
83 }
|
|
84 catch (IOException ignored) {
|
|
85 }
|
|
86 }
|
|
87 }
|
273
|
88 }
|