2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2000-07-01 14:25:25 +04:00
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-07-01 14:25:25 +04:00
|
|
|
|
2016-09-13 11:36:12 +03:00
|
|
|
#include "nsNamedPipeIOLayer.h"
|
2000-06-16 21:53:22 +04:00
|
|
|
#include "nsSOCKSSocketProvider.h"
|
|
|
|
#include "nsSOCKSIOLayer.h"
|
2003-09-12 00:32:33 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2000-06-16 21:53:22 +04:00
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
using mozilla::OriginAttributes;
|
2016-11-21 06:43:06 +03:00
|
|
|
|
2000-06-16 21:53:22 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsSOCKSSocketProvider, nsISocketProvider)
|
2000-06-16 21:53:22 +04:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
nsresult nsSOCKSSocketProvider::CreateV4(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISocketProvider> inst =
|
|
|
|
new nsSOCKSSocketProvider(NS_SOCKS_VERSION_4);
|
|
|
|
if (!inst)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
else
|
|
|
|
rv = inst->QueryInterface(aIID, aResult);
|
2000-07-01 14:25:25 +04:00
|
|
|
return rv;
|
2000-06-16 21:53:22 +04:00
|
|
|
}
|
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
nsresult nsSOCKSSocketProvider::CreateV5(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult) {
|
2000-07-01 14:25:25 +04:00
|
|
|
nsresult rv;
|
2003-09-12 00:32:33 +04:00
|
|
|
nsCOMPtr<nsISocketProvider> inst =
|
|
|
|
new nsSOCKSSocketProvider(NS_SOCKS_VERSION_5);
|
|
|
|
if (!inst)
|
2000-07-01 14:25:25 +04:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
2003-09-12 00:32:33 +04:00
|
|
|
else
|
|
|
|
rv = inst->QueryInterface(aIID, aResult);
|
2000-07-01 14:25:25 +04:00
|
|
|
return rv;
|
2000-06-16 21:53:22 +04:00
|
|
|
}
|
|
|
|
|
2018-03-07 20:58:00 +03:00
|
|
|
// Per-platform implemenation of OpenTCPSocket helper function
|
|
|
|
// Different platforms have special cases to handle
|
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
// The proxy host on Windows may be a named pipe uri, in which
|
|
|
|
// case a named-pipe (rather than a socket) should be returned
|
|
|
|
static PRFileDesc* OpenTCPSocket(int32_t family, nsIProxyInfo* proxy) {
|
|
|
|
PRFileDesc* sock = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-07 20:58:00 +03:00
|
|
|
nsAutoCString proxyHost;
|
|
|
|
proxy->GetHost(proxyHost);
|
|
|
|
if (IsNamedPipePath(proxyHost)) {
|
|
|
|
sock = CreateNamedPipeLayer();
|
|
|
|
} else {
|
|
|
|
sock = PR_OpenTCPSocket(family);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-07 20:58:00 +03:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
// The proxy host on UNIX systems may point to a local file uri
|
|
|
|
// in which case we should create an AF_LOCAL (UNIX Domain) socket
|
|
|
|
// instead of the requested AF_INET or AF_INET6 socket.
|
|
|
|
|
|
|
|
// Normally,this socket would get thrown out and recreated later on
|
|
|
|
// with the proper family, but we want to do it early here so that
|
|
|
|
// we can enforce seccomp policy to blacklist socket(AF_INET) calls
|
|
|
|
// to prevent the content sandbox from creating network requests
|
|
|
|
static PRFileDesc* OpenTCPSocket(int32_t family, nsIProxyInfo* proxy) {
|
|
|
|
nsAutoCString proxyHost;
|
|
|
|
proxy->GetHost(proxyHost);
|
2020-07-01 11:29:29 +03:00
|
|
|
if (StringBeginsWith(proxyHost, "file://"_ns)) {
|
2018-03-07 20:58:00 +03:00
|
|
|
family = AF_LOCAL;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-07 20:58:00 +03:00
|
|
|
return PR_OpenTCPSocket(family);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Default, pass-through to PR_OpenTCPSocket
|
|
|
|
static PRFileDesc* OpenTCPSocket(int32_t family, nsIProxyInfo*) {
|
|
|
|
return PR_OpenTCPSocket(family);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-16 21:53:22 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSOCKSSocketProvider::NewSocket(int32_t family, const char* host, int32_t port,
|
2015-11-25 00:56:00 +03:00
|
|
|
nsIProxyInfo* proxy,
|
2017-01-12 19:38:48 +03:00
|
|
|
const OriginAttributes& originAttributes,
|
2017-08-16 22:41:16 +03:00
|
|
|
uint32_t flags, uint32_t tlsFlags,
|
2000-07-01 14:25:25 +04:00
|
|
|
PRFileDesc** result, nsISupports** socksInfo) {
|
2018-03-07 20:58:00 +03:00
|
|
|
PRFileDesc* sock = OpenTCPSocket(family, proxy);
|
|
|
|
if (!sock) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2016-09-13 11:36:12 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
nsresult rv = nsSOCKSIOLayerAddToSocket(family, host, port, proxy, mVersion,
|
|
|
|
flags, tlsFlags, sock, socksInfo);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = sock;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
return NS_ERROR_SOCKET_CREATE_FAILED;
|
2000-06-16 21:53:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsSOCKSSocketProvider::AddToSocket(int32_t family, const char* host,
|
2015-11-25 00:56:00 +03:00
|
|
|
int32_t port, nsIProxyInfo* proxy,
|
2017-01-12 19:38:48 +03:00
|
|
|
const OriginAttributes& originAttributes,
|
2017-08-16 22:41:16 +03:00
|
|
|
uint32_t flags, uint32_t tlsFlags,
|
2000-07-01 14:25:25 +04:00
|
|
|
PRFileDesc* sock, nsISupports** socksInfo) {
|
2003-09-12 00:32:33 +04:00
|
|
|
nsresult rv = nsSOCKSIOLayerAddToSocket(family, host, port, proxy, mVersion,
|
2000-07-01 14:25:25 +04:00
|
|
|
flags, tlsFlags, sock, socksInfo);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-09-12 00:32:33 +04:00
|
|
|
if (NS_FAILED(rv)) rv = NS_ERROR_SOCKET_CREATE_FAILED;
|
|
|
|
return rv;
|
2000-06-16 21:53:22 +04:00
|
|
|
}
|