准备测试环境代码

This commit is contained in:
2025-02-25 18:08:57 +08:00
parent 884faae5ba
commit b50dc3d91c
6 changed files with 568 additions and 223 deletions

View File

@@ -163,14 +163,14 @@ func (s *Service) processCtrlConn(controller net.Conn) {
},
})
if err != nil {
slog.Error("代理服务创建失败", err)
slog.Error("代理服务创建失败", "err", err)
return
}
go func() {
err := proxy.Run()
if err != nil {
slog.Error("代理服务建立失败", err)
slog.Error("代理服务建立失败", "err", err)
return
}
}()
@@ -181,13 +181,13 @@ func (s *Service) processCtrlConn(controller net.Conn) {
tag := user.Tag()
_, err := controller.Write([]byte{byte(len(tag))})
if err != nil {
slog.Error("write error", err)
slog.Error("write error", "err", err)
return
}
_, err = controller.Write([]byte(tag))
slog.Info("已通知客户端建立数据通道")
if err != nil {
slog.Error("write error", err)
slog.Error("write error", "err", err)
return
}
s.connMap[tag] = user
@@ -201,7 +201,7 @@ func (s *Service) startDataTun(ctx context.Context, errCh chan error) {
// 监听端口
lData, err := net.Listen("tcp", ":"+strconv.Itoa(int(dataPort)))
if err != nil {
slog.Error("listen error", err)
slog.Error("listen error", "err", err)
return
}
defer utils.Close(lData)
@@ -255,12 +255,12 @@ func (s *Service) processDataConn(client net.Conn) {
// 读取 tag
tagLen, err := utils.ReadByte(client)
if err != nil {
slog.Error("read error", err)
slog.Error("read error", "err", err)
return
}
tagBuf, err := utils.ReadBuffer(client, int(tagLen))
if err != nil {
slog.Error("read error", err)
slog.Error("read error", "err", err)
return
}
tag := string(tagBuf)
@@ -280,12 +280,12 @@ func (s *Service) processDataConn(client net.Conn) {
// 写入目标地址
_, err = client.Write([]byte{byte(len(data.Dest))})
if err != nil {
slog.Error("写入目标地址失败", err)
slog.Error("写入目标地址失败", "err", err)
return
}
_, err = client.Write([]byte(data.Dest))
if err != nil {
slog.Error("写入目标地址失败", err)
slog.Error("写入目标地址失败", "err", err)
return
}
@@ -295,14 +295,14 @@ func (s *Service) processDataConn(client net.Conn) {
go func() {
_, err := io.Copy(client, user)
if err != nil {
slog.Error("processDataConn error c2u", err)
slog.Error("processDataConn error c2u", "err", err)
}
errCh <- err
}()
go func() {
_, err := io.Copy(user, client)
if err != nil {
slog.Error("processDataConn error u2c", err)
slog.Error("processDataConn error u2c", "err", err)
}
errCh <- err
}()
@@ -366,7 +366,8 @@ func (a *NoAuthAuthenticator) Authenticate(ctx context.Context, reader io.Reader
}
// 检查权限是否过期
timeout := uint(channel.Expiration.Sub(time.Now()).Seconds())
timeout := channel.Expiration.Sub(time.Now()).Seconds()
slog.Info("用户剩余时间", "timeout", timeout)
if timeout <= 0 {
return nil, errors.New("noAuth 权限已过期")
}
@@ -374,7 +375,7 @@ func (a *NoAuthAuthenticator) Authenticate(ctx context.Context, reader io.Reader
return &socks5.AuthContext{
Method: socks5.NoAuth,
Timeout: timeout,
Timeout: uint(timeout),
Payload: nil,
}, nil
}
@@ -443,7 +444,8 @@ func (a *UserPassAuthenticator) Authenticate(ctx context.Context, reader io.Read
}
// 检查权限是否过期
timeout := uint(channel.Expiration.Sub(time.Now()).Seconds())
timeout := channel.Expiration.Sub(time.Now()).Seconds()
slog.Info("用户剩余时间", "timeout", timeout)
if timeout <= 0 {
return nil, errors.New("权限已过期")
}
@@ -484,13 +486,13 @@ func (a *UserPassAuthenticator) Authenticate(ctx context.Context, reader io.Read
// 响应认证成功
_, err = writer.Write([]byte{socks5.AuthVersion, socks5.AuthSuccess})
if err != nil {
slog.Error("响应认证失败", err)
slog.Error("响应认证失败", "err", err)
return nil, err
}
return &socks5.AuthContext{
Method: socks5.UserPassAuth,
Timeout: 300, // todo
Timeout: uint(timeout),
Payload: nil,
}, nil
}

294
server/fwd/service_test.go Normal file
View File

@@ -0,0 +1,294 @@
package fwd
import (
"bytes"
"context"
"io"
"net"
"proxy-server/pkg/utils"
"proxy-server/server/pkg/socks5"
"reflect"
"testing"
)
func TestNew(t *testing.T) {
type args struct {
config *Config
}
tests := []struct {
name string
args args
want *Service
}{
// TODO: Add test cases.
{
name: "server config nil",
args: args{
config: nil,
},
want: &Service{
Config: &Config{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.args.config); !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}
func TestNoAuthAuthenticator_Authenticate(t *testing.T) {
type args struct {
ctx context.Context
reader io.Reader
}
tests := []struct {
name string
args args
wantWriter string
want *socks5.AuthContext
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &NoAuthAuthenticator{}
writer := &bytes.Buffer{}
got, err := a.Authenticate(tt.args.ctx, tt.args.reader, writer)
if (err != nil) != tt.wantErr {
t.Errorf("Authenticate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotWriter := writer.String(); gotWriter != tt.wantWriter {
t.Errorf("Authenticate() gotWriter = %v, want %v", gotWriter, tt.wantWriter)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Authenticate() got = %v, want %v", got, tt.want)
}
})
}
}
func TestNoAuthAuthenticator_Method(t *testing.T) {
tests := []struct {
name string
want socks5.AuthMethod
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &NoAuthAuthenticator{}
if got := a.Method(); got != tt.want {
t.Errorf("Method() = %v, want %v", got, tt.want)
}
})
}
}
func TestService_Run(t *testing.T) {
type fields struct {
Config *Config
connMap map[string]socks5.ProxyData
ctrlConnWg utils.CountWaitGroup
dataConnWg utils.CountWaitGroup
}
type args struct {
ctx context.Context
errCh chan error
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Service{
Config: tt.fields.Config,
connMap: tt.fields.connMap,
ctrlConnWg: tt.fields.ctrlConnWg,
dataConnWg: tt.fields.dataConnWg,
}
s.Run(tt.args.ctx, tt.args.errCh)
})
}
}
func TestService_processCtrlConn(t *testing.T) {
type fields struct {
Config *Config
connMap map[string]socks5.ProxyData
ctrlConnWg utils.CountWaitGroup
dataConnWg utils.CountWaitGroup
}
type args struct {
controller net.Conn
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Service{
Config: tt.fields.Config,
connMap: tt.fields.connMap,
ctrlConnWg: tt.fields.ctrlConnWg,
dataConnWg: tt.fields.dataConnWg,
}
s.processCtrlConn(tt.args.controller)
})
}
}
func TestService_processDataConn(t *testing.T) {
type fields struct {
Config *Config
connMap map[string]socks5.ProxyData
ctrlConnWg utils.CountWaitGroup
dataConnWg utils.CountWaitGroup
}
type args struct {
client net.Conn
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Service{
Config: tt.fields.Config,
connMap: tt.fields.connMap,
ctrlConnWg: tt.fields.ctrlConnWg,
dataConnWg: tt.fields.dataConnWg,
}
s.processDataConn(tt.args.client)
})
}
}
func TestService_startCtrlTun(t *testing.T) {
type fields struct {
Config *Config
connMap map[string]socks5.ProxyData
ctrlConnWg utils.CountWaitGroup
dataConnWg utils.CountWaitGroup
}
type args struct {
ctx context.Context
errCh chan error
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Service{
Config: tt.fields.Config,
connMap: tt.fields.connMap,
ctrlConnWg: tt.fields.ctrlConnWg,
dataConnWg: tt.fields.dataConnWg,
}
s.startCtrlTun(tt.args.ctx, tt.args.errCh)
})
}
}
func TestService_startDataTun(t *testing.T) {
type fields struct {
Config *Config
connMap map[string]socks5.ProxyData
ctrlConnWg utils.CountWaitGroup
dataConnWg utils.CountWaitGroup
}
type args struct {
ctx context.Context
errCh chan error
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Service{
Config: tt.fields.Config,
connMap: tt.fields.connMap,
ctrlConnWg: tt.fields.ctrlConnWg,
dataConnWg: tt.fields.dataConnWg,
}
s.startDataTun(tt.args.ctx, tt.args.errCh)
})
}
}
func TestUserPassAuthenticator_Authenticate(t *testing.T) {
type args struct {
ctx context.Context
reader io.Reader
}
tests := []struct {
name string
args args
wantWriter string
want *socks5.AuthContext
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &UserPassAuthenticator{}
writer := &bytes.Buffer{}
got, err := a.Authenticate(tt.args.ctx, tt.args.reader, writer)
if (err != nil) != tt.wantErr {
t.Errorf("Authenticate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotWriter := writer.String(); gotWriter != tt.wantWriter {
t.Errorf("Authenticate() gotWriter = %v, want %v", gotWriter, tt.wantWriter)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Authenticate() got = %v, want %v", got, tt.want)
}
})
}
}
func TestUserPassAuthenticator_Method(t *testing.T) {
tests := []struct {
name string
want socks5.AuthMethod
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &UserPassAuthenticator{}
if got := a.Method(); got != tt.want {
t.Errorf("Method() = %v, want %v", got, tt.want)
}
})
}
}