headscale 在前面两篇初体验和添加节点 主要是介绍了如何搭建和使用,但是这里有个很大的缺点,比如我在想要两地的网络能够连通,但是两地的网络内都不止一个机器,那想让他们连通就需要把它们都加入这个 headscale 所组成的虚拟局域网,并且每个都是这个虚拟局域网的ip,跟原来的 ip 地址不一样会比较难记忆,所以有没有办法不用这么麻烦呢,当然是有的,那就是 headscale 的路由功能。 原先比如我们有两个设备,在两地,通过headscale我把这两个异地节点都添加进去了,节点 A 的 ip比如是 10.0.0.1,另一个节点 B 是 10.0.0.2,在之前的状态下,我的节点 A 只能通过 10.0.0.2 来连接节点 B,如果节点 A 想要连接节点 B 所在地的局域网内部其他节点,我们就可以通过节点 B 的客户端登录的时候开启路由转发,这样节点 B 就会充当一个路由转发的功能,并且不需要通过10.0.0这个网段来访问,直接可以通过节点 B所在的局域网地址来访问,比如192.168.2.10,具体操作就是把客户端启动方式改成命令行的,主要就是 --advertise-routes, 指明需要转发的网段
1
tailscale up --advertise-routes=192.168.xx.0/24 --accept-dns=false --login-server=http://serverIp:serverPort --unattended
前面的 192.168.xx.0 就是节点 B 所在局域网的网段,然后 login-server 是 headscale 的服务 ip 跟端口,这个本身客户端就会使用,另外这个还有一个前提的,需要开启服务器的端口转发,对于 Windows 其实默认就可以,Linux 的话就需要设置下
1 2 3
$ echo'net.ipv4.ip_forward = 1' | tee /etc/sysctl.d/ipforwarding.conf $ echo'net.ipv6.conf.all.forwarding = 1' | tee -a /etc/sysctl.d/ipforwarding.conf $ sysctl -p /etc/sysctl.d/ipforwarding.conf
if (command == null) thrownewNullPointerException(); /* * Proceed in 3 steps: * * 1. If fewer than corePoolSize threads are running, try to * start a new thread with the given command as its first * task. The call to addWorker atomically checks runState and * workerCount, and so prevents false alarms that would add * threads when it shouldn't, by returning false. * * 2. If a task can be successfully queued, then we still need * to double-check whether we should have added a thread * (because existing ones died since last checking) or that * the pool shut down since entry into this method. So we * recheck state and if necessary roll back the enqueuing if * stopped, or start a new thread if there are none. * * 3. If we cannot queue task, then we try to add a new * thread. If it fails, we know we are shut down or saturated * and so reject the task. */ intc= ctl.get(); if (workerCountOf(c) < corePoolSize) { if (addWorker(command, true)) return; c = ctl.get(); } if (isRunning(c) && workQueue.offer(command)) { intrecheck= ctl.get(); if (! isRunning(recheck) && remove(command)) reject(command); elseif (workerCountOf(recheck) == 0) addWorker(null, false); } elseif (!addWorker(command, false)) reject(command); }