mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-02 00:11:56 +03:00
Add client package
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Init func(net.Conn, *config.Config) (int16, io.ReadWriteCloser, error)
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/juju/errors"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/config"
|
||||||
|
"github.com/9seconds/mtg/obfuscated2"
|
||||||
|
"github.com/9seconds/mtg/wrappers"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DirectInit(conn net.Conn, conf *config.Config) (int16, io.ReadWriteCloser, error) {
|
||||||
|
socket := wrappers.NewTimeoutRWC(conn, conf.TimeoutRead, conf.TimeoutWrite)
|
||||||
|
frame, err := obfuscated2.ExtractFrame(socket)
|
||||||
|
if err != nil {
|
||||||
|
return 0, nil, errors.Annotate(err, "Cannot extract frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
obfs2, dc, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
|
||||||
|
if err != nil {
|
||||||
|
return 0, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
|
||||||
|
}
|
||||||
|
|
||||||
|
socket = wrappers.NewStreamCipherRWC(socket, obfs2.Encryptor, obfs2.Decryptor)
|
||||||
|
|
||||||
|
return dc, socket, nil
|
||||||
|
}
|
||||||
@@ -34,7 +34,7 @@ func TestFrameMagic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFrameDC(t *testing.T) {
|
func TestFrameDC(t *testing.T) {
|
||||||
assert.Equal(t, int16(770), makeFrame().DC())
|
assert.Equal(t, int16(771), makeFrame().DC())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFrameValid(t *testing.T) {
|
func TestFrameValid(t *testing.T) {
|
||||||
|
|||||||
@@ -19,9 +19,7 @@ type Obfuscated2 struct {
|
|||||||
// details: http://telegra.ph/telegram-blocks-wtf-05-26
|
// details: http://telegra.ph/telegram-blocks-wtf-05-26
|
||||||
//
|
//
|
||||||
// Beware, link above is in russian.
|
// Beware, link above is in russian.
|
||||||
func ParseObfuscated2ClientFrame(secret, data []byte) (*Obfuscated2, int16, error) {
|
func ParseObfuscated2ClientFrame(secret []byte, frame Frame) (*Obfuscated2, int16, error) {
|
||||||
frame := Frame(data)
|
|
||||||
|
|
||||||
decHasher := sha256.New()
|
decHasher := sha256.New()
|
||||||
decHasher.Write(frame.Key()) // nolint: errcheck
|
decHasher.Write(frame.Key()) // nolint: errcheck
|
||||||
decHasher.Write(secret) // nolint: errcheck
|
decHasher.Write(secret) // nolint: errcheck
|
||||||
|
|||||||
+11
-16
@@ -10,8 +10,8 @@ import (
|
|||||||
uuid "github.com/satori/go.uuid"
|
uuid "github.com/satori/go.uuid"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/client"
|
||||||
"github.com/9seconds/mtg/config"
|
"github.com/9seconds/mtg/config"
|
||||||
"github.com/9seconds/mtg/obfuscated2"
|
|
||||||
"github.com/9seconds/mtg/telegram"
|
"github.com/9seconds/mtg/telegram"
|
||||||
"github.com/9seconds/mtg/wrappers"
|
"github.com/9seconds/mtg/wrappers"
|
||||||
)
|
)
|
||||||
@@ -22,6 +22,7 @@ type Server struct {
|
|||||||
logger *zap.SugaredLogger
|
logger *zap.SugaredLogger
|
||||||
stats *Stats
|
stats *Stats
|
||||||
tg telegram.Telegram
|
tg telegram.Telegram
|
||||||
|
clientInit client.Init
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve does MTPROTO proxying.
|
// Serve does MTPROTO proxying.
|
||||||
@@ -59,7 +60,7 @@ func (s *Server) accept(conn net.Conn) {
|
|||||||
"socketid", socketID,
|
"socketid", socketID,
|
||||||
)
|
)
|
||||||
|
|
||||||
clientConn, dc, err := s.getClientStream(ctx, cancel, conn, socketID)
|
dc, clientConn, err := s.getClientStream(ctx, cancel, conn, socketID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Warnw("Cannot initialize client connection",
|
s.logger.Warnw("Cannot initialize client connection",
|
||||||
"addr", conn.RemoteAddr().String(),
|
"addr", conn.RemoteAddr().String(),
|
||||||
@@ -99,24 +100,17 @@ func (s *Server) accept(conn net.Conn) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc, conn net.Conn, socketID string) (io.ReadWriteCloser, int16, error) {
|
func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc, conn net.Conn, socketID string) (int16, io.ReadWriteCloser, error) {
|
||||||
wConn := wrappers.NewTimeoutRWC(conn, s.conf.TimeoutRead, s.conf.TimeoutWrite)
|
dc, socket, err := s.clientInit(conn, s.conf)
|
||||||
wConn = wrappers.NewTrafficRWC(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
|
||||||
frame, err := obfuscated2.ExtractFrame(wConn)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
return 0, nil, errors.Annotate(err, "Cannot init client connection")
|
||||||
}
|
}
|
||||||
|
|
||||||
obfs2, dc, err := obfuscated2.ParseObfuscated2ClientFrame(s.conf.Secret, frame)
|
socket = wrappers.NewTrafficRWC(socket, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
||||||
if err != nil {
|
socket = wrappers.NewLogRWC(socket, s.logger, socketID, "client")
|
||||||
return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
socket = wrappers.NewCtxRWC(ctx, cancel, socket)
|
||||||
}
|
|
||||||
|
|
||||||
wConn = wrappers.NewLogRWC(wConn, s.logger, socketID, "client")
|
return dc, socket, nil
|
||||||
wConn = wrappers.NewStreamCipherRWC(wConn, obfs2.Encryptor, obfs2.Decryptor)
|
|
||||||
wConn = wrappers.NewCtxRWC(ctx, cancel, wConn)
|
|
||||||
|
|
||||||
return wConn, dc, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFunc, dc int16, socketID string) (io.ReadWriteCloser, error) {
|
func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFunc, dc int16, socketID string) (io.ReadWriteCloser, error) {
|
||||||
@@ -144,5 +138,6 @@ func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Ser
|
|||||||
logger: logger,
|
logger: logger,
|
||||||
stats: stat,
|
stats: stat,
|
||||||
tg: telegram.NewDirectTelegram(conf),
|
tg: telegram.NewDirectTelegram(conf),
|
||||||
|
clientInit: client.DirectInit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user