mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 16:34:02 +03:00
REPOSITORY / ScuroNeko/mtg
Compare commits
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
006fba1046 | ||
|
|
7b333ed833 | ||
|
|
5adfee5dd4 | ||
|
|
de89de2ad6 | ||
|
|
b0d37de0ec | ||
|
|
0cb25ba7ff | ||
|
|
614acd7303 | ||
|
|
4f5368aa2a | ||
|
|
cfb5fe66be | ||
|
|
fb390d3417 | ||
|
|
f0ae4ce290 | ||
|
|
b6b900e430 | ||
|
|
8154f65e0e | ||
|
|
a60523fed0 | ||
|
|
63b147c287 | ||
|
|
21c0d18c7c | ||
|
|
8f0bf47d56 |
+15
-1
@@ -5,6 +5,19 @@ FROM golang:1.26-alpine AS build
|
||||
|
||||
ENV CGO_ENABLED=0
|
||||
|
||||
# this is done for backward compatibility: before that we mounted a config
|
||||
# into /config.toml. Some application allow mounting directories only,
|
||||
# so it makes problems. So, instead we are going to do 2 steps:
|
||||
# 1. Create /config/config.toml as a symlink to /config.toml
|
||||
# 2. Force /mtg to use /config/config.toml
|
||||
#
|
||||
# it helps in both ways: users with directories could use /config directory
|
||||
# and overlap a symlink by their bind mount. Old users could continue using
|
||||
# /config.toml as a real config.
|
||||
RUN set -x \
|
||||
&& mkdir -p /config \
|
||||
&& ln -sv /config.toml /config/config.toml
|
||||
|
||||
RUN --mount=type=cache,target=/var/cache/apk \
|
||||
set -x \
|
||||
&& apk --update add \
|
||||
@@ -35,8 +48,9 @@ RUN set -x \
|
||||
FROM scratch
|
||||
|
||||
ENTRYPOINT ["/mtg"]
|
||||
CMD ["run", "/config.toml"]
|
||||
CMD ["run", "/config/config.toml"]
|
||||
|
||||
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
COPY --from=build /app/mtg /mtg
|
||||
COPY --from=build /app/example.config.toml /config.toml
|
||||
COPY --from=build /config /config
|
||||
|
||||
@@ -301,6 +301,38 @@ For example, you've bought a VPS from [Digital
|
||||
Ocean](https://www.digitalocean.com/). Then it might be a good idea to
|
||||
generate a secret for _digitalocean.com_ then.
|
||||
|
||||
### Check configuration
|
||||
|
||||
There is a special command for secret verification:
|
||||
|
||||
```
|
||||
$ mtg doctor /path/to/my/config.toml
|
||||
Deprecated options
|
||||
✅ All good
|
||||
Time skewness
|
||||
✅ Time drift is -607.048µs, but tolerate-time-skewness is 5s
|
||||
Validate native network connectivity
|
||||
✅ DC 1
|
||||
✅ DC 2
|
||||
✅ DC 3
|
||||
✅ DC 4
|
||||
✅ DC 5
|
||||
✅ DC 203
|
||||
Validate network connectivity with proxy socks5://127.0.0.1:1080
|
||||
✅ DC 1
|
||||
✅ DC 2
|
||||
✅ DC 3
|
||||
✅ DC 4
|
||||
✅ DC 5
|
||||
✅ DC 203
|
||||
Validate fronting domain connectivity
|
||||
✅ xx.xx.xx.xx:yyy is reachable
|
||||
Validate SNI-DNS match
|
||||
✅ IP address xx.xx.xx.xx matches secret hostname <REDACTED>
|
||||
```
|
||||
|
||||
It aims to find out possible inconsistencies and problems with your
|
||||
configuration. It makes sense to run it before executing any relevant commands.
|
||||
|
||||
### Simple run mode
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package essentials
|
||||
|
||||
// TelegramCoreAddresses are publicly known addresses of Telegram core network.
|
||||
var TelegramCoreAddresses = map[int][]string{
|
||||
1: {
|
||||
"149.154.175.50:443",
|
||||
"[2001:b28:f23d:f001::a]:443",
|
||||
},
|
||||
2: {
|
||||
"149.154.167.51:443",
|
||||
"95.161.76.100:443",
|
||||
"[2001:67c:04e8:f002::a]:443",
|
||||
},
|
||||
3: {
|
||||
"149.154.175.100:443",
|
||||
"[2001:b28:f23d:f003::a]:443",
|
||||
},
|
||||
4: {
|
||||
"149.154.167.91:443",
|
||||
"[2001:67c:04e8:f004::a]:443",
|
||||
},
|
||||
5: {
|
||||
"149.154.171.5:443",
|
||||
"[2001:b28:f23f:f005::a]:443",
|
||||
},
|
||||
203: {
|
||||
"91.105.192.100:443",
|
||||
"[2a0a:f280:0203:000a:5000:0000:0000:0100]:443",
|
||||
},
|
||||
}
|
||||
@@ -11,7 +11,7 @@ require (
|
||||
github.com/d4l3k/messagediff v1.2.1 // indirect
|
||||
github.com/jarcoal/httpmock v1.0.8
|
||||
github.com/mccutchen/go-httpbin v1.1.1
|
||||
github.com/panjf2000/ants/v2 v2.11.6
|
||||
github.com/panjf2000/ants/v2 v2.12.0
|
||||
github.com/prometheus/client_golang v1.23.2
|
||||
github.com/prometheus/common v0.67.5 // indirect
|
||||
github.com/prometheus/procfs v0.20.1 // indirect
|
||||
@@ -27,6 +27,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/beevik/ntp v1.5.0
|
||||
github.com/ncruces/go-dns v1.3.2
|
||||
github.com/pelletier/go-toml/v2 v2.2.4
|
||||
github.com/pires/go-proxyproto v0.11.0
|
||||
|
||||
@@ -12,6 +12,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
|
||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
|
||||
github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
|
||||
github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1:J29hk+f9lJrblVIfiJOtTFk+OblBawmib4uz/VdKzlg=
|
||||
github.com/beevik/ntp v1.5.0 h1:y+uj/JjNwlY2JahivxYvtmv4ehfi3h74fAuABB9ZSM4=
|
||||
github.com/beevik/ntp v1.5.0/go.mod h1:mJEhBrwT76w9D+IfOEGvuzyuudiW9E52U2BaTrMOYow=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
@@ -53,8 +55,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/ncruces/go-dns v1.3.2 h1:kBLuUZBgkQ4qF4WDXZRQ4rG0Gk6sLVJQ5tESkWrxUa0=
|
||||
github.com/ncruces/go-dns v1.3.2/go.mod h1:tuzixNY8PY/M7yUzcvRbUaeLs3ifIdydpi5H2bfRU+s=
|
||||
github.com/panjf2000/ants/v2 v2.11.6 h1:JKsoIUukIoCO0sP0gcOqdyoXmpyKXuU6fC57rODtpug=
|
||||
github.com/panjf2000/ants/v2 v2.11.6/go.mod h1:8u92CYMUc6gyvTIw8Ru7Mt7+/ESnJahz5EVtqfrilek=
|
||||
github.com/panjf2000/ants/v2 v2.12.0 h1:u9JhESo83i/GkZnhfTNuFMMWcNt7mnV1bGJ6FT4wXH8=
|
||||
github.com/panjf2000/ants/v2 v2.12.0/go.mod h1:tSQuaNQ6r6NRhPt+IZVUevvDyFMTs+eS4ztZc52uJTY=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
|
||||
+2
-47
@@ -1,22 +1,16 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/9seconds/mtg/v2/essentials"
|
||||
"github.com/9seconds/mtg/v2/internal/config"
|
||||
"github.com/9seconds/mtg/v2/internal/utils"
|
||||
"github.com/9seconds/mtg/v2/mtglib"
|
||||
)
|
||||
|
||||
type accessResponse struct {
|
||||
@@ -65,7 +59,7 @@ func (a *Access) Run(cli *CLI, version string) error {
|
||||
wg.Go(func() {
|
||||
ip := a.PublicIPv4
|
||||
if ip == nil {
|
||||
ip = a.getIP(ntw, "tcp4")
|
||||
ip = getIP(ntw, "tcp4")
|
||||
}
|
||||
|
||||
if ip != nil {
|
||||
@@ -77,7 +71,7 @@ func (a *Access) Run(cli *CLI, version string) error {
|
||||
wg.Go(func() {
|
||||
ip := a.PublicIPv6
|
||||
if ip == nil {
|
||||
ip = a.getIP(ntw, "tcp6")
|
||||
ip = getIP(ntw, "tcp6")
|
||||
}
|
||||
|
||||
if ip != nil {
|
||||
@@ -100,45 +94,6 @@ func (a *Access) Run(cli *CLI, version string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Access) getIP(ntw mtglib.Network, protocol string) net.IP {
|
||||
dialer := ntw.NativeDialer()
|
||||
client := ntw.MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error) {
|
||||
conn, err := dialer.DialContext(ctx, protocol, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return essentials.WrapNetConn(conn), err
|
||||
})
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) //nolint: noctx
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Add("Accept", "text/plain")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
io.Copy(io.Discard, resp.Body) //nolint: errcheck
|
||||
resp.Body.Close() //nolint: errcheck
|
||||
}()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return net.ParseIP(strings.TrimSpace(string(data)))
|
||||
}
|
||||
|
||||
func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
|
||||
if ip == nil {
|
||||
return nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import "github.com/alecthomas/kong"
|
||||
|
||||
type CLI struct {
|
||||
GenerateSecret GenerateSecret `kong:"cmd,help='Generate new proxy secret'"`
|
||||
Doctor Doctor `kong:"cmd,help='Check that proxy can run correctly'"`
|
||||
Access Access `kong:"cmd,help='Print access information.'"`
|
||||
Run Run `kong:"cmd,help='Run proxy.'"`
|
||||
SimpleRun SimpleRun `kong:"cmd,help='Run proxy without config file.'"`
|
||||
|
||||
@@ -0,0 +1,368 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/9seconds/mtg/v2/essentials"
|
||||
"github.com/9seconds/mtg/v2/internal/config"
|
||||
"github.com/9seconds/mtg/v2/internal/utils"
|
||||
"github.com/9seconds/mtg/v2/mtglib"
|
||||
"github.com/9seconds/mtg/v2/network/v2"
|
||||
"github.com/beevik/ntp"
|
||||
)
|
||||
|
||||
var (
|
||||
tplError = template.Must(
|
||||
template.New("").Parse(" ‼️ {{ .description }}: {{ .error }}\n"),
|
||||
)
|
||||
|
||||
tplWDeprecatedConfig = template.Must(
|
||||
template.New("").
|
||||
Parse(` ⚠️ Option {{ .old | printf "%q" }}{{ if .old_section }} from section [{{ .old_section }}]{{ end }} is deprecated and will be removed in v{{ .when }}. Please use {{ .new | printf "%q" }}{{ if .new_section }} in [{{ .new_section }}] section{{ end }} instead.` + "\n"),
|
||||
)
|
||||
|
||||
tplOTimeSkewness = template.Must(
|
||||
template.New("").
|
||||
Parse(" ✅ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}\n"),
|
||||
)
|
||||
tplWTimeSkewness = template.Must(
|
||||
template.New("").
|
||||
Parse(" ⚠️ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}. Please check ntp.\n"),
|
||||
)
|
||||
tplETimeSkewness = template.Must(
|
||||
template.New("").
|
||||
Parse(" ❌ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}. You will get many rejected connections!\n"),
|
||||
)
|
||||
|
||||
tplODCConnect = template.Must(
|
||||
template.New("").Parse(" ✅ DC {{ .dc }}\n"),
|
||||
)
|
||||
tplEDCConnect = template.Must(
|
||||
template.New("").Parse(" ❌ DC {{ .dc }}: {{ .error }}\n"),
|
||||
)
|
||||
|
||||
tplODNSSNIMatch = template.Must(
|
||||
template.New("").Parse(" ✅ IP address {{ .ip }} matches secret hostname {{ .hostname }}\n"),
|
||||
)
|
||||
tplEDNSSNIMatch = template.Must(
|
||||
template.New("").Parse(" ❌ Hostname {{ .hostname }} {{ if .resolved }}is resolved to {{ .resolved }} addresses, not {{ if .ip4 }}{{ .ip4 }}{{ else }}{{ .ip6 }}{{ end }}{{ else }}cannot be resolved to any host{{ end }}\n"),
|
||||
)
|
||||
|
||||
tplOFrontingDomain = template.Must(
|
||||
template.New("").Parse(" ✅ {{ .address }} is reachable\n"),
|
||||
)
|
||||
tplEFrontingDomain = template.Must(
|
||||
template.New("").Parse(" ❌ {{ .address }}: {{ .error }}\n"),
|
||||
)
|
||||
)
|
||||
|
||||
type Doctor struct {
|
||||
conf *config.Config
|
||||
|
||||
ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` //nolint: lll
|
||||
}
|
||||
|
||||
func (d *Doctor) Run(cli *CLI, version string) error {
|
||||
conf, err := utils.ReadConfig(d.ConfigPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot init config: %w", err)
|
||||
}
|
||||
|
||||
d.conf = conf
|
||||
|
||||
fmt.Println("Deprecated options")
|
||||
everythingOK := d.checkDeprecatedConfig()
|
||||
|
||||
fmt.Println("Time skewness")
|
||||
everythingOK = d.checkTimeSkewness() && everythingOK
|
||||
|
||||
resolver, err := network.GetDNS(conf.GetDNS())
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create DNS resolver: %w", err)
|
||||
}
|
||||
|
||||
base := network.New(
|
||||
resolver,
|
||||
"",
|
||||
conf.Network.Timeout.TCP.Get(10*time.Second),
|
||||
conf.Network.Timeout.HTTP.Get(0),
|
||||
conf.Network.Timeout.Idle.Get(0),
|
||||
)
|
||||
|
||||
fmt.Println("Validate native network connectivity")
|
||||
everythingOK = d.checkNetwork(base) && everythingOK
|
||||
|
||||
for _, url := range conf.Network.Proxies {
|
||||
value, err := network.NewProxyNetwork(base, url.Get(nil))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Validate network connectivity with proxy %s\n", url.Get(nil))
|
||||
everythingOK = d.checkNetwork(value) && everythingOK
|
||||
}
|
||||
|
||||
fmt.Println("Validate fronting domain connectivity")
|
||||
everythingOK = d.checkFrontingDomain(base) && everythingOK
|
||||
|
||||
fmt.Println("Validate SNI-DNS match")
|
||||
everythingOK = d.checkSecretHost(resolver, base) && everythingOK
|
||||
|
||||
if !everythingOK {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Doctor) checkDeprecatedConfig() bool {
|
||||
ok := true
|
||||
|
||||
if d.conf.DomainFrontingIP.Value != nil {
|
||||
ok = false
|
||||
tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
|
||||
"when": "2.3.0",
|
||||
"old": "domain-fronting-ip",
|
||||
"old_section": "",
|
||||
"new": "ip",
|
||||
"new_section": "domain-fronting",
|
||||
})
|
||||
}
|
||||
|
||||
if d.conf.DomainFrontingPort.Value != 0 {
|
||||
ok = false
|
||||
tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
|
||||
"when": "2.3.0",
|
||||
"old": "domain-fronting-port",
|
||||
"old_section": "",
|
||||
"new": "port",
|
||||
"new_section": "domain-fronting",
|
||||
})
|
||||
}
|
||||
|
||||
if d.conf.DomainFrontingProxyProtocol.Value {
|
||||
ok = false
|
||||
tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
|
||||
"when": "2.3.0",
|
||||
"old": "domain-fronting-proxy-protocol",
|
||||
"old_section": "",
|
||||
"new": "proxy-protocol",
|
||||
"new_section": "domain-fronting",
|
||||
})
|
||||
}
|
||||
|
||||
if d.conf.Network.DOHIP.Value != nil {
|
||||
ok = false
|
||||
tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
|
||||
"when": "2.3.0",
|
||||
"old": "doh-ip",
|
||||
"old_section": "network",
|
||||
"new": "dns",
|
||||
"new_section": "network",
|
||||
})
|
||||
}
|
||||
|
||||
if ok {
|
||||
fmt.Println(" ✅ All good")
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (d *Doctor) checkTimeSkewness() bool {
|
||||
response, err := ntp.Query("0.pool.ntp.org")
|
||||
if err != nil {
|
||||
tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"description": "cannot access ntp pool",
|
||||
"error": err,
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
skewness := response.ClockOffset.Abs()
|
||||
confValue := d.conf.TolerateTimeSkewness.Get(mtglib.DefaultTolerateTimeSkewness)
|
||||
diff := float64(skewness) / float64(confValue)
|
||||
tplData := map[string]any{
|
||||
"drift": response.ClockOffset,
|
||||
"value": confValue,
|
||||
}
|
||||
|
||||
switch {
|
||||
case diff < 0.3:
|
||||
tplOTimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
|
||||
return true
|
||||
case diff < 0.7:
|
||||
tplWTimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
|
||||
default:
|
||||
tplETimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (d *Doctor) checkNetwork(ntw mtglib.Network) bool {
|
||||
dcs := slices.Collect(maps.Keys(essentials.TelegramCoreAddresses))
|
||||
slices.Sort(dcs)
|
||||
|
||||
ok := true
|
||||
|
||||
for _, dc := range dcs {
|
||||
err := d.checkNetworkAddresses(ntw, essentials.TelegramCoreAddresses[dc])
|
||||
if err == nil {
|
||||
tplODCConnect.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"dc": dc,
|
||||
})
|
||||
} else {
|
||||
tplEDCConnect.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"dc": dc,
|
||||
"error": err,
|
||||
})
|
||||
ok = false
|
||||
}
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (d *Doctor) checkNetworkAddresses(ntw mtglib.Network, addresses []string) error {
|
||||
checkAddresses := []string{}
|
||||
|
||||
switch d.conf.PreferIP.Get("prefer-ip4") {
|
||||
case "only-ipv4":
|
||||
for _, addr := range addresses {
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if ip := net.ParseIP(host); ip != nil && ip.To4() != nil {
|
||||
checkAddresses = append(checkAddresses, addr)
|
||||
}
|
||||
}
|
||||
case "only-ipv6":
|
||||
for _, addr := range addresses {
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if ip := net.ParseIP(host); ip != nil && ip.To4() == nil {
|
||||
checkAddresses = append(checkAddresses, addr)
|
||||
}
|
||||
}
|
||||
default:
|
||||
checkAddresses = addresses
|
||||
}
|
||||
|
||||
if len(checkAddresses) == 0 {
|
||||
return fmt.Errorf("no suitable addresses after IP version filtering")
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var (
|
||||
conn net.Conn
|
||||
err error
|
||||
)
|
||||
|
||||
for _, addr := range checkAddresses {
|
||||
conn, err = ntw.DialContext(ctx, "tcp", addr)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
conn.Close() //nolint: errcheck
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Doctor) checkFrontingDomain(ntw mtglib.Network) bool {
|
||||
host := d.conf.Secret.Host
|
||||
if ip := d.conf.GetDomainFrontingIP(nil); ip != "" {
|
||||
host = ip
|
||||
}
|
||||
|
||||
port := d.conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort)
|
||||
address := net.JoinHostPort(host, strconv.Itoa(int(port)))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
dialer := ntw.NativeDialer()
|
||||
|
||||
conn, err := dialer.DialContext(ctx, "tcp", address)
|
||||
if err != nil {
|
||||
tplEFrontingDomain.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"address": address,
|
||||
"error": err,
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
conn.Close() //nolint: errcheck
|
||||
|
||||
tplOFrontingDomain.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"address": address,
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (d *Doctor) checkSecretHost(resolver *net.Resolver, ntw mtglib.Network) bool {
|
||||
addresses, err := resolver.LookupIPAddr(context.Background(), d.conf.Secret.Host)
|
||||
if err != nil {
|
||||
tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"description": fmt.Sprintf("cannot resolve DNS name of %s", d.conf.Secret.Host),
|
||||
"error": err,
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
ourIP4 := getIP(ntw, "tcp4")
|
||||
ourIP6 := getIP(ntw, "tcp6")
|
||||
|
||||
if ourIP4 == nil && ourIP6 == nil {
|
||||
tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"description": "cannot detect public IP address",
|
||||
"error": errors.New("ifconfig.co is unreachable for both IPv4 and IPv6"),
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
strAddresses := []string{}
|
||||
for _, value := range addresses {
|
||||
if (ourIP4 != nil && value.IP.String() == ourIP4.String()) ||
|
||||
(ourIP6 != nil && value.IP.String() == ourIP6.String()) {
|
||||
tplODNSSNIMatch.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"ip": value.IP,
|
||||
"hostname": d.conf.Secret.Host,
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
strAddresses = append(strAddresses, `"`+value.IP.String()+`"`)
|
||||
}
|
||||
|
||||
tplEDNSSNIMatch.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
||||
"hostname": d.conf.Secret.Host,
|
||||
"resolved": strings.Join(strAddresses, ", "),
|
||||
"ip4": ourIP4,
|
||||
"ip6": ourIP6,
|
||||
})
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -253,6 +253,7 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen
|
||||
EventStream: eventStream,
|
||||
|
||||
Secret: conf.Secret,
|
||||
Concurrency: conf.GetConcurrency(mtglib.DefaultConcurrency),
|
||||
DomainFrontingPort: conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort),
|
||||
DomainFrontingIP: conf.GetDomainFrontingIP(nil),
|
||||
DomainFrontingProxyProtocol: conf.GetDomainFrontingProxyProtocol(false),
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/9seconds/mtg/v2/essentials"
|
||||
"github.com/9seconds/mtg/v2/mtglib"
|
||||
)
|
||||
|
||||
func getIP(ntw mtglib.Network, protocol string) net.IP {
|
||||
dialer := ntw.NativeDialer()
|
||||
client := ntw.MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error) {
|
||||
conn, err := dialer.DialContext(ctx, protocol, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return essentials.WrapNetConn(conn), err
|
||||
})
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) //nolint: noctx
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Add("Accept", "text/plain")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
io.Copy(io.Discard, resp.Body) //nolint: errcheck
|
||||
resp.Body.Close() //nolint: errcheck
|
||||
}()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return net.ParseIP(strings.TrimSpace(string(data)))
|
||||
}
|
||||
@@ -84,6 +84,13 @@ type Config struct {
|
||||
} `json:"stats"`
|
||||
}
|
||||
|
||||
func (c *Config) GetConcurrency(defaultValue uint) uint {
|
||||
if concurrency := c.Concurrency.Get(0); concurrency != 0 {
|
||||
return concurrency
|
||||
}
|
||||
return c.Concurrency.Get(defaultValue)
|
||||
}
|
||||
|
||||
func (c *Config) GetDNS() *url.URL {
|
||||
var dohURL *url.URL
|
||||
|
||||
|
||||
@@ -1,11 +1,36 @@
|
||||
# @generated - this file is auto-generated by `mise lock` https://mise.jdx.dev/dev-tools/mise-lock.html
|
||||
|
||||
[[tools.go]]
|
||||
version = "1.26.1"
|
||||
backend = "core:go"
|
||||
"platforms.linux-arm64" = { checksum = "sha256:a290581cfe4fe28ddd737dde3095f3dbeb7f2e4065cab4eae44dfc53b760c2f7", url = "https://dl.google.com/go/go1.26.1.linux-arm64.tar.gz"}
|
||||
"platforms.linux-x64" = { checksum = "sha256:031f088e5d955bab8657ede27ad4e3bc5b7c1ba281f05f245bcc304f327c987a", url = "https://dl.google.com/go/go1.26.1.linux-amd64.tar.gz"}
|
||||
"platforms.macos-arm64" = { checksum = "sha256:353df43a7811ce284c8938b5f3c7df40b7bfb6f56cb165b150bc40b5e2dd541f", url = "https://dl.google.com/go/go1.26.1.darwin-arm64.tar.gz"}
|
||||
"platforms.macos-x64" = { checksum = "sha256:65773dab2f8cc4cd23d93ba6d0a805de150ca0b78378879292be0b903b8cdd08", url = "https://dl.google.com/go/go1.26.1.darwin-amd64.tar.gz"}
|
||||
"platforms.windows-x64" = { checksum = "sha256:9b68112c913f45b7aebbf13c036721264bbba7e03a642f8f7490c561eebd1ecc", url = "https://dl.google.com/go/go1.26.1.windows-amd64.zip"}
|
||||
|
||||
[tools.go."platforms.linux-arm64"]
|
||||
checksum = "sha256:a290581cfe4fe28ddd737dde3095f3dbeb7f2e4065cab4eae44dfc53b760c2f7"
|
||||
url = "https://dl.google.com/go/go1.26.1.linux-arm64.tar.gz"
|
||||
|
||||
[tools.go."platforms.linux-arm64-musl"]
|
||||
checksum = "sha256:a290581cfe4fe28ddd737dde3095f3dbeb7f2e4065cab4eae44dfc53b760c2f7"
|
||||
url = "https://dl.google.com/go/go1.26.1.linux-arm64.tar.gz"
|
||||
|
||||
[tools.go."platforms.linux-x64"]
|
||||
checksum = "sha256:031f088e5d955bab8657ede27ad4e3bc5b7c1ba281f05f245bcc304f327c987a"
|
||||
url = "https://dl.google.com/go/go1.26.1.linux-amd64.tar.gz"
|
||||
|
||||
[tools.go."platforms.linux-x64-musl"]
|
||||
checksum = "sha256:031f088e5d955bab8657ede27ad4e3bc5b7c1ba281f05f245bcc304f327c987a"
|
||||
url = "https://dl.google.com/go/go1.26.1.linux-amd64.tar.gz"
|
||||
|
||||
[tools.go."platforms.macos-arm64"]
|
||||
checksum = "sha256:353df43a7811ce284c8938b5f3c7df40b7bfb6f56cb165b150bc40b5e2dd541f"
|
||||
url = "https://dl.google.com/go/go1.26.1.darwin-arm64.tar.gz"
|
||||
|
||||
[tools.go."platforms.macos-x64"]
|
||||
checksum = "sha256:65773dab2f8cc4cd23d93ba6d0a805de150ca0b78378879292be0b903b8cdd08"
|
||||
url = "https://dl.google.com/go/go1.26.1.darwin-amd64.tar.gz"
|
||||
|
||||
[tools.go."platforms.windows-x64"]
|
||||
checksum = "sha256:9b68112c913f45b7aebbf13c036721264bbba7e03a642f8f7490c561eebd1ecc"
|
||||
url = "https://dl.google.com/go/go1.26.1.windows-amd64.zip"
|
||||
|
||||
[[tools."go:golang.org/x/pkgsite/cmd/pkgsite"]]
|
||||
version = "latest"
|
||||
@@ -24,19 +49,73 @@ version = "0.9.2"
|
||||
backend = "go:mvdan.cc/gofumpt"
|
||||
|
||||
[[tools.golangci-lint]]
|
||||
version = "2.11.3"
|
||||
version = "2.11.4"
|
||||
backend = "aqua:golangci/golangci-lint"
|
||||
"platforms.linux-arm64" = { checksum = "sha256:ee3d95f301359e7d578e6d99c8ad5aeadbabc5a13009a30b2b0df11c8058afe9", url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.3/golangci-lint-2.11.3-linux-arm64.tar.gz"}
|
||||
"platforms.linux-x64" = { checksum = "sha256:87bb8cddbcc825d5778b64e8a91b46c0526b247f4e2f2904dea74ec7450475d1", url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.3/golangci-lint-2.11.3-linux-amd64.tar.gz"}
|
||||
"platforms.macos-arm64" = { checksum = "sha256:30ee39979c516b9d1adca289a3f93429d130c4c0fda5e57d637850894221f6cc", url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.3/golangci-lint-2.11.3-darwin-arm64.tar.gz"}
|
||||
"platforms.macos-x64" = { checksum = "sha256:f93bda1f2cc981fd1326464020494be62f387bbf262706e1b3b644e5afacc440", url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.3/golangci-lint-2.11.3-darwin-amd64.tar.gz"}
|
||||
"platforms.windows-x64" = { checksum = "sha256:cd42e890176bc5cfeb36225a77e66b9410ddd3a59a03551e23f6b210d29e1f67", url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.3/golangci-lint-2.11.3-windows-amd64.zip"}
|
||||
|
||||
[tools.golangci-lint."platforms.linux-arm64"]
|
||||
checksum = "sha256:3bcfa2e6f3d32b2bf5cd75eaa876447507025e0303698633f722a05331988db4"
|
||||
url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.4/golangci-lint-2.11.4-linux-arm64.tar.gz"
|
||||
|
||||
[tools.golangci-lint."platforms.linux-arm64-musl"]
|
||||
checksum = "sha256:3bcfa2e6f3d32b2bf5cd75eaa876447507025e0303698633f722a05331988db4"
|
||||
url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.4/golangci-lint-2.11.4-linux-arm64.tar.gz"
|
||||
|
||||
[tools.golangci-lint."platforms.linux-x64"]
|
||||
checksum = "sha256:200c5b7503f67b59a6743ccf32133026c174e272b930ee79aa2aa6f37aca7ef1"
|
||||
url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.4/golangci-lint-2.11.4-linux-amd64.tar.gz"
|
||||
|
||||
[tools.golangci-lint."platforms.linux-x64-musl"]
|
||||
checksum = "sha256:200c5b7503f67b59a6743ccf32133026c174e272b930ee79aa2aa6f37aca7ef1"
|
||||
url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.4/golangci-lint-2.11.4-linux-amd64.tar.gz"
|
||||
|
||||
[tools.golangci-lint."platforms.macos-arm64"]
|
||||
checksum = "sha256:02db2a2dae8b26812e53b0688a6f617e3ef1f489790e829ea22862cf76945675"
|
||||
url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.4/golangci-lint-2.11.4-darwin-arm64.tar.gz"
|
||||
provenance = "github-attestations"
|
||||
|
||||
[tools.golangci-lint."platforms.macos-x64"]
|
||||
checksum = "sha256:c900d4048db75d1edfd550fd11cf6a9b3008e7caa8e119fcddbc700412d63e60"
|
||||
url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.4/golangci-lint-2.11.4-darwin-amd64.tar.gz"
|
||||
|
||||
[tools.golangci-lint."platforms.windows-x64"]
|
||||
checksum = "sha256:4932cfca5e75bf60fe1c576edf459e5e809e6644664a068185d64b84af3fad9e"
|
||||
url = "https://github.com/golangci/golangci-lint/releases/download/v2.11.4/golangci-lint-2.11.4-windows-amd64.zip"
|
||||
|
||||
[[tools.goreleaser]]
|
||||
version = "2.14.3"
|
||||
backend = "aqua:goreleaser/goreleaser"
|
||||
"platforms.linux-arm64" = { checksum = "sha256:581a10e53c1176b3e81ee45cf531e02dbf899db0bc7b795669347df4276ce948", url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Linux_arm64.tar.gz"}
|
||||
"platforms.linux-x64" = { checksum = "sha256:dc7faeeeb6da8bdfda788626263a4ae725892a8c7504b975c3234127d4a44579", url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Linux_x86_64.tar.gz"}
|
||||
"platforms.macos-arm64" = { checksum = "sha256:3507798489e107a78aff36b169de48148a335ac26eb3161608d905f3f3a957bd", url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Darwin_all.tar.gz"}
|
||||
"platforms.macos-x64" = { checksum = "sha256:3507798489e107a78aff36b169de48148a335ac26eb3161608d905f3f3a957bd", url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Darwin_all.tar.gz"}
|
||||
"platforms.windows-x64" = { checksum = "sha256:3deea8ff471aa258a2d99f3e5302971d7028647ae8ddaf103257a8113e485a31", url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Windows_x86_64.zip"}
|
||||
|
||||
[tools.goreleaser."platforms.linux-arm64"]
|
||||
checksum = "sha256:581a10e53c1176b3e81ee45cf531e02dbf899db0bc7b795669347df4276ce948"
|
||||
url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Linux_arm64.tar.gz"
|
||||
provenance = "cosign"
|
||||
|
||||
[tools.goreleaser."platforms.linux-arm64-musl"]
|
||||
checksum = "sha256:581a10e53c1176b3e81ee45cf531e02dbf899db0bc7b795669347df4276ce948"
|
||||
url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Linux_arm64.tar.gz"
|
||||
provenance = "cosign"
|
||||
|
||||
[tools.goreleaser."platforms.linux-x64"]
|
||||
checksum = "sha256:dc7faeeeb6da8bdfda788626263a4ae725892a8c7504b975c3234127d4a44579"
|
||||
url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Linux_x86_64.tar.gz"
|
||||
provenance = "cosign"
|
||||
|
||||
[tools.goreleaser."platforms.linux-x64-musl"]
|
||||
checksum = "sha256:dc7faeeeb6da8bdfda788626263a4ae725892a8c7504b975c3234127d4a44579"
|
||||
url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Linux_x86_64.tar.gz"
|
||||
provenance = "cosign"
|
||||
|
||||
[tools.goreleaser."platforms.macos-arm64"]
|
||||
checksum = "sha256:3507798489e107a78aff36b169de48148a335ac26eb3161608d905f3f3a957bd"
|
||||
url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Darwin_all.tar.gz"
|
||||
provenance = "cosign"
|
||||
|
||||
[tools.goreleaser."platforms.macos-x64"]
|
||||
checksum = "sha256:3507798489e107a78aff36b169de48148a335ac26eb3161608d905f3f3a957bd"
|
||||
url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Darwin_all.tar.gz"
|
||||
provenance = "cosign"
|
||||
|
||||
[tools.goreleaser."platforms.windows-x64"]
|
||||
checksum = "sha256:3deea8ff471aa258a2d99f3e5302971d7028647ae8ddaf103257a8113e485a31"
|
||||
url = "https://github.com/goreleaser/goreleaser/releases/download/v2.14.3/goreleaser_Windows_x86_64.zip"
|
||||
provenance = "cosign"
|
||||
|
||||
+36
-43
@@ -2,7 +2,10 @@ package dc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/9seconds/mtg/v2/essentials"
|
||||
)
|
||||
|
||||
type preferIP uint8
|
||||
@@ -39,46 +42,36 @@ type Updater interface {
|
||||
}
|
||||
|
||||
// https://github.com/telegramdesktop/tdesktop/blob/master/Telegram/SourceFiles/mtproto/mtproto_dc_options.cpp#L30
|
||||
var defaultDCAddrSet = dcAddrSet{
|
||||
v4: map[int][]Addr{
|
||||
1: {
|
||||
{Network: "tcp4", Address: "149.154.175.50:443"},
|
||||
},
|
||||
2: {
|
||||
{Network: "tcp4", Address: "149.154.167.51:443"},
|
||||
{Network: "tcp4", Address: "95.161.76.100:443"},
|
||||
},
|
||||
3: {
|
||||
{Network: "tcp4", Address: "149.154.175.100:443"},
|
||||
},
|
||||
4: {
|
||||
{Network: "tcp4", Address: "149.154.167.91:443"},
|
||||
},
|
||||
5: {
|
||||
{Network: "tcp4", Address: "149.154.171.5:443"},
|
||||
},
|
||||
203: {
|
||||
{Network: "tcp4", Address: "91.105.192.100:443"},
|
||||
},
|
||||
},
|
||||
v6: map[int][]Addr{
|
||||
1: {
|
||||
{Network: "tcp6", Address: "[2001:b28:f23d:f001::a]:443"},
|
||||
},
|
||||
2: {
|
||||
{Network: "tcp6", Address: "[2001:67c:04e8:f002::a]:443"},
|
||||
},
|
||||
3: {
|
||||
{Network: "tcp6", Address: "[2001:b28:f23d:f003::a]:443"},
|
||||
},
|
||||
4: {
|
||||
{Network: "tcp6", Address: "[2001:67c:04e8:f004::a]:443"},
|
||||
},
|
||||
5: {
|
||||
{Network: "tcp6", Address: "[2001:b28:f23f:f005::a]:443"},
|
||||
},
|
||||
203: {
|
||||
{Network: "tcp6", Address: "[2a0a:f280:0203:000a:5000:0000:0000:0100]:443"},
|
||||
},
|
||||
},
|
||||
}
|
||||
var defaultDCAddrSet = (func() dcAddrSet {
|
||||
addrSet := dcAddrSet{
|
||||
v4: make(map[int][]Addr),
|
||||
v6: make(map[int][]Addr),
|
||||
}
|
||||
|
||||
for dcid, ips := range essentials.TelegramCoreAddresses {
|
||||
for _, addr := range ips {
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
panic(addr)
|
||||
}
|
||||
if ip.To4() == nil {
|
||||
addrSet.v6[dcid] = append(addrSet.v6[dcid], Addr{
|
||||
Network: "tcp6",
|
||||
Address: addr,
|
||||
})
|
||||
} else {
|
||||
addrSet.v4[dcid] = append(addrSet.v4[dcid], Addr{
|
||||
Network: "tcp4",
|
||||
Address: addr,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return addrSet
|
||||
})()
|
||||
|
||||
@@ -3,5 +3,4 @@
|
||||
package main
|
||||
|
||||
func runProfile() {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user