001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.net.proxy;
020
021
022
023import java.util.Properties;
024
025import lucee.commons.lang.StringUtil;
026import lucee.commons.lang.lock.KeyLock;
027import lucee.commons.lang.lock.KeyLockListener;
028
029public final class Proxy {
030        
031        //private static Map map=new HashTable();
032        private static KeyLock kl=new KeyLock();
033        public static void start(ProxyData proxyData) {
034                start(proxyData.getServer(),proxyData.getPort(),proxyData.getUsername(),proxyData.getPassword());
035        }
036        
037        public static void start(String server, int port, String user, String password) {
038                String key=StringUtil.toString(server, "")+":"+StringUtil.toString(port+"", "")+":"+StringUtil.toString(user, "")+":"+StringUtil.toString(password, "");
039                kl.setListener(new ProxyListener(server,port,user,password));
040                kl.start(key);
041        }
042        
043        public static void end() {
044                kl.end();
045        }
046        
047        
048        
049
050        /*public static void main(String[] args) throws Exception {
051        
052                new ProxyThread(1,"203.144.160.247",8080,null,null).start();
053                new ProxyThread(2,"203.144.160.247",8080,null,null).start();
054                new ProxyThread(3,"202.144.160.247",8080,null,null).start();
055                new ProxyThread(4,"202.144.160.247",8080,null,null).start();
056                new ProxyThread(5,"204.144.160.247",8080,null,null).start();
057        }*/
058}
059
060class ProxyListener implements KeyLockListener {
061
062        private String server;
063        private int port;
064        private String user;
065        private String password;
066        
067        public ProxyListener(String server, int port, String user, String password) {
068                this.server=server;
069                this.port=port;
070                this.user=user;
071                this.password=password;
072        }
073
074        public void onStart(String key,boolean isFirst) {
075                //print.ln(" start:"+key+" _ "+isFirst);        
076                if(!isFirst) return;
077                
078                Properties props = System.getProperties();
079                if(!StringUtil.isEmpty(server)) {
080                        // Server
081                        props.setProperty("socksProxyHost",     server);
082                        props.setProperty("http.proxyHost",     server);
083                        props.setProperty("https.proxyHost",server);
084                        props.setProperty("ftp.proxyHost",      server);
085                        props.setProperty("smtp.proxyHost", server);
086                        
087                        // Port
088                        if(port>0) {
089                                String strPort= String.valueOf(port);
090                                props.setProperty("socksProxyPort",strPort);
091                                props.setProperty("http.proxyPort",strPort);
092                                props.setProperty("https.proxyPort",strPort);
093                                props.setProperty("ftp.proxyPort",strPort);
094                                props.setProperty("smtp.proxyPort",strPort);
095                        }
096                        else removePort(props);
097                        
098                        if(!StringUtil.isEmpty(user)) {
099                                props.setProperty("socksProxyUser",user);
100                                props.setProperty("java.net.socks.username",user);
101                                props.setProperty("http.proxyUser",user);
102                                props.setProperty("https.proxyUser",user);
103                                props.setProperty("ftp.proxyUser",user);
104                                props.setProperty("smtp.proxyUser",user);
105                                
106                                if(password==null)password="";
107                                props.setProperty("socksProxyPassword",user);
108                                props.setProperty("java.net.socks.password",user);
109                                props.setProperty("http.proxyPassword",user);
110                                props.setProperty("https.proxyPassword",user);
111                                props.setProperty("ftp.proxyPassword",user);
112                                props.setProperty("smtp.proxyPassword",user);
113                        }
114                        else removeUserPass(props);
115                }
116                else {
117                        removeAll(props);
118                }
119        }
120
121        public void onEnd(String key,boolean isLast) {
122                //print.ln(" end:"+key+key+" _ "+isLast);
123                if(!isLast) return;
124                removeAll(System.getProperties());
125        }
126
127        private void removeAll(Properties props) {
128                removeHost(props);
129                removePort(props);
130                removeUserPass(props);
131        
132        }
133        private void removeHost(Properties props) {
134                remove(props,"socksProxyHost");
135        
136        remove(props,"http.proxyHost");
137        remove(props,"https.proxyHost");
138        remove(props,"ftp.proxyHost");
139        remove(props,"smtp.proxyHost");
140        
141        }
142        private void removePort(Properties props) {
143                remove(props,"socksProxyPort");
144        remove(props,"http.proxyPort");
145        remove(props,"https.proxyPort");
146        remove(props,"ftp.proxyPort");
147        remove(props,"smtp.proxyPort");
148        }
149        private void removeUserPass(Properties props) {
150                remove(props,"socksProxyUser");
151        remove(props,"socksProxyPassword");
152        
153        remove(props,"java.net.socks.username");
154        remove(props,"java.net.socks.password");
155        
156        remove(props,"http.proxyUser");
157        remove(props,"http.proxyPassword");
158        
159        remove(props,"https.proxyUser");
160        remove(props,"https.proxyPassword");
161
162        remove(props,"ftp.proxyUser");
163        remove(props,"ftp.proxyPassword");
164        
165        remove(props,"smtp.proxyUser");
166        remove(props,"smtp.proxyPassword");
167        
168        }
169        
170        private static void remove(Properties props, String key) {
171                if(props.containsKey(key))
172                        props.remove(key);
173        }
174        
175}
176
177
178/*class ProxyThread extends Thread {
179        private String s;
180        private int po;
181        private int id;
182        private String u;
183        private String p;
184        
185        public ProxyThread(int id,String s, int po, String u, String p) {
186                this.s=s;
187                this.id=id;
188                this.po=po;
189                this.u=u;
190                this.p=p;
191        }
192        public void run() {
193                try {
194                        _run();
195                } catch (Exception e) {
196                        
197                }
198        }
199        public void _run() throws Exception {
200                //print.ln("start("+Thread.currentThread().getName()+"):"+s+":"+po+":"+u+":"+p);
201                Proxy.start(id,s, po, u, p);
202                sleep(1000);
203                Proxy.end(id);
204        }
205}*/