Changeset d04050e


Ignore:
Timestamp:
Nov 17, 2017 8:14:10 PM (3 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
92a26f6
Parents:
80cb62b
Message:

SOCKS: Move constants and client code from i2ptunnel to
a new package in core, in prep for use by SSLEepGet (ticket #1130)
Make SOCKSException extend IOException, which allows some cleanups.
Untested.

Files:
5 added
6 edited
1 moved

Legend:

Unmodified
Added
Removed
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/I2PSOCKSIRCTunnel.java

    r80cb62b rd04050e  
    1515import net.i2p.i2ptunnel.irc.IrcOutboundFilter;
    1616import net.i2p.i2ptunnel.Logging;
     17import net.i2p.socks.SOCKSException;
    1718import net.i2p.util.EventDispatcher;
    1819import net.i2p.util.I2PAppThread;
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/I2PSOCKSTunnel.java

    r80cb62b rd04050e  
    2222import net.i2p.i2ptunnel.I2PTunnelRunner;
    2323import net.i2p.i2ptunnel.Logging;
     24import net.i2p.socks.SOCKSException;
    2425import net.i2p.util.EventDispatcher;
    2526import net.i2p.util.Log;
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/SOCKS4aServer.java

    r80cb62b rd04050e  
    1313import java.net.InetAddress;
    1414import java.net.Socket;
    15 import java.net.SocketException;
    1615import java.util.List;
    1716import java.util.Locale;
     
    2726import net.i2p.data.DataFormatException;
    2827import net.i2p.data.Destination;
     28import static net.i2p.socks.SOCKS4Constants.*;
    2929import net.i2p.util.HexDump;
    3030import net.i2p.util.Log;
     31import net.i2p.socks.SOCKSException;
    3132
    3233/*
     
    287288            } catch (IOException ioe) {}
    288289            throw new SOCKSException("Error in destination format");
    289         } catch (SocketException e) {
    290             try {
    291                 sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
    292             } catch (IOException ioe) {}
    293             throw new SOCKSException("Error connecting ("
    294                                      + e.getMessage() + ")");
    295290        } catch (IOException e) {
    296291            try {
     
    309304        return destSock;
    310305    }
    311 
    312     /*
    313      * Some namespaces to enclose SOCKS protocol codes
    314      */
    315     private static class Command {
    316         private static final int CONNECT = 0x01;
    317         private static final int BIND = 0x02;
    318     }
    319 
    320     private static class Reply {
    321         private static final int SUCCEEDED = 0x5a;
    322         private static final int CONNECTION_REFUSED = 0x5b;
    323     }
    324306}
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/SOCKS5Server.java

    r80cb62b rd04050e  
    1010import java.io.DataInputStream;
    1111import java.io.DataOutputStream;
     12import java.io.InputStream;
    1213import java.io.IOException;
     14import java.io.OutputStream;
    1315import java.net.InetAddress;
    1416import java.net.Socket;
    15 import java.net.SocketException;
    1617import java.net.UnknownHostException;
    1718import java.util.ArrayList;
     
    3233import net.i2p.i2ptunnel.I2PTunnelHTTPClientBase;
    3334import net.i2p.i2ptunnel.I2PTunnel;
     35import static net.i2p.socks.SOCKS5Constants.*;
    3436import net.i2p.util.HexDump;
    3537import net.i2p.util.Log;
     38import net.i2p.socks.SOCKS5Client;
     39import net.i2p.socks.SOCKSException;
    3640
    3741/*
     
    4246 */
    4347class SOCKS5Server extends SOCKSServer {
    44 
    45     private static final int SOCKS_VERSION_5 = 0x05;
    4648
    4749    private boolean setupCompleted = false;
     
    9698     * SOCKS "VER" field has been stripped from the input stream.
    9799     */
    98     private void init(DataInputStream in, DataOutputStream out) throws IOException, SOCKSException {
     100    private void init(DataInputStream in, DataOutputStream out) throws IOException {
    99101        int nMethods = in.readUnsignedByte();
    100102        int method = Method.NO_ACCEPTABLE_METHODS;
     
    130132     * @since 0.8.2
    131133     */
    132     private void verifyPassword(DataInputStream in, DataOutputStream out) throws IOException, SOCKSException {
     134    private void verifyPassword(DataInputStream in, DataOutputStream out) throws IOException {
    133135        int c = in.readUnsignedByte();
    134136        if (c != AUTH_VERSION) {
     
    172174     * has been stripped out of the input/output streams.
    173175     */
    174     private int manageRequest(DataInputStream in, DataOutputStream out) throws IOException, SOCKSException {
     176    private int manageRequest(DataInputStream in, DataOutputStream out) throws IOException {
    175177        int socksVer = in.readUnsignedByte();
    176178        if (socksVer != SOCKS_VERSION_5) {
     
    450452            } catch (IOException ioe) {}
    451453            throw new SOCKSException("Error in destination format");
    452         } catch (SocketException e) {
    453             if (_log.shouldLog(Log.WARN))
    454                 _log.warn("socks error", e);
    455             try {
    456                 sendRequestReply(Reply.HOST_UNREACHABLE, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
    457             } catch (IOException ioe) {}
    458             throw new SOCKSException("Error connecting: " + e);
    459454        } catch (IOException e) {
    460455            if (_log.shouldLog(Log.WARN))
     
    481476     *  @since 0.8.2
    482477     */
    483     private I2PSocket outproxyConnect(I2PSOCKSTunnel tun, String proxy) throws IOException, SOCKSException, DataFormatException, I2PException {
     478    private I2PSocket outproxyConnect(I2PSOCKSTunnel tun, String proxy) throws IOException, I2PException {
    484479        Properties overrides = new Properties();
    485480        overrides.setProperty("option.i2p.streaming.connectDelay", "1000");
     
    489484            throw new SOCKSException("Outproxy not found");
    490485        I2PSocket destSock = tun.createI2PSocket(dest, proxyOpts);
    491         DataOutputStream out = null;
    492         DataInputStream in = null;
     486        OutputStream out = null;
     487        InputStream in = null;
    493488        try {
    494             out = new DataOutputStream(destSock.getOutputStream());
     489            out = destSock.getOutputStream();
     490            in = destSock.getInputStream();
    495491            boolean authAvail = Boolean.parseBoolean(props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_AUTH));
    496492            String configUser =  null;
     
    502498                    configUser =  props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_USER);
    503499                    configPW = props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_PW);
    504                     if (configUser == null || configPW == null)
    505                         authAvail = false;
    506500                }
    507501            }
    508 
    509             // send the init
    510             out.writeByte(SOCKS_VERSION_5);
    511             if (authAvail) {
    512                 out.writeByte(2);
    513                 out.writeByte(Method.NO_AUTH_REQUIRED);
    514                 out.writeByte(Method.USERNAME_PASSWORD);
    515             } else {
    516                 out.writeByte(1);
    517                 out.writeByte(Method.NO_AUTH_REQUIRED);
    518             }
    519             out.flush();
    520 
    521             // read init reply
    522             in = new DataInputStream(destSock.getInputStream());
    523             // is this right or should we not try to do 5-to-4 conversion?
    524             int hisVersion = in.readByte();
    525             if (hisVersion != SOCKS_VERSION_5 /* && addrtype == AddressType.DOMAINNAME */ )
    526                 throw new SOCKSException("SOCKS Outproxy is not Version 5");
    527             //else if (hisVersion != 4)
    528             //    throw new SOCKSException("Unsupported SOCKS Outproxy Version");
    529 
    530             int method = in.readByte();
    531             if (method == Method.NO_AUTH_REQUIRED) {
    532                 // good
    533             } else if (method == Method.USERNAME_PASSWORD) {
    534                 if (authAvail) {
    535                     // send the auth
    536                     out.writeByte(AUTH_VERSION);
    537                     byte[] user = configUser.getBytes("UTF-8");
    538                     byte[] pw = configPW.getBytes("UTF-8");
    539                     out.writeByte(user.length);
    540                     out.write(user);
    541                     out.writeByte(pw.length);
    542                     out.write(pw);
    543                     out.flush();
    544                     // read the auth reply
    545                     if (in.readByte() != AUTH_VERSION)
    546                         throw new SOCKSException("Bad auth version from outproxy");
    547                     if (in.readByte() != AUTH_SUCCESS)
    548                         throw new SOCKSException("Outproxy authorization failure");
    549                 } else {
    550                     throw new SOCKSException("Outproxy requires authorization, please configure username/password");
    551                 }
    552             } else {
    553                 throw new SOCKSException("Outproxy authorization failure");
    554             }
    555 
    556             // send the connect command
    557             out.writeByte(SOCKS_VERSION_5);
    558             out.writeByte(Command.CONNECT);
    559             out.writeByte(0); // reserved
    560             out.writeByte(addressType);
    561             if (addressType == AddressType.IPV4) {
    562                 out.write(InetAddress.getByName(connHostName).getAddress());
    563             } else if (addressType == AddressType.DOMAINNAME) {
    564                 byte[] d = connHostName.getBytes("ISO-8859-1");
    565                 out.writeByte(d.length);
    566                 out.write(d);
    567             } else {
    568                 // shouldn't happen
    569                 throw new SOCKSException("Unknown address type for outproxy?");
    570             }
    571             out.writeShort(connPort);
    572             out.flush();
    573 
    574             // read the connect reply
    575             hisVersion = in.readByte();
    576             if (hisVersion != SOCKS_VERSION_5)
    577                 throw new SOCKSException("Outproxy response is not Version 5");
    578             int reply = in.readByte();
    579             in.readByte();  // reserved
    580             int type = in.readByte();
    581             int count = 0;
    582             if (type == AddressType.IPV4) {
    583                 count = 4;
    584             } else if (type == AddressType.DOMAINNAME) {
    585                 count = in.readUnsignedByte();
    586             } else if (type == AddressType.IPV6) {
    587                 count = 16;
    588             } else {
    589                 throw new SOCKSException("Unsupported address type in outproxy response");
    590             }
    591             byte[] addr = new byte[count];
    592             in.readFully(addr);  // address
    593             in.readUnsignedShort();  // port
    594             if (reply != Reply.SUCCEEDED)
    595                 throw new SOCKSException("Outproxy rejected request, response = " + reply);
    596             // throw away the address in the response
    597             // todo pass the response through?
     502            SOCKS5Client.connect(in, out, connHostName, connPort, configUser, configPW);
    598503        } catch (IOException e) {
    599             try { destSock.close(); } catch (IOException ioe) {}
    600             if (in != null) try { in.close(); } catch (IOException ioe) {}
    601             if (out != null) try { out.close(); } catch (IOException ioe) {}
    602             throw e;
    603         } catch (SOCKSException e) {
    604504            try { destSock.close(); } catch (IOException ioe) {}
    605505            if (in != null) try { in.close(); } catch (IOException ioe) {}
     
    652552                    break;
    653553            } catch (IOException ioe) { break; }
    654             catch (SOCKSException ioe) { break; }
    655554        }
    656555
     
    664563        throw new SOCKSException("End of UDP Processing");
    665564    }
    666 
    667     /*
    668      * Some namespaces to enclose SOCKS protocol codes
    669      */
    670     private static class Method {
    671         private static final int NO_AUTH_REQUIRED = 0x00;
    672         private static final int USERNAME_PASSWORD = 0x02;
    673         private static final int NO_ACCEPTABLE_METHODS = 0xff;
    674     }
    675 
    676     private static class AddressType {
    677         private static final int IPV4 = 0x01;
    678         private static final int DOMAINNAME = 0x03;
    679         private static final int IPV6 = 0x04;
    680     }
    681 
    682     private static class Command {
    683         private static final int CONNECT = 0x01;
    684         private static final int BIND = 0x02;
    685         private static final int UDP_ASSOCIATE = 0x03;
    686     }
    687 
    688     private static class Reply {
    689         private static final int SUCCEEDED = 0x00;
    690         private static final int GENERAL_SOCKS_SERVER_FAILURE = 0x01;
    691         private static final int CONNECTION_NOT_ALLOWED_BY_RULESET = 0x02;
    692         private static final int NETWORK_UNREACHABLE = 0x03;
    693         private static final int HOST_UNREACHABLE = 0x04;
    694         private static final int CONNECTION_REFUSED = 0x05;
    695         private static final int TTL_EXPIRED = 0x06;
    696         private static final int COMMAND_NOT_SUPPORTED = 0x07;
    697         private static final int ADDRESS_TYPE_NOT_SUPPORTED = 0x08;
    698     }
    699 
    700     private static final int AUTH_VERSION = 1;
    701     private static final int AUTH_SUCCESS = 0;
    702     private static final int AUTH_FAILURE = 1;
    703565}
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/SOCKSServer.java

    r80cb62b rd04050e  
    1616import net.i2p.client.streaming.I2PSocket;
    1717import net.i2p.i2ptunnel.I2PTunnelHTTPClientBase;
     18import net.i2p.socks.SOCKSException;
    1819import net.i2p.util.Log;
    1920
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/SOCKSServerFactory.java

    r80cb62b rd04050e  
    1616import net.i2p.data.DataHelper;
    1717import net.i2p.i2ptunnel.I2PTunnelHTTPClientBase;
     18import net.i2p.socks.SOCKSException;
    1819
    1920/**
  • core/java/src/net/i2p/socks/SOCKSException.java

    r80cb62b rd04050e  
    55 * Copyright (c) 2004 by human
    66 */
    7 package net.i2p.i2ptunnel.socks;
     7package net.i2p.socks;
     8
     9import java.io.IOException;
    810
    911/**
     
    1113 *
    1214 * @author human
     15 * @since 0.9.33 moved from net.i2p.i2ptunnel.socks, and changed to extend IOException
    1316 */
    14 public class SOCKSException extends Exception {
     17public class SOCKSException extends IOException {
    1518
    1619    public SOCKSException() {
Note: See TracChangeset for help on using the changeset viewer.