v2fly / v2ray-core

A platform for building proxies to bypass network restrictions.
https://v2fly.org
MIT License
29.62k stars 4.66k forks source link

Got “bind: address already in use” error on MacOS #2362

Closed zhvala closed 1 year ago

zhvala commented 1 year ago

你正在使用哪个版本的 V2Ray?

V2Ray: v5.3.0 Platform: MacOS 13.2.1 Golang 1.20.1

你的使用场景是什么?

场景特殊需要基于v2ray-core做二次开发,基本架构inbound socks5,outbound vemss,截取出问题的代码:

    localPort := net.Port(8888)
    vmessDomain := "my_vemss.com"
    vmessPort := net.Port(9999)
    vmessUserId := "my_id"

    config := &Config{
        App: []*anypb.Any{
            serial.ToTypedMessage(&dispatcher.Config{}),
            serial.ToTypedMessage(&proxyman.InboundConfig{}),
            serial.ToTypedMessage(&proxyman.OutboundConfig{}),
        },
        Inbound: []*InboundHandlerConfig{
            {
                ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
                    PortRange: net.SinglePortRange(localPort),
                    Listen:    net.NewIPOrDomain(net.LocalHostIP),
                }),
                ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
                    AuthType:   socks.AuthType_NO_AUTH,
                    UdpEnabled: false,
                    UserLevel:  0,
                }),
            },
        },
        Outbound: []*OutboundHandlerConfig{
            {
                ProxySettings: serial.ToTypedMessage(&outbound.Config{
                    Receiver: []*protocol.ServerEndpoint{
                        {
                            Address: net.NewIPOrDomain(net.ParseAddress(vmessDomain)),
                            Port:    uint32(vmessPort),
                            User: []*protocol.User{
                                {
                                    Account: serial.ToTypedMessage(&vmess.Account{
                                        Id:      vmessUserId,
                                        AlterId: 0,
                                        SecuritySettings: &protocol.SecurityConfig{
                                            Type: protocol.SecurityType_AUTO,
                                        },
                                    }),
                                    Level: 0,
                                },
                            },
                        },
                    },
                }),
            },
        },
    }

    cfgBytes, err := proto.Marshal(config)
    if err != nil {
        log.Fatal(err)
    }

    server, err := StartInstance(FormatProtobuf, cfgBytes)
    if err != nil {
        log.Fatal(err)
    }

    err = server.Start()
    if err != nil {
        log.Fatal(err)
    }
    defer server.Close()

你看到的异常现象是什么?

2023/02/28 16:15:16 [Debug] app/proxyman/inbound: creating stream worker on 127.0.0.1:8888
2023/02/28 16:15:16 [Info] transport/internet/tcp: listening TCP on 127.0.0.1:8888
2023/02/28 16:15:16 [Warning] V2Ray 5.3.0 started
2023/02/28 16:15:16 app/proxyman/inbound: failed to listen TCP on 8888 > transport/internet: failed to listen on address: 127.0.0.1:8888 > transport/internet/tcp: failed to listen TCP on 127.0.0.1:8888 > listen tcp 127.0.0.1:8888: bind: address already in us

但实际上8888端口并没有任何程序占用,另外这段代码在Linux 上运行没有问题。

你期待看到的正常表现是怎样的?

Instance 正确启动

请附上你的配置

服务端配置:

// 在这里附上服务器端配置文件

客户端配置:

// 在这里附上客户端配置

请附上出错时软件输出的错误日志

2023/02/28 16:15:16 [Debug] app/proxyman/inbound: creating stream worker on 127.0.0.1:8888
2023/02/28 16:15:16 [Info] transport/internet/tcp: listening TCP on 127.0.0.1:8888
2023/02/28 16:15:16 [Warning] V2Ray 5.3.0 started
2023/02/28 16:15:16 app/proxyman/inbound: failed to listen TCP on 8888 > transport/internet: failed to listen on address: 127.0.0.1:8888 > transport/internet/tcp: failed to listen TCP on 127.0.0.1:8888 > listen tcp 127.0.0.1:8888: bind: address already in us

服务器端错误日志:

// 在这里附上服务器端日志

客户端错误日志:

// 在这里附上客户端日志

请附上访问日志

// 在这里附上服务器端日志

其它相关的配置文件(如 Nginx)和相关日志

如果 V2Ray 无法启动,请附上 --test 命令的输出

如果 V2Ray 服务运行异常,请附上 journal 日志

ghost commented 1 year ago

Please use netstat to check the ports using. Your code runs success on Linux machine doesn't mean it will run success on macOS. It depend on the available ports on each machine.

parsanoori commented 1 year ago

Run:

sudo lsof -n -i :8888 | grep LISTEN

The first string is the command. The next item is the PID. Kill the process with:

kill -9 PID 
github-actions[bot] commented 1 year ago

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 5 days