Add configuration item api->listen. (#3317)

* add api.listen configuration item

* add unit tests

* typo

---------

Co-authored-by: nobody <nobody@nowhere.mars>
This commit is contained in:
nobody 2024-05-02 09:23:55 +08:00 committed by GitHub
parent 51504c624c
commit 447a49d16a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 126 additions and 21 deletions

View file

@ -33,6 +33,80 @@ import (
"google.golang.org/grpc/credentials/insecure"
)
func TestCommanderListenConfigurationItem(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()
clientPort := tcp.PickPort()
cmdPort := tcp.PickPort()
clientConfig := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&commander.Config{
Tag: "api",
Listen: fmt.Sprintf("127.0.0.1:%d", cmdPort),
Service: []*serial.TypedMessage{
serial.ToTypedMessage(&command.Config{}),
},
}),
},
Inbound: []*core.InboundHandlerConfig{
{
Tag: "d",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
Networks: []net.Network{net.Network_TCP},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
Tag: "default-outbound",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
servers, err := InitializeServerConfigs(clientConfig)
common.Must(err)
defer CloseAllServers(servers)
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Fatal(err)
}
cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
common.Must(err)
defer cmdConn.Close()
hsClient := command.NewHandlerServiceClient(cmdConn)
resp, err := hsClient.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
Tag: "d",
})
common.Must(err)
if resp == nil {
t.Error("unexpected nil response")
}
{
_, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
if err == nil {
t.Error("unexpected nil error")
}
}
}
func TestCommanderRemoveHandler(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,