Skip to content

Commit

Permalink
media handing, start imp support for ipv6.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Nov 19, 2024
1 parent 43f8510 commit bc27077
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 497 deletions.
15 changes: 6 additions & 9 deletions examples/progress/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ func main() {

client.LoginBot(botToken)

var pm *telegram.ProgressManager
chat, _ := client.ResolvePeer("chatId")
m, _ := client.SendMessage(chat, "Starting File Upload...")

var pm = telegram.NewProgressManager(5)
pm.Edit(func(a, b int64) {
client.EditMessage(chat, m.ID, pm.GetStats(a))
})

client.SendMedia(chat, "<file-name>", &telegram.MediaOptions{
ProgressCallback: func(total, curr int64) {
if pm == nil {
pm = telegram.NewProgressManager(total, 5) // 5 seconds edit interval
}
if pm.ShouldEdit() {
client.EditMessage(chat, m.ID, pm.GetStats(curr))
}
},
ProgressManager: pm,
})
}
1 change: 1 addition & 0 deletions internal/transport/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type tcpConn struct {
type TCPConnConfig struct {
Ctx context.Context
Host string
IpV6 bool
Timeout time.Duration
Socks *url.URL
}
Expand Down
64 changes: 52 additions & 12 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,74 @@ import (
// ------------------ Telegram Data Center Configs ------------------

var DcList = DCOptions{
DCS: 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"},
DCS: map[int][]DC{
1: {{"149.154.175.58:443", false}},
2: {{"149.154.167.50:443", false}},
3: {{"149.154.175.100:443", false}},
4: {{"149.154.167.91:443", false}},
5: {{"91.108.56.151:443", false}},
},
}

var TestDataCenters = map[int]string{
1: "149.154.175.10:443",
2: "149.154.167.40:443",
3: "149.154.175.117:443",
}

type DC struct {
Addr string
V bool
}

type DCOptions struct {
DCS map[int][]string
DCS map[int][]DC
}

func SetDCs(dcs map[int][]string) {
func SetDCs(dcs map[int][]DC) {
DcList.DCS = dcs
}

func GetAddr(dc int) string {
func GetAddr(dc int) (string, bool) {
if addrs, ok := DcList.DCS[dc]; ok {
return addrs[0]
return addrs[0].Addr, addrs[0].V
}
return "", false
}

func GetHostIp(dc int, test bool, ipv6 bool) string {
dcMap, ok := DcList.DCS[dc]
if !ok {
return ""
}

if test {
if addr, ok := TestDataCenters[dc]; ok {
return addr
}
}

if ipv6 {
for _, dc := range dcMap {
if dc.V {
return dc.Addr
}
}
}
return ""

for _, dc := range dcMap {
if !dc.V {
return dc.Addr
}
}

return dcMap[0].Addr
}

func SearchAddr(addr string) int {
for dc, addrs := range DcList.DCS {
for _, a := range addrs {
if a == addr {
if a.Addr == addr {
return dc
}
}
Expand Down
24 changes: 20 additions & 4 deletions mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type MTProto struct {
serviceModeActivated bool

authKey404 []int64
IpV6 bool

Logger *utils.Logger

Expand All @@ -94,6 +95,7 @@ type Config struct {
LogLevel string
Proxy *url.URL
Mode string
Ipv6 bool
}

func NewMTProto(c Config) (*MTProto, error) {
Expand Down Expand Up @@ -136,6 +138,7 @@ func NewMTProto(c Config) (*MTProto, error) {
proxy: c.Proxy,
floodHandler: func(err error) bool { return false },
mode: parseTransportMode(c.Mode),
IpV6: c.Ipv6,
}

mtproto.Logger.Debug("initializing mtproto...")
Expand Down Expand Up @@ -249,7 +252,7 @@ func (m *MTProto) SwitchDc(dc int) (*MTProto, error) {
if m.noRedirect {
return m, nil
}
newAddr := utils.GetAddr(dc)
newAddr := utils.GetHostIp(dc, false, m.IpV6)
if newAddr == "" {
return nil, errors.New("dc_id not found")
}
Expand All @@ -266,6 +269,7 @@ func (m *MTProto) SwitchDc(dc int) (*MTProto, error) {
LogLevel: m.Logger.Lev(),
Proxy: m.proxy,
AppID: m.appID,
Ipv6: m.IpV6,
}
sender, err := NewMTProto(cfg)
if err != nil {
Expand All @@ -283,13 +287,24 @@ func (m *MTProto) SwitchDc(dc int) (*MTProto, error) {
}

func (m *MTProto) ExportNewSender(dcID int, mem bool) (*MTProto, error) {
newAddr := utils.GetAddr(dcID)
newAddr := utils.GetHostIp(dcID, false, m.IpV6)
execWorkDir, err := os.Executable()
if err != nil {
return nil, errors.Wrap(err, "getting executable directory")
}
wd := filepath.Dir(execWorkDir)
cfg := Config{DataCenter: dcID, PublicKey: m.publicKey, ServerHost: newAddr, AuthKeyFile: filepath.Join(wd, "exported_sender"), MemorySession: mem, LogLevel: "disabled", Proxy: m.proxy, AppID: m.appID}
cfg := Config{
DataCenter: dcID,
PublicKey: m.publicKey,
ServerHost: newAddr,
AuthKeyFile: filepath.Join(wd, "exported_sender"),
MemorySession: mem,
LogLevel: "disabled",
Proxy: m.proxy,
AppID: m.appID,
Ipv6: m.IpV6,
}

if dcID == m.GetDC() {
cfg.SessionStorage = m.sessionStorage
}
Expand Down Expand Up @@ -353,6 +368,7 @@ func (m *MTProto) connect(ctx context.Context) error {
transport.TCPConnConfig{
Ctx: ctx,
Host: m.Addr,
IpV6: m.IpV6,
Timeout: defaultTimeout,
Socks: m.proxy,
},
Expand Down Expand Up @@ -421,7 +437,7 @@ func (m *MTProto) makeRequestCtx(ctx context.Context, data tl.Object, expectedTy

select {
case <-ctx.Done():
m.writeRPCResponse(int(msgId), &objects.Null{})
go m.writeRPCResponse(int(msgId), &objects.Null{})
return nil, ctx.Err()
case response := <-resp:
switch r := response.(type) {
Expand Down
19 changes: 13 additions & 6 deletions telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type ClientConfig struct {
TestMode bool
LogLevel string
Proxy *url.URL
ForceIPv6 bool
TransportMode string
FloodHandler func(err error) bool
}
Expand All @@ -110,7 +111,12 @@ func (s *Session) Encode() string {
}

func NewClient(config ClientConfig) (*Client, error) {
client := &Client{wg: sync.WaitGroup{}, Log: utils.NewLogger("gogram [client]"), stopCh: make(chan struct{})}
client := &Client{
wg: sync.WaitGroup{},
Log: utils.NewLogger("gogram [client]"),
stopCh: make(chan struct{}),
}

config = client.cleanClientConfig(config)
client.setupClientData(config)

Expand Down Expand Up @@ -149,7 +155,7 @@ func (c *Client) setupMTProto(config ClientConfig) error {
if config.IpAddr != "" {
return config.IpAddr
} else {
return GetHostIp(config.DataCenter, config.TestMode)
return utils.GetHostIp(config.DataCenter, config.TestMode, config.ForceIPv6)
}
}

Expand All @@ -163,6 +169,7 @@ func (c *Client) setupMTProto(config ClientConfig) error {
StringSession: config.StringSession,
Proxy: config.Proxy,
MemorySession: config.MemorySession,
Ipv6: config.ForceIPv6,
})
if err != nil {
return errors.Wrap(err, "creating mtproto client")
Expand Down Expand Up @@ -259,14 +266,14 @@ func (c *Client) InitialRequest() error {

c.Log.Debug("received initial invokeWithLayer response")
if config, ok := serverConfig.(*Config); ok {
var dcs = make(map[int][]string)
var dcs = make(map[int][]utils.DC)
for _, dc := range config.DcOptions {
if !dc.Ipv6 && !dc.MediaOnly && !dc.Cdn {
if !dc.MediaOnly && !dc.Cdn {
if _, ok := dcs[int(dc.ID)]; !ok {
dcs[int(dc.ID)] = []string{}
dcs[int(dc.ID)] = []utils.DC{}
}

dcs[int(dc.ID)] = append(dcs[int(dc.ID)], dc.IpAddress+":"+strconv.Itoa(int(dc.Port)))
dcs[int(dc.ID)] = append(dcs[int(dc.ID)], utils.DC{Addr: dc.IpAddress + ":" + strconv.Itoa(int(dc.Port)), V: dc.Ipv6})
}
}

Expand Down
11 changes: 7 additions & 4 deletions telegram/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,17 +472,20 @@ mediaTypeSwitch:
return photoExt, nil
}

if attr == nil {
return nil, errors.New("attributes cannot be nil")
}
documentExt := &InputMediaDocumentExternal{URL: media, TtlSeconds: getValue(attr.TTL, 0), Spoiler: getValue(attr.Spoiler, false)}
if attr != nil && attr.Inline {
if attr.Inline {
return c.uploadToSelf(documentExt)
}

return documentExt, nil
} else {
if _, err := os.Stat(media); err == nil {
uploadOpts := &UploadOptions{}
if attr.ProgressCallback != nil {
uploadOpts.ProgressCallback = attr.ProgressCallback
if attr.ProgressManager != nil {
uploadOpts.ProgressManager = attr.ProgressManager
}
uploadOpts.Threads = attr.UploadThreads

Expand Down Expand Up @@ -596,7 +599,7 @@ mediaTypeSwitch:
case []byte, *io.Reader, *bytes.Buffer, *os.File:
var uopts *UploadOptions = &UploadOptions{}
if attr != nil {
uopts.ProgressCallback = attr.ProgressCallback
uopts.ProgressManager = attr.ProgressManager
if attr.FileName != "" {
uopts.FileName = attr.FileName
}
Expand Down
Loading

0 comments on commit bc27077

Please sign in to comment.