2015-10-11 04:33:07 +00:00
|
|
|
package goricochet
|
|
|
|
|
|
|
|
import (
|
2015-10-12 23:03:48 +00:00
|
|
|
"errors"
|
2016-06-27 01:56:23 +00:00
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
2016-10-02 04:28:57 +00:00
|
|
|
"io"
|
2015-10-11 04:33:07 +00:00
|
|
|
"net"
|
2016-10-09 00:51:13 +00:00
|
|
|
"sync"
|
2015-10-11 04:33:07 +00:00
|
|
|
)
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// Connect sets up a client ricochet connection to host e.g. qn6uo4cmsrfv4kzq.onion. If this
|
2015-10-11 04:33:07 +00:00
|
|
|
// function finished successfully then the connection can be assumed to
|
|
|
|
// be open and authenticated.
|
2016-01-03 23:32:55 +00:00
|
|
|
// To specify a local port using the format "127.0.0.1:[port]|ricochet-id".
|
2016-10-09 00:51:13 +00:00
|
|
|
func Connect(host string) (*OpenConnection, error) {
|
|
|
|
networkResolver := utils.NetworkResolver{}
|
|
|
|
conn, host, err := networkResolver.Resolve(host)
|
2015-10-11 04:33:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-06-27 01:56:23 +00:00
|
|
|
return nil, err
|
2015-10-11 04:33:07 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
return Open(conn, host)
|
2016-10-01 22:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
// Open establishes a protocol session on an established net.Conn, and returns a new
|
|
|
|
// OpenConnection instance representing this connection. On error, the connection
|
|
|
|
// will be closed. This function blocks until version negotiation has completed.
|
|
|
|
// The application should call Process() on the returned OpenConnection to continue
|
|
|
|
// handling protocol messages.
|
|
|
|
func Open(conn net.Conn, remoteHostname string) (*OpenConnection, error) {
|
|
|
|
oc, err := negotiateVersion(conn, true)
|
2016-01-06 04:42:49 +00:00
|
|
|
if err != nil {
|
2016-10-09 00:51:13 +00:00
|
|
|
conn.Close()
|
2016-06-27 01:56:23 +00:00
|
|
|
return nil, err
|
2015-10-12 23:03:48 +00:00
|
|
|
}
|
2016-10-09 00:51:13 +00:00
|
|
|
oc.OtherHostname = remoteHostname
|
2016-06-27 01:56:23 +00:00
|
|
|
return oc, nil
|
2015-10-12 23:03:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
// Serve accepts incoming connections on a net.Listener, negotiates protocol,
|
|
|
|
// and calls methods of the ServiceHandler to handle inbound connections. All
|
|
|
|
// calls to ServiceHandler happen on the caller's goroutine. The listener can
|
|
|
|
// be closed at any time to close the service.
|
|
|
|
func Serve(ln net.Listener, handler ServiceHandler) error {
|
|
|
|
defer ln.Close()
|
2016-11-03 16:30:16 +00:00
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
connChannel := make(chan interface{})
|
|
|
|
listenErrorChannel := make(chan error)
|
2016-10-02 22:39:08 +00:00
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
go func() {
|
|
|
|
var pending sync.WaitGroup
|
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
2016-10-02 22:39:08 +00:00
|
|
|
if err != nil {
|
2016-10-09 00:51:13 +00:00
|
|
|
// Wait for pending connections before returning an error; this
|
|
|
|
// prevents abandoned goroutines when the outer loop stops reading
|
|
|
|
// from connChannel.
|
|
|
|
pending.Wait()
|
|
|
|
listenErrorChannel <- err
|
|
|
|
close(connChannel)
|
|
|
|
return
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
pending.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer pending.Done()
|
|
|
|
oc, err := negotiateVersion(conn, false)
|
2016-02-29 00:18:25 +00:00
|
|
|
if err != nil {
|
2016-10-09 00:51:13 +00:00
|
|
|
conn.Close()
|
|
|
|
connChannel <- err
|
2016-06-27 01:56:23 +00:00
|
|
|
} else {
|
2016-10-09 00:51:13 +00:00
|
|
|
connChannel <- oc
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
2016-10-09 00:51:13 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var listenErr error
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-listenErrorChannel:
|
|
|
|
// Remember error, wait for connChannel to close
|
|
|
|
listenErr = err
|
|
|
|
|
|
|
|
case result, ok := <-connChannel:
|
|
|
|
if !ok {
|
|
|
|
return listenErr
|
2016-10-02 22:39:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 00:51:13 +00:00
|
|
|
switch v := result.(type) {
|
|
|
|
case *OpenConnection:
|
|
|
|
handler.OnNewConnection(v)
|
|
|
|
case error:
|
|
|
|
handler.OnFailedConnection(v)
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 00:51:13 +00:00
|
|
|
|
|
|
|
return nil
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 04:28:57 +00:00
|
|
|
// Perform version negotiation on the connection, and create an OpenConnection if successful
|
2016-10-09 00:51:13 +00:00
|
|
|
func negotiateVersion(conn net.Conn, outbound bool) (*OpenConnection, error) {
|
2016-10-02 04:28:57 +00:00
|
|
|
versions := []byte{0x49, 0x4D, 0x01, 0x01}
|
2016-01-02 01:43:30 +00:00
|
|
|
|
2016-10-02 04:28:57 +00:00
|
|
|
// Outbound side of the connection sends a list of supported versions
|
2016-06-27 01:56:23 +00:00
|
|
|
if outbound {
|
2016-10-02 04:28:57 +00:00
|
|
|
if n, err := conn.Write(versions); err != nil || n < len(versions) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-02 01:43:30 +00:00
|
|
|
|
2016-10-02 04:28:57 +00:00
|
|
|
res := make([]byte, 1)
|
|
|
|
if _, err := io.ReadAtLeast(conn, res, len(res)); err != nil {
|
|
|
|
return nil, err
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 04:28:57 +00:00
|
|
|
if res[0] != 0x01 {
|
|
|
|
return nil, errors.New("unsupported protocol version")
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
2016-06-27 01:56:23 +00:00
|
|
|
} else {
|
2016-10-02 04:28:57 +00:00
|
|
|
// Read version response header
|
|
|
|
header := make([]byte, 3)
|
|
|
|
if _, err := io.ReadAtLeast(conn, header, len(header)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if header[0] != versions[0] || header[1] != versions[1] || header[2] < 1 {
|
|
|
|
return nil, errors.New("invalid protocol response")
|
|
|
|
}
|
2016-01-02 08:47:32 +00:00
|
|
|
|
2016-10-02 04:28:57 +00:00
|
|
|
// Read list of supported versions (which is header[2] bytes long)
|
|
|
|
versionList := make([]byte, header[2])
|
|
|
|
if _, err := io.ReadAtLeast(conn, versionList, len(versionList)); err != nil {
|
2016-06-27 01:56:23 +00:00
|
|
|
return nil, err
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 04:28:57 +00:00
|
|
|
selectedVersion := byte(0xff)
|
|
|
|
for _, v := range versionList {
|
|
|
|
if v == 0x01 {
|
|
|
|
selectedVersion = v
|
|
|
|
break
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
2016-10-02 04:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := conn.Write([]byte{selectedVersion}); err != nil || n < 1 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if selectedVersion == 0xff {
|
|
|
|
return nil, errors.New("no supported protocol version")
|
2016-06-27 01:56:23 +00:00
|
|
|
}
|
2016-01-02 01:43:30 +00:00
|
|
|
}
|
2016-01-03 23:32:55 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
oc := new(OpenConnection)
|
|
|
|
oc.Init(outbound, conn)
|
|
|
|
return oc, nil
|
2016-01-03 23:32:55 +00:00
|
|
|
}
|