Support different client connection types

This commit is contained in:
9seconds
2018-06-21 09:14:00 +03:00
parent ec548e5779
commit 588475268a
11 changed files with 157 additions and 55 deletions
+2 -1
View File
@@ -5,7 +5,8 @@ import (
"net"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/mtproto"
)
// Init has to initialize client connection based on given config.
type Init func(net.Conn, *config.Config) (int16, io.ReadWriteCloser, error)
type Init func(net.Conn, *config.Config) (*mtproto.ConnectionOpts, io.ReadWriteCloser, error)
+6 -5
View File
@@ -7,25 +7,26 @@ import (
"github.com/juju/errors"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/mtproto"
"github.com/9seconds/mtg/obfuscated2"
"github.com/9seconds/mtg/wrappers"
)
// DirectInit initializes client to access Telegram bypassing middleproxies.
func DirectInit(conn net.Conn, conf *config.Config) (int16, io.ReadWriteCloser, error) {
func DirectInit(conn net.Conn, conf *config.Config) (*mtproto.ConnectionOpts, 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")
return nil, nil, errors.Annotate(err, "Cannot extract frame")
}
defer obfuscated2.ReturnFrame(frame)
obfs2, dc, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
obfs2, connOpts, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
if err != nil {
return 0, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
}
socket = wrappers.NewStreamCipherRWC(socket, obfs2.Encryptor, obfs2.Decryptor)
return dc, socket, nil
return connOpts, socket, nil
}