import java.io.*;
import java.util.*;
import java.net.*;
public class SimpleFTP {
protected static int proxySet=0;
protected static String socks4Host=null;
protected static int socks4Port=-1;
protected static String socks4User=null;
String host = null;
int port = -1;
BufferedReader in = null;
PrintStream out = null;
Socket socket = null;
public SimpleFTP() {
}
public static void setSocks4Proxy(String host,String user){
setSocks4Proxy(host,1080,user);
}
public static void setSocks4Proxy(String host,int port,String user){
proxySet=1;
socks4Host=host;
socks4Port=port;
socks4User=user;
}
public static Socket getSocket(String addr,int port)throws UnknownHostException,IOException{
if(proxySet==1){
return getSocks4Socket(addr,port);
}
else {
return new Socket(addr,port);
}
}
private static Socket getSocks4Socket(String address,int port)throws UnknownHostException,IOException{
Socket s=new Socket(socks4Host,socks4Port);
InetAddress ip=InetAddress.getByName(address);
byte[] addr=ip.getAddress();
byte[] user=socks4User.getBytes();
byte[] header=new byte[9+user.length];
header[0]=0x4;
header[1]=0x1;
header[2]=(byte)(port>>8);
header[3]=(byte)port;
System.arraycopy(addr,0,header,4,4);
System.arraycopy(user,0,header,8,user.length);
header[header.length-1]=0x0;
//send request
s.getOutputStream().write(header);
byte[] response=new byte[8];
s.getInputStream().read(response);
//check whether available;if illegal ,the socket is already disconnected by server
int CD=response[1];
switch(CD){
case 90:
return s;
case 91:
throw new IOException("request rejected or failed");
case 92:
throw new IOException("request rejected becasue SOCKS server cannot connect to identd on the client");
case 93:
throw new IOException("request rejected because the client program and identd report different user-ids");
default:
throw new IOException("ERROR RESPONSE CODE:"+CD);
}
}
public void open(String host,String user,String pass)throws IOException,UnknownHostException {
open(host,21,user,pass);
}
public void open(String host, int port,String user,String pass) throws IOException,
UnknownHostException {
if (socket != null) {
disconnect();
}
socket = getSocket(host, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream(), true);
System.out.println("$"+in.readLine());
if(user == null){
user = "anonymous";
}
if(pass == null){
pass = "anything";
}
String code = cmd("USER "+user);
if(code.startsWith("331")){
code = cmd("PASS "+pass);
if(code.startsWith("230")){
return ;
}
}
else if(code.startsWith("230")){
return ;
}
disconnect();
throw new IOException("Authentication Failed");
}
public void noop(){
cmd("NOOP");
}
public String pwd() throws IOException {
String code = cmd("PWD");
return code.substring(code.indexOf("\"")+1,code.lastIndexOf("\""));
}
public boolean cd(String dir) {
String msg = cmd("CWD "+dir);
if(msg != null && msg.startsWith("250")){
return true;
}
else {
return false;
}
}
public boolean cdup() {
String msg = cmd("CDUP");
if(msg != null && msg.startsWith("250")){
return true;
}
else {
return false;
}
}
public String[] ls()throws IOException{
String path = pwd();
Object[] params = pasv();
out.println("LIST");
byte[] data = readBytes((String)params[0],((Integer)params[1]).intValue());
String str = new String(data);
String[] ss = str.split("\n");
String[] fs = new String[ss.length-3];
for(int i=3; i<ss.length; i++){
String postfix = (ss[i].startsWith("d"))? "/":"";
String file = ss[i].substring(ss[i].lastIndexOf(" ")+1,ss[i].length()-1);
fs[i-3] = path+"/"+file+postfix;
}
String msg = in.readLine();
System.out.println("$"+msg);
msg = in.readLine();
System.out.println("$"+msg);
return fs;
}
public int get(String remote_file,String local_file)throws IOException {
if(!remote_file.startsWith("/")){
remote_file = pwd()+"/"+remote_file;
}
Object[] params = pasv();
out.println("RETR "+remote_file);
FileOutputStream fos = new FileOutputStream(local_file);
int size = readStream((String)params[0],((Integer)params[1]).intValue(),fos);
fos.close();
String msg = in.readLine();
System.out.println("$"+msg);
msg = in.readLine();
System.out.println("$"+msg);
return size;
}
public int put(String local_file,String remote_file)throws IOException {
if(!remote_file.startsWith("/")){
remote_file = pwd()+"/"+remote_file;
}
Object[] params = pasv();
out.println("STOR "+remote_file);
FileInputStream fis = new FileInputStream(local_file);
int size = writeStream((String)params[0],((Integer)params[1]).intValue(),fis);
fis.close();
String msg = in.readLine();
System.out.println("$"+msg);
msg = in.readLine();
System.out.println("$"+msg);
return size;
}
public String cmd(String cmd){
return cmd(cmd,true);
}
public String cmd(String cmd,boolean echo) {
check();
try {
if(echo) System.out.println("$"+cmd);
out.println(cmd);
String ret = in.readLine();
if(echo) System.out.println("$"+ret);
if(ret.startsWith("421")){
disconnect();
return null;
}
return ret;
}
catch (Exception e) {
return null;
}
}
private Object[] pasv()throws IOException {
String msg = cmd("PASV");
//hardly return error code, I do none check here
int[] nums = new int[6];
msg = msg.substring(msg.indexOf("(") + 1, msg.lastIndexOf(")"));
String[] ps = msg.split(",");
for (int i = 0; i < nums.length; i++) {
nums[i] = Integer.parseInt(ps[i]);
}
String host = nums[0]+"."+nums[1]+"."+nums[2]+"."+nums[3];
int port = nums[4]*256 + nums[5];
Object[] os = new Object[2];
os[0] = host;
os[1] = new Integer(port);
return os;
}
private byte[] readBytes(String host,int port)throws UnknownHostException,IOException{
Socket s = getSocket(host,port);
byte[] buffer = new byte[512];
int pos = 0;
int num;
while((num = s.getInputStream().read(buffer,pos,buffer.length-pos)) != -1){
pos += num;
if(pos >= buffer.length){
byte[] bs = new byte[pos+512];
System.arraycopy(buffer,0,bs,0,pos);
buffer = bs;
}
}
s.close();
if(pos >= buffer.length){
return buffer;
}
else {
byte[] bs = new byte[pos];
System.arraycopy(buffer,0,bs,0,pos);
buffer = null;
return bs;
}
}
private int readStream(String host,int port,OutputStream os)throws UnknownHostException,IOException {
Socket s = getSocket(host,port);
byte[] buffer = new byte[512];
int total = 0;
int num;
int count = 0;
long time = System.currentTimeMillis();
long cur_time = time;
long last = 0;
while((num = s.getInputStream().read(buffer)) != -1){
total += num;
os.write(buffer,0,num);
if(count%5 == 0){
System.out.print("*");
}
cur_time = System.currentTimeMillis();
if((cur_time-time-last) >10000){
cmd("NOOP");
System.out.print("\nSIZE:"+total/1024+" k\tkps=");
last = cur_time - time;
System.out.println(1000.0*total/1024/last);
}
}
s.close();
os.flush();
return total;
}
private int writeBytes(String host,int port,byte[] data)throws UnknownHostException,IOException{
Socket s = getSocket(host,port);
s.getOutputStream().write(data);
s.close();
return data.length;
}
private int writeStream(String host,int port,InputStream is)throws UnknownHostException,IOException {
Socket s = getSocket(host,port);
byte[] buffer = new byte[512];
int num = 0;
int total = 0;
while((num = is.read(buffer)) != -1){
s.getOutputStream().write(buffer,0,num);
total += num;
}
s.close();
return total;
}
private void check() {
if (socket == null) {
throw new IllegalStateException("connection already disconnected");
}
}
public void disconnect() {
try {
in.close();
out.close();
socket.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
socket = null;
}
}
public static void main(String[] args) throws Exception {
//ftp://www.175youxi.com:11211@down.175youxi.com/即时战略/帝国时代4合1.rar
SimpleFTP ftp = new SimpleFTP();
ftp.setSocks4Proxy("192.168.4.7","ruanjue");
ftp.open("down.175youxi.com",21,"www.175youxi.com","11211");
ftp.cd("即时战略");
ftp.cmd("TYPE I");
ftp.get("帝国时代4合1.rar","E:/game/帝国时代4合1.rar");
ftp.disconnect();
/*Socket socket = new Socket("192.168.1.161", 21);
PrintStream out = new PrintStream(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.
getInputStream()));
String msg = in.readLine();
System.out.println(msg);
out.println("USER webdb");
msg = in.readLine();
System.out.println(msg);
out.println("PASS apples");
msg = in.readLine();
System.out.println(msg);
out.println("PWD");
msg = in.readLine();
System.out.println(msg);
out.println("CWD /disk/web/");
msg = in.readLine();
System.out.println(msg);
out.println("TYPE A");
msg = in.readLine();
System.out.println(msg);
out.println("PASV");
msg = in.readLine();
System.out.println(msg);
int[] nums = new int[6];
msg = msg.substring(msg.indexOf("(") + 1, msg.lastIndexOf(")"));
String[] ps = msg.split(",");
for (int i = 0; i < nums.length; i++) {
nums[i] = Integer.parseInt(ps[i]);
}
Socket s2 = new Socket(nums[0] + "." + nums[1] + "." + nums[2] + "." +
nums[3], nums[4] * 256 + nums[5]);
out.println("RETR /disk/web/ruanjue/application/perl/study.pl");
msg = in.readLine();
System.out.println(msg);
BufferedReader buffer = new BufferedReader(new InputStreamReader(s2.
getInputStream()));
String str = null;
while ( (str = buffer.readLine()) != null) {
System.out.println(str);
}
buffer.close();
s2.close();
ServerSocket server = new ServerSocket(nums[4] * 256 + nums[5]);
out.println("PORT 192,168,4,124," + nums[4] + "," + nums[5]);
msg = in.readLine();
System.out.println(msg);
out.println("LIST");
msg = in.readLine();
System.out.println(msg);
s2 = server.accept();
buffer = new BufferedReader(new InputStreamReader(s2.getInputStream()));
str = null;
while ( (str = buffer.readLine()) != null) {
System.out.println(str);
}
buffer.close();
s2.close();
server.close();
out.println("QUIT");
msg = in.readLine();
System.out.println(msg);
out.close();
in.close();
socket.close();*/
}
}
分享到:
相关推荐
在给定的标题“使用Java基于Netty+Socks5+TLS实现的代理服务.zip”中,我们可以看到三个核心概念:Netty、Socks5和TLS,这些都是构建高效、安全网络服务的关键组件。 Netty是一个高性能、异步事件驱动的网络应用...
ftp4j是个很年轻的开源项目,但是试用后发现很好很强大,如果你找一个纯java的FTP库,要支持socks4,socks4a,socks5,http代理,就是他了! 比apache的FTPClient(不支持代理)、半商业的edtFTPj(PRO支持代理,...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能。可以将ftp4j嵌到你的Java应用中,来传输文件(包括上传和下载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp...
本项目是一个基于Netty框架实现的Socks5代理服务器,支持Socks5协议的代理功能。项目包含了多个模块,涵盖了从客户端连接处理到服务器端代理转发的完整流程。主要功能包括 处理客户端的Socks5协议请求。 建立与...
Java提供了一些库来支持FTP操作,例如Apache Commons Net库,它提供了一个FTPClient类,使得在Java中实现FTP文件上传和下载变得简单。 以下是一个Java利用Apache Commons Net库实现FTP文件下载的示例代码: ```...
在C#中实现一个SOCKS5代理服务器涉及到多个关键概念和技术,这些包括网络编程、套接字(Sockets)、协议解析以及多线程等。下面我们将详细探讨这些知识点。 1. **网络编程基础**: 在网络编程中,我们通常使用TCP/...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
文档详细描述,linux不通外网的情况下,通过在另一台通外网的服务器搭建socks5代理服务,不通外网的服务器通过socks5代理,成功向外网发送邮件的功能。
Java 实现的代理服务器是一种网络通信工具,它允许客户端通过该服务器作为中介与目标服务器进行交互,从而隐藏了客户端的真实身份或提供了额外的...通过阅读和分析这些代码,你可以深入理解Java代理服务器的实现细节。
易语言实现超级代理服务端源码,支持http+socks4+socks5.zip
自己改编的Socks4代理源代码,稳定能用。没有写成服务形式,有此需要的朋友自己改一下就可以了
标题中的"C#写的代理服务器源代码 功能强大 支持http和socks5"表明这是一个用C#编程语言编写的代理服务器程序,它具备处理HTTP和SOCKS5两种协议的能力。HTTP代理主要处理Web浏览器的请求,而SOCKS5代理则是一个更...
本文将深入探讨C#编程语言实现SOCKS5代理协议的基础知识。 SOCKS5是一种广泛使用的互联网协议,用于在客户端和目标服务器之间建立安全的隧道,以实现代理服务。它支持多种网络协议,如TCP和UDP,同时提供了身份验证...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能。可以将ftp4j嵌到你的Java应用中,来传输文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。...
Socks5进程代理DLL,配套调用表,在压缩包里面
代理IP分为多种类型,其中HTTP和HTTPS代理主要处理基于HTTP(超文本传输协议)和HTTPS(安全超文本传输协议)的网络请求,而SOCKS5代理则是一种通用的代理协议,支持TCP/IP层的各种协议,包括HTTP、FTP等。...
袜子在ftp上通过FTP的SOCKS代理
Socks5代理服务器是代理协议的一种,相较于早期的Socks4,Socks5提供了更全面的功能和更高的安全性。它允许客户端通过代理服务器与任何类型的网络服务进行通信,包括TCP和UDP。Socks5的主要优点在于其身份验证机制,...