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

修一个有关检测端口占用的小问题 #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

racpast
Copy link

@racpast racpast commented Jan 12, 2025

概述

启动全局伪造的时候对已占用端口的检测不正确,当端口被释放时不能及时检测。

问题复现

先启动Sheas-Cealer,再占用80或443端口(如启动Nginx模拟端口占用的情况),然后点击启动全局伪造,此时端口占用不能被正确检测到。
同样,先占用80或443端口,然后再启动Sheas-Cealer,点击启动全局伪造,此时可以正确检测端口占用,但停止对80或443端口的占用后,再点击启动全局伪造,此时依然提示端口占用。

问题分析

观察有关源码发现,变量NginxHttpPortNginxHttpsPort依靠触发NginxConfWatcher_Changed来更新,但是NginxConfWatcher_Changed在上述问题中只在MainWin_Loaded中触发。需要在合适的时机添加对NginxConfWatcher_Changed的触发,例如NginxButtonHoldTimer_Tick

修改建议

    private async void NginxButtonHoldTimer_Tick(object? sender, EventArgs e)
    {
        HoldButtonTimer?.Stop();

        NginxConfWatcher_Changed(null!, null!);

        // 省略其他代码
    }
    private async void NginxConfWatcher_Changed(object sender, FileSystemEventArgs e)
    {
        // 省略其他代码

        // 采用更高性能的方式检测端口占用,按原逻辑并未考虑极端情况
        HashSet<int> activePorts = new(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners().Select(listener => listener.Port));
        while (activePorts.Contains(NginxHttpPort)) NginxHttpPort++;
        while (activePorts.Contains(NginxHttpsPort)) NginxHttpsPort++;

        // 省略其他代码
    }

*开Nginx忘关再使用Sheas-Cealer的时候发现的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant