Skip to content

Commit

Permalink
feat: reverse listener configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
shoriwe committed May 22, 2023
1 parent adaa1de commit fc5e41a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
33 changes: 33 additions & 0 deletions config/reverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package config

import (
"net"

"github.com/shoriwe/fullproxy/v3/reverse"
)

type Reverse struct {
Listener *Listener `yaml:"listener"`
Controller Listener `yaml:"controller"`
}

func (r *Reverse) Master() (net.Listener, error) {
l, lErr := r.Listener.Listen()
if lErr != nil {
return nil, lErr
}
cl, clErr := r.Listener.Listen()
if clErr != nil {
l.Close()
return nil, clErr
}
return reverse.NewMaster(l, cl)
}

func (r *Reverse) Slave() (*reverse.Slave, error) {
cl, clErr := r.Controller.Dial()
if clErr != nil {
return nil, clErr
}
return reverse.NewSlave(cl)
}
53 changes: 53 additions & 0 deletions config/reverse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestReverse_Master(t *testing.T) {
t.Run("Valid", func(tt *testing.T) {
r := Reverse{
Listener: &Listener{
Network: "tcp",
Address: "localhost:0",
},
Controller: Listener{
Network: "tcp",
Address: "localhost:0",
},
}
m, err := r.Master()
assert.Nil(tt, err)
defer m.Close()
})
t.Run("Invalid User listener", func(tt *testing.T) {
r := Reverse{
Listener: &Listener{
Network: "tcp",
Address: "localhost:99999999",
},
Controller: Listener{
Network: "tcp",
Address: "localhost:0",
},
}
_, err := r.Master()
assert.NotNil(tt, err)
})
t.Run("Invalid Controller listener", func(tt *testing.T) {
r := Reverse{
Listener: &Listener{
Network: "tcp",
Address: "localhost:0",
},
Controller: Listener{
Network: "tcp",
Address: "localhost:9999999999",
},
}
_, err := r.Master()
assert.NotNil(tt, err)
})
}

0 comments on commit fc5e41a

Please sign in to comment.