public interface Handler { public void handle(); }
public class Proxy { public static final int DEFAULT_PORT = 1080; public static final int DEFAULT_VERSION = 0x05; private Socket socket; private ThreadPoolExecutor ioexecutor; private int version; private int method; public Proxy(String host) throws IOException { this(host, DEFAULT_PORT); } public Proxy(String host, int port) throws IOException { this.socket = new Socket(host, port); this.ioexecutor = new ThreadPoolExecutor(8, 50, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); this.version = DEFAULT_VERSION; } private void initConfig(Properties conf) { String ver = conf.getProperty("ver"); if (ver != null) { version = Integer.parseInt(ver); } } public void init(Properties conf) throws IOException { initConfig(conf); Nego nego = new Nego(this); String methods = conf.getProperty("methods"); if (methods != null) { String[] ss = methods.split(" "); for (String s : ss) { nego.setMethod(Integer.parseInt(s)); } } nego.setHandler(new NegotiationReplyHandler(this)); ioexecutor.execute(nego); } public int version() { return version; } public OutputStream getOutputStream() throws IOException { return socket.getOutputStream(); } public InputStream getInputStream() throws IOException { return socket.getInputStream(); } public void setMethod(int method) { this.method = method; } public int getMethod() { return method; } /** * negotiation reply message handler */ private static class NegotiationReplyHandler implements Handler { private Proxy proxy; public NegotiationReplyHandler(Proxy proxy) { this.proxy = proxy; } @Override public void handle() { try { InputStream is = proxy.getInputStream(); ByteArrayOutputStream rs = new ByteArrayOutputStream(); byte[] bytes = new byte[128]; int nbyte = -1; while ((nbyte = is.read(bytes)) != -1) { rs.write(bytes, 0, nbyte); if (rs.size() == 2) { break; } } bytes = rs.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); bis.read(); int method = bis.read(); System.out.println("method selected: " + method); proxy.setMethod(method); } catch (IOException e) { e.printStackTrace(); } } } }
public class Nego extends Thread { private Proxy proxy; private Set<Integer> methods; private Handler handler; public Nego(Proxy proxy) { this.proxy = proxy; this.methods = new HashSet<Integer>(); } public void setMethods(Set<Integer> newmethods) { methods.addAll(newmethods); } public void setMethod(int method) { methods.add(method); } public void setHandler(Handler handler) { this.handler = handler; } private void write(ByteArrayOutputStream buffer) throws IOException { OutputStream os = proxy.getOutputStream(); os.write(buffer.toByteArray()); os.flush(); } public void run() { if (handler == null) { throw new NullPointerException("handler is null"); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write((proxy.version() << 24) >> 24); if (methods.isEmpty()) { methods.add(0x00); } bos.write((methods.size() << 24) >> 24); for (Integer method : methods) { bos.write((method << 24) >> 24); } try { write(bos); } catch (IOException e) { e.printStackTrace(); } handler.handle(); } }
// private static String host = "198.41.0.4"; private static String host = "localhost"; private static int port = 1080; private static Proxy proxy; @BeforeClass public static void initialize() throws IOException { proxy = new Proxy(host, port); Properties conf = new Properties(); // conf.setProperty("methods", "0 1 2 3 255"); proxy.init(conf); } @Test public void test() throws IOException, InterruptedException { Thread.sleep(10000); }
相关推荐
随便写的代码,大家随便看看
扫雷最简单的做法,大家来看看 内有代码。 扫雷游戏
闲来无事,上来看看,每次来都有点愧疚的感觉,老是索取代码提问,今天把我写的一个网络录音的程序放上来,本来是监视我女友用来的,我就随便写了写,录音部分是参照vckbase的录音api代码,结合了一小段socket(TCP)就可以...
1.手机先建立一个wifi网络 ssid随便写一个就ok 选中设备到设备 在填写个ip地址,跟子网掩码跟笔记本一个网段就行了 2.vs写个Server段程序,服务端负责接受客户端传过来的文件名,跟文件数据 接收文件名的代码如下 ...
然后,想想这样一个问题-----如何在已存在的文件中特定的位置上不定期地插入一段代码?(如:在典型的SSH框架的xml配置文件中,陆陆续续的添加Action,Domain,Service) 第一种方法: 解析这个文件,定位到插入位置,插入...
因为硬盘上有几个版本,懒得去看到底哪一个是图形识别版本或者内存扫描版本了,随便上传了一个,除了识别部分,其他差别不大,内存扫描版本也会遗留有图形识别的代码。有兴趣的可以研究其中的图形模糊识别定位以及...
没错就是万恶的8086汇编. 介绍不知道怎么写,随便贴一段代码吧.懂得人也大概知道这是大概什么样的了
这时候后如果我们在软件的注册失败的那段代码前面加一个跳转,说得容易理解一点就是:跳过软件注册码验证失败后应该运行的分支,而将它转到注册成功的那个分支上。那么这时候不管你输入的注册码是对是错,都会提示...
前言 快捷方式是一种特殊的文件,扩展名为 lnk。...这是最方便的方式了,因为这段代码随便放到一段代码中就能运行: /// /// 为当前正在运行的程序创建一个快捷方式。 /// /// 快捷方式的完全限定路径
PC-Lint是C/C++软件代码静态分析工具,你可以把它看作是一种更加严格的编译器。它不仅可以检查出一般的语法错误,还可以检查出那些虽然符合语法要求但不易发现的潜在错误。 C语言的灵活性带来了代码效率的提升,但...
第二,也就是代码的内容,这个全世界的影视作品都差不多,随便找一段代码粘上。战狼也不例外,东拼西凑了一些C语言代码。 只是战狼这个实在是太简单了,小编都能看懂,是粘贴的《C语言入门100例》中的一例,内容是...
这是一个纯java写的魔兽对战平台.不抓包.原理非常简单. 可以聊天,可以发布自己的魔兽主机.有人建立主机或者人满了也会有声音提示. 学习网络编程.非常好! 里面有许多注释.应该能看懂. 关于原理.博客上有详细介绍....
有的时候不知道自己的数据库连接是否存在问题的时候,可以用一小段代码进行测试下。这个测试代码较少,主要就是测试数据库的连接。如果输出“连接成功”说明没问题(注:这是连接sql server 数据库的代码,连接前先...
闲来无事,上来看看,每次来都有点愧疚的感觉,老是索取代码提问,今天把我写的一个网络录音的程序放上来,本来是监视我女友用来的,我就随便写了写,录音部分是参照 vckbase 的录音api代码 ,结合了一小段 socket(TCP) 就...
这个跟我在第二节里介绍的是一模一样的(第二节第10行),还有数据的处理函数,数据发送函数,判断连接已断开的代码与第二节也是一模一样的,不过在这里我们需要额外的添加一段代码,当判断出连接已断开的时候,我们要...
自己随便写的 用C语言实现的凯撒密码的加密解密小程序 密钥可以为任意整数 明文密文可以任意字符 含实验报告
3)但是过了一段时间,老板又跑过来和xx说,这一千个方法又要调用日志打印的方法功能,同时又要添加用户合法性验证。这个时候xx有两种选择: 1)对老板说:滚。 2)使用aop技术来实现这个功能,以后老板随便加都可以...
这是一个纯java写的魔兽对战平台.不抓包.原理非常简单. 可以聊天,可以发布自己的魔兽主机.有人建立主机或者人满了也会有声音提示. 主机是自动发布的,不需要客服端搜索。 也不需要连、接转。整个过程就是一个局域网...
在淘宝上随便找一个做毕设的都要好多钱的,而且客户本身的代码能力、数学、逻辑能力都很差,导致我每行都给注释以及看不懂,在我交付代码后又纠缠了我一个多礼拜。反正总体做下来的感觉就是烦躁。头一次感觉到了客户...
没有用哦,只是一小段代码,随便上传的,谢谢