首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行各种Wireshark服务

执行各种Wireshark服务
EN

Code Review用户
提问于 2012-12-11 15:39:26
回答 2查看 837关注 0票数 1

我正在构建一个打开Wireshark服务的应用程序(Wireshark有几个服务),以便对Wireshark文件进行不同的处理,比如编辑、更改格式、统计信息等。每个选项通常使用不同的服务,因此我希望构建具有继承的类。

我想知道我想做的事是否合适。

主类WiresharkServices具有以下成员和方法:

代码语言:javascript
复制
public class WiresharkProcesses
{
    protected string _filePath; //the file path who send to Wiresahrk process
    protected string _capinfos;
    protected string _dumpcap;
    protected string _editcap;
    protected string _mergecap;
    protected string _rawshark;
    protected string _text2pcap;
    protected string _tshark;
    protected string _wireshark;

    public void initializeServices()
    {
        if (Directory.Exists(@"C:\Program Files (x86)\Wireshark"))
        {
            _capinfos = @"C:\Program Files (x86)\Wireshark\_capinfos.exe";
            _dumpcap = @"C:\Program Files (x86)\Wireshark\_dumpcap.exe";
            _editcap = @"C:\Program Files (x86)\Wireshark\editcap.exe";
            _mergecap = @"C:\Program Files (x86)\Wireshark\_mergecap.exe";
            _rawshark = @"C:\Program Files (x86)\Wireshark\_rawshark.exe";
            _text2pcap = @"C:\Program Files (x86)\Wireshark\_text2pcap.exe";
            _tshark = @"C:\Program Files (x86)\Wireshark\_tshark.exe";
            _wireshark = @"C:\Program Files (x86)\Wireshark\_wireshark.exe";
        }
        else if (Directory.Exists(@"C:\Program Files\Wireshark"))
        {
            _capinfos = @"C:\Program File)\Wireshark\_capinfos.exe";
            _dumpcap = @"C:\Program Files\Wireshark\_dumpcap.exe";
            _editcap = @"C:\Program Files\Wireshark\editcap.exe";
            _mergecap = @"C:\Program Files\Wireshark\_mergecap.exe";
            _rawshark = @"C:\Program Files\Wireshark\_rawshark.exe";
            _text2pcap = @"C:\Program Files\Wireshark\_text2pcap.exe";
            _tshark = @"C:\Program Files\Wireshark\_tshark.exe";
            _wireshark = @"C:\Program Files\Wireshark\_wireshark.exe";
        }
    }
}

当应用程序运行时,我当然是在检查机器上是否安装了Wireshark。如果没有,我抛出一个异常:

代码语言:javascript
复制
WiresharkServices wservices = new WiresharkServices();
wservices .initializeServices();

每个类都有自己的方法。

子类示例,它接收文件路径将其转换为另一种Wireshark格式:

代码语言:javascript
复制
public class Editcap : WiresharkProcesses
{
    private string _newFileName;

    public void startProcess(string filePath)
    {
        FileInfo file = new FileInfo(filePath);
        _newFileName = file.FullName.Replace(file.Extension, "_new") + ".pcap";
        ProcessStartInfo editcapProcess = new ProcessStartInfo(string.Format("\"{0}\"", _editcap))
        {
            Arguments = string.Format("{2}{0}{2} -F libpcap {2}{1}{2}", file.FullName, _newFileName, "\""),
            WindowStyle = ProcessWindowStyle.Hidden,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true,
            UseShellExecute = false,
            ErrorDialog = false
        };

        using (Process editcap = Process.Start(editcapProcess))
        {
            editcap.WaitForExit();
        }
    }

    public string getNewFileName()
    {
        return _newFileName;
    }
}
EN

回答 2

Code Review用户

回答已采纳

发布于 2012-12-11 18:45:38

从继承中获得的唯一好处是应该使用哪个目录来访问可执行文件。您只使用它来定义多个(应该是常量的)字符串。在秒的情况下,您的_capinfos值出现了错误,这表明这不是执行此操作的最佳方式。

为了完成初始化字符串的任务,我会这样做。

代码语言:javascript
复制
public void initializeCommands(String path) {
    _capinfos = path + "_capinfos.exe";
    _dumpcap = path + "_dumpcap.exe";
    _editcap = path + "editcap.exe";
    _mergecap = path + "_mergecap.exe";
    _rawshark = path + "_rawshark.exe";
    _text2pcap = path + "_text2pcap.exe";
    _tshark = path + "_tshark.exe";
    _wireshark = path + "_wireshark.exe";
}

您可以将其封装在确定路径的内容中,甚至支持非标准路径。

但真正的原因在于,您使用的是继承,这样您就可以轻松地访问一些您可能想要使用的常量。您没有定义任何方法来指定在执行Wireshark进程的多个类之间使用的实际代码。如果Editcap在可执行文件的路径中被传递,那么它就不再需要父类了。如果耦合是这样松散的,继承通常不是最好的主意。

票数 3
EN

Code Review用户

发布于 2012-12-14 01:22:33

这应该会好一点,但不是完美的。请注意,在没有Visual在我面前的情况下编写了这篇文章。

注意,WiresharkProcesses类是静态的,因此不能继承它,也不能实例化它。它就像一个带有一系列函数的命名空间,只是静态构造函数做了一些工作。这并不完美。我很乐意接受社会人士的一些建议。您可以打开一些私有只读字段,或者至少将它们公开为属性。注意,对属性的重复访问将导致重复的字符串连接,但这是一个非常快速的操作。另一种选择是使用像public static string CapInfosExePath { get; private set;}这样的自动属性而不是受保护的字段。无论哪种方式,继承都不是必需的。EditCap类有太多的锅炉板代码.坦率地说,您只需要一个或两个类,可能会使它们成为partial,并将它们分成两个单独的文件(只是一个想法)。

代码语言:javascript
复制
using System.IO;

// TODO: also add namespace around this
public static class WiresharkProcesses
{
    // TODO: actually use proper library for this.
    // http://stackoverflow.com/questions/1085584/how-do-i-programmatically-retrieve-the-actual-path-to-the-program-files-folder?lq=1
    // Note that a spanish Windows version will have "C:\Archivos de programa\" instead
    private static readonly string ProgramFiles64Path = @"C:\Program Files";
    private static readonly string ThirtyTwoBitSuffix = @" (x86)";
    private static readonly string ProgramFiles32Path = Path.Join(ProgramFiles64Path, ThirtyTwoBitSuffix);
    private static readonly string WiresharkDirName = @"Wireshark";
    private static bool Is64Bit = true;
    private static string ProgramFilesPath = null;
    private static readonly string WiresharkDirectoryPath = null;

    // Static constructor runs only once
    public static WiresharkProcesses()
    {
        if (Directory.Exists(ProgramFiles64Path))
        {
            Is64Bit = true;
            ProgramFilesPath = ProgramFiles64Path;
        }
        else if (Directory.Exists(ProgramFiles32Path))
        {
            Is64Bit = false;
            ProgramFilesPath = ProgramFiles32Path;
        }
        else
        {
            throw new AppropriateException("WTF!");
        }

        WiresharkDirectoryPath = Path.Combine(ProgramFilesPath, WiresharkDirName);
    }

    private static string GetFullPath(string exeName)
    {
        return Path.Combine(WiresharkDirectoryPath, exeName);
    }

    public static string CapInfosExePath
    {
        get
        {
            // Perhaps this arg should be a variable as well.
            return GetFullPath("_capinfos.exe");
        }
    }

    ...

    public static string WiresharkExePath
    {
        get
        {
            // Perhaps this arg should be a variable as well.
            return GetFullPath("_wireshark.exe");
        }
    }
}
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/19493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档