Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support specify buffer size in config #365

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package proxy
import "github.com/p4gefau1t/trojan-go/config"

type Config struct {
RunType string `json:"run_type" yaml:"run-type"`
LogLevel int `json:"log_level" yaml:"log-level"`
LogFile string `json:"log_file" yaml:"log-file"`
RunType string `json:"run_type" yaml:"run-type"`
LogLevel int `json:"log_level" yaml:"log-level"`
LogFile string `json:"log_file" yaml:"log-file"`
RelayBufferSize int `json:"relay_buffer_size" yaml:"relay_buffer_size"`
}

func init() {
config.RegisterConfigCreator(Name, func() interface{} {
return &Config{
LogLevel: 1,
LogLevel: 1,
RelayBufferSize: 4 * 1024,
}
})
}
31 changes: 15 additions & 16 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"io"
"math/rand"
"net"
"os"
"strings"

Expand All @@ -16,16 +15,13 @@ import (

const Name = "PROXY"

const (
MaxPacketSize = 1024 * 8
)

// Proxy relay connections and packets
type Proxy struct {
sources []tunnel.Server
sink tunnel.Client
ctx context.Context
cancel context.CancelFunc
sources []tunnel.Server
sink tunnel.Client
ctx context.Context
cancel context.CancelFunc
relayBufferSize int
}

func (p *Proxy) Run() error {
Expand Down Expand Up @@ -68,8 +64,9 @@ func (p *Proxy) relayConnLoop() {
}
defer outbound.Close()
errChan := make(chan error, 2)
copyConn := func(a, b net.Conn) {
_, err := io.Copy(a, b)
copyConn := func(dst io.Writer, src io.Reader) {
buffer := make([]byte, p.relayBufferSize)
_, err := io.CopyBuffer(dst, src, buffer)
errChan <- err
}
go copyConn(inbound, outbound)
Expand Down Expand Up @@ -116,7 +113,7 @@ func (p *Proxy) relayPacketLoop() {
errChan := make(chan error, 2)
copyPacket := func(a, b tunnel.PacketConn) {
for {
buf := make([]byte, MaxPacketSize)
buf := make([]byte, p.relayBufferSize)
n, metadata, err := a.ReadWithMetadata(buf)
if err != nil {
errChan <- err
Expand Down Expand Up @@ -151,11 +148,13 @@ func (p *Proxy) relayPacketLoop() {
}

func NewProxy(ctx context.Context, cancel context.CancelFunc, sources []tunnel.Server, sink tunnel.Client) *Proxy {
cfg := config.FromContext(ctx, Name).(*Config)
return &Proxy{
sources: sources,
sink: sink,
ctx: ctx,
cancel: cancel,
sources: sources,
sink: sink,
ctx: ctx,
cancel: cancel,
relayBufferSize: cfg.RelayBufferSize,
}
}

Expand Down