mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 16:04:02 +03:00
Introduce explicit config
This commit is contained in:
+17
-38
@@ -4,34 +4,27 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/9seconds/mtg/obfuscated2"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
"github.com/juju/errors"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/obfuscated2"
|
||||
"github.com/9seconds/mtg/wrappers"
|
||||
)
|
||||
|
||||
// Server is an insgtance of MTPROTO proxy.
|
||||
type Server struct {
|
||||
ip net.IP
|
||||
port int
|
||||
secret []byte
|
||||
logger *zap.SugaredLogger
|
||||
ctx context.Context
|
||||
readTimeout time.Duration
|
||||
writeTimeout time.Duration
|
||||
stats *Stats
|
||||
ipv6 bool
|
||||
conf *config.Config
|
||||
logger *zap.SugaredLogger
|
||||
stats *Stats
|
||||
}
|
||||
|
||||
// Serve does MTPROTO proxying.
|
||||
func (s *Server) Serve() error {
|
||||
addr := net.JoinHostPort(s.ip.String(), strconv.Itoa(s.port))
|
||||
lsock, err := net.Listen("tcp", addr)
|
||||
lsock, err := net.Listen("tcp", s.conf.BindAddr())
|
||||
if err != nil {
|
||||
return errors.Annotate(err, "Cannot create listen socket")
|
||||
}
|
||||
@@ -57,10 +50,9 @@ func (s *Server) accept(conn net.Conn) {
|
||||
|
||||
s.stats.newConnection()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
socketID := s.makeSocketID()
|
||||
socketID := uuid.NewV4().String()
|
||||
|
||||
s.logger.Debugw("Client connected",
|
||||
"secret", s.secret,
|
||||
"addr", conn.RemoteAddr().String(),
|
||||
"socketid", socketID,
|
||||
)
|
||||
@@ -68,7 +60,6 @@ func (s *Server) accept(conn net.Conn) {
|
||||
clientConn, dc, err := s.getClientStream(ctx, cancel, conn, socketID)
|
||||
if err != nil {
|
||||
s.logger.Warnw("Cannot initialize client connection",
|
||||
"secret", s.secret,
|
||||
"addr", conn.RemoteAddr().String(),
|
||||
"socketid", socketID,
|
||||
"error", err,
|
||||
@@ -101,25 +92,20 @@ func (s *Server) accept(conn net.Conn) {
|
||||
wait.Wait()
|
||||
|
||||
s.logger.Debugw("Client disconnected",
|
||||
"secret", s.secret,
|
||||
"addr", conn.RemoteAddr().String(),
|
||||
"socketid", socketID,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) makeSocketID() string {
|
||||
return uuid.NewV4().String()
|
||||
}
|
||||
|
||||
func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc, conn net.Conn, socketID string) (io.ReadWriteCloser, int16, error) {
|
||||
wConn := wrappers.NewTimeoutRWC(conn, s.readTimeout, s.writeTimeout)
|
||||
wConn := wrappers.NewTimeoutRWC(conn, s.conf.TimeoutRead, s.conf.TimeoutWrite)
|
||||
wConn = wrappers.NewTrafficRWC(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
||||
frame, err := obfuscated2.ExtractFrame(wConn)
|
||||
if err != nil {
|
||||
return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
||||
}
|
||||
|
||||
obfs2, dc, err := obfuscated2.ParseObfuscated2ClientFrame(s.secret, frame)
|
||||
obfs2, dc, err := obfuscated2.ParseObfuscated2ClientFrame(s.conf.Secret, frame)
|
||||
if err != nil {
|
||||
return nil, 0, errors.Annotate(err, "Cannot create client stream")
|
||||
}
|
||||
@@ -132,11 +118,11 @@ func (s *Server) getClientStream(ctx context.Context, cancel context.CancelFunc,
|
||||
}
|
||||
|
||||
func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFunc, dc int16, socketID string) (io.ReadWriteCloser, error) {
|
||||
socket, err := dialToTelegram(s.ipv6, dc, s.readTimeout)
|
||||
socket, err := dialToTelegram(dc, s.conf.TimeoutRead)
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot dial")
|
||||
}
|
||||
wConn := wrappers.NewTimeoutRWC(socket, s.readTimeout, s.writeTimeout)
|
||||
wConn := wrappers.NewTimeoutRWC(socket, s.conf.TimeoutRead, s.conf.TimeoutWrite)
|
||||
wConn = wrappers.NewTrafficRWC(wConn, s.stats.addIncomingTraffic, s.stats.addOutgoingTraffic)
|
||||
|
||||
obfs2, frame := obfuscated2.MakeTelegramObfuscated2Frame()
|
||||
@@ -152,17 +138,10 @@ func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFun
|
||||
}
|
||||
|
||||
// NewServer creates new instance of MTPROTO proxy.
|
||||
func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
|
||||
readTimeout, writeTimeout time.Duration, ipv6 bool, stat *Stats) *Server {
|
||||
func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Server {
|
||||
return &Server{
|
||||
ip: ip,
|
||||
port: port,
|
||||
secret: secret,
|
||||
ctx: context.Background(),
|
||||
logger: logger,
|
||||
readTimeout: readTimeout,
|
||||
writeTimeout: writeTimeout,
|
||||
stats: stat,
|
||||
ipv6: ipv6,
|
||||
conf: conf,
|
||||
logger: logger,
|
||||
stats: stat,
|
||||
}
|
||||
}
|
||||
|
||||
+14
-67
@@ -2,13 +2,12 @@ package proxy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
)
|
||||
|
||||
type statsUptime time.Time
|
||||
@@ -26,13 +25,10 @@ type Stats struct {
|
||||
Incoming uint64 `json:"incoming"`
|
||||
Outgoing uint64 `json:"outgoing"`
|
||||
} `json:"traffic"`
|
||||
URLs struct {
|
||||
TG string `json:"tg_url"`
|
||||
TMe string `json:"tme_url"`
|
||||
TGQRCode string `json:"tg_qrcode"`
|
||||
TMeQRCode string `json:"tme_qrcode"`
|
||||
} `json:"urls"`
|
||||
Uptime statsUptime `json:"uptime"`
|
||||
URLs config.IPURLs `json:"urls"`
|
||||
Uptime statsUptime `json:"uptime"`
|
||||
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func (s *Stats) newConnection() {
|
||||
@@ -53,7 +49,7 @@ func (s *Stats) addOutgoingTraffic(n int) {
|
||||
}
|
||||
|
||||
// Serve runs statistics HTTP server.
|
||||
func (s *Stats) Serve(host fmt.Stringer, port uint16) {
|
||||
func (s *Stats) Serve() {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
@@ -63,65 +59,16 @@ func (s *Stats) Serve(host fmt.Stringer, port uint16) {
|
||||
encoder.Encode(s) // nolint: errcheck, gas
|
||||
})
|
||||
|
||||
addr := net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
|
||||
http.ListenAndServe(addr, nil) // nolint: errcheck, gas
|
||||
http.ListenAndServe(s.conf.StatAddr(), nil) // nolint: errcheck, gas
|
||||
}
|
||||
|
||||
// NewStats returns new instance of statistics datastructure.
|
||||
func NewStats(serverName string, port uint16, secret string) *Stats {
|
||||
urlQuery := makeURLQuery(serverName, port, secret)
|
||||
|
||||
stat := &Stats{Uptime: statsUptime(time.Now())}
|
||||
stat.URLs.TG = makeTGURL(urlQuery)
|
||||
stat.URLs.TMe = makeTMeURL(urlQuery)
|
||||
stat.URLs.TGQRCode = makeQRCodeURL(stat.URLs.TG)
|
||||
stat.URLs.TMeQRCode = makeQRCodeURL(stat.URLs.TMe)
|
||||
func NewStats(conf *config.Config) *Stats {
|
||||
stat := &Stats{
|
||||
Uptime: statsUptime(time.Now()),
|
||||
conf: conf,
|
||||
}
|
||||
stat.URLs = conf.GetURLs()
|
||||
|
||||
return stat
|
||||
}
|
||||
|
||||
func makeURLQuery(serverName string, port uint16, secret string) url.Values {
|
||||
values := url.Values{}
|
||||
values.Set("server", serverName)
|
||||
values.Set("port", strconv.Itoa(int(port)))
|
||||
values.Set("secret", secret)
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
func makeTGURL(values url.Values) string {
|
||||
tgURL := url.URL{
|
||||
Scheme: "tg",
|
||||
Host: "proxy",
|
||||
RawQuery: values.Encode(),
|
||||
}
|
||||
|
||||
return tgURL.String()
|
||||
}
|
||||
|
||||
func makeTMeURL(values url.Values) string {
|
||||
tMeURL := url.URL{
|
||||
Scheme: "https",
|
||||
Host: "t.me",
|
||||
Path: "proxy",
|
||||
RawQuery: values.Encode(),
|
||||
}
|
||||
|
||||
return tMeURL.String()
|
||||
}
|
||||
|
||||
func makeQRCodeURL(data string) string {
|
||||
QRURL := url.URL{
|
||||
Scheme: "https",
|
||||
Host: "api.qrserver.com",
|
||||
Path: "v1/create-qr-code",
|
||||
}
|
||||
|
||||
values := url.Values{}
|
||||
values.Set("qzone", "4")
|
||||
values.Set("format", "svg")
|
||||
values.Set("data", data)
|
||||
QRURL.RawQuery = values.Encode()
|
||||
|
||||
return QRURL.String()
|
||||
}
|
||||
|
||||
+5
-7
@@ -37,12 +37,12 @@ const telegramPort = "443"
|
||||
|
||||
const telegramKeepAlive = 30 * time.Second
|
||||
|
||||
func dialToTelegram(ipv6 bool, dcIdx int16, timeout time.Duration) (net.Conn, error) {
|
||||
func dialToTelegram(dcIdx int16, timeout time.Duration) (net.Conn, error) {
|
||||
if dcIdx < 0 || dcIdx >= 5 {
|
||||
return nil, errors.New("Incorrect DC IDX")
|
||||
}
|
||||
|
||||
conn, err := doDial(ipv6, dcIdx, timeout)
|
||||
conn, err := doDial(dcIdx, timeout)
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot dial")
|
||||
}
|
||||
@@ -57,14 +57,12 @@ func dialToTelegram(ipv6 bool, dcIdx int16, timeout time.Duration) (net.Conn, er
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func doDial(ipv6 bool, dcIdx int16, timeout time.Duration) (*net.TCPConn, error) {
|
||||
func doDial(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
|
||||
}
|
||||
if conn, err := dialer.Dial("tcp", addr.IPv6()); err == nil {
|
||||
return conn.(*net.TCPConn), nil
|
||||
}
|
||||
|
||||
conn, err := dialer.Dial("tcp", addr.IPv4())
|
||||
|
||||
Reference in New Issue
Block a user