Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
geovex committed Aug 14, 2024
1 parent 3e27247 commit ba16df1
Show file tree
Hide file tree
Showing 24 changed files with 149 additions and 141 deletions.
2 changes: 1 addition & 1 deletion cmd/tgp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"

"github.com/geovex/tgp/internal/config"
o "github.com/geovex/tgp/internal/obfuscated"
o "github.com/geovex/tgp/internal/network_exchange"
"github.com/geovex/tgp/internal/stats"
)

Expand Down
17 changes: 7 additions & 10 deletions internal/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/BurntSushi/toml"
"github.com/geovex/tgp/internal/tgcrypt"
"github.com/geovex/tgp/internal/tgcrypt_encryption"
)

func ReadConfig(path string) (*Config, error) {
Expand Down Expand Up @@ -92,8 +92,8 @@ func configFromParsedUnchecked(parsed *parsedConfig, md *toml.MetaData) (*Config
for name, data := range *parsed.Users {
var u User
// user defined by it's secret
utype := md.Type("users", name)
if utype == "String" {
userRecordType := md.Type("users", name)
if userRecordType == "String" {
var secret string
err := md.PrimitiveDecode(data, &secret)
if err != nil {
Expand All @@ -108,15 +108,12 @@ func configFromParsedUnchecked(parsed *parsedConfig, md *toml.MetaData) (*Config
Socks5_user: parsed.Socks5_user,
Socks5_pass: parsed.Socks5_pass,
}
} else if utype == "Hash" { // user fully defined
} else if userRecordType == "Hash" { // user fully defined
var pu parsedUserPrimitive
err := md.PrimitiveDecode(data, &pu)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
u = User{
Name: name,
Secret: pu.Secret,
Expand All @@ -127,7 +124,7 @@ func configFromParsedUnchecked(parsed *parsedConfig, md *toml.MetaData) (*Config
Socks5_pass: pu.Socks5_pass,
}
} else {
return nil, fmt.Errorf("unknown type for user %s: %s ", name, utype)
return nil, fmt.Errorf("unknown type for user %s: %s ", name, userRecordType)
}
users.Users[name] = &u
}
Expand Down Expand Up @@ -161,8 +158,8 @@ func checkUser(user *User) error {
if err != nil {
return fmt.Errorf("can't parse adtag: %w", err)
}
if len(adTag) != tgcrypt.AddTagLength {
return fmt.Errorf("adtag must be %d bytes", tgcrypt.AddTagLength)
if len(adTag) != tgcrypt_encryption.AddTagLength {
return fmt.Errorf("adtag must be %d bytes", tgcrypt_encryption.AddTagLength)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package obfuscated
package network_exchange

import (
"bytes"
Expand All @@ -9,7 +9,7 @@ import (

"github.com/geovex/tgp/internal/config"
"github.com/geovex/tgp/internal/stats"
"github.com/geovex/tgp/internal/tgcrypt"
"github.com/geovex/tgp/internal/tgcrypt_encryption"
)

type ClientHandler struct {
Expand All @@ -18,7 +18,7 @@ type ClientHandler struct {
config *config.Config
// available after handshake
user *config.User
cliCtx *tgcrypt.ObfCtx
cliCtx *tgcrypt_encryption.ObfCtx
cliStream dataStream
}

Expand All @@ -33,7 +33,7 @@ func NewClient(cfg *config.Config, statsHandle *stats.StatsHandle, client net.Co
func (o *ClientHandler) HandleClient() (err error) {
defer o.client.Close()
defer o.statsHandle.Close()
var initialPacket tgcrypt.Nonce
var initialPacket tgcrypt_encryption.Nonce
n, err := io.ReadFull(o.client, initialPacket[:])
if err != nil {
if o.config.GetHost() != nil {
Expand All @@ -43,7 +43,7 @@ func (o *ClientHandler) HandleClient() (err error) {
}
}
//check for tls in handshake
if bytes.Equal(initialPacket[0:len(tgcrypt.FakeTlsHeader)], tgcrypt.FakeTlsHeader[:]) {
if bytes.Equal(initialPacket[0:len(tgcrypt_encryption.FakeTlsHeader)], tgcrypt_encryption.FakeTlsHeader[:]) {
return o.handleFakeTls(initialPacket)
} else {
return o.handleObfClient(initialPacket)
Expand Down Expand Up @@ -93,7 +93,7 @@ func (o *ClientHandler) processWithConfig() (err error) {
}
var dcStream dataStream
if o.user.Obfuscate != nil && *o.user.Obfuscate {
dcCtx := tgcrypt.DcCtxNew(o.cliCtx.Dc, o.cliCtx.Protocol)
dcCtx := tgcrypt_encryption.DcCtxNew(o.cliCtx.Dc, o.cliCtx.Protocol)
dcStream = ObfuscateDC(sock, dcCtx)
o.statsHandle.SetState(stats.Obfuscated)
} else {
Expand All @@ -111,14 +111,14 @@ func (o *ClientHandler) processWithConfig() (err error) {
if err != nil {
return err
}
mp, err := mpm.connect(o.cliCtx.Dc, o.client, o.cliCtx.Protocol, adTag)
middleProxyStream, err := mpm.connect(o.cliCtx.Dc, o.client, o.cliCtx.Protocol, adTag)
if err != nil {
return err
}
defer mp.CloseStream()
defer middleProxyStream.CloseStream()
clientMsgStream := newMsgStream(o.cliStream)
o.statsHandle.SetState(stats.Middleproxy)
transceiveMsg(clientMsgStream, mp)
transceiveMsg(clientMsgStream, middleProxyStream)
}
return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package obfuscated
package network_exchange

import (
"bytes"
Expand All @@ -14,24 +14,24 @@ import (
"sync"
"time"

"github.com/geovex/tgp/internal/tgcrypt"
"github.com/geovex/tgp/internal/tgcrypt_encryption"
)

func (o *ClientHandler) handleFakeTls(initialPacket [tgcrypt.NonceSize]byte) (err error) {
var tlsHandshake [tgcrypt.FakeTlsHandshakeLen]byte
copy(tlsHandshake[:tgcrypt.FakeTlsHandshakeLen], initialPacket[:])
_, err = io.ReadFull(o.client, tlsHandshake[tgcrypt.NonceSize:])
var clientCtx *tgcrypt.FakeTlsCtx
func (o *ClientHandler) handleFakeTls(initialPacket [tgcrypt_encryption.NonceSize]byte) (err error) {
var tlsHandshake [tgcrypt_encryption.FakeTlsHandshakeLen]byte
copy(tlsHandshake[:tgcrypt_encryption.FakeTlsHandshakeLen], initialPacket[:])
_, err = io.ReadFull(o.client, tlsHandshake[tgcrypt_encryption.NonceSize:])
var clientCtx *tgcrypt_encryption.FakeTlsCtx
if err != nil {
return
}
for u := range o.config.IterateUsers() {
runtime.Gosched()
userSecret, err := tgcrypt.NewSecretHex(u.Secret)
userSecret, err := tgcrypt_encryption.NewSecretHex(u.Secret)
if err != nil {
continue
}
clientCtx, err = tgcrypt.FakeTlsCtxFromTlsHeader(tlsHandshake, userSecret)
clientCtx, err = tgcrypt_encryption.FakeTlsCtxFromTlsHeader(tlsHandshake, userSecret)
if err != nil {
continue
} else {
Expand All @@ -49,7 +49,7 @@ func (o *ClientHandler) handleFakeTls(initialPacket [tgcrypt.NonceSize]byte) (er
return err
}

func (o *ClientHandler) transceiveFakeTls(cryptClient *tgcrypt.FakeTlsCtx) error {
func (o *ClientHandler) transceiveFakeTls(cryptClient *tgcrypt_encryption.FakeTlsCtx) error {
if !o.config.GetIgnoreTimestamp() {
// checking timestamp
skew := time.Now().UTC().Unix() - int64(cryptClient.Timestamp)
Expand Down Expand Up @@ -111,13 +111,13 @@ func (o *ClientHandler) transceiveFakeTls(cryptClient *tgcrypt.FakeTlsCtx) error
if err != nil {
return err
}
fts := newFakeTlsStream(o.client, cryptClient)
var simpleHeader [tgcrypt.NonceSize]byte
fts := newFakeTlsStream(o.client)
var simpleHeader [tgcrypt_encryption.NonceSize]byte
_, err = io.ReadFull(fts, simpleHeader[:])
if err != nil {
return fmt.Errorf("can't read inner simple header: %w", err)
}
o.cliCtx, err = tgcrypt.ObfCtxFromNonce(simpleHeader, cryptClient.Secret)
o.cliCtx, err = tgcrypt_encryption.ObfCtxFromNonce(simpleHeader, cryptClient.Secret)
if err != nil {
return fmt.Errorf("can't create simple ctx from inner simple header: %w", err)
}
Expand All @@ -133,7 +133,7 @@ type fakeTlsStream struct {
readerTail []byte
}

func newFakeTlsStream(client io.ReadWriteCloser, crypt *tgcrypt.FakeTlsCtx) *fakeTlsStream {
func newFakeTlsStream(client io.ReadWriteCloser) *fakeTlsStream {
return &fakeTlsStream{
readlock: sync.Mutex{},
writelock: sync.Mutex{},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package obfuscated
package network_exchange

import (
"fmt"
"runtime"

"github.com/geovex/tgp/internal/config"
"github.com/geovex/tgp/internal/tgcrypt"
"github.com/geovex/tgp/internal/tgcrypt_encryption"
)

func (o *ClientHandler) handleObfClient(initialPacket [tgcrypt.NonceSize]byte) (err error) {
func (o *ClientHandler) handleObfClient(initialPacket [tgcrypt_encryption.NonceSize]byte) (err error) {
var user *string
for u := range o.config.IterateUsers() {
runtime.Gosched()
if tgcrypt.IsWrongNonce(initialPacket) {
if tgcrypt_encryption.IsWrongNonce(initialPacket) {
continue
}
userSecret, err := tgcrypt.NewSecretHex(u.Secret)
userSecret, err := tgcrypt_encryption.NewSecretHex(u.Secret)
if err != nil {
continue
}
o.cliCtx, err = tgcrypt.ObfCtxFromNonce(initialPacket, userSecret)
o.cliCtx, err = tgcrypt_encryption.ObfCtxFromNonce(initialPacket, userSecret)
if err != nil {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions internal/obfuscated/dc.go → internal/network_exchange/dc.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package obfuscated
package network_exchange

import (
"fmt"
"io"
"net"

"github.com/geovex/tgp/internal/maplist"
"github.com/geovex/tgp/internal/tgcrypt"
"github.com/geovex/tgp/internal/tgcrypt_encryption"
"golang.org/x/net/proxy"
)

Expand Down Expand Up @@ -205,7 +205,7 @@ func LoginDC(sock io.ReadWriteCloser, protocol uint8) *rawStream {
return newRawStream(sock, protocol)
}

func ObfuscateDC(sock io.ReadWriteCloser, ctx *tgcrypt.DcCtx) *obfuscatedStream {
func ObfuscateDC(sock io.ReadWriteCloser, ctx *tgcrypt_encryption.DcCtx) *obfuscatedStream {
// TODO: handle negative dc
return newObfuscatedStream(sock, ctx, &ctx.Nonce, ctx.Protocol)
}
Loading

0 comments on commit ba16df1

Please sign in to comment.