mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 16:01:55 +03:00
Fix lint errors
This commit is contained in:
Generated
+1
-1
@@ -86,6 +86,6 @@
|
|||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "312c9fb15085cbe9660443b15a07981990e1f70ec3ddfcce1b7e6cd5902307da"
|
inputs-digest = "c4fdd3664f683342ad0c2509f4a8bcfe5b267a6e8cdaf36f70d39536bbf89834"
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|||||||
@@ -8,4 +8,5 @@ import (
|
|||||||
"github.com/9seconds/mtg/wrappers"
|
"github.com/9seconds/mtg/wrappers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Init defines common method for initializing client connections.
|
||||||
type Init func(net.Conn, string, *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error)
|
type Init func(net.Conn, string, *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error)
|
||||||
|
|||||||
+5
-3
@@ -18,6 +18,8 @@ const (
|
|||||||
writeBufferSize = 64 * 1024
|
writeBufferSize = 64 * 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DirectInit initializes client connection for proxy which connects to
|
||||||
|
// Telegram directly.
|
||||||
func DirectInit(socket net.Conn, connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
|
func DirectInit(socket net.Conn, connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
|
||||||
tcpSocket := socket.(*net.TCPConn)
|
tcpSocket := socket.(*net.TCPConn)
|
||||||
if err := tcpSocket.SetNoDelay(false); err != nil {
|
if err := tcpSocket.SetNoDelay(false); err != nil {
|
||||||
@@ -30,14 +32,14 @@ func DirectInit(socket net.Conn, connID string, conf *config.Config) (wrappers.W
|
|||||||
return nil, nil, errors.Annotate(err, "Cannot set write buffer size of client socket")
|
return nil, nil, errors.Annotate(err, "Cannot set write buffer size of client socket")
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.SetReadDeadline(time.Now().Add(handshakeTimeout))
|
socket.SetReadDeadline(time.Now().Add(handshakeTimeout)) // nolint: errcheck
|
||||||
frame, err := obfuscated2.ExtractFrame(socket)
|
frame, err := obfuscated2.ExtractFrame(socket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, errors.Annotate(err, "Cannot extract frame")
|
return nil, nil, errors.Annotate(err, "Cannot extract frame")
|
||||||
}
|
}
|
||||||
socket.SetReadDeadline(time.Time{})
|
socket.SetReadDeadline(time.Time{}) // nolint: errcheck
|
||||||
conn := wrappers.NewConn(socket, connID, wrappers.ConnPurposeClient, conf.PublicIPv4, conf.PublicIPv6)
|
|
||||||
|
|
||||||
|
conn := wrappers.NewConn(socket, connID, wrappers.ConnPurposeClient, conf.PublicIPv4, conf.PublicIPv6)
|
||||||
obfs2, connOpts, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
|
obfs2, connOpts, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
|
return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
"github.com/9seconds/mtg/wrappers"
|
"github.com/9seconds/mtg/wrappers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MiddleInit initializes client connection for proxy which has to
|
||||||
|
// support promoted channels, connect to Telegram middle proxies etc.
|
||||||
func MiddleInit(socket net.Conn, connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
|
func MiddleInit(socket net.Conn, connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
|
||||||
conn, opts, err := DirectInit(socket, connID, conf)
|
conn, opts, err := DirectInit(socket, connID, conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -58,6 +58,8 @@ func (c *Config) StatAddr() string {
|
|||||||
return getAddr(c.StatsIP, c.StatsPort)
|
return getAddr(c.StatsIP, c.StatsPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UseMiddleProxy defines if this proxy has to connect middle proxies
|
||||||
|
// which supports promoted channels or directly access Telegram.
|
||||||
func (c *Config) UseMiddleProxy() bool {
|
func (c *Config) UseMiddleProxy() bool {
|
||||||
return len(c.AdTag) > 0
|
return len(c.AdTag) > 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ func main() {
|
|||||||
atom,
|
atom,
|
||||||
))
|
))
|
||||||
zap.ReplaceGlobals(logger)
|
zap.ReplaceGlobals(logger)
|
||||||
defer logger.Sync()
|
defer logger.Sync() // nolint: errcheck
|
||||||
|
|
||||||
printURLs(conf.GetURLs())
|
printURLs(conf.GetURLs())
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,10 @@ import (
|
|||||||
// by the user.
|
// by the user.
|
||||||
type ConnectionType uint8
|
type ConnectionType uint8
|
||||||
|
|
||||||
|
// ConnectionProtocol is a type of IP protocol to use.
|
||||||
type ConnectionProtocol uint8
|
type ConnectionProtocol uint8
|
||||||
|
|
||||||
|
// Hacks is a simple structure to store flags for packet transmission.
|
||||||
type Hacks struct {
|
type Hacks struct {
|
||||||
SimpleAck bool
|
SimpleAck bool
|
||||||
QuickAck bool
|
QuickAck bool
|
||||||
@@ -24,9 +26,12 @@ type ConnectionOpts struct {
|
|||||||
DC int16
|
DC int16
|
||||||
ConnectionType ConnectionType
|
ConnectionType ConnectionType
|
||||||
ConnectionProto ConnectionProtocol
|
ConnectionProto ConnectionProtocol
|
||||||
ReadHacks Hacks
|
// Read and Write means direction related to the client.
|
||||||
WriteHacks Hacks
|
// ReadHacks are meant to be flushed on client read
|
||||||
ClientAddr *net.TCPAddr
|
// WriteHacks are meant to be flushed on client write.
|
||||||
|
ReadHacks Hacks
|
||||||
|
WriteHacks Hacks
|
||||||
|
ClientAddr *net.TCPAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Different connection types which user requests from Telegram.
|
// Different connection types which user requests from Telegram.
|
||||||
@@ -36,6 +41,8 @@ const (
|
|||||||
ConnectionTypeIntermediate
|
ConnectionTypeIntermediate
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConnectionProtocol* define which connection protocols to use.
|
||||||
|
// ConnectionProtocolAny means that any is suitable.
|
||||||
const (
|
const (
|
||||||
ConnectionProtocolIPv4 ConnectionProtocol = 1
|
ConnectionProtocolIPv4 ConnectionProtocol = 1
|
||||||
ConnectionProtocolIPv6 = ConnectionProtocolIPv4 << 1
|
ConnectionProtocolIPv6 = ConnectionProtocolIPv4 << 1
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ package rpc
|
|||||||
|
|
||||||
import "bytes"
|
import "bytes"
|
||||||
|
|
||||||
|
// HandshakeRequest is the data type which is responsible for
|
||||||
|
// constructing of correct handshake request.
|
||||||
type HandshakeRequest struct {
|
type HandshakeRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bytes returns serialized handshake request.
|
||||||
func (r *HandshakeRequest) Bytes() []byte {
|
func (r *HandshakeRequest) Bytes() []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
buf.Grow(len(TagHandshake) + len(HandshakeFlags) + len(HandshakeSenderPID) + len(HandshakePeerPID))
|
buf.Grow(len(TagHandshake) + len(HandshakeFlags) + len(HandshakeSenderPID) + len(HandshakePeerPID))
|
||||||
@@ -17,6 +20,7 @@ func (r *HandshakeRequest) Bytes() []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewHandshakeRequest creates new HandshakeRequest instance.
|
||||||
func NewHandshakeRequest() *HandshakeRequest {
|
func NewHandshakeRequest() *HandshakeRequest {
|
||||||
return &HandshakeRequest{}
|
return &HandshakeRequest{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// HandshakeResponse defines data structure which is used for storage of
|
||||||
|
// handshake response.
|
||||||
type HandshakeResponse struct {
|
type HandshakeResponse struct {
|
||||||
Type []byte
|
Type []byte
|
||||||
Flags []byte
|
Flags []byte
|
||||||
@@ -13,6 +15,7 @@ type HandshakeResponse struct {
|
|||||||
PeerPID []byte
|
PeerPID []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bytes returns a serialized handshake response.
|
||||||
func (r *HandshakeResponse) Bytes() []byte {
|
func (r *HandshakeResponse) Bytes() []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
@@ -24,6 +27,7 @@ func (r *HandshakeResponse) Bytes() []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Valid checks that handshake response compliments request.
|
||||||
func (r *HandshakeResponse) Valid(req *HandshakeRequest) error {
|
func (r *HandshakeResponse) Valid(req *HandshakeRequest) error {
|
||||||
if !bytes.Equal(r.Type, TagHandshake) {
|
if !bytes.Equal(r.Type, TagHandshake) {
|
||||||
return errors.New("Unexpected handshake tag")
|
return errors.New("Unexpected handshake tag")
|
||||||
@@ -35,6 +39,8 @@ func (r *HandshakeResponse) Valid(req *HandshakeRequest) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewHandshakeResponse constructs new handshake response from the given
|
||||||
|
// data.
|
||||||
func NewHandshakeResponse(data []byte) (*HandshakeResponse, error) {
|
func NewHandshakeResponse(data []byte) (*HandshakeResponse, error) {
|
||||||
if len(data) != 32 {
|
if len(data) != 32 {
|
||||||
return nil, errors.New("Incorrect handshake response length")
|
return nil, errors.New("Incorrect handshake response length")
|
||||||
|
|||||||
@@ -9,12 +9,15 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NonceRequest is the data type which contains all the data for correct
|
||||||
|
// nonce request.
|
||||||
type NonceRequest struct {
|
type NonceRequest struct {
|
||||||
KeySelector []byte
|
KeySelector []byte
|
||||||
CryptoTS []byte
|
CryptoTS []byte
|
||||||
Nonce []byte
|
Nonce []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bytes returns serialized nonce request.
|
||||||
func (r *NonceRequest) Bytes() []byte {
|
func (r *NonceRequest) Bytes() []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
@@ -27,6 +30,7 @@ func (r *NonceRequest) Bytes() []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewNonceRequest builds new none request based on proxy secret.
|
||||||
func NewNonceRequest(proxySecret []byte) (*NonceRequest, error) {
|
func NewNonceRequest(proxySecret []byte) (*NonceRequest, error) {
|
||||||
nonce := make([]byte, 16)
|
nonce := make([]byte, 16)
|
||||||
keySelector := make([]byte, 4)
|
keySelector := make([]byte, 4)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NonceResponse is the data type which contains data of nonce response.
|
||||||
type NonceResponse struct {
|
type NonceResponse struct {
|
||||||
NonceRequest
|
NonceRequest
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ type NonceResponse struct {
|
|||||||
Crypto []byte
|
Crypto []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bytes returns serialized form of the nonce response.
|
||||||
func (r *NonceResponse) Bytes() []byte {
|
func (r *NonceResponse) Bytes() []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
@@ -25,6 +27,7 @@ func (r *NonceResponse) Bytes() []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Valid checks that nonce response compliments nonce request.
|
||||||
func (r *NonceResponse) Valid(req *NonceRequest) error {
|
func (r *NonceResponse) Valid(req *NonceRequest) error {
|
||||||
if !bytes.Equal(r.Type, TagNonce) {
|
if !bytes.Equal(r.Type, TagNonce) {
|
||||||
return errors.New("Unexpected RPC type")
|
return errors.New("Unexpected RPC type")
|
||||||
@@ -39,6 +42,7 @@ func (r *NonceResponse) Valid(req *NonceRequest) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewNonceResponse build new nonce response based on the given data.
|
||||||
func NewNonceResponse(data []byte) (*NonceResponse, error) {
|
func NewNonceResponse(data []byte) (*NonceResponse, error) {
|
||||||
if len(data) != 32 {
|
if len(data) != 32 {
|
||||||
return nil, errors.New("Unexpected message length")
|
return nil, errors.New("Unexpected message length")
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import (
|
|||||||
"github.com/9seconds/mtg/mtproto"
|
"github.com/9seconds/mtg/mtproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ProxyRequest is the data type for storing data required to compose
|
||||||
|
// RPC_PROXY_REQ request.
|
||||||
type ProxyRequest struct {
|
type ProxyRequest struct {
|
||||||
Flags proxyRequestFlags
|
Flags proxyRequestFlags
|
||||||
ConnectionID []byte
|
ConnectionID []byte
|
||||||
@@ -21,6 +23,8 @@ type ProxyRequest struct {
|
|||||||
Options *mtproto.ConnectionOpts
|
Options *mtproto.ConnectionOpts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MakeHeader makes RPC_PROXY_REQ header. We need only to append the
|
||||||
|
// data for it.
|
||||||
func (r *ProxyRequest) MakeHeader(message []byte) (*bytes.Buffer, fmt.Stringer) {
|
func (r *ProxyRequest) MakeHeader(message []byte) (*bytes.Buffer, fmt.Stringer) {
|
||||||
bufferLength := len(TagProxyRequest) +
|
bufferLength := len(TagProxyRequest) +
|
||||||
4 + // len(flags)
|
4 + // len(flags)
|
||||||
@@ -59,6 +63,7 @@ func (r *ProxyRequest) MakeHeader(message []byte) (*bytes.Buffer, fmt.Stringer)
|
|||||||
return buf, flags
|
return buf, flags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewProxyRequest build new ProxyRequest data structure.
|
||||||
func NewProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*ProxyRequest, error) {
|
func NewProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*ProxyRequest, error) {
|
||||||
flags := proxyRequestFlagsHasAdTag | proxyRequestFlagsMagic | proxyRequestFlagsExtMode2
|
flags := proxyRequestFlagsHasAdTag | proxyRequestFlagsMagic | proxyRequestFlagsExtMode2
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
|
// SeqNo* is the number of the sequence which have special meaning for
|
||||||
|
// the Telegram.
|
||||||
const (
|
const (
|
||||||
SeqNoNonce = -2
|
SeqNoNonce = -2
|
||||||
SeqNoHandshake = -1
|
SeqNoHandshake = -1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Different constants for RPC protocol
|
||||||
var (
|
var (
|
||||||
TagCloseExt = []byte{0xa2, 0x34, 0xb6, 0x5e}
|
TagCloseExt = []byte{0xa2, 0x34, 0xb6, 0x5e}
|
||||||
TagProxyAns = []byte{0x0d, 0xda, 0x03, 0x44}
|
TagProxyAns = []byte{0x0d, 0xda, 0x03, 0x44}
|
||||||
|
|||||||
+12
-9
@@ -17,12 +17,14 @@ import (
|
|||||||
"github.com/9seconds/mtg/wrappers"
|
"github.com/9seconds/mtg/wrappers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Proxy is a core of this program.
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
clientInit client.Init
|
clientInit client.Init
|
||||||
tg telegram.Telegram
|
tg telegram.Telegram
|
||||||
conf *config.Config
|
conf *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serve runs TCP proxy server.
|
||||||
func (p *Proxy) Serve() error {
|
func (p *Proxy) Serve() error {
|
||||||
lsock, err := net.Listen("tcp", p.conf.BindAddr())
|
lsock, err := net.Listen("tcp", p.conf.BindAddr())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -43,7 +45,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
log := zap.S().With("connection_id", connID).Named("main")
|
log := zap.S().With("connection_id", connID).Named("main")
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
conn.Close()
|
conn.Close() // nolint: errcheck
|
||||||
|
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
stats.NewCrash()
|
stats.NewCrash()
|
||||||
@@ -58,7 +60,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
log.Errorw("Cannot initialize client connection", "error", err)
|
log.Errorw("Cannot initialize client connection", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer client.(io.Closer).Close()
|
defer client.(io.Closer).Close() // nolint: errcheck
|
||||||
|
|
||||||
stats.ClientConnected(opts.ConnectionType, client.RemoteAddr())
|
stats.ClientConnected(opts.ConnectionType, client.RemoteAddr())
|
||||||
defer stats.ClientDisconnected(opts.ConnectionType, client.RemoteAddr())
|
defer stats.ClientDisconnected(opts.ConnectionType, client.RemoteAddr())
|
||||||
@@ -68,7 +70,7 @@ func (p *Proxy) accept(conn net.Conn) {
|
|||||||
log.Errorw("Cannot initialize server connection", "error", err)
|
log.Errorw("Cannot initialize server connection", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer server.(io.Closer).Close()
|
defer server.(io.Closer).Close() // nolint: errcheck
|
||||||
|
|
||||||
wait := &sync.WaitGroup{}
|
wait := &sync.WaitGroup{}
|
||||||
wait.Add(2)
|
wait.Add(2)
|
||||||
@@ -104,10 +106,10 @@ func (p *Proxy) getTelegramConn(opts *mtproto.ConnectionOpts, connID string) (wr
|
|||||||
return packetConn, nil
|
return packetConn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) middlePipe(src wrappers.PacketReadCloser, dst wrappers.PacketWriteCloser, wait *sync.WaitGroup, hacks *mtproto.Hacks) {
|
func (p *Proxy) middlePipe(src wrappers.PacketReadCloser, dst io.WriteCloser, wait *sync.WaitGroup, hacks *mtproto.Hacks) {
|
||||||
defer func() {
|
defer func() {
|
||||||
src.Close()
|
src.Close() // nolint: errcheck
|
||||||
dst.Close()
|
dst.Close() // nolint: errcheck
|
||||||
wait.Done()
|
wait.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -127,10 +129,10 @@ func (p *Proxy) middlePipe(src wrappers.PacketReadCloser, dst wrappers.PacketWri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) directPipe(src wrappers.StreamReadCloser, dst wrappers.StreamWriteCloser, wait *sync.WaitGroup) {
|
func (p *Proxy) directPipe(src wrappers.StreamReadCloser, dst io.WriteCloser, wait *sync.WaitGroup) {
|
||||||
defer func() {
|
defer func() {
|
||||||
src.Close()
|
src.Close() // nolint: errcheck
|
||||||
dst.Close()
|
dst.Close() // nolint: errcheck
|
||||||
wait.Done()
|
wait.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -139,6 +141,7 @@ func (p *Proxy) directPipe(src wrappers.StreamReadCloser, dst wrappers.StreamWri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewProxy returns new proxy instance.
|
||||||
func NewProxy(conf *config.Config) *Proxy {
|
func NewProxy(conf *config.Config) *Proxy {
|
||||||
var clientInit client.Init
|
var clientInit client.Init
|
||||||
var tg telegram.Telegram
|
var tg telegram.Telegram
|
||||||
|
|||||||
+7
-2
@@ -21,8 +21,8 @@ var (
|
|||||||
|
|
||||||
type connectionData struct {
|
type connectionData struct {
|
||||||
connectionType mtproto.ConnectionType
|
connectionType mtproto.ConnectionType
|
||||||
addr *net.TCPAddr
|
|
||||||
connected bool
|
connected bool
|
||||||
|
addr *net.TCPAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
type trafficData struct {
|
type trafficData struct {
|
||||||
@@ -40,7 +40,7 @@ func crashManager() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func connectionManager() {
|
func connectionManager() { // nolint: gocyclo
|
||||||
for event := range connectionsChan {
|
for event := range connectionsChan {
|
||||||
instance.mutex.RLock()
|
instance.mutex.RLock()
|
||||||
|
|
||||||
@@ -111,10 +111,12 @@ func trafficManager() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewCrash indicates new crash.
|
||||||
func NewCrash() {
|
func NewCrash() {
|
||||||
crashesChan <- struct{}{}
|
crashesChan <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClientConnected indicates that new client was connected.
|
||||||
func ClientConnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
|
func ClientConnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
|
||||||
connectionsChan <- &connectionData{
|
connectionsChan <- &connectionData{
|
||||||
connectionType: connectionType,
|
connectionType: connectionType,
|
||||||
@@ -123,6 +125,7 @@ func ClientConnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClientDisconnected indicates that client was disconnected.
|
||||||
func ClientDisconnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
|
func ClientDisconnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
|
||||||
connectionsChan <- &connectionData{
|
connectionsChan <- &connectionData{
|
||||||
connectionType: connectionType,
|
connectionType: connectionType,
|
||||||
@@ -131,6 +134,7 @@ func ClientDisconnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IngressTraffic accounts new ingress traffic.
|
||||||
func IngressTraffic(traffic int) {
|
func IngressTraffic(traffic int) {
|
||||||
trafficChan <- &trafficData{
|
trafficChan <- &trafficData{
|
||||||
traffic: traffic,
|
traffic: traffic,
|
||||||
@@ -138,6 +142,7 @@ func IngressTraffic(traffic int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EgressTraffic accounts new ingress traffic.
|
||||||
func EgressTraffic(traffic int) {
|
func EgressTraffic(traffic int) {
|
||||||
trafficChan <- &trafficData{
|
trafficChan <- &trafficData{
|
||||||
traffic: traffic,
|
traffic: traffic,
|
||||||
|
|||||||
+2
-1
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
var instance *stats
|
var instance *stats
|
||||||
|
|
||||||
|
// Start starts new statisitcs server.
|
||||||
func Start(conf *config.Config) {
|
func Start(conf *config.Config) {
|
||||||
log := zap.S().Named("stats")
|
log := zap.S().Named("stats")
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ func Start(conf *config.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interm := map[string]interface{}{}
|
interm := map[string]interface{}{}
|
||||||
json.Unmarshal(first, &interm)
|
json.Unmarshal(first, &interm) // nolint: errcheck
|
||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
encoder.SetEscapeHTML(false)
|
encoder.SetEscapeHTML(false)
|
||||||
|
|||||||
+4
-4
@@ -28,11 +28,11 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type DirectTelegram struct {
|
type directTelegram struct {
|
||||||
baseTelegram
|
baseTelegram
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *DirectTelegram) Dial(connID string, connOpts *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error) {
|
func (t *directTelegram) Dial(connID string, connOpts *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error) {
|
||||||
dc := connOpts.DC
|
dc := connOpts.DC
|
||||||
if dc < 0 {
|
if dc < 0 {
|
||||||
dc = -dc
|
dc = -dc
|
||||||
@@ -43,7 +43,7 @@ func (t *DirectTelegram) Dial(connID string, connOpts *mtproto.ConnectionOpts) (
|
|||||||
return t.baseTelegram.dial(dc-1, connID, connOpts.ConnectionProto)
|
return t.baseTelegram.dial(dc-1, connID, connOpts.ConnectionProto)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *DirectTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.StreamReadWriteCloser) (wrappers.Wrap, error) {
|
func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.StreamReadWriteCloser) (wrappers.Wrap, error) {
|
||||||
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame(connOpts)
|
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame(connOpts)
|
||||||
|
|
||||||
if _, err := conn.Write(frame); err != nil {
|
if _, err := conn.Write(frame); err != nil {
|
||||||
@@ -56,7 +56,7 @@ func (t *DirectTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.St
|
|||||||
// NewDirectTelegram returns Telegram instance which connects directly
|
// NewDirectTelegram returns Telegram instance which connects directly
|
||||||
// to Telegram bypassing middleproxies.
|
// to Telegram bypassing middleproxies.
|
||||||
func NewDirectTelegram(conf *config.Config) Telegram {
|
func NewDirectTelegram(conf *config.Config) Telegram {
|
||||||
return &DirectTelegram{baseTelegram{
|
return &directTelegram{baseTelegram{
|
||||||
dialer: tgDialer{
|
dialer: tgDialer{
|
||||||
Dialer: net.Dialer{Timeout: telegramDialTimeout},
|
Dialer: net.Dialer{Timeout: telegramDialTimeout},
|
||||||
conf: conf,
|
conf: conf,
|
||||||
|
|||||||
+10
-7
@@ -1,6 +1,7 @@
|
|||||||
package telegram
|
package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -13,13 +14,13 @@ import (
|
|||||||
"github.com/9seconds/mtg/wrappers"
|
"github.com/9seconds/mtg/wrappers"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MiddleTelegram struct {
|
type middleTelegram struct {
|
||||||
middleTelegramCaller
|
middleTelegramCaller
|
||||||
|
|
||||||
conf *config.Config
|
conf *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *MiddleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.StreamReadWriteCloser) (wrappers.Wrap, error) {
|
func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.StreamReadWriteCloser) (wrappers.Wrap, error) {
|
||||||
rpcNonceConn := wrappers.NewMTProtoFrame(conn, rpc.SeqNoNonce)
|
rpcNonceConn := wrappers.NewMTProtoFrame(conn, rpc.SeqNoNonce)
|
||||||
|
|
||||||
rpcNonceReq, err := t.sendRPCNonceRequest(rpcNonceConn)
|
rpcNonceReq, err := t.sendRPCNonceRequest(rpcNonceConn)
|
||||||
@@ -52,7 +53,7 @@ func (t *MiddleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.St
|
|||||||
return proxyConn, nil
|
return proxyConn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *MiddleTelegram) sendRPCNonceRequest(conn wrappers.PacketWriter) (*rpc.NonceRequest, error) {
|
func (t *middleTelegram) sendRPCNonceRequest(conn io.Writer) (*rpc.NonceRequest, error) {
|
||||||
rpcNonceReq, err := rpc.NewNonceRequest(t.proxySecret)
|
rpcNonceReq, err := rpc.NewNonceRequest(t.proxySecret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot create RPC nonce request")
|
return nil, errors.Annotate(err, "Cannot create RPC nonce request")
|
||||||
@@ -64,7 +65,7 @@ func (t *MiddleTelegram) sendRPCNonceRequest(conn wrappers.PacketWriter) (*rpc.N
|
|||||||
return rpcNonceReq, nil
|
return rpcNonceReq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *MiddleTelegram) receiveRPCNonceResponse(conn wrappers.PacketReader, req *rpc.NonceRequest) (*rpc.NonceResponse, error) {
|
func (t *middleTelegram) receiveRPCNonceResponse(conn wrappers.PacketReader, req *rpc.NonceRequest) (*rpc.NonceResponse, error) {
|
||||||
packet, err := conn.Read()
|
packet, err := conn.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot read RPC nonce response")
|
return nil, errors.Annotate(err, "Cannot read RPC nonce response")
|
||||||
@@ -81,7 +82,7 @@ func (t *MiddleTelegram) receiveRPCNonceResponse(conn wrappers.PacketReader, req
|
|||||||
return rpcNonceResp, nil
|
return rpcNonceResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *MiddleTelegram) sendRPCHandshakeRequest(conn wrappers.PacketWriter) (*rpc.HandshakeRequest, error) {
|
func (t *middleTelegram) sendRPCHandshakeRequest(conn io.Writer) (*rpc.HandshakeRequest, error) {
|
||||||
req := rpc.NewHandshakeRequest()
|
req := rpc.NewHandshakeRequest()
|
||||||
if _, err := conn.Write(req.Bytes()); err != nil {
|
if _, err := conn.Write(req.Bytes()); err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot send RPC handshake request")
|
return nil, errors.Annotate(err, "Cannot send RPC handshake request")
|
||||||
@@ -90,7 +91,7 @@ func (t *MiddleTelegram) sendRPCHandshakeRequest(conn wrappers.PacketWriter) (*r
|
|||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *MiddleTelegram) receiveRPCHandshakeResponse(conn wrappers.PacketReader, req *rpc.HandshakeRequest) (*rpc.HandshakeResponse, error) {
|
func (t *middleTelegram) receiveRPCHandshakeResponse(conn wrappers.PacketReader, req *rpc.HandshakeRequest) (*rpc.HandshakeResponse, error) {
|
||||||
packet, err := conn.Read()
|
packet, err := conn.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot read RPC handshake response")
|
return nil, errors.Annotate(err, "Cannot read RPC handshake response")
|
||||||
@@ -107,8 +108,10 @@ func (t *MiddleTelegram) receiveRPCHandshakeResponse(conn wrappers.PacketReader,
|
|||||||
return rpcHandshakeResp, nil
|
return rpcHandshakeResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewMiddleTelegram creates new instance of Telegram which works with
|
||||||
|
// middle proxies.
|
||||||
func NewMiddleTelegram(conf *config.Config) Telegram {
|
func NewMiddleTelegram(conf *config.Config) Telegram {
|
||||||
tg := &MiddleTelegram{
|
tg := &middleTelegram{
|
||||||
middleTelegramCaller: middleTelegramCaller{
|
middleTelegramCaller: middleTelegramCaller{
|
||||||
baseTelegram: baseTelegram{
|
baseTelegram: baseTelegram{
|
||||||
dialer: tgDialer{
|
dialer: tgDialer{
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ func (t *middleTelegramCaller) getTelegramAddresses(url string) (map[int16][]str
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot access telegram server")
|
return nil, errors.Annotate(err, "Cannot access telegram server")
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close() // nolint: errcheck
|
||||||
|
|
||||||
scanner := bufio.NewScanner(resp.Body)
|
scanner := bufio.NewScanner(resp.Body)
|
||||||
data := map[int16][]string{}
|
data := map[int16][]string{}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/9seconds/mtg/wrappers"
|
"github.com/9seconds/mtg/wrappers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Telegram is an interface for different Telegram work modes.
|
||||||
type Telegram interface {
|
type Telegram interface {
|
||||||
Dial(string, *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error)
|
Dial(string, *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error)
|
||||||
Init(*mtproto.ConnectionOpts, wrappers.StreamReadWriteCloser) (wrappers.Wrap, error)
|
Init(*mtproto.ConnectionOpts, wrappers.StreamReadWriteCloser) (wrappers.Wrap, error)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import "io"
|
|||||||
|
|
||||||
const readCurrentDataBufferSize = 1024 + 1 // + 1 because telegram operates with blocks mod 4
|
const readCurrentDataBufferSize = 1024 + 1 // + 1 because telegram operates with blocks mod 4
|
||||||
|
|
||||||
|
// ReadCurrentData reads all data from io.Reader which is ready to be read.
|
||||||
func ReadCurrentData(src io.Reader) (rv []byte, err error) {
|
func ReadCurrentData(src io.Reader) (rv []byte, err error) {
|
||||||
buf := make([]byte, readCurrentDataBufferSize)
|
buf := make([]byte, readCurrentDataBufferSize)
|
||||||
n := readCurrentDataBufferSize
|
n := readCurrentDataBufferSize
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
|
// ReverseBytes is a common slice reverser.
|
||||||
func ReverseBytes(data []byte) []byte {
|
func ReverseBytes(data []byte) []byte {
|
||||||
dataLen := len(data)
|
dataLen := len(data)
|
||||||
rv := make([]byte, dataLen)
|
rv := make([]byte, dataLen)
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
|
// Uint24 is a replacement for the absent Go uint24 data type.
|
||||||
|
// This data type is little endian.
|
||||||
type Uint24 [3]byte
|
type Uint24 [3]byte
|
||||||
|
|
||||||
|
// ToUint24 converts number to Uint24.
|
||||||
func ToUint24(number uint32) Uint24 {
|
func ToUint24(number uint32) Uint24 {
|
||||||
return Uint24{byte(number), byte(number >> 8), byte(number >> 16)}
|
return Uint24{byte(number), byte(number >> 8), byte(number >> 16)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FromUint24 converts Uint24 to number.
|
||||||
func FromUint24(number Uint24) uint32 {
|
func FromUint24(number Uint24) uint32 {
|
||||||
return uint32(number[0]) + (uint32(number[1]) << 8) + (uint32(number[2]) << 16)
|
return uint32(number[0]) + (uint32(number[1]) << 8) + (uint32(number[2]) << 16)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// BlockCipher is a stream writer which encrypts/decrypts blocks of data
|
||||||
|
// with AES CBC. This also is buffered reader. It means, that block
|
||||||
|
// reading is transparent for it, you can assume you are working with
|
||||||
|
// good old io.Reader.
|
||||||
type BlockCipher struct {
|
type BlockCipher struct {
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
|
|
||||||
@@ -63,22 +67,27 @@ func (b *BlockCipher) Write(p []byte) (int, error) {
|
|||||||
return b.conn.Write(encrypted)
|
return b.conn.Write(encrypted)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logger returns an instance of the logger for this wrapper.
|
||||||
func (b *BlockCipher) Logger() *zap.SugaredLogger {
|
func (b *BlockCipher) Logger() *zap.SugaredLogger {
|
||||||
return b.logger
|
return b.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LocalAddr returns local address of the underlying net.Conn.
|
||||||
func (b *BlockCipher) LocalAddr() *net.TCPAddr {
|
func (b *BlockCipher) LocalAddr() *net.TCPAddr {
|
||||||
return b.conn.LocalAddr()
|
return b.conn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns remote address of the underlying net.Conn.
|
||||||
func (b *BlockCipher) RemoteAddr() *net.TCPAddr {
|
func (b *BlockCipher) RemoteAddr() *net.TCPAddr {
|
||||||
return b.conn.RemoteAddr()
|
return b.conn.RemoteAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes underlying net.Conn.
|
||||||
func (b *BlockCipher) Close() error {
|
func (b *BlockCipher) Close() error {
|
||||||
return b.conn.Close()
|
return b.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBlockCipher creates new instance of BlockCipher based on given data.
|
||||||
func NewBlockCipher(conn StreamReadWriteCloser, encryptor, decryptor cipher.BlockMode) StreamReadWriteCloser {
|
func NewBlockCipher(conn StreamReadWriteCloser, encryptor, decryptor cipher.BlockMode) StreamReadWriteCloser {
|
||||||
return &BlockCipher{
|
return &BlockCipher{
|
||||||
buf: &bytes.Buffer{},
|
buf: &bytes.Buffer{},
|
||||||
|
|||||||
+18
-7
@@ -9,6 +9,9 @@ import (
|
|||||||
"github.com/9seconds/mtg/stats"
|
"github.com/9seconds/mtg/stats"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConnPurpose is intented to be identifier of connection purpose. We
|
||||||
|
// sometimes want to treat client/telegram connection differently (for
|
||||||
|
// logging for example).
|
||||||
type ConnPurpose uint8
|
type ConnPurpose uint8
|
||||||
|
|
||||||
func (c ConnPurpose) String() string {
|
func (c ConnPurpose) String() string {
|
||||||
@@ -22,6 +25,7 @@ func (c ConnPurpose) String() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConnPurpose* define different connection types.
|
||||||
const (
|
const (
|
||||||
ConnPurposeClient = iota
|
ConnPurposeClient = iota
|
||||||
ConnPurposeTelegram
|
ConnPurposeTelegram
|
||||||
@@ -32,6 +36,8 @@ const (
|
|||||||
connTimeoutWrite = 5 * time.Minute
|
connTimeoutWrite = 5 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Conn is a basic wrapper for net.Conn providing the most low-level
|
||||||
|
// logic and management as possible.
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
connID string
|
connID string
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
@@ -42,7 +48,7 @@ type Conn struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Write(p []byte) (int, error) {
|
func (c *Conn) Write(p []byte) (int, error) {
|
||||||
c.conn.SetWriteDeadline(time.Now().Add(connTimeoutWrite))
|
c.conn.SetWriteDeadline(time.Now().Add(connTimeoutWrite)) // nolint: errcheck
|
||||||
n, err := c.conn.Write(p)
|
n, err := c.conn.Write(p)
|
||||||
|
|
||||||
c.logger.Debugw("Write to stream", "bytes", n, "error", err)
|
c.logger.Debugw("Write to stream", "bytes", n, "error", err)
|
||||||
@@ -52,7 +58,7 @@ func (c *Conn) Write(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Read(p []byte) (int, error) {
|
func (c *Conn) Read(p []byte) (int, error) {
|
||||||
c.conn.SetReadDeadline(time.Now().Add(connTimeoutRead))
|
c.conn.SetReadDeadline(time.Now().Add(connTimeoutRead)) // nolint: errcheck
|
||||||
n, err := c.conn.Read(p)
|
n, err := c.conn.Read(p)
|
||||||
|
|
||||||
c.logger.Debugw("Read from stream", "bytes", n, "error", err)
|
c.logger.Debugw("Read from stream", "bytes", n, "error", err)
|
||||||
@@ -61,11 +67,18 @@ func (c *Conn) Read(p []byte) (int, error) {
|
|||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes underlying net.Conn instance.
|
||||||
func (c *Conn) Close() error {
|
func (c *Conn) Close() error {
|
||||||
defer c.logger.Debugw("Closed connection")
|
defer c.logger.Debugw("Close connection")
|
||||||
return c.conn.Close()
|
return c.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logger returns an instance of the logger for this wrapper.
|
||||||
|
func (c *Conn) Logger() *zap.SugaredLogger {
|
||||||
|
return c.logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// LocalAddr returns local address of the underlying net.Conn.
|
||||||
func (c *Conn) LocalAddr() *net.TCPAddr {
|
func (c *Conn) LocalAddr() *net.TCPAddr {
|
||||||
addr := c.conn.LocalAddr().(*net.TCPAddr)
|
addr := c.conn.LocalAddr().(*net.TCPAddr)
|
||||||
newAddr := *addr
|
newAddr := *addr
|
||||||
@@ -81,14 +94,12 @@ func (c *Conn) LocalAddr() *net.TCPAddr {
|
|||||||
return &newAddr
|
return &newAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns remote address of the underlying net.Conn.
|
||||||
func (c *Conn) RemoteAddr() *net.TCPAddr {
|
func (c *Conn) RemoteAddr() *net.TCPAddr {
|
||||||
return c.conn.RemoteAddr().(*net.TCPAddr)
|
return c.conn.RemoteAddr().(*net.TCPAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Logger() *zap.SugaredLogger {
|
// NewConn initializes Conn wrapper for net.Conn.
|
||||||
return c.logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewConn(conn net.Conn, connID string, purpose ConnPurpose, publicIPv4, publicIPv6 net.IP) StreamReadWriteCloser {
|
func NewConn(conn net.Conn, connID string, purpose ConnPurpose, publicIPv4, publicIPv6 net.IP) StreamReadWriteCloser {
|
||||||
logger := zap.S().With(
|
logger := zap.S().With(
|
||||||
"connection_id", connID,
|
"connection_id", connID,
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ const (
|
|||||||
mtprotoAbridgedLargePacketLength = 16777216 // 256 ^ 3
|
mtprotoAbridgedLargePacketLength = 16777216 // 256 ^ 3
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MTProtoAbridged presents abridged connection between client and
|
||||||
|
// middle proxy.
|
||||||
type MTProtoAbridged struct {
|
type MTProtoAbridged struct {
|
||||||
conn StreamReadWriteCloser
|
conn StreamReadWriteCloser
|
||||||
opts *mtproto.ConnectionOpts
|
opts *mtproto.ConnectionOpts
|
||||||
@@ -127,22 +129,27 @@ func (m *MTProtoAbridged) Write(p []byte) (int, error) {
|
|||||||
return 0, errors.Errorf("Packet is too big %d", len(p))
|
return 0, errors.Errorf("Packet is too big %d", len(p))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logger returns an instance of the logger for this wrapper.
|
||||||
func (m *MTProtoAbridged) Logger() *zap.SugaredLogger {
|
func (m *MTProtoAbridged) Logger() *zap.SugaredLogger {
|
||||||
return m.logger
|
return m.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LocalAddr returns local address of the underlying net.Conn.
|
||||||
func (m *MTProtoAbridged) LocalAddr() *net.TCPAddr {
|
func (m *MTProtoAbridged) LocalAddr() *net.TCPAddr {
|
||||||
return m.conn.LocalAddr()
|
return m.conn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns remote address of the underlying net.Conn.
|
||||||
func (m *MTProtoAbridged) RemoteAddr() *net.TCPAddr {
|
func (m *MTProtoAbridged) RemoteAddr() *net.TCPAddr {
|
||||||
return m.conn.RemoteAddr()
|
return m.conn.RemoteAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes underlying net.Conn instance.
|
||||||
func (m *MTProtoAbridged) Close() error {
|
func (m *MTProtoAbridged) Close() error {
|
||||||
return m.conn.Close()
|
return m.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewMTProtoAbridged creates new wrapper for abridged client connection.
|
||||||
func NewMTProtoAbridged(conn StreamReadWriteCloser, opts *mtproto.ConnectionOpts) PacketReadWriteCloser {
|
func NewMTProtoAbridged(conn StreamReadWriteCloser, opts *mtproto.ConnectionOpts) PacketReadWriteCloser {
|
||||||
return &MTProtoAbridged{
|
return &MTProtoAbridged{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
|||||||
+13
-11
@@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/md5"
|
"crypto/md5" // nolint: gas
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"net"
|
"net"
|
||||||
@@ -13,21 +13,23 @@ import (
|
|||||||
"github.com/9seconds/mtg/utils"
|
"github.com/9seconds/mtg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CipherPurpose uint8
|
type cipherPurpose uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CipherPurposeClient CipherPurpose = iota
|
cipherPurposeClient cipherPurpose = iota
|
||||||
CipherPurposeServer
|
cipherPurposeServer
|
||||||
)
|
)
|
||||||
|
|
||||||
var emptyIP = [4]byte{0x00, 0x00, 0x00, 0x00}
|
var emptyIP = [4]byte{0x00, 0x00, 0x00, 0x00}
|
||||||
|
|
||||||
|
// NewMiddleProxyCipher creates new block cipher to proxy<->telegram
|
||||||
|
// connection.
|
||||||
func NewMiddleProxyCipher(conn StreamReadWriteCloser, req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) StreamReadWriteCloser {
|
func NewMiddleProxyCipher(conn StreamReadWriteCloser, req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) StreamReadWriteCloser {
|
||||||
localAddr := conn.LocalAddr()
|
localAddr := conn.LocalAddr()
|
||||||
remoteAddr := conn.RemoteAddr()
|
remoteAddr := conn.RemoteAddr()
|
||||||
|
|
||||||
encKey, encIV := deriveKeys(CipherPurposeClient, req, resp, localAddr, remoteAddr, secret)
|
encKey, encIV := deriveKeys(cipherPurposeClient, req, resp, localAddr, remoteAddr, secret)
|
||||||
decKey, decIV := deriveKeys(CipherPurposeServer, req, resp, localAddr, remoteAddr, secret)
|
decKey, decIV := deriveKeys(cipherPurposeServer, req, resp, localAddr, remoteAddr, secret)
|
||||||
|
|
||||||
enc, _ := makeEncrypterDecrypter(encKey, encIV)
|
enc, _ := makeEncrypterDecrypter(encKey, encIV)
|
||||||
_, dec := makeEncrypterDecrypter(decKey, decIV)
|
_, dec := makeEncrypterDecrypter(decKey, decIV)
|
||||||
@@ -35,7 +37,7 @@ func NewMiddleProxyCipher(conn StreamReadWriteCloser, req *rpc.NonceRequest, res
|
|||||||
return NewBlockCipher(conn, enc, dec)
|
return NewBlockCipher(conn, enc, dec)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deriveKeys(purpose CipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
|
func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
|
||||||
message := bytes.Buffer{}
|
message := bytes.Buffer{}
|
||||||
message.Write(resp.Nonce[:])
|
message.Write(resp.Nonce[:])
|
||||||
message.Write(req.Nonce[:])
|
message.Write(req.Nonce[:])
|
||||||
@@ -54,9 +56,9 @@ func deriveKeys(purpose CipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceRes
|
|||||||
message.Write(port[:])
|
message.Write(port[:])
|
||||||
|
|
||||||
switch purpose {
|
switch purpose {
|
||||||
case CipherPurposeClient:
|
case cipherPurposeClient:
|
||||||
message.WriteString("CLIENT")
|
message.WriteString("CLIENT")
|
||||||
case CipherPurposeServer:
|
case cipherPurposeServer:
|
||||||
message.WriteString("SERVER")
|
message.WriteString("SERVER")
|
||||||
default:
|
default:
|
||||||
panic("Unexpected cipher purpose")
|
panic("Unexpected cipher purpose")
|
||||||
@@ -75,11 +77,11 @@ func deriveKeys(purpose CipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceRes
|
|||||||
message.Write(req.Nonce[:])
|
message.Write(req.Nonce[:])
|
||||||
|
|
||||||
data := message.Bytes()
|
data := message.Bytes()
|
||||||
md5sum := md5.Sum(data[1:])
|
md5sum := md5.Sum(data[1:]) // nolint: gas
|
||||||
sha1sum := sha1.Sum(data)
|
sha1sum := sha1.Sum(data)
|
||||||
|
|
||||||
key := append(md5sum[:12], sha1sum[:]...)
|
key := append(md5sum[:12], sha1sum[:]...)
|
||||||
iv := md5.Sum(data[2:])
|
iv := md5.Sum(data[2:]) // nolint: gas
|
||||||
|
|
||||||
return key, iv[:]
|
return key, iv[:]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,18 @@ const (
|
|||||||
|
|
||||||
var mtprotoFramePadding = []byte{0x04, 0x00, 0x00, 0x00}
|
var mtprotoFramePadding = []byte{0x04, 0x00, 0x00, 0x00}
|
||||||
|
|
||||||
|
// MTProtoFrame is a wrapper which converts written data to the MTProtoFrame.
|
||||||
|
// The format of the frame:
|
||||||
|
//
|
||||||
|
// [ MSGLEN(4) | SEQNO(4) | MSG(...) | CRC32(4) | PADDING(4*x) ]
|
||||||
|
//
|
||||||
|
// MSGLEN is the length of the message + len of seqno and msglen.
|
||||||
|
// SEQNO is the number of frame in the receive/send sequence. If client
|
||||||
|
// sends a message with SeqNo 18, it has to receive message with SeqNo 18.
|
||||||
|
// MSG is the data which has to be written
|
||||||
|
// CRC32 is the CRC32 checksum of MSGLEN + SEQNO + MSG
|
||||||
|
// PADDING is custom padding schema to complete frame length to such that
|
||||||
|
// len(frame) % 16 == 0
|
||||||
type MTProtoFrame struct {
|
type MTProtoFrame struct {
|
||||||
conn StreamReadWriteCloser
|
conn StreamReadWriteCloser
|
||||||
logger *zap.SugaredLogger
|
logger *zap.SugaredLogger
|
||||||
@@ -28,7 +40,7 @@ type MTProtoFrame struct {
|
|||||||
writeSeqNo int32
|
writeSeqNo int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MTProtoFrame) Read() ([]byte, error) {
|
func (m *MTProtoFrame) Read() ([]byte, error) { // nolint: gocyclo
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
sum := crc32.NewIEEE()
|
sum := crc32.NewIEEE()
|
||||||
writer := io.MultiWriter(buf, sum)
|
writer := io.MultiWriter(buf, sum)
|
||||||
@@ -60,7 +72,7 @@ func (m *MTProtoFrame) Read() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var seqNo int32
|
var seqNo int32
|
||||||
binary.Read(buf, binary.LittleEndian, &seqNo)
|
binary.Read(buf, binary.LittleEndian, &seqNo) // nolint: errcheck
|
||||||
if seqNo != m.readSeqNo {
|
if seqNo != m.readSeqNo {
|
||||||
return nil, errors.Errorf("Unexpected sequence number %d (wait for %d)", seqNo, m.readSeqNo)
|
return nil, errors.Errorf("Unexpected sequence number %d (wait for %d)", seqNo, m.readSeqNo)
|
||||||
}
|
}
|
||||||
@@ -96,12 +108,12 @@ func (m *MTProtoFrame) Write(p []byte) (int, error) {
|
|||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
buf.Grow(messageLength + paddingLength)
|
buf.Grow(messageLength + paddingLength)
|
||||||
|
|
||||||
binary.Write(buf, binary.LittleEndian, uint32(messageLength))
|
binary.Write(buf, binary.LittleEndian, uint32(messageLength)) // nolint: errcheck
|
||||||
binary.Write(buf, binary.LittleEndian, m.writeSeqNo)
|
binary.Write(buf, binary.LittleEndian, m.writeSeqNo) // nolint: errcheck
|
||||||
buf.Write(p)
|
buf.Write(p)
|
||||||
|
|
||||||
checksum := crc32.ChecksumIEEE(buf.Bytes())
|
checksum := crc32.ChecksumIEEE(buf.Bytes())
|
||||||
binary.Write(buf, binary.LittleEndian, checksum)
|
binary.Write(buf, binary.LittleEndian, checksum) // nolint: errcheck
|
||||||
buf.Write(bytes.Repeat(mtprotoFramePadding, paddingLength/4))
|
buf.Write(bytes.Repeat(mtprotoFramePadding, paddingLength/4))
|
||||||
|
|
||||||
m.logger.Debugw("Write MTProto frame",
|
m.logger.Debugw("Write MTProto frame",
|
||||||
@@ -117,22 +129,27 @@ func (m *MTProtoFrame) Write(p []byte) (int, error) {
|
|||||||
return len(p), err
|
return len(p), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logger returns an instance of the logger for this wrapper.
|
||||||
func (m *MTProtoFrame) Logger() *zap.SugaredLogger {
|
func (m *MTProtoFrame) Logger() *zap.SugaredLogger {
|
||||||
return m.logger
|
return m.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LocalAddr returns local address of the underlying net.Conn.
|
||||||
func (m *MTProtoFrame) LocalAddr() *net.TCPAddr {
|
func (m *MTProtoFrame) LocalAddr() *net.TCPAddr {
|
||||||
return m.conn.LocalAddr()
|
return m.conn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns remote address of the underlying net.Conn.
|
||||||
func (m *MTProtoFrame) RemoteAddr() *net.TCPAddr {
|
func (m *MTProtoFrame) RemoteAddr() *net.TCPAddr {
|
||||||
return m.conn.RemoteAddr()
|
return m.conn.RemoteAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes underlying net.Conn instance.
|
||||||
func (m *MTProtoFrame) Close() error {
|
func (m *MTProtoFrame) Close() error {
|
||||||
return m.conn.Close()
|
return m.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewMTProtoFrame creates new PacketWrapper for underlying connection.
|
||||||
func NewMTProtoFrame(conn StreamReadWriteCloser, seqNo int32) PacketReadWriteCloser {
|
func NewMTProtoFrame(conn StreamReadWriteCloser, seqNo int32) PacketReadWriteCloser {
|
||||||
return &MTProtoFrame{
|
return &MTProtoFrame{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import (
|
|||||||
|
|
||||||
const mtprotoIntermediateQuickAckLength = 0x80000000
|
const mtprotoIntermediateQuickAckLength = 0x80000000
|
||||||
|
|
||||||
|
// MTProtoIntermediate presents intermediate connection between client
|
||||||
|
// and Telegram.
|
||||||
type MTProtoIntermediate struct {
|
type MTProtoIntermediate struct {
|
||||||
conn StreamReadWriteCloser
|
conn StreamReadWriteCloser
|
||||||
opts *mtproto.ConnectionOpts
|
opts *mtproto.ConnectionOpts
|
||||||
@@ -88,22 +90,28 @@ func (m *MTProtoIntermediate) Write(p []byte) (int, error) {
|
|||||||
return m.conn.Write(append(length[:], p...))
|
return m.conn.Write(append(length[:], p...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logger returns an instance of the logger for this wrapper.
|
||||||
func (m *MTProtoIntermediate) Logger() *zap.SugaredLogger {
|
func (m *MTProtoIntermediate) Logger() *zap.SugaredLogger {
|
||||||
return m.logger
|
return m.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LocalAddr returns local address of the underlying net.Conn.
|
||||||
func (m *MTProtoIntermediate) LocalAddr() *net.TCPAddr {
|
func (m *MTProtoIntermediate) LocalAddr() *net.TCPAddr {
|
||||||
return m.conn.LocalAddr()
|
return m.conn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns remote address of the underlying net.Conn.
|
||||||
func (m *MTProtoIntermediate) RemoteAddr() *net.TCPAddr {
|
func (m *MTProtoIntermediate) RemoteAddr() *net.TCPAddr {
|
||||||
return m.conn.RemoteAddr()
|
return m.conn.RemoteAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes underlying net.Conn instance.
|
||||||
func (m *MTProtoIntermediate) Close() error {
|
func (m *MTProtoIntermediate) Close() error {
|
||||||
return m.conn.Close()
|
return m.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewMTProtoIntermediate creates new PacketWrapper for intermediate
|
||||||
|
// client connection.
|
||||||
func NewMTProtoIntermediate(conn StreamReadWriteCloser, opts *mtproto.ConnectionOpts) PacketReadWriteCloser {
|
func NewMTProtoIntermediate(conn StreamReadWriteCloser, opts *mtproto.ConnectionOpts) PacketReadWriteCloser {
|
||||||
return &MTProtoIntermediate{
|
return &MTProtoIntermediate{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/9seconds/mtg/mtproto/rpc"
|
"github.com/9seconds/mtg/mtproto/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MTProtoProxy is a wrapper which creates/reads RPC responses from Telegram.
|
||||||
type MTProtoProxy struct {
|
type MTProtoProxy struct {
|
||||||
conn PacketReadWriteCloser
|
conn PacketReadWriteCloser
|
||||||
req *rpc.ProxyRequest
|
req *rpc.ProxyRequest
|
||||||
@@ -128,22 +129,27 @@ func (m *MTProtoProxy) Write(p []byte) (int, error) {
|
|||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logger returns an instance of the logger for this wrapper.
|
||||||
func (m *MTProtoProxy) Logger() *zap.SugaredLogger {
|
func (m *MTProtoProxy) Logger() *zap.SugaredLogger {
|
||||||
return m.logger
|
return m.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LocalAddr returns local address of the underlying net.Conn.
|
||||||
func (m *MTProtoProxy) LocalAddr() *net.TCPAddr {
|
func (m *MTProtoProxy) LocalAddr() *net.TCPAddr {
|
||||||
return m.conn.LocalAddr()
|
return m.conn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns remote address of the underlying net.Conn.
|
||||||
func (m *MTProtoProxy) RemoteAddr() *net.TCPAddr {
|
func (m *MTProtoProxy) RemoteAddr() *net.TCPAddr {
|
||||||
return m.conn.RemoteAddr()
|
return m.conn.RemoteAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes underlying net.Conn instance.
|
||||||
func (m *MTProtoProxy) Close() error {
|
func (m *MTProtoProxy) Close() error {
|
||||||
return m.conn.Close()
|
return m.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewMTProtoProxy creates new RPC wrapper.
|
||||||
func NewMTProtoProxy(conn PacketReadWriteCloser, connOpts *mtproto.ConnectionOpts, adTag []byte) (PacketReadWriteCloser, error) {
|
func NewMTProtoProxy(conn PacketReadWriteCloser, connOpts *mtproto.ConnectionOpts, adTag []byte) (PacketReadWriteCloser, error) {
|
||||||
req, err := rpc.NewProxyRequest(connOpts.ClientAddr, conn.LocalAddr(), connOpts, adTag)
|
req, err := rpc.NewProxyRequest(connOpts.ClientAddr, conn.LocalAddr(), connOpts, adTag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// StreamCipher is a wrapper which encrypts/decrypts stream with AES-CTR
|
||||||
|
// (as a part of obfuscated2 protocol).
|
||||||
type StreamCipher struct {
|
type StreamCipher struct {
|
||||||
encryptor cipher.Stream
|
encryptor cipher.Stream
|
||||||
decryptor cipher.Stream
|
decryptor cipher.Stream
|
||||||
@@ -32,22 +34,27 @@ func (s *StreamCipher) Write(p []byte) (int, error) {
|
|||||||
return s.conn.Write(encrypted)
|
return s.conn.Write(encrypted)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logger returns an instance of the logger for this wrapper.
|
||||||
func (s *StreamCipher) Logger() *zap.SugaredLogger {
|
func (s *StreamCipher) Logger() *zap.SugaredLogger {
|
||||||
return s.logger
|
return s.logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LocalAddr returns local address of the underlying net.Conn.
|
||||||
func (s *StreamCipher) LocalAddr() *net.TCPAddr {
|
func (s *StreamCipher) LocalAddr() *net.TCPAddr {
|
||||||
return s.conn.LocalAddr()
|
return s.conn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoteAddr returns remote address of the underlying net.Conn.
|
||||||
func (s *StreamCipher) RemoteAddr() *net.TCPAddr {
|
func (s *StreamCipher) RemoteAddr() *net.TCPAddr {
|
||||||
return s.conn.RemoteAddr()
|
return s.conn.RemoteAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes underlying net.Conn instance.
|
||||||
func (s *StreamCipher) Close() error {
|
func (s *StreamCipher) Close() error {
|
||||||
return s.conn.Close()
|
return s.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewStreamCipher creates new stream cipher wrapper.
|
||||||
func NewStreamCipher(conn StreamReadWriteCloser, encryptor, decryptor cipher.Stream) StreamReadWriteCloser {
|
func NewStreamCipher(conn StreamReadWriteCloser, encryptor, decryptor cipher.Stream) StreamReadWriteCloser {
|
||||||
return &StreamCipher{
|
return &StreamCipher{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
|||||||
@@ -7,78 +7,104 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Wrap is a base interface for all wrappers in this package.
|
||||||
type Wrap interface {
|
type Wrap interface {
|
||||||
Logger() *zap.SugaredLogger
|
Logger() *zap.SugaredLogger
|
||||||
LocalAddr() *net.TCPAddr
|
LocalAddr() *net.TCPAddr
|
||||||
RemoteAddr() *net.TCPAddr
|
RemoteAddr() *net.TCPAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Writer is a base interface for writers of this package.
|
||||||
type Writer interface {
|
type Writer interface {
|
||||||
io.Writer
|
io.Writer
|
||||||
Wrap
|
Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Closer is a base interface for wrappers of this package which can
|
||||||
|
// close connections.
|
||||||
type Closer interface {
|
type Closer interface {
|
||||||
io.Closer
|
io.Closer
|
||||||
Wrap
|
Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteCloser is a base interface for wrappers of this package which
|
||||||
|
// can write to and close connections.
|
||||||
type WriteCloser interface {
|
type WriteCloser interface {
|
||||||
io.Closer
|
io.Closer
|
||||||
Writer
|
Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StreamReader is a base interface for wrappers which can read from the
|
||||||
|
// stream.
|
||||||
type StreamReader interface {
|
type StreamReader interface {
|
||||||
io.Reader
|
io.Reader
|
||||||
Wrap
|
Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StreamReadCloser is a base interface for wrappers which can read from
|
||||||
|
// and close the connections.
|
||||||
type StreamReadCloser interface {
|
type StreamReadCloser interface {
|
||||||
io.Closer
|
io.Closer
|
||||||
StreamReader
|
StreamReader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StreamReadWriter is a base interface for wrappers which can read from
|
||||||
|
// and write to the connections.
|
||||||
type StreamReadWriter interface {
|
type StreamReadWriter interface {
|
||||||
io.Writer
|
io.Writer
|
||||||
StreamReader
|
StreamReader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StreamWriteCloser is a base interface for wrappers which can write to
|
||||||
|
// and close the connections.
|
||||||
type StreamWriteCloser interface {
|
type StreamWriteCloser interface {
|
||||||
io.WriteCloser
|
io.WriteCloser
|
||||||
Wrap
|
Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StreamReadWriteCloser is a base interface for stream processors.
|
||||||
type StreamReadWriteCloser interface {
|
type StreamReadWriteCloser interface {
|
||||||
io.Closer
|
io.Closer
|
||||||
StreamReadWriter
|
StreamReadWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PacketReader is a base interface for wrappers which reads 'packets'.
|
||||||
|
// packets are atoms so you either get a packet or you get an error You
|
||||||
|
// cannot resume reading from packet.
|
||||||
type PacketReader interface {
|
type PacketReader interface {
|
||||||
Read() ([]byte, error)
|
Read() ([]byte, error)
|
||||||
Wrap
|
Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PacketWriter is a base interface for wrappers which can write packets.
|
||||||
type PacketWriter interface {
|
type PacketWriter interface {
|
||||||
io.Writer
|
io.Writer
|
||||||
Wrap
|
Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PacketReadWriter is a base interface for wrappers which can read from
|
||||||
|
// and write packets.
|
||||||
type PacketReadWriter interface {
|
type PacketReadWriter interface {
|
||||||
io.Writer
|
io.Writer
|
||||||
PacketReader
|
PacketReader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PacketReadCloser is a base interface for wrappers which can read
|
||||||
|
// packets and close the connection.
|
||||||
type PacketReadCloser interface {
|
type PacketReadCloser interface {
|
||||||
io.Closer
|
io.Closer
|
||||||
PacketReader
|
PacketReader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PacketWriteCloser is a base interface for wrappers which can write
|
||||||
|
// packets and close the connection.
|
||||||
type PacketWriteCloser interface {
|
type PacketWriteCloser interface {
|
||||||
io.Writer
|
io.Writer
|
||||||
io.Closer
|
io.Closer
|
||||||
Wrap
|
Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PacketReadWriteCloser is a base interface for packet processors.
|
||||||
type PacketReadWriteCloser interface {
|
type PacketReadWriteCloser interface {
|
||||||
io.Closer
|
io.Closer
|
||||||
PacketReadWriter
|
PacketReadWriter
|
||||||
|
|||||||
Reference in New Issue
Block a user