1083 字
5 分钟
以一种简单的方式维持内网穿透的稳定性

在使用 openfrp 时时常出现因更新导致无法连接的问题,虽然只需要我们手动重启一下就行,但是还是挺麻烦的,还得别人提醒。

但是 openfrp 提供的地址是附有端口的,我们需要检查某一端口是否能连接正常。普通的 ping 无法实现,我们可以使用 tcping。下载地址

将下载的 tcping.exe 或者 tcping64.exe 放置到 C:\Windows\System32 目录下(即与 ping.exe 同目录)

如果你下载的是 tcping.exe,命令则使用 tcping 开头。如果是 tcping64.exe,命令则以 tcping64 开头。

当然,如果你愿意,你可以将你的 tcping64.exe 改名为 tcping.exe。

官方 -help:

NAME
    tcping - simulate "ping" over tcp by establishing a connection to network hosts.
    Measures the time for your system to [SYN], receive the target's [SYN][ACK] and send [ACK].  Note that the travel time for
    the last ACK is not included - only the time it takes to be put on the wire a the sending end.
SYNOPSIS
    tcping [-tdsvf46] [-i interval] [-n times] [-w interval] [-b n] [-r times][-j depth] [--tee filename] [-f] destination [port]
DESCRIPTION
    tcping measures the time it takes to perform a TCP 3-way handshake (SYN, SYN/ACK, ACK) between itself and a remote host.
    The travel time of the outgoing final ACK is not included, only the (minimal) amount of time it has taken to drop it on
    the wire at the near end.  This allows the travel time of the (SYN, SYN/ACK) to approximate the travel time of the
    ICMP (request, response) equivalent.
OPTIONS
    -4      Prefer using IPv4
    -6      Prefer using IPv6
    -t      ping continuously until stopped via control-c
    -n count
            send _count_ pings and then stop.  Default 4.
    -i interval
            Wait _interval_ seconds between pings.  Default 1.  Decimals permitted.
    -w interval
            Wait _interval_ seconds for a response.  Default 2.  Decimals permitted.
    -d      include date and time on every output line
    -f      Force sending at least one byte in addition to making the connection.
    -g count
            Give up after _count_ failed pings.
    -b type
            Enable audible beeps.
            '-b 1' will beep "on down".  If a host was up, but now its not, beep.
            '-b 2' will beep "on up".  If a host was down, but now its up, beep.
            '-b 3' will beep "on change".  If a host was one way, but now its the other, beep.
            '-b 4' will beep "always".
    -c      only show output on a changed state
    -r count
            Every _count_ pings, we will perform a new DNS lookup for the host in case it changed.
    -s      Exit immediately upon a success. 
    -v      Print version and exit.
    -S source_address
            Use _source_address_ as the tcp client's source address.  Must be a valid IP address on the client system.

    -j      Calculate jitter.  Jitter is defined as the difference between the last response time and the historical average.
    -js depth
            Calculate jitter, as with -j but with an optional _depth_ argument specified. If _depth_ is specified tcping will
            use the prior _depth_ values to calculate a rolling average.
    --tee _filename_
            Duplicate output to the _filename_ specified.  Windows can still not be depended upon to have a useful command line 
            environment. Don't tease me, *nix guys.   
    --file
            Treat the "destination" option as a filename.  That file becomes a source of destinations, looped through on a
            line by line basis.  Some options don't work in this mode and statistics will not be kept.
    destination
            A DNS name, an IP address, or (in "http" mode) a URL.
            Do not specify the protocol ("http://") in "http" mode.  Also do not specify server port via ":port" syntax.
            For instance:   "tcping http://www.elifulkerson.com:8080/index.html" would fail
            Use the style:  "tcping www.elifulkerson.com/index.html 8080" instead.                       
    port
            A numeric TCP port, 1-65535.  If not specified, defaults to 80.
    --header
            include a header with the command line arguments and timestamp.  Header is implied if using --tee.
    --block
            use a "blocking" socket to connect.  This prevents -w from working, uses the default timeout of around 20 seconds,
            and might break other expected behavior.  However, it can detect an actively refused connection vs a timeout.   
HTTP MODE OPTIONS   
    -h      Use "http" mode.  In http mode we will attempt to GET the specified document and return additional values including
            the document's size, http response code, kbit/s.
    -u      In "http" mode, include the target URL on each output line.
    --post Use POST instead of GET in http mode.
    --head Use HEAD instead of GET in http mode.
    --get Shorthand to invoke "http" mode for consistency's sake.
    --proxy-server _proxyserver_
            Connect to _proxyserver_ to request the url rather than the server indicated in the url itself.
    --proxy-port _port_
            Specify the numeric TCP port of the proxy server.  Defaults to 3128.
    --proxy-credentials username:password
            Specify a username:password pair which is sent as a 'Proxy-Authorization: Basic' header.
RETURN VALUE
    tcping returns 0 if all pings are successful, 1 if zero pings are successful and 2 for mixed outcome.
BUGS/REQUESTS
    Please report bugs and feature requests to the author via contact information on http://www.elifulkerson.com
AVAILABILITY
    tcping is available at http://www.elifulkerson.com/projects/tcping.php

此外,这个命令使用 system() 运行后会返回结果,1 为无法连接,0 为连接成功。因此我们可以写出一段周期检测对应远程端口是否可访问的代码:

#include<iostream>
#include<climits>
#include<cstring>
#include<cstdio>
#include<Windows.h>
#include<chrono>
using namespace std;

int main() {
    while (1) {
	    time_t now = time(nullptr);
	    string s = ctime(&now);
		cout << ">>> [" << s.substr(0, s.size() - 1) << ']';
        if (system("tcping -n 1 [你的远程地址] [端口]")) {
        	cout << ">>> START FINAL CHECK." << endl;
        	bool flag = false;
        	for (int i = 1;i <= 3;i++) {
        		cout << ">>> CHECK NO. " << i << "";
        		if (!system("tcping -n 1 [你的远程地址] [端口]")) {
        			flag = true;
        			break;
				}
				Sleep(2000);
			}
			if (!flag) {
				system("start rfrp.bat");
			}
			cout << ">>> FINAL CHECK DONE." << endl;
        }
        Sleep(60000);
    } 
    return 0;
}

其中 if(!flag) 后会运行重启等相关操作,自行修改。

以一种简单的方式维持内网穿透的稳定性
https://www.mitufun.top/posts/以一种简单的方式维持内网穿透的稳定性/
作者
MituFun
发布于
2024-08-06
许可协议
CC BY-NC-SA 4.0