2021-10-08 12:39:31 +00:00
|
|
|
// SPDX-FileCopyrightText: 2021 The NGI Pointer Secure-Scuttlebutt Team of 2020/2021
|
|
|
|
//
|
2021-02-09 11:53:33 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-01-25 12:23:03 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-29 10:23:11 +00:00
|
|
|
"encoding/base64"
|
2021-01-25 12:23:03 +00:00
|
|
|
"errors"
|
2021-03-29 10:23:11 +00:00
|
|
|
"fmt"
|
2021-01-25 12:23:03 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-04-19 12:52:12 +00:00
|
|
|
"net/url"
|
2021-01-25 12:23:03 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.cryptoscope.co/muxrpc/v2"
|
|
|
|
"go.cryptoscope.co/netwrap"
|
|
|
|
"go.cryptoscope.co/secretstream"
|
|
|
|
refs "go.mindeco.de/ssb-refs"
|
|
|
|
)
|
|
|
|
|
2021-03-29 10:23:11 +00:00
|
|
|
// ServerEndpointDetails encapsulates the endpoint information.
|
|
|
|
// Like domain name of the room, it's ssb/secret-handshake public key and the HTTP and MUXRPC TCP ports.
|
|
|
|
type ServerEndpointDetails struct {
|
|
|
|
RoomID refs.FeedRef
|
|
|
|
|
2021-04-19 12:52:12 +00:00
|
|
|
ListenAddressMUXRPC string // defaults to ":8008"
|
|
|
|
|
|
|
|
// Domain sets the DNS name for all the HTTP(S) URLs.
|
|
|
|
Domain string
|
|
|
|
PortHTTPS uint // 0 assumes default (443)
|
|
|
|
|
|
|
|
// UseSubdomainForAliases controls wether urls for alias resolving
|
|
|
|
// are generated as https://$alias.$domain instead of https://$domain/alias/$alias
|
|
|
|
UseSubdomainForAliases bool
|
2021-03-26 19:08:13 +00:00
|
|
|
|
2021-04-19 12:52:12 +00:00
|
|
|
// Development instructs url building to happen with http and include the http port
|
2021-03-26 19:08:13 +00:00
|
|
|
Development bool
|
2021-03-29 10:23:11 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 12:52:12 +00:00
|
|
|
func (sed ServerEndpointDetails) URLForAlias(a string) string {
|
|
|
|
var u url.URL
|
|
|
|
|
|
|
|
if sed.Development {
|
|
|
|
u.Path = "/alias/" + a
|
|
|
|
u.Scheme = "http"
|
|
|
|
u.Host = fmt.Sprintf("localhost:%d", sed.PortHTTPS)
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Scheme = "https"
|
|
|
|
|
|
|
|
if sed.UseSubdomainForAliases {
|
|
|
|
u.Host = a + "." + sed.Domain
|
|
|
|
} else {
|
|
|
|
u.Path = "/alias/" + a
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:23:11 +00:00
|
|
|
// MultiserverAddress returns net:domain:muxport~shs:roomPubKeyInBase64
|
|
|
|
// ie: the room servers https://github.com/ssbc/multiserver-address
|
|
|
|
func (sed ServerEndpointDetails) MultiserverAddress() string {
|
2021-04-19 12:52:12 +00:00
|
|
|
addr, err := net.ResolveTCPAddr("tcp", sed.ListenAddressMUXRPC)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-29 10:23:11 +00:00
|
|
|
var roomPubKey = base64.StdEncoding.EncodeToString(sed.RoomID.PubKey())
|
2021-04-19 12:52:12 +00:00
|
|
|
return fmt.Sprintf("net:%s:%d~shs:%s", sed.Domain, addr.Port, roomPubKey)
|
2021-03-29 10:23:11 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 12:23:03 +00:00
|
|
|
// EndpointStat gives some information about a connected peer
|
|
|
|
type EndpointStat struct {
|
|
|
|
ID *refs.FeedRef
|
|
|
|
Addr net.Addr
|
|
|
|
Since time.Duration
|
|
|
|
Endpoint muxrpc.Endpoint
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:33:58 +00:00
|
|
|
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o mocked/endpoints.go . Endpoints
|
2021-03-17 09:46:05 +00:00
|
|
|
|
2021-03-26 08:47:52 +00:00
|
|
|
// Endpoints returns the connected endpoint for the passed feed,
|
|
|
|
// or false if there is none.
|
2021-03-17 09:46:05 +00:00
|
|
|
type Endpoints interface {
|
|
|
|
GetEndpointFor(refs.FeedRef) (muxrpc.Endpoint, bool)
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:47:52 +00:00
|
|
|
// Network supplies all network related functionalitiy
|
2021-01-25 12:23:03 +00:00
|
|
|
type Network interface {
|
|
|
|
Connect(ctx context.Context, addr net.Addr) error
|
|
|
|
Serve(context.Context, ...muxrpc.HandlerWrapper) error
|
|
|
|
GetListenAddr() net.Addr
|
|
|
|
|
|
|
|
GetAllEndpoints() []EndpointStat
|
2021-03-17 09:46:05 +00:00
|
|
|
Endpoints
|
2021-01-25 12:23:03 +00:00
|
|
|
|
|
|
|
GetConnTracker() ConnTracker
|
|
|
|
|
2021-04-20 15:13:50 +00:00
|
|
|
// WebsockHandler returns a "middleware" like thing that is able to upgrade a
|
|
|
|
// websocket request to a muxrpc connection and authenticate using shs.
|
2021-04-05 09:33:28 +00:00
|
|
|
// It calls the next handler if it fails to upgrade the connection to websocket.
|
|
|
|
// However, it will error on the request and not call the passed handler
|
|
|
|
// if the websocket upgrade is successfull.
|
|
|
|
WebsockHandler(next http.Handler) http.Handler
|
2021-01-25 12:23:03 +00:00
|
|
|
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnTracker decides if connections should be established and keeps track of them
|
|
|
|
type ConnTracker interface {
|
|
|
|
// Active returns true and since when a peer connection is active
|
|
|
|
Active(net.Addr) (bool, time.Duration)
|
|
|
|
|
|
|
|
// OnAccept receives a new connection as an argument.
|
|
|
|
// If it decides to accept it, it returns true and a context that will be canceled once it should shut down
|
|
|
|
// If it decides to deny it, it returns false (and a nil context)
|
|
|
|
OnAccept(context.Context, net.Conn) (bool, context.Context)
|
|
|
|
|
|
|
|
// OnClose notifies the tracker that a connection was closed
|
|
|
|
OnClose(conn net.Conn) time.Duration
|
|
|
|
|
|
|
|
// Count returns the number of open connections
|
|
|
|
Count() uint
|
|
|
|
|
|
|
|
// CloseAll closes all tracked connections
|
|
|
|
CloseAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeedRefFromAddr uses netwrap to get the secretstream address and then uses ParseFeedRef
|
|
|
|
func GetFeedRefFromAddr(addr net.Addr) (*refs.FeedRef, error) {
|
|
|
|
addr = netwrap.GetAddr(addr, secretstream.NetworkString)
|
|
|
|
if addr == nil {
|
|
|
|
return nil, errors.New("no shs-bs address found")
|
|
|
|
}
|
|
|
|
ssAddr := addr.(secretstream.Addr)
|
|
|
|
return refs.ParseFeedRef(ssAddr.String())
|
|
|
|
}
|