Stop exporting code that should be internal

This commit is contained in:
Cecylia Bocovich 2021-09-09 11:34:07 -04:00
parent 4396d505a3
commit 624750d5a8
7 changed files with 44 additions and 56 deletions

View file

@ -1,9 +1,5 @@
package lib package lib
import (
"net"
)
// Interface for catching Snowflakes. (aka the remote dialer) // Interface for catching Snowflakes. (aka the remote dialer)
type Tongue interface { type Tongue interface {
Catch() (*WebRTCPeer, error) Catch() (*WebRTCPeer, error)
@ -25,10 +21,3 @@ type SnowflakeCollector interface {
// Signal when the collector has stopped collecting. // Signal when the collector has stopped collecting.
Melted() <-chan struct{} Melted() <-chan struct{}
} }
// Interface to adapt to goptlib's SocksConn struct.
type SocksConnector interface {
Grant(*net.TCPAddr) error
Reject() error
net.Conn
}

View file

@ -21,7 +21,7 @@ import (
// version of Snowflake) // version of Snowflake)
type Peers struct { type Peers struct {
Tongue Tongue
BytesLogger BytesLogger bytesLogger bytesLogger
snowflakeChan chan *WebRTCPeer snowflakeChan chan *WebRTCPeer
activePeers *list.List activePeers *list.List
@ -88,7 +88,7 @@ func (p *Peers) Pop() *WebRTCPeer {
continue continue
} }
// Set to use the same rate-limited traffic logger to keep consistency. // Set to use the same rate-limited traffic logger to keep consistency.
snowflake.BytesLogger = p.BytesLogger snowflake.bytesLogger = p.bytesLogger
return snowflake return snowflake
} }
} }

View file

@ -42,14 +42,14 @@ type rendezvousMethod interface {
type BrokerChannel struct { type BrokerChannel struct {
rendezvous rendezvousMethod rendezvous rendezvousMethod
keepLocalAddresses bool keepLocalAddresses bool
NATType string natType string
lock sync.Mutex lock sync.Mutex
} }
// We make a copy of DefaultTransport because we want the default Dial // We make a copy of DefaultTransport because we want the default Dial
// and TLSHandshakeTimeout settings. But we want to disable the default // and TLSHandshakeTimeout settings. But we want to disable the default
// ProxyFromEnvironment setting. // ProxyFromEnvironment setting.
func CreateBrokerTransport() http.RoundTripper { func createBrokerTransport() http.RoundTripper {
transport := http.DefaultTransport.(*http.Transport) transport := http.DefaultTransport.(*http.Transport)
transport.Proxy = nil transport.Proxy = nil
transport.ResponseHeaderTimeout = 15 * time.Second transport.ResponseHeaderTimeout = 15 * time.Second
@ -59,7 +59,7 @@ func CreateBrokerTransport() http.RoundTripper {
// Construct a new BrokerChannel, where: // Construct a new BrokerChannel, where:
// |broker| is the full URL of the facilitating program which assigns proxies // |broker| is the full URL of the facilitating program which assigns proxies
// to clients, and |front| is the option fronting domain. // to clients, and |front| is the option fronting domain.
func NewBrokerChannel(broker, ampCache, front string, transport http.RoundTripper, keepLocalAddresses bool) (*BrokerChannel, error) { func NewBrokerChannel(broker, ampCache, front string, keepLocalAddresses bool) (*BrokerChannel, error) {
log.Println("Rendezvous using Broker at:", broker) log.Println("Rendezvous using Broker at:", broker)
if ampCache != "" { if ampCache != "" {
log.Println("Through AMP cache at:", ampCache) log.Println("Through AMP cache at:", ampCache)
@ -71,9 +71,9 @@ func NewBrokerChannel(broker, ampCache, front string, transport http.RoundTrippe
var rendezvous rendezvousMethod var rendezvous rendezvousMethod
var err error var err error
if ampCache != "" { if ampCache != "" {
rendezvous, err = newAMPCacheRendezvous(broker, ampCache, front, transport) rendezvous, err = newAMPCacheRendezvous(broker, ampCache, front, createBrokerTransport())
} else { } else {
rendezvous, err = newHTTPRendezvous(broker, front, transport) rendezvous, err = newHTTPRendezvous(broker, front, createBrokerTransport())
} }
if err != nil { if err != nil {
return nil, err return nil, err
@ -82,7 +82,7 @@ func NewBrokerChannel(broker, ampCache, front string, transport http.RoundTrippe
return &BrokerChannel{ return &BrokerChannel{
rendezvous: rendezvous, rendezvous: rendezvous,
keepLocalAddresses: keepLocalAddresses, keepLocalAddresses: keepLocalAddresses,
NATType: nat.NATUnknown, natType: nat.NATUnknown,
}, nil }, nil
} }
@ -110,7 +110,7 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
bc.lock.Lock() bc.lock.Lock()
req := &messages.ClientPollRequest{ req := &messages.ClientPollRequest{
Offer: offerSDP, Offer: offerSDP,
NAT: bc.NATType, NAT: bc.natType,
} }
encReq, err := req.EncodePollRequest() encReq, err := req.EncodePollRequest()
bc.lock.Unlock() bc.lock.Unlock()
@ -138,7 +138,7 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
func (bc *BrokerChannel) SetNATType(NATType string) { func (bc *BrokerChannel) SetNATType(NATType string) {
bc.lock.Lock() bc.lock.Lock()
bc.NATType = NATType bc.natType = NATType
bc.lock.Unlock() bc.lock.Unlock()
log.Printf("NAT Type: %s", NATType) log.Printf("NAT Type: %s", NATType)
} }

View file

@ -71,8 +71,7 @@ func NewSnowflakeClient(config ClientConfig) (*Transport, error) {
// Rendezvous with broker using the given parameters. // Rendezvous with broker using the given parameters.
broker, err := NewBrokerChannel( broker, err := NewBrokerChannel(
config.BrokerURL, config.AmpCacheURL, config.FrontDomain, CreateBrokerTransport(), config.BrokerURL, config.AmpCacheURL, config.FrontDomain, config.KeepLocalAddresses)
config.KeepLocalAddresses)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -103,7 +102,7 @@ func (t *Transport) Dial() (net.Conn, error) {
cleanup = append(cleanup, func() { snowflakes.End() }) cleanup = append(cleanup, func() { snowflakes.End() })
// Use a real logger to periodically output how much traffic is happening. // Use a real logger to periodically output how much traffic is happening.
snowflakes.BytesLogger = NewBytesSyncLogger() snowflakes.bytesLogger = newBytesSyncLogger()
log.Printf("---- SnowflakeConn: begin collecting snowflakes ---") log.Printf("---- SnowflakeConn: begin collecting snowflakes ---")
go connectLoop(snowflakes) go connectLoop(snowflakes)
@ -198,7 +197,7 @@ func newSession(snowflakes SnowflakeCollector) (net.PacketConn, *smux.Session, e
// We build a persistent KCP session on a sequence of ephemeral WebRTC // We build a persistent KCP session on a sequence of ephemeral WebRTC
// connections. This dialContext tells RedialPacketConn how to get a new // connections. This dialContext tells RedialPacketConn how to get a new
// WebRTC connection when the previous one dies. Inside each WebRTC // WebRTC connection when the previous one dies. Inside each WebRTC
// connection, we use EncapsulationPacketConn to encode packets into a // connection, we use encapsulationPacketConn to encode packets into a
// stream. // stream.
dialContext := func(ctx context.Context) (net.PacketConn, error) { dialContext := func(ctx context.Context) (net.PacketConn, error) {
log.Printf("redialing on same connection") log.Printf("redialing on same connection")
@ -218,7 +217,7 @@ func newSession(snowflakes SnowflakeCollector) (net.PacketConn, *smux.Session, e
if err != nil { if err != nil {
return nil, err return nil, err
} }
return NewEncapsulationPacketConn(dummyAddr{}, dummyAddr{}, conn), nil return newEncapsulationPacketConn(dummyAddr{}, dummyAddr{}, conn), nil
} }
pconn := turbotunnel.NewRedialPacketConn(dummyAddr{}, dummyAddr{}, dialContext) pconn := turbotunnel.NewRedialPacketConn(dummyAddr{}, dummyAddr{}, dialContext)

View file

@ -12,10 +12,10 @@ import (
var errNotImplemented = errors.New("not implemented") var errNotImplemented = errors.New("not implemented")
// EncapsulationPacketConn implements the net.PacketConn interface over an // encapsulationPacketConn implements the net.PacketConn interface over an
// io.ReadWriteCloser stream, using the encapsulation package to represent // io.ReadWriteCloser stream, using the encapsulation package to represent
// packets in a stream. // packets in a stream.
type EncapsulationPacketConn struct { type encapsulationPacketConn struct {
io.ReadWriteCloser io.ReadWriteCloser
localAddr net.Addr localAddr net.Addr
remoteAddr net.Addr remoteAddr net.Addr
@ -23,11 +23,11 @@ type EncapsulationPacketConn struct {
} }
// NewEncapsulationPacketConn makes // NewEncapsulationPacketConn makes
func NewEncapsulationPacketConn( func newEncapsulationPacketConn(
localAddr, remoteAddr net.Addr, localAddr, remoteAddr net.Addr,
conn io.ReadWriteCloser, conn io.ReadWriteCloser,
) *EncapsulationPacketConn { ) *encapsulationPacketConn {
return &EncapsulationPacketConn{ return &encapsulationPacketConn{
ReadWriteCloser: conn, ReadWriteCloser: conn,
localAddr: localAddr, localAddr: localAddr,
remoteAddr: remoteAddr, remoteAddr: remoteAddr,
@ -36,7 +36,7 @@ func NewEncapsulationPacketConn(
} }
// ReadFrom reads an encapsulated packet from the stream. // ReadFrom reads an encapsulated packet from the stream.
func (c *EncapsulationPacketConn) ReadFrom(p []byte) (int, net.Addr, error) { func (c *encapsulationPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
data, err := encapsulation.ReadData(c.ReadWriteCloser) data, err := encapsulation.ReadData(c.ReadWriteCloser)
if err != nil { if err != nil {
return 0, c.remoteAddr, err return 0, c.remoteAddr, err
@ -45,7 +45,7 @@ func (c *EncapsulationPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
} }
// WriteTo writes an encapsulated packet to the stream. // WriteTo writes an encapsulated packet to the stream.
func (c *EncapsulationPacketConn) WriteTo(p []byte, addr net.Addr) (int, error) { func (c *encapsulationPacketConn) WriteTo(p []byte, addr net.Addr) (int, error) {
// addr is ignored. // addr is ignored.
_, err := encapsulation.WriteData(c.bw, p) _, err := encapsulation.WriteData(c.bw, p)
if err == nil { if err == nil {
@ -59,10 +59,10 @@ func (c *EncapsulationPacketConn) WriteTo(p []byte, addr net.Addr) (int, error)
// LocalAddr returns the localAddr value that was passed to // LocalAddr returns the localAddr value that was passed to
// NewEncapsulationPacketConn. // NewEncapsulationPacketConn.
func (c *EncapsulationPacketConn) LocalAddr() net.Addr { func (c *encapsulationPacketConn) LocalAddr() net.Addr {
return c.localAddr return c.localAddr
} }
func (c *EncapsulationPacketConn) SetDeadline(t time.Time) error { return errNotImplemented } func (c *encapsulationPacketConn) SetDeadline(t time.Time) error { return errNotImplemented }
func (c *EncapsulationPacketConn) SetReadDeadline(t time.Time) error { return errNotImplemented } func (c *encapsulationPacketConn) SetReadDeadline(t time.Time) error { return errNotImplemented }
func (c *EncapsulationPacketConn) SetWriteDeadline(t time.Time) error { return errNotImplemented } func (c *encapsulationPacketConn) SetWriteDeadline(t time.Time) error { return errNotImplemented }

View file

@ -9,27 +9,27 @@ const (
LogTimeInterval = 5 * time.Second LogTimeInterval = 5 * time.Second
) )
type BytesLogger interface { type bytesLogger interface {
AddOutbound(int) addOutbound(int)
AddInbound(int) addInbound(int)
} }
// Default BytesLogger does nothing. // Default bytesLogger does nothing.
type BytesNullLogger struct{} type bytesNullLogger struct{}
func (b BytesNullLogger) AddOutbound(amount int) {} func (b bytesNullLogger) addOutbound(amount int) {}
func (b BytesNullLogger) AddInbound(amount int) {} func (b bytesNullLogger) addInbound(amount int) {}
// BytesSyncLogger uses channels to safely log from multiple sources with output // bytesSyncLogger uses channels to safely log from multiple sources with output
// occuring at reasonable intervals. // occuring at reasonable intervals.
type BytesSyncLogger struct { type bytesSyncLogger struct {
outboundChan chan int outboundChan chan int
inboundChan chan int inboundChan chan int
} }
// NewBytesSyncLogger returns a new BytesSyncLogger and starts it loggin. // newBytesSyncLogger returns a new bytesSyncLogger and starts it loggin.
func NewBytesSyncLogger() *BytesSyncLogger { func newBytesSyncLogger() *bytesSyncLogger {
b := &BytesSyncLogger{ b := &bytesSyncLogger{
outboundChan: make(chan int, 5), outboundChan: make(chan int, 5),
inboundChan: make(chan int, 5), inboundChan: make(chan int, 5),
} }
@ -37,7 +37,7 @@ func NewBytesSyncLogger() *BytesSyncLogger {
return b return b
} }
func (b *BytesSyncLogger) log() { func (b *bytesSyncLogger) log() {
var outbound, inbound, outEvents, inEvents int var outbound, inbound, outEvents, inEvents int
ticker := time.NewTicker(LogTimeInterval) ticker := time.NewTicker(LogTimeInterval)
for { for {
@ -61,10 +61,10 @@ func (b *BytesSyncLogger) log() {
} }
} }
func (b *BytesSyncLogger) AddOutbound(amount int) { func (b *bytesSyncLogger) addOutbound(amount int) {
b.outboundChan <- amount b.outboundChan <- amount
} }
func (b *BytesSyncLogger) AddInbound(amount int) { func (b *bytesSyncLogger) addInbound(amount int) {
b.inboundChan <- amount b.inboundChan <- amount
} }

View file

@ -32,7 +32,7 @@ type WebRTCPeer struct {
once sync.Once // Synchronization for PeerConnection destruction once sync.Once // Synchronization for PeerConnection destruction
BytesLogger BytesLogger bytesLogger bytesLogger
} }
// Construct a WebRTC PeerConnection. // Construct a WebRTC PeerConnection.
@ -49,7 +49,7 @@ func NewWebRTCPeer(config *webrtc.Configuration,
connection.closed = make(chan struct{}) connection.closed = make(chan struct{})
// Override with something that's not NullLogger to have real logging. // Override with something that's not NullLogger to have real logging.
connection.BytesLogger = &BytesNullLogger{} connection.bytesLogger = &bytesNullLogger{}
// Pipes remain the same even when DataChannel gets switched. // Pipes remain the same even when DataChannel gets switched.
connection.recvPipe, connection.writePipe = io.Pipe() connection.recvPipe, connection.writePipe = io.Pipe()
@ -75,7 +75,7 @@ func (c *WebRTCPeer) Write(b []byte) (int, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
c.BytesLogger.AddOutbound(len(b)) c.bytesLogger.addOutbound(len(b))
return len(b), nil return len(b), nil
} }
@ -186,7 +186,7 @@ func (c *WebRTCPeer) preparePeerConnection(config *webrtc.Configuration) error {
log.Println("0 length message---") log.Println("0 length message---")
} }
n, err := c.writePipe.Write(msg.Data) n, err := c.writePipe.Write(msg.Data)
c.BytesLogger.AddInbound(n) c.bytesLogger.addInbound(n)
if err != nil { if err != nil {
// TODO: Maybe shouldn't actually close. // TODO: Maybe shouldn't actually close.
log.Println("Error writing to SOCKS pipe") log.Println("Error writing to SOCKS pipe")