Skip to content

Instantly share code, notes, and snippets.

@diyism
Last active September 26, 2022 07:50
Show Gist options
  • Save diyism/e5428bd2465ddeb8448c to your computer and use it in GitHub Desktop.
Save diyism/e5428bd2465ddeb8448c to your computer and use it in GitHub Desktop.
通过usb线把android手机变成无线路由器
#如果没有root的话(gateway没法设, dns也没法设), 则不使用系统的usb tethering而是使用gnirehtet而更简单些:
https://github.com/Genymobile/gnirehtet
sudo iptables -t nat -A OUTPUT -p udp ! -d 127.0.0.1 --dport=53 -j DNAT --to 192.168.0.25:53
./gnirehtet run
(可以设置unbound.conf的verbosity: 3<\n> logfile: unbound.log<\n> log-queries: yes 来观察是否生效, log文件位置可以用lsof -c unbound看到)
用来收android的更新包非常快, 打google voice也是这个gnirehtet+iptables可以用, 用手机上的lantern或wireguard应用都不行
#手机usb线连pc并启用tether
(manual switch on: settings/network & internet/hotspot & tethering/usb tethering
default on: settings/system/developer/networking/select usb configuration/rndis(usb ethernet)
同时adb shell到手机里ifconfig可以看到rndis0 inet addr:192.168.42.129
而pc上ifconfig -a可以看到usb0但没有ip, 需要sudo ifconfig usb0 192.168.42.1 up
)
#pc上编辑/etc/sysctl.conf修改为:
net.ipv4.ip_forward=1
并执行:
sudo sysctl -p
使其生效, 即允许pc转发手机的数据
#pc上创建桥接(需装:sudo apt-get install bridge-utils):
sudo ifconfig eth0 0.0.0.0
sudo ifconfig usb0 0.0.0.0
sudo brctl addbr br0
sudo brctl addif br0 eth0
sudo brctl addif br0 usb0
sudo ifconfig br0 up
sudo dhclient br0 #sudo ifconfig br0 192.168.0.241
#如果要持久化, 在/etc/network/interfaces里加:
auto br0
iface br0 inet static
bridge_ports eth0 usb0
address 192.168.0.241
broadcast 192.168.0.255
netmask 255.255.255.0
gateway 192.168.0.1
#centos上是/etc/sysconfig/network-scripts/ifcfg-eth0(BOOTPROTO="dhcp" or BOOTPROTO="none" IPADDR="192.168.0.241" GATEWAY="192.168.0.1")
#并去掉里面eth0的设置
#如果usb线断开后再接上,usb0可能起不来(ifconfig查看),需要:
sudo service networking restart #centos是: sudo service network restart
#ubuntu 14.04禁用了networking(干掉the two "if...fi" blocks in pre-start and post-stop in /etc/init/networking.conf 来恢复会引起dbus问题, sudo service network-manager restart 也不管/etc/network/interfaces设置), 只能用这个:
sudo ifdown -a && sudo ifup -a
#in phone terminal:
su netcfg rndis0 dhcp
(如果想指定ip:
ifconfig rndis0 192.168.0.242 up #或者ip addr add 192.168.0.242 dev rndis0
如果想指定网关:
ip route del default via 192.168.0.1
ip route add default via 192.168.0.241 dev rndis0
#在没有root的手机上会失败:
ip route add default via 192.168.42.1 dev rndis0
Cannot talk to rtnetlink: Permission denied
如果想设置下http代理(比较粗糙):
iptables -t nat -I OUTPUT -p tcp ! --dport 22:53 -j DNAT --to 192.168.0.241:8081
如果8081是privoxy在监听, 还要设privoxy的accept-intercepted-requests为1
)
#再用netcfg或ip route看获取到的ip地址
#还要装xposed(http://repo.xposed.info/module/de.robv.android.xposed.installer)
#以及Fake Wifi Connection的module(http://repo.xposed.info/module/com.lemonsqueeze.fakewificonnection)
#才能让普通应用把usb0当作wifi
#实现了reverse tether(手机通过电脑上网)
#然后手机上开wifi热点
#手机上打通usb tether接口和wifi ap接口:
iptables -A POSTROUTING -s 192.168.43.1/24 -j MASQUERADE -t nat
iptables -A FORWARD -j ACCEPT -i rndis0 -o wlan0
iptables -A FORWARD -j ACCEPT -i wlan0 -o rndis0
#最后其他手机连上这个wifi热点就能 设网关192.168.43.1, 设ip:192.168.43.2, 设dns: 8.8.4.4 就能上网了
#pc想恢复设置, 删除桥接:
sudo ifconfig eth0 down
sudo ifconfig usb0 down
sudo ifconfig br0 down
sudo brctl delbr br0
sudo ifconfig eth0 up
sudo dhclient eth0
#pc上设置在手机启动usb tether时自动启动usb0并设置ip(因为用/etc/network/interfaces里的allow-hotplug usb0似乎无效):
lsusb #get vendor id and device id after "USB tethering" enabled
sudo subl /etc/udev/rules.d/70-adb.rules
#add one line:
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee4", ACTION=="add", RUN+="/sbin/ifconfig usb0 192.168.42.1 up"
(just after: SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee4", OWNER="malcolm", MODE="0666")
#then:
sudo udevadm control --reload-rules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment