mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 14:04:02 +03:00
Use dc indexes accordingly
This commit is contained in:
@@ -3,6 +3,8 @@ package obfuscated2
|
||||
import "encoding/binary"
|
||||
|
||||
const (
|
||||
DefaultDC = 2
|
||||
|
||||
handshakeFrameLen = 64
|
||||
|
||||
handshakeFrameLenKey = 32
|
||||
@@ -41,11 +43,11 @@ func (h *handshakeFrame) dc() int {
|
||||
|
||||
switch {
|
||||
case idx > 0:
|
||||
return int(idx) - 1
|
||||
return int(idx)
|
||||
case idx < 0:
|
||||
return -int(idx + 1)
|
||||
return -int(idx)
|
||||
default:
|
||||
return 0
|
||||
return DefaultDC
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,13 +31,13 @@ func (suite *HandshakeFrameTestSuite) TestOk() {
|
||||
suite.Equal("zyjgwaWXlTP6qjVMUP2scQbqdUtspmyH1Hen+2Ess8w", suite.Encode(hf.key()))
|
||||
suite.Equal("z0SZMW/b9fMaokYQe6PbOg", suite.Encode(hf.iv()))
|
||||
suite.Equal("H5lBgQ", suite.Encode(hf.connectionType()))
|
||||
suite.EqualValues(2093, hf.dc())
|
||||
suite.EqualValues(2094, hf.dc())
|
||||
|
||||
inverted := hf.invert()
|
||||
suite.Equal("OtujexBGohrz9dtvMZlEz8yzLGH7p3fUh2ymbEt16gY", suite.Encode(inverted.key()))
|
||||
suite.Equal("caz9UEw1qvozlZelweAozw", suite.Encode(inverted.iv()))
|
||||
suite.Equal("H5lBgQ", suite.Encode(inverted.connectionType()))
|
||||
suite.EqualValues(2093, inverted.dc())
|
||||
suite.EqualValues(2094, inverted.dc())
|
||||
}
|
||||
|
||||
func TestHandshakeFrame(t *testing.T) {
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"secret": "NnoYmu4Y+jHBkAVO/UqOlQ",
|
||||
"frame": "gDcXwaMY4RwlR+nJw+ILDr123UJHHjjE/U5pF4m/Y04AmH7lEpEL6UYRnIYDbDlOHSDxc1ToziPvNlJJh8RMow",
|
||||
"dc": 1,
|
||||
"dc": 2,
|
||||
"encrypted": {
|
||||
"text": "AQIDBAUGBwgJCg",
|
||||
"cipher": "wZV3TR39l9nRoQ"
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"secret": "NnoYmu4Y+jHBkAVO/UqOlQ",
|
||||
"frame": "M2WyxeiwIQB+ZOFxNzSNHtu9OdESkfxv3JkKFimCxUoYA3BD/Ql9nXB/OIonCKLUKCcS0VzZ2P6/+5oQ9GI8YA",
|
||||
"dc": 1,
|
||||
"dc": 2,
|
||||
"encrypted": {
|
||||
"text": "AQIDBAUGBwgJCg",
|
||||
"cipher": "tzAwrCz00odERg"
|
||||
|
||||
@@ -14,16 +14,16 @@ type Telegram struct {
|
||||
}
|
||||
|
||||
func (t Telegram) Dial(ctx context.Context, dc int) (net.Conn, error) {
|
||||
if dc < 0 || dc > 4 {
|
||||
if dc <= 0 || dc > 5 {
|
||||
return nil, fmt.Errorf("do not know how to dial to %d", dc)
|
||||
}
|
||||
|
||||
var addresses []tgAddr
|
||||
|
||||
if t.preferIP == preferIPOnlyIPv6 {
|
||||
addresses = []tgAddr{v6Addresses[dc]}
|
||||
addresses = []tgAddr{v6Addresses[dc-1]}
|
||||
} else {
|
||||
addresses = append(addresses, v4Addresses[dc]...)
|
||||
addresses = append(addresses, v4Addresses[dc-1]...)
|
||||
rand.Shuffle(len(addresses), func(i, j int) {
|
||||
addresses[i], addresses[j] = addresses[j], addresses[i]
|
||||
})
|
||||
@@ -31,9 +31,9 @@ func (t Telegram) Dial(ctx context.Context, dc int) (net.Conn, error) {
|
||||
|
||||
switch t.preferIP {
|
||||
case preferIPPreferIPv4:
|
||||
addresses = append(addresses, v6Addresses[dc])
|
||||
addresses = append(addresses, v6Addresses[dc-1])
|
||||
case preferIPPreferIPv6:
|
||||
addresses = append([]tgAddr{v6Addresses[dc]}, addresses...)
|
||||
addresses = append([]tgAddr{v6Addresses[dc-1]}, addresses...)
|
||||
case preferIPOnlyIPv4, preferIPOnlyIPv6:
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,8 @@ func (suite *TelegramTestSuite) TearDownTest() {
|
||||
func (suite *TelegramTestSuite) TestUnknownDC() {
|
||||
testData := []int{
|
||||
-1,
|
||||
5,
|
||||
0,
|
||||
6,
|
||||
100,
|
||||
}
|
||||
|
||||
@@ -50,10 +51,10 @@ func (suite *TelegramTestSuite) TestUnknownDC() {
|
||||
func (suite *TelegramTestSuite) TestDialToCorrectIPs() {
|
||||
testData := map[int][]tgAddr{}
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
for i := 1; i <= 5; i++ {
|
||||
testData[i] = []tgAddr{}
|
||||
testData[i] = append(testData[i], v4Addresses[i]...)
|
||||
testData[i] = append(testData[i], v6Addresses[i])
|
||||
testData[i] = append(testData[i], v4Addresses[i-1]...)
|
||||
testData[i] = append(testData[i], v6Addresses[i-1])
|
||||
}
|
||||
|
||||
for i, v := range testData {
|
||||
@@ -95,7 +96,7 @@ func (suite *TelegramTestSuite) TestDialPreferIPRange() {
|
||||
}
|
||||
|
||||
tg, _ := New(suite.dialerMock, name)
|
||||
_, err := tg.Dial(context.Background(), 0)
|
||||
_, err := tg.Dial(context.Background(), 1)
|
||||
|
||||
assert.True(t, errors.Is(err, io.EOF))
|
||||
})
|
||||
@@ -122,7 +123,7 @@ func (suite *TelegramTestSuite) TestDialPreferIPPriority() {
|
||||
|
||||
tg, _ := New(suite.dialerMock, name)
|
||||
|
||||
res, err := tg.Dial(context.Background(), 0)
|
||||
res, err := tg.Dial(context.Background(), 1)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, conn, res)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user