Skip to content

Commit

Permalink
full ipv6 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Nov 19, 2024
1 parent 37d6a3c commit 3a77228
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
5 changes: 2 additions & 3 deletions internal/transport/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net"
"net/url"
"strings"
"time"

"github.com/pkg/errors"
Expand All @@ -32,10 +33,8 @@ func NewTCP(cfg TCPConnConfig) (Conn, error) {
}

tcpPrefix := "tcp"
if cfg.IpV6 {
if cfg.IpV6 && !strings.Contains(cfg.Host, ".") {
tcpPrefix = "tcp6"

cfg.Host, _ = formatIPv6WithPort(cfg.Host)
}

tcpAddr, err := net.ResolveTCPAddr(tcpPrefix, cfg.Host)
Expand Down
13 changes: 0 additions & 13 deletions internal/transport/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package transport

import (
"fmt"
"strings"
)

type ErrNotMultiple struct {
Expand All @@ -18,15 +17,3 @@ func (e *ErrNotMultiple) Error() string {
}
return msg
}

func formatIPv6WithPort(ipv6WithPort string) (string, error) {
lastColon := strings.LastIndex(ipv6WithPort, ":")
if lastColon == -1 {
return "", fmt.Errorf("invalid IPv6 address with port")
}

address := ipv6WithPort[:lastColon]
port := ipv6WithPort[lastColon+1:]

return fmt.Sprintf("[%s]:%s", address, port), nil
}
50 changes: 40 additions & 10 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ var DcList = DCOptions{
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}},
4: {{"149.154.167.91:443", false},
{"[2001:067c:04e8:f002::a]:443", true}},
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",
TestDCs: map[int]string{
1: "149.154.175.10:443",
2: "149.154.167.40:443",
3: "149.154.175.117:443",
},
}

type DC struct {
Expand All @@ -38,7 +38,8 @@ type DC struct {
}

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

func SetDCs(dcs map[int][]DC) {
Expand All @@ -59,7 +60,7 @@ func GetHostIp(dc int, test bool, ipv6 bool) string {
}

if test {
if addr, ok := TestDataCenters[dc]; ok {
if addr, ok := DcList.TestDCs[dc]; ok {
return addr
}
}
Expand All @@ -74,7 +75,7 @@ func GetHostIp(dc int, test bool, ipv6 bool) string {

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

Expand All @@ -101,6 +102,35 @@ func SearchAddr(addr string) int {
return 4
}

func FmtIp(ipv6WithPort string) string {
if strings.HasPrefix(ipv6WithPort, "[") || strings.Contains(ipv6WithPort, ".") {
return ipv6WithPort
}

lastColon := strings.LastIndex(ipv6WithPort, ":")
if lastColon == -1 {
return ipv6WithPort
}

address := ipv6WithPort[:lastColon]
port := ipv6WithPort[lastColon+1:]

// 2001:0b28:f23f:f005:0000:0000:0000:000a -> [2001:0b28:f23f:f005::a]
// convert 0000:0000:0000:0000:0000:0000:0000:000a -> ::a
address = strings.Replace(address, "0000:0000:0000:000", ":", 1)
// remove preceding zeros
address = strings.Replace(address, ":0", ":", -1)

return fmt.Sprintf("[%s]:%s", address, port)
}

func Vtcp(isV6 bool) string {
if isV6 {
return "Tcp6"
}
return "Tcp"
}

type PingParams struct {
PingID int64
}
Expand Down
14 changes: 7 additions & 7 deletions mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ func (m *MTProto) CreateConnection(withLog bool) error {
ctx, cancelfunc := context.WithCancel(context.Background())
m.stopRoutines = cancelfunc
if withLog {
m.Logger.Info(fmt.Sprintf("connecting to [%s] - <Tcp> ...", m.Addr))
m.Logger.Info(fmt.Sprintf("connecting to [%s] - <%s> ...", utils.FmtIp(m.Addr), utils.Vtcp(m.IpV6)))
} else {
m.Logger.Debug("connecting to [" + m.Addr + "] - <Tcp> ...")
m.Logger.Debug(fmt.Sprintf("connecting to [%s] - <%s> ...", utils.FmtIp(m.Addr), utils.Vtcp(m.IpV6)))
}
err := m.connect(ctx)
if err != nil {
Expand All @@ -335,15 +335,15 @@ func (m *MTProto) CreateConnection(withLog bool) error {
m.tcpActive = true
if withLog {
if m.proxy != nil && m.proxy.Host != "" {
m.Logger.Info(fmt.Sprintf("connection to (~%s)[%s] - <Tcp> established", m.proxy.Host, m.Addr))
m.Logger.Info(fmt.Sprintf("connection to (~%s)[%s] - <%s> established", utils.FmtIp(m.proxy.Host), m.Addr, utils.Vtcp(m.IpV6)))
} else {
m.Logger.Info(fmt.Sprintf("connection to [%s] - <Tcp> established", m.Addr))
m.Logger.Info(fmt.Sprintf("connection to [%s] - <%s> established", utils.FmtIp(m.Addr), utils.Vtcp(m.IpV6)))
}
} else {
if m.proxy != nil && m.proxy.Host != "" {
m.Logger.Debug("connection to (~" + m.proxy.Host + ")[" + m.Addr + "] - <Tcp> established")
m.Logger.Debug(fmt.Sprintf("connection to (~%s)[%s] - <%s> established", utils.FmtIp(m.proxy.Host), m.Addr, utils.Vtcp(m.IpV6)))
} else {
m.Logger.Debug("connection to [" + m.Addr + "] - <Tcp> established")
m.Logger.Debug(fmt.Sprintf("connection to [%s] - <%s> established", utils.FmtIp(m.Addr), utils.Vtcp(m.IpV6)))
}
}

Expand All @@ -367,7 +367,7 @@ func (m *MTProto) connect(ctx context.Context) error {
m,
transport.TCPConnConfig{
Ctx: ctx,
Host: m.Addr,
Host: utils.FmtIp(m.Addr),
IpV6: m.IpV6,
Timeout: defaultTimeout,
Socks: m.proxy,
Expand Down

0 comments on commit 3a77228

Please sign in to comment.