mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:34:02 +03:00
Prefer ipv6 setting
This commit is contained in:
@@ -67,6 +67,10 @@ var (
|
|||||||
Short('s').
|
Short('s').
|
||||||
Envar("MTG_SERVER").
|
Envar("MTG_SERVER").
|
||||||
String()
|
String()
|
||||||
|
preferIPv6 = app.Flag("prefer-ipv6", "Use IPv6").
|
||||||
|
Short('6').
|
||||||
|
Envar("MTG_USE_IPV6").
|
||||||
|
Bool()
|
||||||
|
|
||||||
secret = app.Arg("secret", "Secret of this proxy.").String()
|
secret = app.Arg("secret", "Secret of this proxy.").String()
|
||||||
)
|
)
|
||||||
@@ -117,7 +121,7 @@ func main() {
|
|||||||
go stat.Serve(*statsIP, *statsPort)
|
go stat.Serve(*statsIP, *statsPort)
|
||||||
|
|
||||||
srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
|
srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
|
||||||
*readTimeout, *writeTimeout, stat)
|
*readTimeout, *writeTimeout, *preferIPv6, stat)
|
||||||
if err := srv.Serve(); err != nil {
|
if err := srv.Serve(); err != nil {
|
||||||
logger.Fatal(err.Error())
|
logger.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-2
@@ -26,6 +26,7 @@ type Server struct {
|
|||||||
readTimeout time.Duration
|
readTimeout time.Duration
|
||||||
writeTimeout time.Duration
|
writeTimeout time.Duration
|
||||||
stats *Stats
|
stats *Stats
|
||||||
|
ipv6 bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Serve() error {
|
func (s *Server) Serve() error {
|
||||||
@@ -124,7 +125,7 @@ func (s *Server) getClientStream(conn net.Conn, ctx context.Context, cancel cont
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context.CancelFunc, socketID string) (io.ReadWriteCloser, error) {
|
func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context.CancelFunc, socketID string) (io.ReadWriteCloser, error) {
|
||||||
socket, err := dialToTelegram(dc, s.readTimeout)
|
socket, err := dialToTelegram(s.ipv6, dc, s.readTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot dial")
|
return nil, errors.Annotate(err, "Cannot dial")
|
||||||
}
|
}
|
||||||
@@ -149,7 +150,7 @@ func (s *Server) pipe(wait *sync.WaitGroup, reader io.Reader, writer io.Writer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
|
func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
|
||||||
readTimeout, writeTimeout time.Duration, stat *Stats) *Server {
|
readTimeout, writeTimeout time.Duration, ipv6 bool, stat *Stats) *Server {
|
||||||
return &Server{
|
return &Server{
|
||||||
ip: ip,
|
ip: ip,
|
||||||
port: port,
|
port: port,
|
||||||
@@ -159,5 +160,6 @@ func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
|
|||||||
readTimeout: readTimeout,
|
readTimeout: readTimeout,
|
||||||
writeTimeout: writeTimeout,
|
writeTimeout: writeTimeout,
|
||||||
stats: stat,
|
stats: stat,
|
||||||
|
ipv6: ipv6,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-10
@@ -7,24 +7,37 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var telegramDCIPs = [5]string{
|
type TelegramAddress struct {
|
||||||
"149.154.175.50:443",
|
v4 string
|
||||||
"149.154.167.51:443",
|
v6 string
|
||||||
"149.154.175.100:443",
|
|
||||||
"149.154.167.91:443",
|
|
||||||
"149.154.171.5:443",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TelegramAddress) IPv4() string {
|
||||||
|
return net.JoinHostPort(t.v4, telegramPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelegramAddress) IPv6() string {
|
||||||
|
return net.JoinHostPort(t.v6, telegramPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
var TelegramAddresses = []TelegramAddress{
|
||||||
|
TelegramAddress{v4: "149.154.175.50", v6: "2001:b28:f23d:f001::a"},
|
||||||
|
TelegramAddress{v4: "149.154.167.51", v6: "2001:67c:04e8:f002::a"},
|
||||||
|
TelegramAddress{v4: "149.154.175.100", v6: "2001:b28:f23d:f003::a"},
|
||||||
|
TelegramAddress{v4: "149.154.167.91", v6: "2001:67c:04e8:f004::a"},
|
||||||
|
TelegramAddress{v4: "149.154.171.5", v6: "2001:b28:f23f:f005::a"},
|
||||||
|
}
|
||||||
|
|
||||||
|
const telegramPort = "443"
|
||||||
|
|
||||||
const telegramKeepAlive = 30 * time.Second
|
const telegramKeepAlive = 30 * time.Second
|
||||||
|
|
||||||
func dialToTelegram(dcIdx int16, timeout time.Duration) (net.Conn, error) {
|
func dialToTelegram(ipv6 bool, dcIdx int16, timeout time.Duration) (net.Conn, error) {
|
||||||
if dcIdx < 0 || dcIdx >= 5 {
|
if dcIdx < 0 || dcIdx >= 5 {
|
||||||
return nil, errors.New("Incorrect DC IDX")
|
return nil, errors.New("Incorrect DC IDX")
|
||||||
}
|
}
|
||||||
|
|
||||||
dialer := net.Dialer{Timeout: timeout}
|
conn, err := doDial(ipv6, dcIdx, timeout)
|
||||||
rawConn, err := dialer.Dial("tcp", telegramDCIPs[dcIdx])
|
|
||||||
conn := rawConn.(*net.TCPConn)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot dial")
|
return nil, errors.Annotate(err, "Cannot dial")
|
||||||
}
|
}
|
||||||
@@ -38,3 +51,20 @@ func dialToTelegram(dcIdx int16, timeout time.Duration) (net.Conn, error) {
|
|||||||
|
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doDial(ipv6 bool, dcIdx int16, timeout time.Duration) (*net.TCPConn, error) {
|
||||||
|
dialer := net.Dialer{Timeout: timeout}
|
||||||
|
addr := TelegramAddresses[dcIdx]
|
||||||
|
|
||||||
|
if ipv6 {
|
||||||
|
if conn, err := dialer.Dial("tcp", addr.IPv6()); err == nil {
|
||||||
|
return conn.(*net.TCPConn), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := dialer.Dial("tcp", addr.IPv4())
|
||||||
|
if err == nil {
|
||||||
|
return conn.(*net.TCPConn), nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user