Skip to content

Commit

Permalink
fragmentation rework
Browse files Browse the repository at this point in the history
  • Loading branch information
uoosef committed Sep 8, 2023
1 parent 8bbcc4e commit dc8363d
Show file tree
Hide file tree
Showing 18 changed files with 815 additions and 558 deletions.
26 changes: 13 additions & 13 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
package main

import (
"bepass/cmd/core"
"bepass/config"
"bepass/logger"
"bepass/server"
"encoding/json"
"errors"
"fmt"
Expand All @@ -25,47 +26,46 @@ func main() {
err := ff.Parse(fs, os.Args[1:])
switch {
case errors.Is(err, ff.ErrHelp):
fmt.Fprintf(os.Stderr, "%s\n", ffhelp.Flags(fs))
logger.Errorf("%s\n", ffhelp.Flags(fs))
os.Exit(0)
case err != nil:
fmt.Fprintf(os.Stderr, "error: %v\n", err)
logger.Errorf("error: %v\n", err)
os.Exit(1)
}

// Load and validate configuration from JSON file
config, err := loadConfig(configPath)
err = loadConfig(configPath)
if err != nil {
logger.Fatal("", err)
}

// Run the server with the loaded configuration
err = core.RunServer(config, true)
err = server.Run(true)
if err != nil {
logger.Fatal("", err)
}

// Handle graceful shutdown
// HandleTCPTunnel graceful shutdown
handleShutdown()
}

func loadConfig(configPath string) (*core.Config, error) {
func loadConfig(configPath string) error {
file, err := os.Open(configPath)
if err != nil {
return nil, err
return err
}
defer file.Close()

config := &core.Config{}
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
err = decoder.Decode(config.G)
if err != nil {
if strings.Contains(err.Error(), "invalid character") {
return nil, fmt.Errorf("configuration file is not valid JSON")
return fmt.Errorf("configuration file is not valid JSON")
}
return nil, err
return err
}

return config, nil
return nil
}

func handleShutdown() {
Expand Down
163 changes: 0 additions & 163 deletions cmd/core/core.go

This file was deleted.

12 changes: 6 additions & 6 deletions cmd/gui/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main

import (
"bepass/cmd/core"
"bepass/config"
"bepass/server"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -48,7 +49,7 @@ type UIComponents struct {
openFileButton *widget.Button
connectButton *widget.Button
isConnected bool
coreConfig *core.Config
coreConfig *config.Config
}

func createUIComponents(myWindow *fyne.Window) *UIComponents {
Expand Down Expand Up @@ -132,7 +133,7 @@ func (ui *UIComponents) Connect(myWindow *fyne.Window) {
firstValue := ui.dohInput.Text
secondValue := ui.listenInput.Text

ui.coreConfig = &core.Config{
ui.coreConfig = &config.Config{
TLSHeaderLength: 5,
DnsCacheTTL: 3600,
WorkerAddress: "worker.example.com",
Expand All @@ -146,7 +147,6 @@ func (ui *UIComponents) Connect(myWindow *fyne.Window) {
ChunksLengthAfterSni: [2]int{50, 60},
DelayBetweenChunks: [2]int{70, 80},
ResolveSystem: "doh",
DoHClient: nil, // Initialize appropriately
}
}

Expand All @@ -156,7 +156,7 @@ func (ui *UIComponents) Connect(myWindow *fyne.Window) {
}

go func() {
err := core.RunServer(ui.coreConfig, true)
err := server.Run(true)
if err != nil {
dialog.ShowError(err, *myWindow)
ui.isConnected = false
Expand All @@ -175,7 +175,7 @@ func (ui *UIComponents) Connect(myWindow *fyne.Window) {
func (ui *UIComponents) Disconnect(myWindow *fyne.Window) {
go func() {
if ui.coreConfig != nil {
err := core.ShutDown()
err := server.ShutDown()

if err != nil {
dialog.ShowError(err, *myWindow)
Expand Down
7 changes: 4 additions & 3 deletions cmd/mobile/tun2socks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tun2socks

import (
"bepass/config"
"encoding/json"
"errors"
"io"
Expand All @@ -18,18 +19,18 @@ import (
"github.com/eycorsican/go-tun2socks/core"
"github.com/eycorsican/go-tun2socks/proxy/socks"

bepassCore "bepass/cmd/core"
bepassCore "bepass/server"

"github.com/songgao/water"
)

func StartClient(cfg string) bool {
config := &bepassCore.Config{}
config := &config.Config{}
err := json.Unmarshal([]byte(cfg), config)
if err != nil {
return false
}
err = bepassCore.RunServer(config, false)
err = bepassCore.Run(false)
if err != nil {
return false
}
Expand Down
37 changes: 37 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"bepass/resolve"
)

type Config struct {
TLSHeaderLength int `mapstructure:"TLSHeaderLength"`
TLSPaddingEnabled bool `mapstructure:"TLSPaddingEnabled"`
TLSPaddingSize [2]int `mapstructure:"TLSPaddingSize"`
DnsCacheTTL int `mapstructure:"DnsCacheTTL"`
DnsRequestTimeout int `mapstructure:"DnsRequestTimeout"`
WorkerAddress string `mapstructure:"WorkerAddress"`
WorkerIPPortAddress string `mapstructure:"WorkerIPPortAddress"`
WorkerEnabled bool `mapstructure:"WorkerEnabled"`
WorkerDNSOnly bool `mapstructure:"WorkerDNSOnly"`
EnableLowLevelSockets bool `mapstructure:"EnableLowLevelSockets"`
EnableDNSFragmentation bool `mapstructure:"EnableDNSFragmentation"`
RemoteDNSAddr string `mapstructure:"RemoteDNSAddr"`
BindAddress string `mapstructure:"BindAddress"`
UDPBindAddress string `mapstructure:"UDPBindAddress"`
ChunksLengthBeforeSni [2]int `mapstructure:"ChunksLengthBeforeSni"`
SniChunksLength [2]int `mapstructure:"SniChunksLength"`
ChunksLengthAfterSni [2]int `mapstructure:"ChunksLengthAfterSni"`
UDPReadTimeout int `mapstructure:"UDPReadTimeout"`
UDPWriteTimeout int `mapstructure:"UDPWriteTimeout"`
UDPLinkIdleTimeout int64 `mapstructure:"UDPLinkIdleTimeout"`
DelayBetweenChunks [2]int `mapstructure:"DelayBetweenChunks"`
Hosts []resolve.Hosts `mapstructure:"Hosts"`
ResolveSystem string `mapstructure:"-"`
}

var G *Config

func init() {
G = &Config{}
}
20 changes: 19 additions & 1 deletion dialer/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
package dialer

import (
"bepass/net/adapter/fragment"
"bepass/net/adapter/http"
"net"
)

// PlainTCPDial is a type representing a function for plain TCP dialing.
type PlainTCPDial func(network, addr, hostPort string) (net.Conn, error)
type PlainTCPDial func(network, addr string) (net.Conn, error)

// Dialer is a struct that holds various options for custom dialing.
type Dialer struct {
Expand All @@ -15,3 +17,19 @@ type Dialer struct {
TLSPaddingSize [2]int // Size of TLS padding.
ProxyAddress string // Address of the proxy server.
}

func (d *Dialer) FragmentDial(network, addr string) (net.Conn, error) {
tcpConn, err := d.TCPDial(network, addr)
if err != nil {
return nil, err
}
return fragment.New(tcpConn), nil
}

func (d *Dialer) HttpDial(network, addr string) (net.Conn, error) {
tcpConn, err := d.TCPDial(network, addr)
if err != nil {
return nil, err
}
return http.New(tcpConn), nil
}
Loading

0 comments on commit dc8363d

Please sign in to comment.