Check if OpenVPN UDP Port is open [closed]

若如初见. 提交于 2019-12-08 10:49:27

问题


I want to check if the upd port for OpenVPN is open. For Tcp Port it was really simple, but now I struggle with Udp ports.

This is my TCP Implementation

private static bool TestConnectionInternal(string hostname, int port, int timeOutMs, int maxTries, int count)
{
    using (var tcpClient = new TcpClient())
    {
        try
        {
            Task result = tcpClient.ConnectAsync(hostname, port);
            return result.Wait(timeOutMs);

        }
        catch (Exception e)
        {
            count += 1;
            if (count < maxTries)
            {
                return TestConnectionInternal(hostname, port, timeOutMs, maxTries, count);
            }
            return false;
        }
    }
}

回答1:


Simple check OpenVPN UDP service (except that one uses --tls-auth or --secret)

bool CheckOpenVPNudp(string ip, int port)
        {
            IPEndPoint RemoteEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            byte[] data = { 56, 1, 0, 0, 0, 0, 0, 0, 0 }; //OpenVPN client welcome datagram
            server.SendTo(data, data.Length, SocketFlags.None, RemoteEndPoint);
            server.ReceiveTimeout = 15000; //15 seconds timeout
            EndPoint Remote = (EndPoint)(RemoteEndPoint);
            try
            {
                byte[] answer = new byte[1024];
                int recv = server.ReceiveFrom(answer, ref Remote);
                Console.WriteLine("Message received from {0}:", Remote.ToString());
                Console.WriteLine(System.Text.Encoding.ASCII.GetString(answer, 0, recv));
                return true;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }

        }




回答2:


There no way to know if a UDP port is open. If you lucky you get a icmp unreachable port closed negative answer. For some protocols like NTP you can try to send a valid query and check for a response. If OpenVPN is configured with --tls-auth or --secret you cannot produce a valid packet to trigger a repsonse if you don know the secret key.



来源:https://stackoverflow.com/questions/21462339/check-if-openvpn-udp-port-is-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!