Skip to content

Commit

Permalink
respect custom ipaddress even if session is present
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Dec 18, 2024
1 parent 3d256e9 commit 4fa3649
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 9 additions & 4 deletions mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type MTProto struct {
serviceChannel chan tl.Object
serviceModeActivated bool

authKey404 []int64
authKey404 [2]int64
IpV6 bool

Logger *utils.Logger
Expand All @@ -98,6 +98,7 @@ type Config struct {
Proxy *url.URL
Mode string
Ipv6 bool
CustomHost bool
}

func NewMTProto(c Config) (*MTProto, error) {
Expand Down Expand Up @@ -155,6 +156,10 @@ func NewMTProto(c Config) (*MTProto, error) {
return nil, errors.Wrap(err, "loading auth")
}

if c.CustomHost {
mtproto.Addr = c.ServerHost
}

if c.FloodHandler != nil {
mtproto.floodHandler = c.FloodHandler
}
Expand All @@ -177,7 +182,7 @@ func parseTransportMode(sMode string) mode.Variant {

func (m *MTProto) LoadSession(sess *session.Session) error {
m.authKey, m.authKeyHash, m.Addr, m.appID = sess.Key, sess.Hash, sess.Hostname, sess.AppID
m.Logger.Debug("importing Auth from session...")
m.Logger.Debug("importing auth from session...")
if !m.memorySession {
if err := m.SaveSession(); err != nil {
return errors.Wrap(err, "saving session")
Expand Down Expand Up @@ -625,7 +630,7 @@ func (m *MTProto) startReadingResponses(ctx context.Context) {

func (m *MTProto) handle404Error() {
if len(m.authKey404) == 0 {
m.authKey404 = []int64{1, time.Now().Unix()}
m.authKey404 = [2]int64{1, time.Now().Unix()}
} else {
if time.Now().Unix()-m.authKey404[1] < 30 { // repeated failures
m.authKey404[0]++
Expand All @@ -641,7 +646,7 @@ func (m *MTProto) handle404Error() {
if err != nil {
m.Logger.Error(errors.Wrap(err, "reconnecting"))
}
} else if m.authKey404[0] > 8 {
} else if m.authKey404[0] > 16 {
panic("[AUTH_KEY_INVALID] (code -404)")
}
}
Expand Down
3 changes: 3 additions & 0 deletions telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ func NewClient(config ClientConfig) (*Client, error) {
}

func (c *Client) setupMTProto(config ClientConfig) error {
var customHost bool
toIpAddr := func() string {
if config.IpAddr != "" {
customHost = true
return config.IpAddr
} else {
return utils.GetHostIp(config.DataCenter, config.TestMode, config.ForceIPv6)
Expand All @@ -169,6 +171,7 @@ func (c *Client) setupMTProto(config ClientConfig) error {
Proxy: config.Proxy,
MemorySession: config.MemorySession,
Ipv6: config.ForceIPv6,
CustomHost: customHost,
})
if err != nil {
return errors.Wrap(err, "creating mtproto client")
Expand Down

0 comments on commit 4fa3649

Please sign in to comment.