aboutsummaryrefslogtreecommitdiff
path: root/subsonic-main/src/main/java/net/sourceforge/subsonic/service/NetworkService.java
blob: b54026a0dd7fca708667ff9a58c142fd93a809ed (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 This file is part of Subsonic.

 Subsonic 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.

 Subsonic 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 Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */
package net.sourceforge.subsonic.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

import net.sourceforge.subsonic.Logger;
import net.sourceforge.subsonic.domain.NATPMPRouter;
import net.sourceforge.subsonic.domain.Router;
import net.sourceforge.subsonic.domain.SBBIRouter;
import net.sourceforge.subsonic.domain.WeUPnPRouter;
import net.sourceforge.subsonic.util.StringUtil;
import net.sourceforge.subsonic.util.Util;

/**
 * Provides network-related services, including port forwarding on UPnP routers and
 * URL redirection from http://xxxx.subsonic.org.
 *
 * @author Sindre Mehus
 */
public class NetworkService {

    private static final Logger LOG = Logger.getLogger(NetworkService.class);
    private static final long PORT_FORWARDING_DELAY = 3600L;
    private static final long URL_REDIRECTION_DELAY = 2 * 3600L;

    private static final String URL_REDIRECTION_REGISTER_URL = getBackendUrl() + "/backend/redirect/register.view";
    private static final String URL_REDIRECTION_UNREGISTER_URL = getBackendUrl() + "/backend/redirect/unregister.view";
    private static final String URL_REDIRECTION_TEST_URL = getBackendUrl() + "/backend/redirect/test.view";

    private SettingsService settingsService;
    private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
    private final PortForwardingTask portForwardingTask = new PortForwardingTask();
    private final URLRedirectionTask urlRedirectionTask = new URLRedirectionTask();
    private Future<?> portForwardingFuture;
    private Future<?> urlRedirectionFuture;

    private final Status portForwardingStatus = new Status();
    private final Status urlRedirectionStatus = new Status();
    private boolean testUrlRedirection;

    public void init() {
        initPortForwarding();
        initUrlRedirection(false);
    }

    /**
     * Configures UPnP port forwarding.
     */
    public synchronized void initPortForwarding() {
        portForwardingStatus.setText("Idle");
        if (portForwardingFuture != null) {
            portForwardingFuture.cancel(true);
        }
        portForwardingFuture = executor.scheduleWithFixedDelay(portForwardingTask, 0L, PORT_FORWARDING_DELAY, TimeUnit.SECONDS);
    }

    /**
     * Configures URL redirection.
     *
     * @param test Whether to test that the redirection works.
     */
    public synchronized void initUrlRedirection(boolean test) {
        urlRedirectionStatus.setText("Idle");
        if (urlRedirectionFuture != null) {
            urlRedirectionFuture.cancel(true);
        }
        testUrlRedirection = test;
        urlRedirectionFuture = executor.scheduleWithFixedDelay(urlRedirectionTask, 0L, URL_REDIRECTION_DELAY, TimeUnit.SECONDS);
    }

    public Status getPortForwardingStatus() {
        return portForwardingStatus;
    }

    public Status getURLRedirecionStatus() {
        return urlRedirectionStatus;
    }

    public static String getBackendUrl() {
        return "true".equals(System.getProperty("subsonic.test")) ? "http://localhost:8181" : "http://subsonic.org";
    }

    public void setSettingsService(SettingsService settingsService) {
        this.settingsService = settingsService;
    }

    private class PortForwardingTask extends Task {

        @Override
        protected void execute() {

            boolean enabled = settingsService.isPortForwardingEnabled();
            portForwardingStatus.setText("Looking for router...");
            Router router = findRouter();
            if (router == null) {
                LOG.warn("No UPnP router found.");
                portForwardingStatus.setText("No router found.");
            } else {

                portForwardingStatus.setText("Router found.");

                int port = settingsService.getPort();
                int httpsPort = settingsService.getHttpsPort();

                // Create new NAT entry.
                if (enabled) {
                    try {
                        router.addPortMapping(port, port, 0);
                        String message = "Successfully forwarding port " + port;

                        if (httpsPort != 0 && httpsPort != port) {
                            router.addPortMapping(httpsPort, httpsPort, 0);
                            message += " and port " + httpsPort;
                        }
                        message += ".";

                        LOG.info(message);
                        portForwardingStatus.setText(message);
                    } catch (Throwable x) {
                        String message = "Failed to create port forwarding.";
                        LOG.warn(message, x);
                        portForwardingStatus.setText(message + " See log for details.");
                    }
                }

                // Delete NAT entry.
                else {
                    try {
                        router.deletePortMapping(port, port);
                        LOG.info("Deleted port mapping for port " + port);
                        if (httpsPort != 0 && httpsPort != port) {
                            router.deletePortMapping(httpsPort, httpsPort);
                            LOG.info("Deleted port mapping for port " + httpsPort);
                        }
                    } catch (Throwable x) {
                        LOG.warn("Failed to delete port mapping.", x);
                    }
                    portForwardingStatus.setText("Port forwarding disabled.");
                }
            }

            //  Don't do it again if disabled.
            if (!enabled && portForwardingFuture != null) {
                portForwardingFuture.cancel(false);
            }
        }

        private Router findRouter() {
            try {
                Router router = SBBIRouter.findRouter();
                if (router != null) {
                    return router;
                }
            } catch (Throwable x) {
                LOG.warn("Failed to find UPnP router using SBBI library.", x);
            }

            try {
                Router router = WeUPnPRouter.findRouter();
                if (router != null) {
                    return router;
                }
            } catch (Throwable x) {
                LOG.warn("Failed to find UPnP router using WeUPnP library.", x);
            }

            try {
                Router router = NATPMPRouter.findRouter();
                if (router != null) {
                    return router;
                }
            } catch (Throwable x) {
                LOG.warn("Failed to find NAT-PMP router.", x);
            }

            return null;
        }
    }

    private class URLRedirectionTask extends Task {

        @Override
        protected void execute() {

            boolean enable = settingsService.isUrlRedirectionEnabled();
            HttpPost request = new HttpPost(enable ? URL_REDIRECTION_REGISTER_URL : URL_REDIRECTION_UNREGISTER_URL);

            int port = settingsService.getPort();
            boolean trial = !settingsService.isLicenseValid();
            Date trialExpires = settingsService.getUrlRedirectTrialExpires();

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("serverId", settingsService.getServerId()));
            params.add(new BasicNameValuePair("redirectFrom", settingsService.getUrlRedirectFrom()));
            params.add(new BasicNameValuePair("port", String.valueOf(port)));
            params.add(new BasicNameValuePair("localIp", Util.getLocalIpAddress()));
            params.add(new BasicNameValuePair("localPort", String.valueOf(port)));
            params.add(new BasicNameValuePair("contextPath", settingsService.getUrlRedirectContextPath()));
            params.add(new BasicNameValuePair("trial", String.valueOf(trial)));
            if (trial && trialExpires != null) {
                params.add(new BasicNameValuePair("trialExpires", String.valueOf(trialExpires.getTime())));
            } else {
                params.add(new BasicNameValuePair("licenseHolder", settingsService.getLicenseEmail()));
            }

            HttpClient client = new DefaultHttpClient();

            try {
                urlRedirectionStatus.setText(enable ? "Registering web address..." : "Unregistering web address...");
                request.setEntity(new UrlEncodedFormEntity(params, StringUtil.ENCODING_UTF8));

                HttpResponse response = client.execute(request);
                StatusLine status = response.getStatusLine();

                switch (status.getStatusCode()) {
                    case HttpStatus.SC_BAD_REQUEST:
                        urlRedirectionStatus.setText(EntityUtils.toString(response.getEntity()));
                        break;
                    case HttpStatus.SC_OK:
                        urlRedirectionStatus.setText(enable ? "Successfully registered web address." : "Web address disabled.");
                        break;
                    default:
                        throw new IOException(status.getStatusCode() + " " + status.getReasonPhrase());
                }

            } catch (Throwable x) {
                LOG.warn(enable ? "Failed to register web address." : "Failed to unregister web address.", x);
                urlRedirectionStatus.setText(enable ? ("Failed to register web address. " + x.getMessage() +
                        " (" + x.getClass().getSimpleName() + ")") : "Web address disabled.");
            } finally {
                client.getConnectionManager().shutdown();
            }

            // Test redirection, but only once.
            if (testUrlRedirection) {
                testUrlRedirection = false;
                testUrlRedirection();
            }

            //  Don't do it again if disabled.
            if (!enable && urlRedirectionFuture != null) {
                urlRedirectionFuture.cancel(false);
            }
        }

        private void testUrlRedirection() {

            HttpGet request = new HttpGet(URL_REDIRECTION_TEST_URL + "?redirectFrom=" + settingsService.getUrlRedirectFrom());
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
            HttpConnectionParams.setSoTimeout(client.getParams(), 30000);

            try {
                urlRedirectionStatus.setText("Testing web address " + settingsService.getUrlRedirectFrom() + ".subsonic.org. Please wait...");
                String response = client.execute(request, new BasicResponseHandler());
                urlRedirectionStatus.setText(response);

            } catch (Throwable x) {
                LOG.warn("Failed to test web address.", x);
                urlRedirectionStatus.setText("Failed to test web address. " + x.getMessage() + " (" + x.getClass().getSimpleName() + ")");
            } finally {
                client.getConnectionManager().shutdown();
            }
        }
    }

    private abstract class Task implements Runnable {
        public void run() {
            String name = getClass().getSimpleName();
            try {
                execute();
            } catch (Throwable x) {
                LOG.error("Error executing " + name + ": " + x.getMessage(), x);
            }
        }

        protected abstract void execute();
    }

    public static class Status {

        private String text;
        private Date date;

        public void setText(String text) {
            this.text = text;
            date = new Date();
        }

        public String getText() {
            return text;
        }

        public Date getDate() {
            return date;
        }
    }
}