Skip to content

Commit

Permalink
bug fixes on session file saving, add friendly methods to support cal…
Browse files Browse the repository at this point in the history
…ls, general bug fixes
  • Loading branch information
AmarnathCJD committed Apr 23, 2024
1 parent a29ca14 commit f1f89b3
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 48 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ client.AddInlineHandler("<pattern>", func(iq *telegram.InlineQuery) error {
- [x] Updates handling system + Cache
- [x] HTML, Markdown Parsing, Friendly Methods
- [x] Support for Flag2.0, Layer 147
- [ ] WebRTC Calls Support
- [x] WebRTC Calls Support
- [ ] Documentation for all methods
- [x] Stabilize File Uploading
- [ ] Stabilize File Downloading
Expand All @@ -153,9 +153,9 @@ client.AddInlineHandler("<pattern>", func(iq *telegram.InlineQuery) error {
- [x] MessageMediaPoll, UserFull Decode Fails
- [x] invokeWithLayer channel missing while bad Salt
- [x] tcp.io.Reader.Read unstable
- [ ] Session File some issues
- [x] Session File some issues
- [ ] Unidentified RPCError decoding fails
- [ ] HTML Parser
- [ ] Perfect HTML Parser

## Contributing

Expand Down
20 changes: 11 additions & 9 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import (
"time"
)

var (
DcList = map[int]string{
1: "149.154.175.58:443",
2: "149.154.167.50:443",
3: "149.154.175.100:443",
4: "149.154.167.91:443",
5: "91.108.56.151:443",
}
)
var DcList = map[int]string{
1: "149.154.175.58:443",
2: "149.154.167.50:443",
3: "149.154.175.100:443",
4: "149.154.167.91:443",
5: "91.108.56.151:443",
}

func SetDCs(dcList map[int]string) {
DcList = dcList
}

type PingParams struct {
PingID int64
Expand Down
6 changes: 3 additions & 3 deletions mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (m *MTProto) ExportAuth() (*session.Session, int) {
}, m.GetDC()
}

func (m *MTProto) ImportRawAuth(authKey, authKeyHash []byte, addr string, _ int, appID int32) (bool, error) {
func (m *MTProto) ImportRawAuth(authKey, authKeyHash []byte, addr string, appID int32) (bool, error) {
m.authKey, m.authKeyHash, m.Addr, m.appID = authKey, authKeyHash, addr, appID
m.Logger.Debug("imported auth key, auth key hash, addr, dc, appID")
if !m.memorySession {
Expand Down Expand Up @@ -210,7 +210,7 @@ func (m *MTProto) AppID() int32 {
func (m *MTProto) ReconnectToNewDC(dc int) (*MTProto, error) {
newAddr, isValid := utils.DcList[dc]
if !isValid {
return nil, errors.New("invalid DC ID provided")
return nil, errors.New("invalid data center id provided")
}
m.sessionStorage.Delete()
m.Logger.Debug("deleted old auth key file")
Expand All @@ -230,7 +230,7 @@ func (m *MTProto) ReconnectToNewDC(dc int) (*MTProto, error) {
}
sender.serverRequestHandlers = m.serverRequestHandlers
m.stopRoutines()
m.Logger.Info(fmt.Sprintf("user migrated to -> [DC %d]", dc))
m.Logger.Info(fmt.Sprintf("user migrated to DC%s - %s", strconv.Itoa(dc), newAddr))
m.Logger.Debug("reconnecting to new DC with new auth key")
errConn := sender.CreateConnection(true)
if errConn != nil {
Expand Down
1 change: 1 addition & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func (m *MTProto) AddCustomServerRequestHandler(handler func(i any) bool) {
}

func (m *MTProto) SaveSession() (err error) {
m.Logger.Debug("saving session ->", m.sessionStorage.Path())
return m.sessionStorage.Store(&session.Session{
Key: m.authKey,
Hash: m.authKeyHash,
Expand Down
5 changes: 3 additions & 2 deletions telegram/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (c *Client) LoginBot(botToken string) error {
if au, _ := c.IsAuthorized(); au {
return nil
}

_, err := c.AuthImportBotAuthorization(1, c.AppID(), c.AppHash(), botToken)
if err == nil {
c.clientData.botAcc = true
Expand Down Expand Up @@ -202,10 +203,10 @@ func codeAuthAttempt(c *Client, phoneNumber string, opts *LoginOptions) (AuthAut
return authResp, nil
}

if matchError(err, "[PHONE_CODE_INVALID]") {
if matchError(err, "PHONE_CODE_INVALID") {
c.Log.Error(errors.Wrap(err, "invalid phone code"))
continue
} else if matchError(err, "Two-steps verification is enabled") || matchError(err, "2FA is enabled, use a password to login") { // TODO: Implement matchRPCError
} else if matchError(err, "SESSION_PASSWORD_NEEDED") {
acceptPasswordInput:
if opts.Password == "" {
for {
Expand Down
26 changes: 11 additions & 15 deletions telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
Expand Down Expand Up @@ -127,9 +126,9 @@ func NewClient(config ClientConfig) (*Client, error) {
func genCacheFileName(stringSession string) string {
if stringSession != "" {
// return middle 10 characters of the string session
return "gogram_" + stringSession[len(stringSession)/2-2:len(stringSession)/2+2]
return "cache_" + stringSession[len(stringSession)/2-1:len(stringSession)/2+1]
}
return "gogram_main"
return "cache"
}

func (c *Client) setupMTProto(config ClientConfig) error {
Expand Down Expand Up @@ -167,7 +166,7 @@ func (c *Client) clientWarnings(config ClientConfig) error {
return errors.New("your app id or app hash is empty, please provide them")
}
if config.AppHash == "" {
c.Log.Warn("appHash is empty, some features may not work")
c.Log.Debug("appHash is empty, some features may not work")
}

if !IsFfmpegInstalled() {
Expand All @@ -186,15 +185,10 @@ func (c *Client) setupDispatcher() {
}

func (c *Client) cleanClientConfig(config ClientConfig) ClientConfig {
if config.Session != "" {
configSession, err := filepath.Abs(config.Session)
if err != nil {
c.Log.Error("error getting absolute path of session file: ", err)
} else {
config.Session = configSession
}
}
config.Session = getStr(config.Session, filepath.Join(getAbsWorkingDir(), "session.session"))

// if config.Session is a filename, join it with the working directory

config.Session = joinAbsWorkingDir(config.Session)
config.DataCenter = getInt(config.DataCenter, DefaultDataCenter)
config.PublicKeys, _ = keys.GetRSAKeys()
return config
Expand Down Expand Up @@ -238,6 +232,8 @@ func (c *Client) InitialRequest() error {
DataCenters[int(dc.ID)] = dc.IpAddress + ":" + strconv.Itoa(int(dc.Port))
}
}

utils.SetDCs(DataCenters)
}

return nil
Expand Down Expand Up @@ -516,8 +512,8 @@ func (c *Client) ImportSession(sessionString string) (bool, error) {
// IpAddr: The IP address of the DC
// DcID: The DC ID to connect to
// AppID: The App ID to use
func (c *Client) ImportRawSession(authKey, authKeyHash []byte, IpAddr string, DcID int, AppID int32) (bool, error) {
return c.MTProto.ImportRawAuth(authKey, authKeyHash, IpAddr, DcID, AppID)
func (c *Client) ImportRawSession(authKey, authKeyHash []byte, IpAddr string, AppID int32) (bool, error) {
return c.MTProto.ImportRawAuth(authKey, authKeyHash, IpAddr, AppID)
}

// ExportRawSession exports a session to raw TData
Expand Down
25 changes: 20 additions & 5 deletions telegram/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,27 @@ func getInt(a, b int) int {
return a
}

func getAbsWorkingDir() string {
dirEx, err := os.Executable()
if err != nil {
panic(err)
func joinAbsWorkingDir(filename string) string {
if filename == "" {
filename = "session.dat" // Default filename for session file
}

if !filepath.IsAbs(filename) || !strings.Contains(filename, string(filepath.Separator)) {
workDir, err := os.Getwd()
if err != nil {
panic(err)
}

return filepath.Join(workDir, filename)
}
return filepath.Dir(dirEx)

return filename

// dirEx, err := os.Executable()
// if err != nil {
// panic(err)
// }
// return filepath.Dir(dirEx)
}

func PathIsWritable(path string) bool {
Expand Down
25 changes: 14 additions & 11 deletions telegram/tgcalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@ package telegram

import "fmt"

type GroupCallMedia struct {
Peer Peer
Started bool
CurrentFile string
}

func (c *Client) StartGroupCallMedia(peer interface{}) (*GroupCallMedia, error) {
func (c *Client) StartGroupCallMedia(peer interface{}) (PhoneCall, error) {
peerDialog, err := c.GetSendablePeer(peer)
if err != nil {
return nil, err
}
// Start the group call
_, e := c.PhoneCreateGroupCall(&PhoneCreateGroupCallParams{
updates, e := c.PhoneCreateGroupCall(&PhoneCreateGroupCallParams{
Peer: peerDialog,
RandomID: int32(GenRandInt()),
})
if e != nil {
return nil, e
}
// TODO : Check if the group call is already started
// TODO : Implement this
return &GroupCallMedia{}, nil

switch updates.(type) {
case *UpdatesObj:
for _, update := range updates.(*UpdatesObj).Updates {
switch update := update.(type) {
case *UpdatePhoneCall:
return update.PhoneCall, nil
}
}
}

return nil, fmt.Errorf("StartGroupCallMedia: failed to start group call")
}

// TODO: after implementing latest Layer.
Expand Down

0 comments on commit f1f89b3

Please sign in to comment.