Skip to content

Commit

Permalink
support mtproxy fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Dec 20, 2023
1 parent 5ec5b01 commit f54c875
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type FallbackConfig struct {
type TLSFallbackConfig struct {
SNI string `yaml:"sni"`
Address string `yaml:"address"`
Mtp string `yaml:"mtp"`
}

type QuicFallbackConfig struct {
Expand Down
16 changes: 14 additions & 2 deletions fallback/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/wwqgtxx/wstunnel/common"
"github.com/wwqgtxx/wstunnel/config"
"github.com/wwqgtxx/wstunnel/fallback/mtproxy"
"github.com/wwqgtxx/wstunnel/fallback/ss2022"
"github.com/wwqgtxx/wstunnel/fallback/ssaead"
"github.com/wwqgtxx/wstunnel/fallback/tls"
Expand Down Expand Up @@ -179,9 +180,20 @@ func NewFallback(fallbackConfig Config) (*Fallback, error) {
if len(fallbackConfig.TLSFallback) > 0 {
tlsTester = tls.NewTester[common.ClientImpl]()
for _, tlsFallbackConfig := range fallbackConfig.TLSFallback {
var sni string
var clientImpl common.ClientImpl
if len(tlsFallbackConfig.Mtp) > 0 {
clientImpl, sni, err = mtproxy.NewClient(tlsFallbackConfig.Mtp, fallbackConfig.ProxyConfig.Proxy)
if err != nil {
return nil, err
}
} else {
sni = tlsFallbackConfig.SNI
clientImpl = NewClientImpl(config.ClientConfig{TargetAddress: tlsFallbackConfig.Address, ProxyConfig: fallbackConfig.ProxyConfig})
}
err = tlsTester.Add(
tlsFallbackConfig.SNI,
NewClientImpl(config.ClientConfig{TargetAddress: tlsFallbackConfig.Address, ProxyConfig: fallbackConfig.ProxyConfig}),
sni,
clientImpl,
)
if err != nil {
return nil, err
Expand Down
99 changes: 99 additions & 0 deletions fallback/mtproxy/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package mtproxy

import (
"context"
"errors"
"log"
"net"
"net/http"
"time"

"github.com/wwqgtxx/wstunnel/common"
"github.com/wwqgtxx/wstunnel/fallback/mtproxy/tools"
"github.com/wwqgtxx/wstunnel/proxy"
"github.com/wwqgtxx/wstunnel/tunnel"
"github.com/wwqgtxx/wstunnel/utils"
)

const DialTimeout = 8 * time.Second

type client struct {
serverInfo *tools.ServerInfo
dialer proxy.ContextDialer
proxyStr string
}

var _ common.ClientImpl = (*client)(nil)

func (c *client) Target() string {
return "TELEGRAM"
}

func (c *client) Proxy() string {
return c.proxyStr
}

func (c *client) Handle(tcp net.Conn) {
panic("don't implement")
}

func (c *client) Dial(edBuf []byte, inHeader http.Header) (common.ClientConn, error) {
return &conn{client: c}, nil
}

type conn struct {
*client
}

func (c *conn) Close() {}

func (c *conn) TunnelTcp(tcp net.Conn) {
serverProtocol := c.serverInfo.ServerProtocolMaker(
c.serverInfo.Secret,
c.serverInfo.SecretMode,
c.serverInfo.CloakHost,
c.serverInfo.CloakPort,
)
serverConn, err := serverProtocol.Handshake(tcp)
if err != nil {
//log.Warnln("Cannot perform client handshake: %s", err)

return
}
defer serverConn.Close()

ctx, cancel := context.WithTimeout(context.Background(), DialTimeout)
defer cancel()

telegramConn, err := c.serverInfo.TelegramDialer.Dial(
serverProtocol,
func(addr string) (net.Conn, error) {
return c.dialer.DialContext(ctx, "tcp", addr)
})
if err != nil {
return
}
defer telegramConn.Close()

log.Println("Tunnel MTP From", tcp.RemoteAddr(), " --> ", telegramConn.RemoteAddr())

tunnel.Tunnel(serverConn, telegramConn)
}

func (c *conn) TunnelWs(wsConn *utils.WebsocketConn) {
panic("don't implement")
}

var _ common.ClientConn = (*conn)(nil)

func NewClient(hexed string, proxyString string) (common.ClientImpl, string, error) {
serverInfo, err := tools.ParseHexedSecret(hexed)
if err != nil {
return nil, "", err
}
if serverInfo.SecretMode != tools.SecretModeTLS {
return nil, "", errors.New("not faketls")
}
dialer, proxyStr := proxy.FromProxyString(proxyString)
return &client{serverInfo: serverInfo, dialer: dialer, proxyStr: proxyStr}, serverInfo.CloakHost, nil
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import (

"github.com/wwqgtxx/wstunnel/client"
"github.com/wwqgtxx/wstunnel/config"
"github.com/wwqgtxx/wstunnel/fallback/mtproxy/tools"
"github.com/wwqgtxx/wstunnel/server"
"github.com/wwqgtxx/wstunnel/udp"
)

func main() {
if len(os.Args) > 2 && os.Args[1] == "generate-secret" {
tools.Generate(os.Args[2])
return
}
log.SetFlags(log.Lshortfile | log.LstdFlags)
configFile := "config.yaml"
if len(os.Args) == 2 {
Expand Down

0 comments on commit f54c875

Please sign in to comment.