Git Product home page Git Product logo

natmap's Introduction

NATMap

status chat

This project is used to establish a TCP/UDP port mapping from ISP NAT public address to local private address. If all layers of NAT are full cones (NAT-1), any host can access internal services through the mapped public address. In bind mode, all traffic does not go through this program.

中文文档

How to Build

git clone --recursive https://github.com/heiher/natmap.git
cd natmap
make

# statically link
make ENABLE_STATIC=1

# cross compile
make CROSS_PREFIX=${cross-toolchain}/bin/x86_64-unknown-linux-

# android
mkdir natmap
cd natmap
git clone --recursive https://github.com/heiher/natmap.git jni
ndk-build

How to Use

Usage

Usage:
 natmap [options]

Options:
 -4                  use IPv4
 -6                  use IPv6
 -u                  UDP mode
 -d                  run as daemon
 -i <interface>      network interface
 -k <interval>       seconds between each keep-alive
 -s <addr>[:port]    domain name or address to STUN server
 -h <addr>[:port]    domain name or address to HTTP server
 -e <path>           script path for notify mapped address

Bind options:
 -b <port>           port number for binding

Forward options:
 -T <timeout>        port forwarding timeout in seconds
 -t <address>        domain name or address to forward target
 -p <port>           port number to forward target (0: use public port)

Bind mode

# TCP
natmap -s turn.cloudflare.com -h example.com -b 80

# UDP
natmap -u -s turn.cloudflare.com -b 443

In TCP mode, this program will establishs a TCP port mapping in two steps:

  1. Establish a connection with the HTTP server from the specified bind port and keep-alive.
  2. Establish a connection with the STUN server from the same port and obtain the public address.

And this program will call the script specified by the argument to inform the public address after the port mapping is established. and the script can update to the DNS record for external access.

Please note that you need to open the firewall to allow access to the bind port.

OpenWrt

Goto Network -> Firewall -> Traffic Rules

Add a traffic rule:

  • Protocol: TCP/UDP
  • Source zone: wan
  • Destination zone: Device (input)
  • Destination port: [bind port]
  • Action: accept
  • Others: keep default values

If the port binding fails, because it is already in use. This program will try to find out which local service process takes up the port and enable reuse port remotely. This works in Linux kernel 5.6 and later, and needs to run as root.

Forward mode

# TCP
natmap -s turn.cloudflare.com -h example.com -b 80 -t 10.0.0.2 -p 80

# UDP
natmap -u -s turn.cloudflare.com -b 443 -t 10.0.0.2 -p 443

Similar to bind mode, this program will listening on bound port and accepts the incoming connections and forward to target address.

Another way is to use firewall's DNAT to forward, and this way should uses bind mode.

OpenWrt

Goto Network -> Firewall -> Port Forwards

Add a port forward rule:

  • Protocol: TCP/UDP
  • Source zone: wan
  • External port: [bind port]
  • Destination zone: lan
  • Internal IP address: 10.0.0.2
  • Internal port: 80
  • Others: keep default values

Script arguments

{public-addr} {public-port} {ip4p} {private-port} {protocol} {private-addr}
  • argv[0]: Script path
  • argv[1]: Public address (IPv4/IPv6)
  • argv[2]: Public port
  • argv[3]: IP4P
  • argv[4]: Bind port (private port)
  • argv[5]: Protocol (TCP/UDP)
  • argv[6]: Private address (IPv4/IPv6)

IP4P address

The IP4P address format uses IPv6 special addresses to encode IPv4 addresses and ports for easy distribution through DNS AAAA records.

2001::{port}:{ipv4-hi16}:{ipv4-lo16}

Contributors

License

MIT

natmap's People

Contributors

1715173329 avatar abgelehnt avatar heiher avatar mikewang000000 avatar muink avatar xhebox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

natmap's Issues

[Feature Request] UPnP Server

Add upnp function to the router so that port mapping can be completed automatically.
This should be an exciting feature.

ssh断开连接问题

使用natmap,ipv4打洞,同时建立了一个ss服务端,将natmap的流量是设置转发到本地ss服务端的,在外面分流走这个ss节点,通过ssh访问内网设备,ssh空闲2分钟会断开,很准确的2分钟断开,做了几次测试。

使用ipv6搭建的ss节点,ssh访问家中内网设备,不会有2分钟断开的问题,这样应该可以排除此ssh设备的设置问题。

疑问是,到底是什么原因导致ipv4打洞下2分钟空闲,ssh就断连呢?是natmap设计中的问题还是运营商干的好事

启动脚本命令如下:
image

参数http server是做什么用的

参数http server是做什么用的? 看说明是写用来保活的,但是为什么需要HTTP地址来保活,这个HTTP地址有要求吗,比如可以任意填一个网址,www.qq.com www.baidu.com 192.168.1.1 之类的?

build failed with error 'fatal error: hev-task.h'

In file included from src/hev-conf.c:14:
src/hev-misc.h:13:10: fatal error: hev-task.h: No such file or directory
   13 | #include <hev-task.h>
      |          ^~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:75: build/hev-conf.o] Error 1

命令行以及luci均报错无法使用

运行在openwrt主路由上
使用命令natmap -s stun.stunprotocol.org -h qq.com -b 5665
[E] hev_sock_client_stun src/hev-sock.c:281
[E] task_entry src/hev-stun.c:289
[E] hev_sock_client_tcp src/hev-sock.c:167
[E] tnsk_run src/hev-tnsk.c:93
luci界面按示例图片配置后错误相同
麻烦看看是哪里的问题
同时麻烦详细说明一下配置Cloudflare api的文件这两个变量${RULE}和${RECORD}应该如何填写,谢谢

tcp和udp能否共存?

看到wiki写的,支持tcp和udp协议,默认是tcp,启用-u参数时,是udp协议,那么能否tcp和udp共存呢?

添加docker支持

Dockerfile:

FROM ubuntu:latest
ENV LANG        C.UTF-8
ENV LANGUAGE    C.UTF-8
ENV LC_ALL      C.UTF-8
RUN apt update \
    && apt upgrade -y \
    && apt install -y gcc git make curl
RUN git clone --recursive https://github.com/heiher/natmap.git \
    && cd natmap \
    && make \
    && cp ./bin/natmap /opt/natmap
ENTRYPOINT ["/opt/natmap"]

构建镜像

docker build -f ./dockerfiles/natmap/Dockerfile -t natmap .

docker-compose.yml

  natmap-wg:
    container_name: natmap-wg
    image: natmap
    command: -u -s stun.qq.com -b 30101 -t 127.0.0.1 -p 51820 -e /opt/cf_ip4p.sh -k 60
    volumes:
      - ./natmap/cf_ip4p.sh:/opt/cf_ip4p.sh
    cap_add:
      - NET_ADMIN
      - NET_RAW
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    restart: always

如果用python脚本的话,需要exec到容器内,再apt update && apt install python,其他的类似

网络变动后故障

系统是armbian
正常不安装 netbird tailscale zerotier 的情况下是没有任何问题的 是否捆绑网口都能正常启动
但是安装这三个软件后 启动就会报错 而且 多次重装系统 都是这个情况
如果可以的话 能列出软件依赖 或环境修复工具 就好了 因为这个问题实在是无法避免
root@arm:/natmap_make/natmap-20240409-next# make
BUILD src/hev-conf.c
BUILD src/hev-sock.c
BUILD src/hev-main.c
BUILD src/hev-ufwd.c
BUILD src/hev-misc.c
BUILD src/hev-exec.c
BUILD src/hev-tfwd.c
BUILD src/hev-tnsk.c
BUILD src/hev-stun.c
BUILD src/hev-unsk.c
BUILD src/hev-xnsk.c
BUILD src/kern/aide/hev-task-aide.c
BUILD src/kern/io/hev-task-io-reactor-kqueue.c
BUILD src/kern/io/hev-task-io-reactor-epoll.c
BUILD src/kern/io/hev-task-io-reactor.c
BUILD src/kern/core/hev-task-system.c
BUILD src/kern/core/hev-task-system-schedule.c
BUILD src/kern/task/hev-task-executer.c
BUILD src/kern/task/hev-task.c
BUILD src/kern/task/hev-task-stack-mmap.c
BUILD src/kern/task/hev-task-stack-heap.c
BUILD src/kern/task/hev-task-call.c
BUILD src/kern/sync/hev-task-mutex.c
BUILD src/kern/sync/hev-task-cond.c
BUILD src/kern/itc/hev-task-channel-select.c
BUILD src/kern/itc/hev-task-channel.c
BUILD src/kern/time/hev-task-timer.c
BUILD src/kern/time/hev-task-timer-timerfd.c
BUILD src/kern/time/hev-task-timer-kevent.c
BUILD src/mem/simple/hev-memory-allocator-simple.c
BUILD src/mem/api/hev-memory-allocator-api.c
BUILD src/mem/base/hev-memory-allocator.c
BUILD src/mem/slice/hev-memory-allocator-slice.c
BUILD src/lib/rbtree/hev-rbtree-cached.c
BUILD src/lib/rbtree/hev-rbtree.c
BUILD src/lib/io/basic/hev-task-io.c
BUILD src/lib/io/poll/hev-task-io-poll.c
BUILD src/lib/io/pipe/hev-task-io-pipe.c
BUILD src/lib/io/buffer/hev-circular-buffer.c
BUILD src/lib/io/socket/hev-task-io-socket.c
BUILD src/lib/list/hev-list.c
BUILD src/lib/dns/hev-task-dns.c
BUILD src/lib/dns/hev-task-dns-proxy.c
BUILD src/lib/object/hev-object.c
BUILD src/lib/object/hev-object-atomic.c
BUILD src/lib/misc/hev-debugger.c
BUILD src/lib/cio/buffer/hev-task-cio-buffer.c
BUILD src/lib/cio/fd/hev-task-cio-fd.c
BUILD src/lib/cio/null/hev-task-cio-null.c
BUILD src/lib/cio/socket/hev-task-cio-socket.c
BUILD src/lib/cio/base/hev-task-cio.c
BUILD src/kern/task/hev-task-execute.S
LINK bin/libhev-task-system.a
LINK bin/natmap
STRIP bin/natmap
root@arm:/natmap_make/natmap-20240409-next# ./bin/natmap -s stunserver.stunprotocol.org -h example.com -b 29414 -i eth0
[E] stun_tcp src/hev-stun.c:131 STUN TCP recv failed.
[E] stun_bind src/hev-stun.c:259
[E] task_entry src/hev-stun.c:311
[E] stun_tcp src/hev-stun.c:131 STUN TCP recv failed.
[E] stun_bind src/hev-stun.c:259
[E] task_entry src/hev-stun.c:311

成功获取端口,但是连接Timeout

Hi,
我目前的环境是江苏移动大内网,Nat类型是Full Cone
目前是路由器(192.168.10.1)拨号上网,开启dmz到我的一个小服务器上(192.168.10.129)
当我运行
root@dsus4serv:/home/dsus4# ./natmap -s stunserver.stunprotocol.org -h qq.com -b 22 -t localhost -p 22
183.xx.xx.11 16835 2001::xxx:xxx:070b 22 tcp
用远端电脑连接时,提示timeout
服务器端无任何报错
机器防火墙已完全关闭
请问有什么可以排查的办法吗,谢谢
Wang

udp协议还是无法正常更新IP。

主路由下挂了一个armbian,开了两个tcp映射和一个udp映射,在主路由做好了端口转发。程序启动后都能正常工作,但当外部IP变动时,tcp都能正常更新,通过脚本能收到提醒,但udp根本没有提醒,也没有更新,导致外部连接失效,要关闭这个映射,重新启动程序才能正常工作。希望大佬能解决这个问题。

无法通讯

我在电脑上运行NatTypeTest显示是fullcone,然后在路由器上(istoreos)运行
./natmap -u -s stun.qq.com -b 0 -t 192.168.2.115 -p 8083
192.168.2.115是我的电脑ip,然后natmap反回了一个公网ip和端口。用另外一台电脑往这个端口发送udp数据包,但我192.168.2.115的电脑并不能收到这个数据包是配置有问题吗

问一个非常弱智的问题

打洞成功了,通过 ip+端口也能访问到内网的服务!
然后我使用 cloudflare 做了 dns 和 origin 规则之后,直接使用域名是不能访问的。
通过nslookup 看解析结果也是正常的。
这种我直接通过域名不加端口的方式不能访问是不是因为我的域名没有备案啊?
如果是的话,请问有没有什么办法让我用域名+固定端口来访问呢?
感谢大佬

一开通知脚本就不能运行了,请求大佬协助

环境

路由器:Xiaomi Mi Router AC2100
固件版本:OpenWrt 22.03.3 r20028-43d71ad93e / LuCI openwrt-22.03 branch git-23.093.57104-ce20b4a
软件版本:luci-app-natmap git-22.356.23645-3d37ac8
         natmap 20230322-1

脚本

#!/bin/sh

# Natter/NATMap
private_port=$4 # Natter: $3; NATMap: $4
public_port=$2 # Natter: $5; NATMap: $2

# qBittorrent.
qb_host="192.168.1.122"
qb_web_port="8080"
qb_username="xxxxx"
qb_password="xxxxxxxxx"


echo "Update qBittorrent listen port to $public_port..."

# Update qBittorrent listen port.
qb_cookie=$(curl -s -i --header "Referer: http://$qb_host:$qb_web_port" --data "username=$qb_username&password=$qb_password" http://$qb_host:$qb_web_port/api/v2/auth/login | grep -i set-cookie | cut -c13-48)
curl -X POST -b "$qb_cookie" -d 'json={"listen_port":"'$public_port'"}' "http://$qb_host:$qb_web_port/api/v2/app/setPreferences"

echo "Update iptables..."

# Use iptables to forward traffic.

uci set firewall.@redirect[0].dest_port=$public_port
uci commit firewall
/etc/init.d/firewall reload

echo "Done."

现象

因为我有两台电脑,两个qbittorrent,所以写了两个脚本,只修改了防火墙和qb_host那一行,就不能运行了。

qb_host="192.168.1.123"
...
uci set firewall.@redirect[1].dest_port=$public_port

其中一个能正常运行,脚本也没问题,可以修改端口转发规则和qb监听端口,
但另一个完全运行不起来。连ps w 都不显示,
明明是两个一模一样的脚本,就是不能运行。
是不支持同时给两个任务设置通知脚本吗?

# 运行命令
/usr/bin/natmap -s stun.freeswitch.org -h qq.com -b 13376 -4 -e /usr/lib/natmap/update.sh

# logread有错误提示
Sat Apr  8 03:12:54 2023 daemon.err natmap[17163]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:12:54 2023 daemon.err natmap[17163]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:12:54 2023 daemon.err natmap[17164]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:12:54 2023 daemon.err natmap[17164]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:12:54 2023 daemon.err natmap[17161]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:12:54 2023 daemon.err natmap[17161]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:12:59 2023 daemon.err natmap[17163]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:12:59 2023 daemon.err natmap[17163]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:12:59 2023 daemon.err natmap[17161]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:12:59 2023 daemon.err natmap[17161]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:13:04 2023 daemon.err natmap[17161]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:13:04 2023 daemon.err natmap[17161]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:13:09 2023 daemon.err natmap[17161]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:13:09 2023 daemon.err natmap[17161]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:13:14 2023 daemon.err natmap[17161]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:13:14 2023 daemon.err natmap[17161]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:50:05 2023 daemon.err natmap[18849]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:50:05 2023 daemon.err natmap[18849]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:50:05 2023 daemon.err natmap[18850]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:50:05 2023 daemon.err natmap[18850]: [E] tnsk_run src/hev-tnsk.c:103
Sat Apr  8 03:50:10 2023 daemon.err natmap[18849]: [E] hev_sock_client_tcp src/hev-sock.c:166
Sat Apr  8 03:50:10 2023 daemon.err natmap[18849]: [E] tnsk_run src/hev-tnsk.c:103

[E] hev_sock_client_stun src/hev-sock.c:281 [E] task_entry src/hev-stun.c:289

不断出现[E] hev_sock_client_stun src/hev-sock.c:281
[E] task_entry src/hev-stun.c:289 错误,无论是自己编译还是用release里现成的.
stun服务器换了无数个均无法解决,使用pystun测得nat类型为全锥型,手动指定stun服务器为stun.ekiga.net(pystun使用的,可以正常在我的网络环境中使用),也出现此错误.
换用natter则可以正常打洞,偶发提示nat类型为端口限制型.

Releases: add specific x86_64 binary

Congratulations for this very interesting tool. 👍

However, regarding the binary releases I'm missing the natmap-linux-x86_64 version. The reason is because not all X86 64bit machines have the 32bit library support. For example inside an OpenWRT x86_64 device I can't execute the 32bit binary.

I hope then that you want to add it.
Regards.

[E] Cannot assign requested address

Log output

[E] ... Cannot assign requested address, Please check is another instance exists or wait a minute.
[E] ... Start TCP keep-alive service failed.

Root cause

In the case where the IP addresses and ports of both the client and server remain unchanged, according to the TCP protocol, after an active connection is closed, it enters the TIME_WAIT state and waits for a short period of time. During this period, if a new connection has the same IP addresses and ports on both ends as the old connection, it will trigger an error indicating that the address is already in use.

在客户端和服务器的IP地址、端口固定不变的情况下,根据TCP协议,一个连接主动关闭后,将转入TIME_WAIT状态并等待一小段时间。在此期间,新的连接如果两端的IP地址和端口与旧的连接相同,则会触发地址不可分配的错误。

Additionally, if there is already an existing natmap instance with the same configuration, such as running in the background, it will also result in the error of address allocation failure.

另外,如果已经存在一个相同配置的natmap实例,比如运行在后台。也会导致地址不可分配的错误。

How to workaround

Firstly, you should check if there is an existing running instance with the same configuration, and if so, you need to stop it before proceeding. As for TIME_WAIT, you don't need to take any action as natmap will automatically retry until it connects to the server. If you cannot tolerate TIME_WAIT, a viable approach is to use dynamic port binding, indicated by -b 0. However, this means that the bound port will no longer be fixed.

首先,你应该检查是否存在正在运行的相同配置的实例,如果有需要先停止它。而对于TIME_WAIT,你无需做任何事,natmap会自动重试直到连接上服务器。如果你不能接受发生TIME_WAIT,一个有效的方法是使用动态绑定端口,即 -b 0。但也这意味着绑定端口不再固定。

[Feature Request] iptables / nftables forwarding

Natter 在 v2.0 版本中新增了 iptables / nftables 转发功能,通过 -m 选项可指定转发的方法。

可以考虑在 NATMap 添加同样的功能。

考虑设计此功能的原因是:

  • 省去手动设置防火墙端口转发的步骤。虽然不同发行版的防火墙各不相同,但都以 iptables / nftables 作为后端。由 NATMap 设置可一步到位;
  • BT 类程序打洞需要保持内外部端口号一致,需要 NATMap 动态调整端口转发规则;
  • 虽然通过通知脚本能达到类似目的,但是在 NATMap 退出时会有规则残留。

对于 NATMap :

  • 可以考虑使用 libiptc (iptables (legacy))、libnftl (nftables) 提供的 API,这样比直接调用命令行再解析文本更科学;
  • 可以考虑使用宏隔离,不编译不支持的平台,如 FreeBSD / macOS

Natter 参考:

https://github.com/MikeWang000000/Natter/blob/6e1c66db06e1bb746e55288da1b9fb012bf701b5/natter.py#L479-L637

一些经验:

几乎所有 Linux 发行版包括 OpenWRT、CentOS、Debian、Alpine Linux 还有一些 NAS 专用系统例如威联通的 QTS、QuTS 都可以顺利自动设置转发。这些都通过了测试。

部分系统会存在一些问题,例如群晖 DSM,还有跑在光猫里的嵌入式定制Linux。它们的 iptables 内核模块是修剪过的,不认识规则的 --comment 备注。但是去除后也能正常转发。

所以考虑仅设置最简单的 NAT 转发规则即可。因为经过裁剪的系统也会保留最基本的 NAT 功能,系统本身会用到。


这个坑有点大,先提出来看看可行性。

在Android上执行时发生了错误

x1q:/data/local/tmp $ ./natmap-linux-arm64 -s stunserver.stunprotocol.org -h example.com -b 80
[E] get_addr src/hev-sock.c:40
[E] hev_sock_client_tcp src/hev-sock.c:125
[E] tnsk_run src/hev-tnsk.c:103 Start TCP keep-alive service failed.
[E] get_addr src/hev-sock.c:40
[E] hev_sock_client_tcp src/hev-sock.c:125
[E] tnsk_run src/hev-tnsk.c:103 Start TCP keep-alive service failed.
[E] get_addr src/hev-sock.c:40
[E] hev_sock_client_tcp src/hev-sock.c:125
[E] tnsk_run src/hev-tnsk.c:103 Start TCP keep-alive service failed.
^C

Android9 和13都是这样,adb shell中执行,没有root权限

IP4P版的FRPC无法正常解析IP4P地址

大佬,您好!我在NAT后部署FRPS,在用您编译的FRP-IP4P客户端FRPC连接NAT-1后面的FRPS的时候报错,根据报错提示可以看出FRPC并不能正常解析IP4P为正常的公网IP和端口。这是怎么回事呢?
客户端输出如下,域名已隐藏...

D:\local\frp_ip4p>ipconfig /flushdns

Windows IP 配置

已成功刷新 DNS 解析缓存。

D:\local\frp_ip4p>nslookup natmapfrps.example.com 223.5.5.5
服务器: public1.alidns.com
Address: 223.5.5.5

名称: natmapfrps.example.com
Address: 2001::5e23:2*5f:e557

D:\local\frp_ip4p>frpc.exe tcp -s natmapfrps.example.com:0 -l 55555
2023/09/22 20:43:49 [W] [service.go:133] login to server failed: dial tcp: lookup natmapfrps.example.com: no such host

如果将上述IP4P地址改为穿透的公网IP和端口号,FRPC可以正常连接。
客户端系统:WIN10
FRP版本:frp_0.51.1_windows_amd64 (ip4p)

[E] tnsk_run src/hev-tnsk.c:103 Start TCP keep-alive service failed.

不知是不是我的问题,我无法启动natmap
只有反复提示

izC:system:# natmap -s stunserver.stunprotocol.org -h qq.com -b 2000 -t 127.0.0.1 -p 80 -6 -k 60 -e /lib/natupdate
[E] get_addr src/hev-sock.c:40
[E] hev_sock_client_tcp src/hev-sock.c:125
[E] tnsk_run src/hev-tnsk.c:103 Start TCP keep-alive service failed.

看了一下好像是keepalive server的getaddr步骤,试着ping了一下

# ping qq.com
PING qq.com (112.53.26.232) 56(84) bytes of data.
64 bytes from 112.53.26.232 (112.53.26.232): icmp_seq=1 ttl=54 time=30.0 ms
64 bytes from 112.53.26.232 (112.53.26.232): icmp_seq=2 ttl=54 time=30.3 ms
^C
--- qq.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 29.989/30.127/30.266/0.138 ms

理论上完全没有getaddr的问题

请帮助我,谢谢

public STUN servers

TCP/UDP

Primary address Primary port Alternate port Notes
turn.cloudflare.com 3478 53/udp,80/tcp
stunserver.stunprotocol.org 3478
fwa.lifesizecloud.com 3478
stun.isp.net.au 3478
stun.freeswitch.org 3478
stun.voip.blackberry.com 3478
stun.nextcloud.com 3478
stun.sipnet.com 3478
stun.radiojar.com 3478
stun.sonetel.com 3478
stun.voipgate.com 3478

UDP Only

Primary address Primary port Alternate port Notes
stun.miwifi.com 3478
stun.chat.bilibili.com 3478
stun.cloudflare.com 3478 53
stun.qq.com 3478 Unavaiable

Add more relaxed open socket

Hi,

I know that the technical base of this tool is to open the socket from the bind port. But in some platforms the current implementation fails with messages like:

for UDP:

[E] get_sock src/hev-sock.c:66
[E] hev_sock_client_tcp src/hev-sock.c:130
[E] tnsk_run src/hev-tnsk.c:93

or for TCP:

[E] get_sock src/hev-sock.c:66
[E] hev_sock_client_udp src/hev-sock.c:189
[E] unsk_run src/hev-unsk.c:48

If you check the source code, the trouble is gneerated in these lines:

natmap/src/hev-sock.c

Lines 63 to 66 in 2075302

res |= setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (int));
res |= setsockopt (fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof (int));
if (res < 0) {
LOG (E);

And althrough this error could be created by insufficient privileges to bind to that port, in some platforms the problem is different: for example because the socket can't be opened reusing the address:port. But this could not be a problem if you use the firewall method to redirect the incoming connections. In this case you could bind without the REUSE options and all will continue working. The only requirement in this case is that you can't open other process using the same "bind" port for listening.

Therefore, I suggest that you incorporate these two changes:

  1. Spplit the check for the SO_REUSEADDR from the SO_REUSEPORT option. The reason is to know which call is specifically responsible of the error.
  2. Convert these errors to warnings. Then the user could use the firewall method in platforms without reuse support.

Please, consider it.
Regards.

是不是调用脚本一定时间内没响应就终止本进程?

用的脚本 https://github.com/wits-fe/bittorrent-NAT-hole-punching/blob/main/update-ut.sh
这个脚本达到某种条件后退出,否则驻留一定时间

调用

nohup ./run.sh /root/app/natmap -i pppoe-wan2 -s stunserver.stunprotocol.org -h qq.com -b 3434 -e /root/app/qb/update-qb.sh >>out1 2>>out2 &

其中 ./run.sh(失败后输出exitcode,接着再次运行)

#!/bin/sh

while true; do
   "$@"
   echo "$?" > exitcode
   sleep 10
done

输出的 out1 内容

External IP - 111.94.30.217:55411, bind port 3434, tcp

out2

nohup: ignoring input
Aborted
[E] hev_sock_client_stun src/hev-sock.c:281
[E] task_entry src/hev-stun.c:289
[E] hev_sock_client_stun src/hev-sock.c:281
[E] task_entry src/hev-stun.c:289
[E] hev_sock_client_tcp src/hev-sock.c:167
[E] tnsk_run src/hev-tnsk.c:93
[E] hev_sock_client_tcp src/hev-sock.c:167
[E] tnsk_run src/hev-tnsk.c:93
[E] hev_sock_client_tcp src/hev-sock.c:167
...

可看到第一次运行时,调用的脚本在执行,一段时间后(几分钟?),natmap 退出,输出 Aborted
exitcode 134
./run.sh 循环再次运行 natmap ,这次运行后没调用脚本(输出的out1 内容没更新,所以脚本没执行)
然后追加到 out2 的信息如上面 [E] hev_sock_client_stun src/hev-sock.c:281..

natmap 用不用-d 结果都会退出

问题 natmap 是以什么条件终止的?

How does stun - http work?

I'm having trouble understanding what this app does. If it's able to make internal network ports, like an HTTP server, accessible despite being behind a NAT, that's fantastic! Does it utilize NAT hole punching? And how does it interact with STUN or an HTTP server?

Could you explain how data is transferred using STUN or an HTTP server? My understanding is that modern routers typically don't allow for anything other than solutions like Rathole (https://github.com/rapiz1/rathole), which requires both a server and a client. How is it that this solution doesn't need that?

Thank you

My opinion on “IP4P address”

  1. It should not use public internet address space 2001::/32. It's better to use private address space, for example: fc00::/7.
  2. It should contain some “magic numbers”. For example SLAAC address has FF:FE in it: https://en.wikipedia.org/wiki/IPv6_address#Stateless_address_autoconfiguration

I suggest using DNS SRV record to store port info. It's a internet standard and is more likely to be accepted by client apps. But it requires more code on the client side, and some DNS providers may not support SRV record.
https://en.wikipedia.org/wiki/SRV_record
https://www.cloudflare.com/learning/dns/dns-records/dns-srv-record/

外部的端口不固定理论上是会变的,怎么处理

比如这次打洞成功以后,外部端口是5000
在公网IP发生变化以后,可能那个公网下的5000端口被人占用了,那又会重新分配端口
或者是openwrt在重启之后,端口也可能会发生变化
有没有办法用自动化的程序去获取这个端口号?

我提供一些思路:
用一些支持tcp检测的服务实时检测是不是通的,如果不通就发推送告警到企业微信或者是邮件(139邮箱)免费的短信提醒
然后人工登录openwrt查看新的端口号
可是又迎来新的问题,如果我做的是ftp服务器,那岂不是要在每个客户端上面都更改成新的端口号?
问题是frp都中断了,远程不了客户端,如何修改。。。用zerotier做为备用方案,通过zerotier登录到frp客户端,再修改?
头大。。。

Origin rule无法完成

刚编译的,openwrt系统image,DNS代码执行正常,origin rule代码无法完成,在网站上看无修改。

打洞失败:[E] hev_sock_client_pfwd src/hev-sock.c:316 [W] client_task_entry src/hev-tfwd.c:52

感谢大佬的工具,很好用,已经在家里试验成功了
可是当我尝试亲戚家时出了点问题,TCP端口总是很小且反复报错

root@aml:~# natmap -s stun.stunprotocol.org -h qq.com -b 22 -t localhost -p 81
39.171.157.89 5883 2001::16fb:27ab:9d59 22 tcp
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:52
[E] hev_sock_client_pfwd src/hev-sock.c:316
[W] client_task_entry src/hev-tfwd.c:5

然后就是浏览器:连接已重置

这是我成功的那台的显示:
39.171.171.169 60832 2001::eda0:27ab:aba9 12345 tcp

还有一个问题:当我再次尝试打个洞时,最初成功的那台也不行了,显示:

root@izS:~# natmap -s stun.stunprotocol.org -h qq.com -b 22 -t localhost -p 88
[E] hev_sock_client_tcp src/hev-sock.c:167
[E] tnsk_run src/hev-tnsk.c:93

请大佬不吝解惑,谢谢

启动服务失败

尝试在kali和centos7上运行,发生同样报错:
[E] hev_sock_client_tcp src/hev-sock.c:152 Permission denied
[E] tnsk_run src/hev-tnsk.c:103 Start TCP keep-alive service failed.
看起来是文件夹权限不足,但是我下载的是编译好的单个文件

不知道怎么给wiki提pr,大佬看下能不能更新下wiki上关于ssh的说明

windows上的使用方法

下载并安装nmap: https://nmap.org/download.html#windows

创建powershell文件C:\Users\yourname\proxy.ps1,并写入以下内容:

param(
    [string]$sshhost
)

# 使用 Resolve-DnsName 获取 IPv6 地址
$raw = (Resolve-DnsName -Name $sshhost -Type AAAA).IPAddress

# 将 IPv6 地址的端口和地址部分提取出来
$port = [int]('0x'+($raw -split ':')[2])
$ipab = [int]('0x'+($raw -split ':')[3])
$ipcd = [int]('0x'+($raw -split ':')[4])

# 计算 IP 地址的各个部分
$ipa = $ipab -shr 8
$ipb = $ipab -band 0xFF
$ipc = $ipcd -shr 8
$ipd = $ipcd -band 0xFF

# 使用 ncat 进行连接
ncat "$ipa.$ipb.$ipc.$ipd" $port

再向~/.ssh/config中增加ssh配置:

Host ssh.hev.cc
    ProxyCommand powershell C:\Users\yourname\proxy.ps1 %h

可以实现DDNS自动更新IP+端口吗?

可以实现DDNS自动更新IP+端口吗?
我看到下面这句话,应该是可以使用CF实现的吧?

在NAT-1私网IPv4的宽带上,情况稍微有些不同,不仅打洞后获得的运营商侧公网IPv4是动态变化的,端口同样也是动态变化的。为了同样能够使用固定的域名入口访问服务,推荐使用支持自定义源站端口的CDN服务,比如Cloudflare的免费CDN服务。

CF实现DDNS下面的代码是什么意思呀?从哪里可以获取到?

ZONE=''
RECORD=''
RULE=''
EMAIL=''
AUTH=''
DOMAIN=''

UDP不能绑定服务端口

UDP不能绑定服务端口,只能绑定空闲端口。通过iptables的nat端口映射测试失败。只能通过socat做socket层面的转发。希望后续版本能够解决此问题。

udp模式下断线重连后貌似不能调用更新脚本

使用了很多天,功能正常,非常稳定,但貌似有个小问题:
udp模式下断线重连后貌似不能调用更新脚本
我开了两个转发,一个tcp,一个udp,当pppoe拨号断线重连后,tcp模式下能正常调用更新脚本更新IP,udp模式下不能正常调用脚本。
image
上图两个,上面tcp是断线重连后自动调用的日志,下面udp的是我发现不能自动更新后手动杀掉进程重新运行后更新的日志

请教连接问题

出口路由器是家庭路由器netgear,路由器wan口有公网ip地址,经过NatTypeTester测试是fullcone状态

在内网ubuntu 20系统上使用root用户运行下面的命令

./natmap -s stun.voip.blackberry.com -h qq.com -b 19999 -t 10.0.0.59 -p 80

./natmap -s stun.voip.blackberry.com -h qq.com -b 19999 -t 127.0.0.1 -p 80

输出下面信息,而且1.2.3.4的确也是路由器wan口现在的公网ip地址
1.2.3.4 19999 2001::4e1f:d848:d8ba 19999 tcp 10.0.0.59

并且这台ubuntu 20(10.0.0.59)已经安装nginx并监听80端口,内网其他机器可以直接用浏览器访问http://10.0.0.59:80,能够显示正常的web页面

然后使用外网其他机器的浏览器访问natmap输出的公网地址及端口,浏览器报告连接失败

然后在外网其他ubuntu 20机器使用nc测试natmap输出的公网地址及端口
./nc -vz 1.2.3.4 19999

输出下面的结果
nc: connect to 1.2.3.4 port 19999 (tcp) failed: Connection refused

也试过把10.0.0.59机器放到路由器的DMZ区域,情况一样,都是不能访问

想请教一下这个问题出现在什么地方?

非常感谢!

怎样在FreeBSD上编译?

FreeBSD 13.1-RELEASE使用gmake编译之后运行文件报错:
[E] hev_sock_client_tcp src/hev-sock.c:152
[E] tnsk_run src/hev-tnsk.c:93

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.