1 import java.util.*; 2 import java.io.*; 3 import java.net.*; 4 import socks.server.*; 5 import socks.*; 6 7 public class SOCKS{ 8 9 static public void usage(){ 10 System.out.println( 11 "Usage: java SOCKS [inifile1 inifile2 ...]\n"+ 12 "If none inifile is given, uses socks.properties.\n" 13 ); 14 } 15 16 static public void main(String [] args){ 17 18 String [] file_names; 19 int port=1080; 20 String logFile = null; 21 String host = null; 22 23 IdentAuthenticator auth = new IdentAuthenticator(); 24 OutputStream log = null; 25 InetAddress localIP = null; 26 27 if(args.length == 0){ 28 file_names = new String [1]; 29 file_names[0] = "socks.properties"; 30 }else{ 31 file_names = args; 32 } 33 34 35 inform("Loading properties"); 36 for(int i=0;i<file_names.length;++i){ 37 38 inform("Reading file "+file_names[i]); 39 40 Properties pr = loadProperties(file_names[i]); 41 if(pr==null){ 42 System.err.println("Loading of properties from "+ 43 file_names[i]+"failed."); 44 usage(); 45 return; 46 } 47 if(!addAuth(auth,pr)){ 48 System.err.println("Error in file "+file_names[i]+"."); 49 usage(); 50 return; 51 } 52 if(i==0){ 55 String port_s = (String ) pr.get("port"); 56 if(port_s != null) 57 try{ 58 port = Integer.parseInt(port_s); 59 }catch(NumberFormatException nfe){ 60 System.err.println("Can't parse port: "+port_s); 61 return; 62 } 63 64 serverInit(pr); 65 logFile = (String ) pr.get("log"); 66 host = (String ) pr.get("host"); 67 } 68 69 } 71 72 if(logFile!=null){ 73 if(logFile.equals("-")) 74 log = System.out; 75 else 76 try{ 77 log = new FileOutputStream(logFile); 78 }catch(IOException ioe){ 79 System.err.println("Can't open log file "+logFile); 80 return; 81 } 82 } 83 if(host!=null) 84 try{ 85 localIP = InetAddress.getByName(host); 86 }catch(UnknownHostException uhe){ 87 System.err.println("Can't resolve local ip: "+host); 88 return; 89 } 90 91 92 inform("Using Ident Authentication scheme:\n"+auth+"\n"); 93 ProxyServer server = new ProxyServer(auth); 94 server.setLog(log); 95 server.start(port,5,localIP); 96 } 97 98 static Properties loadProperties(String file_name){ 99 100 Properties pr = new Properties(); 101 102 try{ 103 InputStream fin = new FileInputStream(file_name); 104 pr.load(fin); 105 fin.close(); 106 }catch(IOException ioe){ 107 return null; 108 } 109 return pr; 110 } 111 112 static boolean addAuth(IdentAuthenticator ident,Properties pr){ 113 114 InetRange irange; 115 116 String range = (String ) pr.get("range"); 117 if(range == null) return false; 118 irange = parseInetRange(range); 119 120 121 String users = (String ) pr.get("users"); 122 123 if(users == null){ 124 ident.add(irange,null); 125 return true; 126 } 127 128 Hashtable uhash = new Hashtable(); 129 130 StringTokenizer st = new StringTokenizer(users,";"); 131 while(st.hasMoreTokens()) 132 uhash.put(st.nextToken(),""); 133 134 ident.add(irange,uhash); 135 return true; 136 } 137 138 141 static void serverInit(Properties props){ 142 int val; 143 val = readInt(props,"iddleTimeout"); 144 if(val>=0){ 145 ProxyServer.setIddleTimeout(val); 146 inform("Setting iddle timeout to "+val+" ms."); 147 } 148 val = readInt(props,"acceptTimeout"); 149 if(val>=0){ 150 ProxyServer.setAcceptTimeout(val); 151 inform("Setting accept timeout to "+val+" ms."); 152 } 153 val = readInt(props,"udpTimeout"); 154 if(val>=0){ 155 ProxyServer.setUDPTimeout(val); 156 inform("Setting udp timeout to "+val+" ms."); 157 } 158 159 val = readInt(props,"datagramSize"); 160 if(val>=0){ 161 ProxyServer.setDatagramSize(val); 162 inform("Setting datagram size to "+val+" bytes."); 163 } 164 165 proxyInit(props); 166 167 } 168 169 172 static void proxyInit(Properties props){ 173 String proxy_list; 174 Proxy proxy = null; 175 StringTokenizer st; 176 177 proxy_list = (String ) props.get("proxy"); 178 if(proxy_list == null) return; 179 180 st = new StringTokenizer(proxy_list,";"); 181 while(st.hasMoreTokens()){ 182 String proxy_entry = st.nextToken(); 183 184 Proxy p = Proxy.parseProxy(proxy_entry); 185 186 if(p == null) 187 exit("Can't parse proxy entry:"+proxy_entry); 188 189 190 inform("Adding Proxy:"+p); 191 192 if(proxy != null) 193 p.setChainProxy(proxy); 194 195 proxy = p; 196 197 } 198 if(proxy == null) return; 200 String direct_hosts = (String ) props.get("directHosts"); 201 if(direct_hosts!=null){ 202 InetRange ir = parseInetRange(direct_hosts); 203 inform("Setting direct hosts:"+ir); 204 proxy.setDirect(ir); 205 } 206 207 208 ProxyServer.setProxy(proxy); 209 } 210 213 static InetRange parseInetRange(String source){ 214 InetRange irange = new InetRange(); 215 216 StringTokenizer st = new StringTokenizer(source,";"); 217 while(st.hasMoreTokens()) 218 irange.add(st.nextToken()); 219 220 return irange; 221 } 222 223 227 static int readInt(Properties props,String name){ 228 int result = -1; 229 String val = (String ) props.get(name); 230 if(val==null) return -1; 231 StringTokenizer st = new StringTokenizer(val); 232 if(!st.hasMoreElements()) return -1; 233 try{ 234 result = Integer.parseInt(st.nextToken()); 235 }catch(NumberFormatException nfe){ 236 inform("Bad value for "+name+":"+val); 237 } 238 return result; 239 } 240 241 244 static void inform(String s){ 245 System.out.println(s); 246 } 247 248 static void exit(String msg){ 249 System.err.println("Error:"+msg); 250 System.err.println("Aborting operation"); 251 System.exit(0); 252 } 253 } 254 | Popular Tags |