2018-07-04 11:48:48 -04:00
|
|
|
package mtproto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-04 12:58:19 -04:00
|
|
|
"time"
|
2018-07-04 11:48:48 -04:00
|
|
|
|
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/buf"
|
2018-07-12 17:38:10 -04:00
|
|
|
"v2ray.com/core/common/compare"
|
2018-07-04 11:48:48 -04:00
|
|
|
"v2ray.com/core/common/crypto"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
2018-07-04 12:58:19 -04:00
|
|
|
"v2ray.com/core/common/session"
|
|
|
|
"v2ray.com/core/common/signal"
|
2018-07-04 11:48:48 -04:00
|
|
|
"v2ray.com/core/common/task"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/pipe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dcList = []net.Address{
|
|
|
|
net.ParseAddress("149.154.175.50"),
|
|
|
|
net.ParseAddress("149.154.167.51"),
|
|
|
|
net.ParseAddress("149.154.175.100"),
|
|
|
|
net.ParseAddress("149.154.167.91"),
|
|
|
|
net.ParseAddress("149.154.171.5"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
user *protocol.User
|
|
|
|
account *Account
|
2018-07-04 12:58:19 -04:00
|
|
|
policy core.PolicyManager
|
2018-07-04 11:48:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
|
|
|
if len(config.User) == 0 {
|
|
|
|
return nil, newError("no user configured.")
|
|
|
|
}
|
|
|
|
|
|
|
|
user := config.User[0]
|
|
|
|
rawAccount, err := config.User[0].GetTypedAccount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid account").Base(err)
|
|
|
|
}
|
|
|
|
account, ok := rawAccount.(*Account)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not a MTProto account")
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:58:19 -04:00
|
|
|
v := core.MustFromContext(ctx)
|
|
|
|
|
2018-07-04 11:48:48 -04:00
|
|
|
return &Server{
|
|
|
|
user: user,
|
|
|
|
account: account,
|
2018-07-04 12:58:19 -04:00
|
|
|
policy: v.PolicyManager(),
|
2018-07-04 11:48:48 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Network() net.NetworkList {
|
|
|
|
return net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher core.Dispatcher) error {
|
2018-07-04 12:58:19 -04:00
|
|
|
sPolicy := s.policy.ForLevel(s.user.Level)
|
|
|
|
|
|
|
|
if err := conn.SetDeadline(time.Now().Add(sPolicy.Timeouts.Handshake)); err != nil {
|
|
|
|
newError("failed to set deadline").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
}
|
2018-07-04 11:48:48 -04:00
|
|
|
auth, err := ReadAuthentication(conn)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to read authentication header").Base(err)
|
|
|
|
}
|
2018-07-04 12:47:06 -04:00
|
|
|
defer putAuthenticationObject(auth)
|
2018-07-04 11:48:48 -04:00
|
|
|
|
2018-07-04 12:58:19 -04:00
|
|
|
if err := conn.SetDeadline(time.Time{}); err != nil {
|
|
|
|
newError("failed to clear deadline").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:48:48 -04:00
|
|
|
auth.ApplySecret(s.account.Secret)
|
|
|
|
|
|
|
|
decryptor := crypto.NewAesCTRStream(auth.DecodingKey[:], auth.DecodingNonce[:])
|
|
|
|
decryptor.XORKeyStream(auth.Header[:], auth.Header[:])
|
|
|
|
|
2018-07-12 17:38:10 -04:00
|
|
|
if !compare.BytesAll(auth.Header[56:60], 0xef) {
|
2018-07-04 12:47:06 -04:00
|
|
|
return newError("invalid connection type: ", auth.Header[56:60])
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:48:48 -04:00
|
|
|
dcID := auth.DataCenterID()
|
2018-07-05 08:47:46 -04:00
|
|
|
if dcID >= uint16(len(dcList)) {
|
|
|
|
return newError("invalid datacenter id: ", dcID)
|
|
|
|
}
|
2018-07-04 11:48:48 -04:00
|
|
|
|
|
|
|
dest := net.Destination{
|
|
|
|
Network: net.Network_TCP,
|
|
|
|
Address: dcList[dcID],
|
|
|
|
Port: net.Port(443),
|
|
|
|
}
|
2018-07-04 12:58:19 -04:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, sPolicy.Timeouts.ConnectionIdle)
|
|
|
|
ctx = core.ContextWithBufferPolicy(ctx, sPolicy.Buffer)
|
|
|
|
|
2018-07-04 11:48:48 -04:00
|
|
|
link, err := dispatcher.Dispatch(ctx, dest)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to dispatch request to: ", dest).Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request := func() error {
|
2018-07-04 12:58:19 -04:00
|
|
|
defer timer.SetTimeout(sPolicy.Timeouts.DownlinkOnly)
|
|
|
|
|
2018-07-04 11:48:48 -04:00
|
|
|
reader := buf.NewReader(crypto.NewCryptionReader(decryptor, conn))
|
2018-07-05 08:47:46 -04:00
|
|
|
return buf.Copy(reader, link.Writer, buf.UpdateActivity(timer))
|
2018-07-04 11:48:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
response := func() error {
|
2018-07-04 12:58:19 -04:00
|
|
|
defer timer.SetTimeout(sPolicy.Timeouts.UplinkOnly)
|
|
|
|
|
2018-07-04 11:48:48 -04:00
|
|
|
encryptor := crypto.NewAesCTRStream(auth.EncodingKey[:], auth.EncodingNonce[:])
|
|
|
|
writer := buf.NewWriter(crypto.NewCryptionWriter(encryptor, conn))
|
2018-07-05 08:47:46 -04:00
|
|
|
return buf.Copy(link.Reader, writer, buf.UpdateActivity(timer))
|
2018-07-04 11:48:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseDoneAndCloseWriter = task.Single(response, task.OnSuccess(task.Close(link.Writer)))
|
|
|
|
if err := task.Run(task.WithContext(ctx), task.Parallel(request, responseDoneAndCloseWriter))(); err != nil {
|
|
|
|
pipe.CloseError(link.Reader)
|
|
|
|
pipe.CloseError(link.Writer)
|
|
|
|
return newError("connection ends").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewServer(ctx, config.(*ServerConfig))
|
|
|
|
}))
|
|
|
|
}
|