From 5a93123d125d217388b674b83d2b21f703c018a5 Mon Sep 17 00:00:00 2001 From: Martijn Vegter Date: Thu, 28 Dec 2023 22:40:35 +0100 Subject: [PATCH] Introduce JSchProxyException to replace generic JschException in Proxy implementations --- .../com/jcraft/jsch/JSchProxyException.java | 39 +++++++++++++++++++ src/main/java/com/jcraft/jsch/ProxyHTTP.java | 2 +- .../java/com/jcraft/jsch/ProxySOCKS4.java | 10 ++--- .../java/com/jcraft/jsch/ProxySOCKS5.java | 8 ++-- 4 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/jcraft/jsch/JSchProxyException.java diff --git a/src/main/java/com/jcraft/jsch/JSchProxyException.java b/src/main/java/com/jcraft/jsch/JSchProxyException.java new file mode 100644 index 00000000..88f8d99f --- /dev/null +++ b/src/main/java/com/jcraft/jsch/JSchProxyException.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2002-2018 ymnk, JCraft,Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided with + * the distribution. + * + * 3. The names of the authors may not be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JCRAFT, INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.jcraft.jsch; + +public class JSchProxyException extends JSchException { + private static final long serialVersionUID = -1L; + + public JSchProxyException(String s) { + super(s); + } + + public JSchProxyException(String s, Throwable e) { + super(s, e); + } +} diff --git a/src/main/java/com/jcraft/jsch/ProxyHTTP.java b/src/main/java/com/jcraft/jsch/ProxyHTTP.java index 028cf830..e69c5b9d 100644 --- a/src/main/java/com/jcraft/jsch/ProxyHTTP.java +++ b/src/main/java/com/jcraft/jsch/ProxyHTTP.java @@ -163,7 +163,7 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim } catch (Exception eee) { } String message = "ProxyHTTP: " + e.toString(); - throw new JSchException(message, e); + throw new JSchProxyException(message, e); } } diff --git a/src/main/java/com/jcraft/jsch/ProxySOCKS4.java b/src/main/java/com/jcraft/jsch/ProxySOCKS4.java index aad0c017..69ab8f42 100644 --- a/src/main/java/com/jcraft/jsch/ProxySOCKS4.java +++ b/src/main/java/com/jcraft/jsch/ProxySOCKS4.java @@ -120,7 +120,7 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim buf[index++] = byteAddress[i]; } } catch (UnknownHostException uhe) { - throw new JSchException("ProxySOCKS4: " + uhe.toString(), uhe); + throw new JSchProxyException("ProxySOCKS4: " + uhe.toString(), uhe); } if (user != null) { @@ -156,12 +156,12 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim while (s < len) { int i = in.read(buf, s, len - s); if (i <= 0) { - throw new JSchException("ProxySOCKS4: stream is closed"); + throw new JSchProxyException("ProxySOCKS4: stream is closed"); } s += i; } if (buf[0] != 0) { - throw new JSchException("ProxySOCKS4: server returns VN " + buf[0]); + throw new JSchProxyException("ProxySOCKS4: server returns VN " + buf[0]); } if (buf[1] != 90) { try { @@ -169,7 +169,7 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim } catch (Exception eee) { } String message = "ProxySOCKS4: server returns CD " + buf[1]; - throw new JSchException(message); + throw new JSchProxyException(message); } } catch (RuntimeException e) { throw e; @@ -179,7 +179,7 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim socket.close(); } catch (Exception eee) { } - throw new JSchException("ProxySOCKS4: " + e.toString(), e); + throw new JSchProxyException("ProxySOCKS4: " + e.toString(), e); } } diff --git a/src/main/java/com/jcraft/jsch/ProxySOCKS5.java b/src/main/java/com/jcraft/jsch/ProxySOCKS5.java index b1b556dc..2160ce50 100644 --- a/src/main/java/com/jcraft/jsch/ProxySOCKS5.java +++ b/src/main/java/com/jcraft/jsch/ProxySOCKS5.java @@ -177,7 +177,7 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim socket.close(); } catch (Exception eee) { } - throw new JSchException("fail in SOCKS5 proxy"); + throw new JSchProxyException("fail in SOCKS5 proxy"); } /* @@ -239,7 +239,7 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim socket.close(); } catch (Exception eee) { } - throw new JSchException("ProxySOCKS5: server returns " + buf[1]); + throw new JSchProxyException("ProxySOCKS5: server returns " + buf[1]); } switch (buf[3] & 0xff) { @@ -268,7 +268,7 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim } catch (Exception eee) { } String message = "ProxySOCKS5: " + e.toString(); - throw new JSchException(message, e); + throw new JSchProxyException(message, e); } } @@ -312,7 +312,7 @@ private void fill(InputStream in, byte[] buf, int len) throws JSchException, IOE while (s < len) { int i = in.read(buf, s, len - s); if (i <= 0) { - throw new JSchException("ProxySOCKS5: stream is closed"); + throw new JSchProxyException("ProxySOCKS5: stream is closed"); } s += i; }