Advertisement
Guest User

Untitled

a guest
Apr 4th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.56 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.ConnectException;
  3. import java.net.InetAddress;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.net.SocketTimeoutException;
  7.  
  8. import javax.net.ServerSocketFactory;
  9.  
  10. import org.apache.http.HttpException;
  11. import org.apache.http.HttpRequest;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.entity.ByteArrayEntity;
  14. import org.apache.http.impl.bootstrap.HttpServer;
  15. import org.apache.http.impl.bootstrap.ServerBootstrap;
  16. import org.apache.http.protocol.HttpContext;
  17. import org.apache.http.protocol.HttpRequestHandler;
  18.  
  19. import net.i2p.I2PException;
  20. import net.i2p.client.I2PSession;
  21. import net.i2p.client.streaming.I2PServerSocket;
  22. import net.i2p.client.streaming.I2PSocket;
  23. import net.i2p.client.streaming.I2PSocketManager;
  24. import net.i2p.client.streaming.I2PSocketManagerFactory;
  25.  
  26. public class HttpTest {
  27.  
  28.     public static void main(String[] argv) throws IOException {
  29.         I2PSocketManager manager = I2PSocketManagerFactory.createManager();
  30.         I2PServerSocket serverSocket = manager.getServerSocket();
  31.         I2PSession session = manager.getSession();
  32.        
  33.         System.out.println("*** Address is: " + session.getMyDestination().toBase32());
  34.        
  35.         final ServerSocketFactory serverSocketFactory = new ServerSocketFactory() {
  36.            
  37.             @Override
  38.             public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
  39.                 return new ServerSocket() {
  40.                     @Override
  41.                     public Socket accept() throws IOException {
  42.                         try {
  43.                             final I2PSocket i2pClientSocket = serverSocket.accept();
  44.                             return new I2PSocketAdapter(i2pClientSocket);
  45.                         } catch (ConnectException | SocketTimeoutException | I2PException e) {
  46.                             throw new IOException(e);
  47.                         }
  48.                     }
  49.                 };
  50.             }
  51.            
  52.             @Override
  53.             public ServerSocket createServerSocket(int port, int backlog) throws IOException {
  54.                 System.out.println("2");
  55.                 return null;
  56.             }
  57.            
  58.             @Override
  59.             public ServerSocket createServerSocket(int port) throws IOException {
  60.                 System.out.println("3");
  61.                 return null;
  62.             }
  63.         };
  64.        
  65.         final HttpServer server = ServerBootstrap.bootstrap()
  66.                 .setServerSocketFactory(serverSocketFactory)
  67.                 .registerHandler("*", new HttpRequestHandler() {
  68.                    
  69.                     @Override
  70.                     public void handle(HttpRequest request, HttpResponse response, HttpContext context)
  71.                             throws HttpException, IOException {
  72.                         response.setEntity(new ByteArrayEntity("Hello World!".getBytes()));
  73.                     }
  74.                 }).create();
  75.         server.start();
  76.     }
  77. }
  78.  
  79.  
  80. import java.io.IOException;
  81. import java.io.InputStream;
  82. import java.io.OutputStream;
  83. import java.net.InetAddress;
  84. import java.net.Socket;
  85. import java.net.SocketAddress;
  86. import java.net.SocketException;
  87. import java.nio.channels.SocketChannel;
  88.  
  89. import net.i2p.client.streaming.I2PSocket;
  90. import net.i2p.client.streaming.I2PSocketAddress;
  91. import net.i2p.client.streaming.I2PSocketOptions;
  92.  
  93. public class I2PSocketAdapter extends Socket {
  94.     private final I2PSocket _socket;
  95.  
  96.     I2PSocketAdapter(I2PSocket socket) {
  97.         _socket = socket;
  98.     }
  99.  
  100.     /**
  101.      *  @throws UnsupportedOperationException always
  102.      */
  103.     @Override
  104.     public void bind(SocketAddress bindpoint) {
  105.         throw new UnsupportedOperationException();
  106.     }
  107.  
  108.     @Override
  109.     public void close() throws IOException {
  110.         if (_socket.isClosed())
  111.             throw new IOException("Already closed");
  112.         _socket.close();
  113.     }
  114.  
  115.     /**
  116.      *  @throws UnsupportedOperationException always
  117.      */
  118.     @Override
  119.     public void connect(SocketAddress endpoint) {
  120.         throw new UnsupportedOperationException();
  121.     }
  122.  
  123.     /**
  124.      *  @throws UnsupportedOperationException always
  125.      */
  126.     @Override
  127.     public void connect(SocketAddress endpoint, int timeout) {
  128.         throw new UnsupportedOperationException();
  129.     }
  130.  
  131.     /**
  132.      *  @return null always, unimplemented
  133.      */
  134.     @Override
  135.     public SocketChannel getChannel() {
  136.         //return _socket.getChannel();
  137.         return null;
  138.     }
  139.  
  140.     /**
  141.      *  @return null always
  142.      */
  143.     @Override
  144.     public InetAddress getInetAddress() {
  145.         return null;
  146.     }
  147.  
  148.     @Override
  149.     public InputStream getInputStream() throws IOException {
  150.         InputStream rv = _socket.getInputStream();
  151.         if (rv != null)
  152.             return rv;
  153.         throw new IOException("No stream");
  154.     }
  155.  
  156.     @Override
  157.     public boolean getKeepAlive() {
  158.         return false;
  159.         /*
  160.         ConnectionOptions opts = (ConnectionOptions) _socket.getOptions();
  161.         if (opts == null)
  162.             return false;
  163.         return opts.getInactivityAction() == ConnectionOptions.INACTIVITY_ACTION_SEND;
  164.         */
  165.     }
  166.  
  167.     /**
  168.      *  @return null always
  169.      */
  170.     @Override
  171.     public InetAddress getLocalAddress() {
  172.         return null;
  173.     }
  174.  
  175.     /**
  176.      *  @return the port or 0 if unknown
  177.      */
  178.     @Override
  179.     public int getLocalPort() {
  180.         return _socket.getLocalPort();
  181.     }
  182.  
  183.     /**
  184.      *  @return an I2PSocketAddress as of 0.9.26; prior to that, returned null
  185.      *  @since implemented in 0.9.26
  186.      */
  187.     @Override
  188.     public SocketAddress getLocalSocketAddress() {
  189.         return new I2PSocketAddress(_socket.getThisDestination(), _socket.getLocalPort());
  190.     }
  191.  
  192.     /**
  193.      *  @return false always
  194.      */
  195.     @Override
  196.     public boolean getOOBInline() {
  197.         return false;
  198.     }
  199.  
  200.     @Override
  201.     public OutputStream getOutputStream() throws IOException {
  202.         OutputStream rv = _socket.getOutputStream();
  203.         if (rv != null)
  204.             return rv;
  205.         throw new IOException("No stream");
  206.     }
  207.  
  208.     /**
  209.      *  @return the port or 0 if unknown
  210.      */
  211.     @Override
  212.     public int getPort() {
  213.         return _socket.getPort();
  214.     }
  215.  
  216.     @Override
  217.     public int getReceiveBufferSize() {
  218.         return 64*1024;
  219.         /*
  220.         ConnectionOptions opts = (ConnectionOptions) _socket.getOptions();
  221.         if (opts == null)
  222.             return 64*1024;
  223.         return opts.getInboundBufferSize();
  224.         */
  225.     }
  226.  
  227.     /**
  228.      *  @return an I2PSocketAddress as of 0.9.26; prior to that, threw UnsupportedOperationException
  229.      *  @since implemented in 0.9.26
  230.      */
  231.     @Override
  232.     public SocketAddress getRemoteSocketAddress() {
  233.         return new I2PSocketAddress(_socket.getPeerDestination(), _socket.getPort());
  234.     }
  235.  
  236.     /**
  237.      *  @return false always
  238.      */
  239.     @Override
  240.     public boolean getReuseAddress() {
  241.         return false;
  242.     }
  243.  
  244.     @Override
  245.     public int getSendBufferSize() {
  246.         return 64 * 1024;
  247.         /*
  248.         ConnectionOptions opts = (ConnectionOptions) _socket.getOptions();
  249.         if (opts == null)
  250.             return 64*1024;
  251.         return opts.getInboundBufferSize();
  252.        
  253.         */
  254.     }
  255.  
  256.     @Override
  257.     public int getSoLinger() {
  258.         I2PSocketOptions opts = _socket.getOptions();
  259.         if (opts == null)
  260.             return -1;
  261.         return -1;  // fixme really?
  262.     }
  263.  
  264.     @Override
  265.     public int getSoTimeout() {
  266.         I2PSocketOptions opts = _socket.getOptions();
  267.         if (opts == null)
  268.             return 0;
  269.         long rv = opts.getReadTimeout();
  270.         // Java Socket: 0 is forever, and we don't exactly have nonblocking
  271.         if (rv > Integer.MAX_VALUE)
  272.             rv = Integer.MAX_VALUE;
  273.         else if (rv < 0)
  274.             rv = 0;
  275.         else if (rv == 0)
  276.             rv = 1;
  277.         return (int) rv;
  278.     }
  279.  
  280.     /**
  281.      *  @return false always
  282.      */
  283.     @Override
  284.     public boolean getTcpNoDelay() {
  285.         // No option yet. See ConnectionDataReceiver
  286.         return false;
  287.     }
  288.  
  289.     /**
  290.      *  @return 0 always
  291.      */
  292.     @Override
  293.     public int getTrafficClass() {
  294.         return 0;
  295.     }
  296.  
  297.     /**
  298.      *  @return true always
  299.      */
  300.     @Override
  301.     public boolean isBound() {
  302.         return true;
  303.     }
  304.  
  305.     @Override
  306.     public boolean isClosed() {
  307.         return _socket.isClosed();
  308.     }
  309.  
  310.     @Override
  311.     public boolean isConnected() {
  312.         return !_socket.isClosed();
  313.     }
  314.  
  315.     @Override
  316.     public boolean isInputShutdown() {
  317.         return _socket.isClosed();
  318.     }
  319.  
  320.     @Override
  321.     public boolean isOutputShutdown() {
  322.         return _socket.isClosed();
  323.     }
  324.  
  325.     /**
  326.      *  @throws UnsupportedOperationException always
  327.      */
  328.     @Override
  329.     public void sendUrgentData(int data) {
  330.         throw new UnsupportedOperationException();
  331.     }
  332.  
  333.     @Override
  334.     public void setKeepAlive(boolean on) {
  335.         /*
  336.         ConnectionOptions opts = (ConnectionOptions) _socket.getOptions();
  337.         if (opts == null)
  338.             return;
  339.         if (on)
  340.             opts.setInactivityAction(ConnectionOptions.INACTIVITY_ACTION_SEND);
  341.         else
  342.             opts.setInactivityAction(ConnectionOptions.INACTIVITY_ACTION_NOOP);  // DISCONNECT?
  343.         */
  344.     }
  345.  
  346.     /**
  347.      *  @throws UnsupportedOperationException if on is true
  348.      */
  349.     @Override
  350.     public void setOOBInline(boolean on) {
  351.         if (on)
  352.             throw new UnsupportedOperationException();
  353.     }
  354.  
  355.     /**
  356.      *  Does nothing.
  357.      */
  358.     @Override
  359.     public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
  360.     }
  361.  
  362.     /**
  363.      *  Does nothing.
  364.      */
  365.     @Override
  366.     public void setReceiveBufferSize(int size) {
  367.     }
  368.  
  369.     /**
  370.      *  Does nothing.
  371.      */
  372.     @Override
  373.     public void setReuseAddress(boolean on) {
  374.     }
  375.  
  376.     /**
  377.      *  Does nothing.
  378.      */
  379.     @Override
  380.     public void setSendBufferSize(int size) {
  381.     }
  382.  
  383.     /**
  384.      *  Does nothing.
  385.      */
  386.     @Override
  387.     public void setSoLinger(boolean on, int linger) {
  388.     }
  389.  
  390.     @Override
  391.     public void setSoTimeout(int timeout) throws SocketException {
  392.         I2PSocketOptions opts = _socket.getOptions();
  393.         if (opts == null)
  394.             throw new SocketException("No options");
  395.         // Java Socket: 0 is forever
  396.         if (timeout == 0)
  397.             timeout = -1;
  398.         opts.setReadTimeout(timeout);
  399.     }
  400.  
  401.     /**
  402.      *  Does nothing.
  403.      */
  404.     @Override
  405.     public void setTcpNoDelay(boolean on) {
  406.     }
  407.  
  408.     /**
  409.      *  Does nothing.
  410.      */
  411.     @Override
  412.     public void setTrafficClass(int tc) {
  413.     }
  414.  
  415.     @Override
  416.     public void shutdownInput() throws IOException {
  417.         close();
  418.     }
  419.  
  420.     @Override
  421.     public void shutdownOutput() throws IOException {
  422.         close();
  423.     }
  424.  
  425.     @Override
  426.     public String toString() {
  427.         return _socket.toString();
  428.     }
  429. }
  430.  
  431.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement