11package cli
22
33import (
4- "context"
54 "encoding/json"
65 "fmt"
7- "io"
86 "net"
9- "net/http"
107 "net/url"
118 "os"
129 "strconv"
13- "strings"
1410 "sync"
1511
16- "github.com/9seconds/mtg/v2/essentials"
1712 "github.com/9seconds/mtg/v2/internal/config"
1813 "github.com/9seconds/mtg/v2/internal/utils"
19- "github.com/9seconds/mtg/v2/mtglib"
2014)
2115
2216type accessResponse struct {
@@ -65,7 +59,7 @@ func (a *Access) Run(cli *CLI, version string) error {
6559 wg .Go (func () {
6660 ip := a .PublicIPv4
6761 if ip == nil {
68- ip = a . getIP (ntw , "tcp4" )
62+ ip = getIP (ntw , "tcp4" )
6963 }
7064
7165 if ip != nil {
@@ -77,7 +71,7 @@ func (a *Access) Run(cli *CLI, version string) error {
7771 wg .Go (func () {
7872 ip := a .PublicIPv6
7973 if ip == nil {
80- ip = a . getIP (ntw , "tcp6" )
74+ ip = getIP (ntw , "tcp6" )
8175 }
8276
8377 if ip != nil {
@@ -100,45 +94,6 @@ func (a *Access) Run(cli *CLI, version string) error {
10094 return nil
10195}
10296
103- func (a * Access ) getIP (ntw mtglib.Network , protocol string ) net.IP {
104- dialer := ntw .NativeDialer ()
105- client := ntw .MakeHTTPClient (func (ctx context.Context , network , address string ) (essentials.Conn , error ) {
106- conn , err := dialer .DialContext (ctx , protocol , address )
107- if err != nil {
108- return nil , err
109- }
110- return essentials .WrapNetConn (conn ), err
111- })
112-
113- req , err := http .NewRequest (http .MethodGet , "https://ifconfig.co" , nil ) //nolint: noctx
114- if err != nil {
115- panic (err )
116- }
117-
118- req .Header .Add ("Accept" , "text/plain" )
119-
120- resp , err := client .Do (req )
121- if err != nil {
122- return nil
123- }
124-
125- if resp .StatusCode != http .StatusOK {
126- return nil
127- }
128-
129- defer func () {
130- io .Copy (io .Discard , resp .Body ) //nolint: errcheck
131- resp .Body .Close () //nolint: errcheck
132- }()
133-
134- data , err := io .ReadAll (resp .Body )
135- if err != nil {
136- return nil
137- }
138-
139- return net .ParseIP (strings .TrimSpace (string (data )))
140- }
141-
14297func (a * Access ) makeURLs (conf * config.Config , ip net.IP ) * accessResponseURLs {
14398 if ip == nil {
14499 return nil
0 commit comments