Repository navigation
Expand file tree
/
Copy pathIBControllerServer.java
More file actions
167 lines (140 loc) · 6.4 KB
/
Copy pathIBControllerServer.java
File metadata and controls
167 lines (140 loc) · 6.4 KB
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
// This file is part of the "IBController".
// Copyright (C) 2004 Steven M. Kearns (skearns23@yahoo.com )
// Copyright (C) 2004 - 2011 Richard L King (rlking@aultan.com)
// For conditions of distribution and use, see copyright notice in COPYING.txt
// IBController 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 3 of the License, or
// (at your option) any later version.
// IBController 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 IBController. If not, see <http://www.gnu.org/licenses/>.
package ibcontroller;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
class IBControllerServer
implements Runnable {
private ServerSocket mSocket = null;
private volatile boolean mQuitting = false;
private final boolean isGateway;
IBControllerServer(boolean isGateway) {
this.isGateway = isGateway;
}
@Override
public void run() {
Thread.currentThread().setName("IBControllerServer");
final int port = Settings.settings().getInt("IbControllerPort", 0);
if (port == 0) {
Utils.logToConsole("IBControllerServer is not started because the port is not configured");
return;
}
Utils.logToConsole("IBControllerServer is starting with port " + port);
if (createSocket(port)) {
Utils.logToConsole("IBControllerServer started and is ready to accept commands");
for (; !mQuitting;) {
Socket socket = getClient();
if (socket != null) MyCachedThreadPool.getInstance().execute(new CommandDispatcher(new CommandChannel(socket), isGateway));
}
try {
mSocket.close();
} catch (Exception e) {
}
}
Utils.logToConsole("IBControllerServer is shutdown");
}
public void shutdown() {
mQuitting = true;
}
private boolean createSocket(final int port) {
final int backlog = 5;
try {
final String bindaddr = Settings.settings().getString("IbBindAddress", "");
if (!bindaddr.isEmpty()) {
mSocket = new ServerSocket(port,
backlog,
InetAddress.getByName(bindaddr));
Utils.logToConsole("IBControllerServer listening on address: " +
bindaddr + " port: " +
java.lang.String.valueOf(port));
} else {
mSocket = new ServerSocket(port, backlog);
Utils.logToConsole("IBControllerServer listening on addresses: " +
getAddresses() + "; port: " +
java.lang.String.valueOf(port));
}
} catch (java.net.BindException e) {
Utils.logError("IBControllerServer failed to create socket: " + e.getMessage());
Utils.logToConsole("IBControllerServer cannot process commands");
mSocket = null;
mQuitting = true;
return false;
} catch (IOException e) {
Utils.logError("exception:\n" + e.toString());
Utils.logToConsole("IBControllerServer failed to create socket");
Utils.logToConsole("IBControllerServer cannot process commands");
mSocket = null;
mQuitting = true;
return false;
}
return true;
}
private Socket getClient() {
try {
final Socket socket = mSocket.accept();
final String allowedAddresses =
Settings.settings().getString("IbControlFrom", "");
if (!socket.getInetAddress().getHostAddress().equals(mSocket.getInetAddress().getHostAddress()) &&
!socket.getInetAddress().getHostAddress().equals(InetAddress.getLoopbackAddress().getHostAddress()) &&
!allowedAddresses.contains(socket.getInetAddress().getHostAddress()) &&
!allowedAddresses.contains(socket.getInetAddress().getHostName())) {
Utils.logToConsole("IBControllerServer denied access to: " +
socket.getInetAddress().toString());
socket.close();
return null;
}
Utils.logToConsole("IBControllerServer accepted connection from: " + socket.getInetAddress().getHostAddress());
return socket;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private String getAddresses() {
final List<String> addressList = getAddressList();
String s = addressList.isEmpty() ? "" : addressList.get(0);
for (int i = 1; i < addressList.size(); i++) {
s = s + "," + addressList.get(i);
}
return s;
}
private List<String> getAddressList() {
List<String> addressList = new ArrayList<>();
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
addressList.add(address.getHostAddress());
}
}
} catch (SocketException e) {
Utils.logToConsole("SocketException occurred while enumerating network interfaces");
}
return addressList;
}
}