Add client abridged protocol

This commit is contained in:
9seconds
2019-10-10 07:22:08 +03:00
parent 1a7eee444e
commit e81c5970d4
32 changed files with 538 additions and 174 deletions
+15 -2
View File
@@ -7,6 +7,7 @@ import (
"github.com/9seconds/mtg/conntypes"
"github.com/9seconds/mtg/mtproto"
"github.com/9seconds/mtg/mtproto/rpc"
"github.com/9seconds/mtg/protocol"
)
@@ -73,6 +74,8 @@ func (c *connection) idle() bool {
}
func (c *connection) run() {
logger := c.hub.logger.Named("connection").With("id", c.id)
for {
packet, err := c.read()
if err != nil {
@@ -80,8 +83,18 @@ func (c *connection) run() {
return
}
if channel, ok := Registry.getChannel(conntypes.ConnID{}); ok {
go channel.write(packet) // nolint: errcheck
response, err := rpc.ParseProxyResponse(packet)
if err != nil {
logger.Debugw("Failed response", "error", err)
continue
}
if response.Type == rpc.ProxyResponseTypeCloseExt {
logger.Debugw("Proxy has closed connection")
return
}
if channel, ok := Registry.getChannel(response.ConnID); ok {
go channel.sendBack(response) // nolint: errcheck
}
}
}
+5 -1
View File
@@ -3,6 +3,8 @@ package hub
import (
"time"
"go.uber.org/zap"
"github.com/9seconds/mtg/protocol"
)
@@ -15,6 +17,7 @@ type connectionHubRequest struct {
type connectionHub struct {
sockets map[int]*connection
logger *zap.SugaredLogger
channelBrokenSockets chan int
channelConnectionRequests chan *connectionHubRequest
@@ -76,8 +79,9 @@ func (c *connectionHub) runReturnConnection(conn *connection) {
c.sockets[conn.id] = conn
}
func newConnectionHub() *connectionHub {
func newConnectionHub(logger *zap.SugaredLogger) *connectionHub {
rv := &connectionHub{
logger: logger.Named("connection-hub"),
sockets: map[int]*connection{},
channelBrokenSockets: make(chan int, 1),
channelConnectionRequests: make(chan *connectionHubRequest),
+7 -7
View File
@@ -4,23 +4,23 @@ import (
"context"
"time"
"github.com/9seconds/mtg/conntypes"
"github.com/9seconds/mtg/mtproto/rpc"
)
const closeableChannelReadTimeout = 2 * time.Minute
type ChannelReadCloser interface {
Read() (conntypes.Packet, error)
Read() (*rpc.ProxyResponse, error)
Close() error
}
type ctxChannel struct {
channel chan conntypes.Packet
channel chan *rpc.ProxyResponse
ctx context.Context
cancel context.CancelFunc
}
func (c *ctxChannel) Read() (conntypes.Packet, error) {
func (c *ctxChannel) Read() (*rpc.ProxyResponse, error) {
timer := time.NewTimer(closeableChannelReadTimeout)
defer timer.Stop()
@@ -34,11 +34,11 @@ func (c *ctxChannel) Read() (conntypes.Packet, error) {
}
}
func (c *ctxChannel) write(packet conntypes.Packet) error {
func (c *ctxChannel) sendBack(response *rpc.ProxyResponse) error {
select {
case <-c.ctx.Done():
return ErrClosed
case c.channel <- packet:
case c.channel <- response:
return nil
}
}
@@ -52,7 +52,7 @@ func (c *ctxChannel) Close() error {
func newCtxChannel(ctx context.Context) *ctxChannel {
ctx, cancel := context.WithCancel(ctx)
return &ctxChannel{
channel: make(chan conntypes.Packet),
channel: make(chan *rpc.ProxyResponse),
ctx: ctx,
cancel: cancel,
}
+9 -3
View File
@@ -6,13 +6,16 @@ import (
"strings"
"sync"
"go.uber.org/zap"
"github.com/9seconds/mtg/conntypes"
"github.com/9seconds/mtg/protocol"
)
type hub struct {
subs map[string]*connectionHub
mutex sync.RWMutex
logger *zap.SugaredLogger
subs map[string]*connectionHub
mutex sync.RWMutex
}
func (h *hub) Write(packet conntypes.Packet, req *protocol.TelegramRequest) error {
@@ -51,7 +54,10 @@ func (h *hub) getHub(req *protocol.TelegramRequest) *connectionHub {
rv, ok = h.subs[key]
if !ok {
rv = newConnectionHub()
rv = newConnectionHub(h.logger.With(
"dc", req.ClientProtocol.DC(),
"protocol", req.ClientProtocol.ConnectionProtocol(),
))
h.subs[key] = rv
}
}
+4 -1
View File
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"sync"
"go.uber.org/zap"
)
var (
@@ -24,7 +26,8 @@ func Init(ctx context.Context) {
ctx: ctx,
}
Hub = &hub{
subs: map[string]*connectionHub{},
subs: map[string]*connectionHub{},
logger: zap.S().Named("hub"),
}
})
}