-
Notifications
You must be signed in to change notification settings - Fork 16
/
Configuration.java
187 lines (149 loc) · 6.53 KB
/
Configuration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Copyright (C) 1998-2014 Christian Schmidt
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
import java.net.*;
import java.util.*;
class Configuration {
Properties properties;
int bindPort;
InetAddress bindAddress;
// Variables read from configuration file.
boolean onlyAuto;
String autoHostname;
int autoPort;
String masqueradeHostname;
boolean isUrlSyntaxEnabled;
int[] serverBindPorts;
int[] clientBindPorts;
boolean serverOneBindPort, clientOneBindPort;
boolean validateDataConnection;
boolean debug;
// Lists of subnets.
List useActive, usePassive;
List allowFrom, denyFrom, allowTo, denyTo;
// Messages.
String msgConnect;
String msgConnectionRefused;
String msgOriginAccessDenied;
String msgDestinationAccessDenied;
String msgIncorrectSyntax;
String msgInternalError;
String msgMasqHostDNSError;
public Configuration(Properties properties) throws UnknownHostException {
this.properties = properties;
bindPort = getInt("bind_port", 8089);
String ba = getString("bind_address");
bindAddress = ba == null ? null : InetAddress.getByName(ba.trim());
serverBindPorts = getPortRanges("server_bind_ports");
clientBindPorts = getPortRanges("client_bind_ports");
serverOneBindPort = serverBindPorts != null && serverBindPorts.length == 2 &&
serverBindPorts[0] == serverBindPorts[1];
clientOneBindPort = clientBindPorts != null && clientBindPorts.length == 2 &&
clientBindPorts[0] == clientBindPorts[1];
masqueradeHostname = getString("masquerade_host");
if (masqueradeHostname != null) {
//This is just to throw an UnknownHostException
//if the config is incorrectly set up.
InetAddress.getByName(masqueradeHostname.trim());
}
useActive = getSubnets("use_active");
usePassive = getSubnets("use_passive");
allowFrom = getSubnets("allow_from");
denyFrom = getSubnets("deny_from");
allowTo = getSubnets("allow_to");
denyTo = getSubnets("deny_to");
onlyAuto = getBool("only_auto", false);
autoHostname = getString("auto_host");
if (autoHostname != null) {
autoHostname = autoHostname.trim();
}
autoPort = getInt("auto_port", 21);
isUrlSyntaxEnabled = getBool("enable_url_syntax", true);
validateDataConnection = getBool("validate_data_connection", true);
debug = getBool("output_debug_info", false);
msgConnect = "220 " +
getString("msg_connect", "Java FTP Proxy Server (usage: USERID=user@site) ready.");
msgConnectionRefused = "421 " +
getString("msg_connection_refused", "Connection refused, closing connection.");
msgOriginAccessDenied = "531 " +
getString("msg_origin_access_denied", "Access denied - closing connection.");
msgDestinationAccessDenied = "531 " +
getString("msg_destination_access_denied", "Access denied - closing connection.");
msgIncorrectSyntax = "531 " +
getString("msg_incorrect_syntax", "Incorrect usage - closing connection.");
msgInternalError = "421 " +
getString("msg_internal_error", "Internal error, closing connection.");
msgMasqHostDNSError = "421 " +
getString("msg_masqerade_hostname_dns_error",
"Unable to resolve address for " + masqueradeHostname +
" - closing connection.");
}
public boolean getBool(String name, boolean defaultValue) {
String value = getString(name);
return value == null ? defaultValue : value.trim().equals("1");
}
public int getInt(String name, int defaultValue) {
String value = properties.getProperty(name);
properties.remove(name);
return value == null ? defaultValue : Integer.parseInt(value.trim());
}
public String getString(String name) {
return getString(name, null);
}
public String getString(String name, String defaultValue) {
String value = properties.getProperty(name, defaultValue);
properties.remove(name);
return value;
}
public List getSubnets(String name) {
String s = getString(name);
if (s == null) return null;
List list = new LinkedList();
StringTokenizer st = new StringTokenizer(s.trim(), ",");
while (st.hasMoreTokens()) {
list.add(new Subnet(st.nextToken().trim()));
}
return list;
}
/**
* Returns an array of length 2n, where n is the number of port
* ranges specified. Index 2i will contain the first port number
* in the i'th range, and index 2i+1 will contain the last.
* E.g. the string "111,222-333,444-555,666" will result in the
* following array: {111, 111, 222, 333, 444, 555, 666, 666}
*/
public int[] getPortRanges(String name) {
String s = getString(name);
if (s == null) return null;
StringTokenizer st = new StringTokenizer(s.trim(), ",");
int[] ports = new int[st.countTokens() * 2];
if (ports.length == 0) return null;
int lastPort = 0;
for (int p = 0; st.hasMoreTokens(); p += 2) {
String range = st.nextToken().trim();
int i = range.indexOf('-');
if (i == -1) {
ports[p] = ports[p + 1] = Integer.parseInt(range);
} else {
ports[p] = Integer.parseInt(range.substring(0, i));
ports[p + 1] = Integer.parseInt(range.substring(i + 1));
}
if (ports[p] < lastPort || ports[p] > ports[p + 1]) {
throw new RuntimeException("Ports should be listed in increasing order.");
}
lastPort = ports[p + 1];
}
return ports;
}
}