2018-07-04 11:48:48 -04:00
|
|
|
package mtproto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/crypto"
|
|
|
|
"v2ray.com/core/common/net"
|
2018-09-18 17:09:54 -04:00
|
|
|
"v2ray.com/core/common/session"
|
2018-07-04 11:48:48 -04:00
|
|
|
"v2ray.com/core/common/task"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
|
|
|
return &Client{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Process(ctx context.Context, link *core.Link, dialer proxy.Dialer) error {
|
2018-09-18 17:09:54 -04:00
|
|
|
outbound := session.OutboundFromContext(ctx)
|
|
|
|
if outbound == nil || !outbound.Target.IsValid() {
|
2018-07-04 11:48:48 -04:00
|
|
|
return newError("unknown destination.")
|
|
|
|
}
|
2018-09-18 17:09:54 -04:00
|
|
|
dest := outbound.Target
|
2018-07-04 11:48:48 -04:00
|
|
|
if dest.Network != net.Network_TCP {
|
|
|
|
return newError("not TCP traffic", dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := dialer.Dial(ctx, dest)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to dial to ", dest).Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
defer conn.Close() // nolint: errcheck
|
|
|
|
|
2018-10-11 05:11:11 -04:00
|
|
|
sc := SessionContextFromContext(ctx)
|
|
|
|
auth := NewAuthentication(sc)
|
2018-07-04 11:48:48 -04:00
|
|
|
defer putAuthenticationObject(auth)
|
|
|
|
|
|
|
|
request := func() error {
|
|
|
|
encryptor := crypto.NewAesCTRStream(auth.EncodingKey[:], auth.EncodingNonce[:])
|
|
|
|
|
|
|
|
var header [HeaderSize]byte
|
|
|
|
encryptor.XORKeyStream(header[:], auth.Header[:])
|
|
|
|
copy(header[:56], auth.Header[:])
|
|
|
|
|
|
|
|
if _, err := conn.Write(header[:]); err != nil {
|
|
|
|
return newError("failed to write auth header").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
connWriter := buf.NewWriter(crypto.NewCryptionWriter(encryptor, conn))
|
|
|
|
return buf.Copy(link.Reader, connWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
response := func() error {
|
|
|
|
decryptor := crypto.NewAesCTRStream(auth.DecodingKey[:], auth.DecodingNonce[:])
|
|
|
|
|
|
|
|
connReader := buf.NewReader(crypto.NewCryptionReader(decryptor, conn))
|
|
|
|
return buf.Copy(connReader, link.Writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseDoneAndCloseWriter = task.Single(response, task.OnSuccess(task.Close(link.Writer)))
|
|
|
|
if err := task.Run(task.WithContext(ctx), task.Parallel(request, responseDoneAndCloseWriter))(); err != nil {
|
|
|
|
return newError("connection ends").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewClient(ctx, config.(*ClientConfig))
|
|
|
|
}))
|
|
|
|
}
|