Commit graph

68 commits

Author SHA1 Message Date
Cecylia Bocovich
ead5a960d7
Bump snowflake library imports and go.mod to v2 2021-11-11 10:14:49 -05:00
Cecylia Bocovich
53a2365696 Fix leak in server acceptLoop
Refactor out a separate handleStream function and ensure that all
connections are closed and the references are out of scope.
2021-06-24 13:32:55 -04:00
Cecylia Bocovich
11f0846264 Implement server as a v2.1 PT Go API 2021-05-12 09:08:41 -04:00
Cecylia Bocovich
c0b6e082f2 Don't log errors from callng close on OR conns
Snowflake copies data between the OR connection and the KCP stream,
meaning that in most cases the copy loops will only terminate once the
OR connection times out. In this case the OR connection is already
closed and so calls to CloseRead and CloseWrite will generate errors.
2021-03-18 22:05:40 -04:00
Cecylia Bocovich
720d2b8eb7 Don't log io.ErrClosedPipe in server
These errors are triggered in three places when the OR connection times
out. They don't tell us anything useful and are filling up our logs.
2021-03-18 22:05:40 -04:00
Cecylia Bocovich
f908576c60 Increase the KCP maximum window size 2020-12-17 09:54:18 -05:00
David Fifield
0790954020 USERADDR support for turbotunnel sessions.
The difficulty here is that the whole point of turbotunnel sessions is
that they are not necessarily tied to a single WebSocket connection, nor
even a single client IP address. We use a heuristic: whenever a
WebSocket connection starts that has a new ClientID, we store a mapping
from that ClientID to the IP address attached to the WebSocket
connection in a lookup table. Later, when enough packets have arrived to
establish a turbotunnel session, we recover the ClientID associated with
the session (which kcp-go has stored in the RemoteAddr field), and look
it up in the table to get an IP address. We introduce a new data type,
clientIDMap, to store the clientID-to-IP mapping during the short time
between when a WebSocket connection starts and handleSession receives a
fully fledged KCP session.
2020-04-23 16:03:02 -06:00
David Fifield
70126177fb Turbo Tunnel client and server.
The client opts into turbotunnel mode by sending a magic token at the
beginning of each WebSocket connection (before sending even the
ClientID). The token is just a random byte string I generated. The
server peeks at the token and, if it matches, uses turbotunnel mode.
Otherwise, it unreads the token and continues in the old
one-session-per-WebSocket mode.
2020-04-23 16:02:56 -06:00
Arlo Breault
f58c865d82 Add unsafe logging 2020-03-25 11:53:24 -04:00
David Fifield
c124e8c643 In server, treat a client IP address of 0.0.0.0 as missing.
Some proxies currently send ?client_ip=0.0.0.0 because of an error in
how they attempt to grep the address from the client's SDP. That's
inflating our "%d/%d connections had client_ip" logs. Instead, treat
these cases as if the IP address were absent.
https://bugs.torproject.org/33157
https://bugs.torproject.org/33385
2020-02-22 16:13:17 -07:00
David Fifield
ca9ae12c38 Simplify a conditional. 2020-02-04 22:35:12 -07:00
David Fifield
564d1c8363 Remove unused maxMessageSize constant. 2020-01-31 00:15:11 -07:00
David Fifield
20ac2029fd Have websocketconn.New return a pointer.
This makes the return type satisfy the io.ReadWriteCloser interface
directly.
2020-01-30 10:18:23 -07:00
David Fifield
e47dd5e2b4 Remove some redundancy in websocketconn naming.
Rename websocketconn.WebSocketConn to websocketconn.Conn, and
       websocketconn.NewWebSocketConn to websocketconn.New

Following the guidelines at
https://blog.golang.org/package-names#TOC_3%2e
2020-01-30 10:18:23 -07:00
David Fifield
5b01df9030 Initialize the global upgrader.CheckOrigin statically.
Only once, not again on every call to initServer.
2020-01-30 10:18:23 -07:00
David Fifield
a4287095c0 Also show message in the "error copying WebSocket to ORPort" case.
This was the only case out of the three not to show it.
2020-01-30 10:17:15 -07:00
David Fifield
d6467ff585 Formatting improvements. 2020-01-23 10:43:31 -07:00
David Fifield
5ff75e1034 Remove erroneous logging around pt.*Error calls.
These functions are called for their side effect of sending a PT error
message on stdout; they also return a representation of the error
message as an error object for the caller to use if it wishes. These
functions *always* return a non-nil error object; it is not something to
be logged, any more than the return value of errors.New is.

The mistaken logging was added in
https://bugs.torproject.org/31794
b26c7a7a73
3ec9dd19fa
ed3d42e1ec
2020-01-20 23:57:31 -07:00
Arlo Breault
7092b2cb2c Revert abstracting copyloop 2019-11-21 19:33:39 -05:00
Arlo Breault
30b5ef8a9e Use gorilla websocket in proxy-go too
Trac: 32465
2019-11-20 19:33:28 -05:00
Arlo Breault
abefae1587 Restore sending close message before closing
And simplify EOF check.
2019-11-11 17:20:00 -05:00
Arlo Breault
c417fd5599 Stop using custom websocket library in server
Trac: 31028
2019-11-11 17:20:00 -05:00
David Fifield
b4f4b29a03 Stop counting handlers before terminating.
The requirement to do so is obsolete and has already been removed from
other pluggable transports.

https://bugs.torproject.org/32046
2019-10-11 16:50:25 -06:00
Shane Howearth
8bbdb3b51a Bring code into line with Golangci-lint linters
- Error strings are no longer capitalized nor end with punctuation
- Alias import
- Remove extraneous initilisation code (No need to provide zero value
	for variables, because the compiler does that anyway)
2019-10-08 10:25:44 -04:00
Shane Howearth
ed3d42e1ec Handle generated errors in server 2019-10-08 10:12:36 -04:00
Cecylia Bocovich
49042511a3 Refactored server log scrubber into package
The server log scrubbing code from ticket #21304 is now refactored into
a safelog package, along with the appropriate tests
2019-04-11 14:43:59 -04:00
Cecylia Bocovich
611cb889c5 Made regular expressions more precise
Modified regular expressions to not scrub fingerprints, but catch all
instances of IPv4 and IPv6 addresses. Expanded test cases with those
suggested by dcf.
2019-03-25 15:06:17 -04:00
Cecylia Bocovich
c6a4a4191f Modified log scrubber to handle split lines
Log scrubber now scrubs addresses even in the case where they are split
across calls to the scrubber's output io.Writer.

Added test cases to test that the writer behaves correctly across split
lines.
2019-03-25 15:06:08 -04:00
Cecylia Bocovich
1ea467c4cf Restructured scrubbing code and tests
It is now more readable, and the regexp's are only compiled once
2019-03-25 15:05:58 -04:00
Cecylia Bocovich
5bc8817028 Simplified log scrubber
IPv6 regexes didn't need to be that precise, added more tests for
edge-cases.
2019-03-25 15:05:47 -04:00
Cecylia Bocovich
f586a4bab8 Sanitize IP addresses from server log output
Added a scrubber that takes all logging output to the standard logger
and passes through a series of regular expressions to replace IP
addresses with safe strings (e.g., X.X.X.X:443).

Ensure server logs to stdout are also scrubbed
2019-03-25 15:05:31 -04:00
David Fifield
019e2cea23 Update server shutdown procedure.
Ignore SIGINT, honor TOR_PT_EXIT_ON_STDIN_CLOSE.
2018-03-21 00:53:31 -07:00
David Fifield
bdc1798adb Exit immediately after SIGTERM is there are no signals running.
Cf. https://bugs.torproject.org/24875
2018-03-21 00:51:04 -07:00
David Fifield
ea7b9c0223 Wait briefly after calling ListenAndServe{TLS} to see if it errors.
This is a port of commit e3f3054f8b74caa639a6d9be09702693af9a70e7 from
meek.

In the previous commit, we changed from separate Listen and Serve steps
to always calling ListenAndServe. However, we would really like to
immediately get feedback if any errors happen in the Listen step inside
the call, because it's much better for debugging if those errors get
reported to tor through SMETHOD-ERROR--rather than reporting success to
tor and actually logging an error only in the snowflake log. So we wait
100 ms for an error to occur before deciding that the Listen succeeded.

We don't need to apply this hack to the ACME HTTP-01 listener, because
it's a plaintext listener. Unlike in the TLS case, there isn't any
internal magic that the net library does that we have to rely on. We
just call net.ListenTCP and check for an error.
2018-03-13 19:18:52 -07:00
David Fifield
19b317e781 Use ListenAndServe{TLS} rather than separate Listen and Serve.
This is a port of commit cea86c937dc278ba6b2100c238b1d5206bbae2f0 from
meek. Its purpose is to remove the need to copy-paste parts of
net/http.Server.ListenAndServeTLS. Here is a copy of the commit message
from meek:

    The net/http package provides ListenAndServe and ListenAndServeTLS
    functions, but it doesn't provide a way to set up a listener without
    also entering an infinite serve loop. This matters for
    ListenAndServeTLS, which sets up a lot of magic behind the scenes for
    TLS and HTTP/2 support. Formerly, we had copy-pasted code from
    ListenAndServeTLS, but that code has only gotten more complicated in
    upstream net/http.

    The price we pay for this is that it's no longer possible for a server
    bindaddr to ask to listen on port 0 (i.e., a random ephemeral port).
    That's because we never get a change to find out what the listening
    address is, before entering the serve loop.

    What we gain is HTTP/2 support; formerly our copy-pasted code had the
    side effect of disabling HTTP/2, because it was copied from an older
    version and did things like
            config.NextProtos = []string{"http/1.1"}

    The new code calls http2.ConfigureServer first, but that's not what's
    providing HTTP/2 support. HTTP/2 support happens by default. The reason
    we call http2.ConfigureServer is because we need to set
    TLSConfig.GetCertificate, and http2.ConfigureServer is a convenient way
    to initialize TLSConfig in a way that is guaranteed to work with HTTP/2.
2018-03-13 19:18:52 -07:00
David Fifield
d0686b1c8d Use Manager.HTTPHandler for automatic TLS support in the server.
As with commit fcc274ac68 for the broker,
we need to start using the HTTP-01 challenge type in the Snowflake
websocket server transport plugin.
https://bugs.torproject.org/25346
2018-03-05 21:16:51 -08:00
David Fifield
ee4e0aa160 Trivial doc update: we do report the USERADDR now. 2018-03-05 15:27:27 -08:00
David Fifield
58556dc07b Keep track of clientAddr in statsChannel. 2017-10-17 22:19:43 -07:00
David Fifield
4697746120 Actually call clientAddr. 2017-10-17 22:19:23 -07:00
David Fifield
d9e8f8f647 Log once a day how many connections had client_ip.
This is a sanity check against any catastrophic failure of our parsing
code.
2017-10-17 22:12:21 -07:00
David Fifield
c84e1a2e03 Add a dummy port number to USERADDR.
Current versions of tor accept USERADDR with or without a port number,
but future versions may become more strict and require the port number.
https://bugs.torproject.org/23080
2017-10-17 22:12:21 -07:00
David Fifield
83f8712078 Factor out a function to extract the client IP address. 2017-10-17 22:12:21 -07:00
David Fifield
9e5eb7f5ee Pass the incoming client_ip into the ExtORPort. 2017-10-14 15:06:36 -04:00
David Fifield
db2251345d Close the log file in server. 2017-07-16 15:03:56 -07:00
David Fifield
a936fc7e9b README and documentation for server. 2017-01-21 14:53:51 -08:00
David Fifield
1f8be86a01 Add a DirCache for certificates under TOR_PT_STATE_LOCATION.
This way, we don't lose state of certificates every time the process is
restarted. There's a possibility, otherwise, that if you have to restart
the server rapidly, you might run into Let's Encrypt rate limits and be
unable to create a cert for a while.
https://godoc.org/rsc.io/letsencrypt#hdr-Persistent_Storage
2017-01-21 14:23:15 -08:00
David Fifield
b0826304a4 Make certManager a pointer and only set it when !disableTLS. 2017-01-21 14:01:43 -08:00
David Fifield
1b1fb37afe Add "hostname" args to the bridge descriptor as well. 2017-01-20 19:17:08 -08:00
David Fifield
80acfbd8d8 Explain more in usage. 2017-01-20 19:17:08 -08:00
David Fifield
b86bbd748d Add --acme-email option. 2017-01-20 19:17:08 -08:00