当我们连上某个热点, 会自动弹出登录窗口的专业名称叫做: Captive portal

  原理, 实现方式有三种

  1 : dns 跳转, 在热点上面实现配置, 把所有dns请求返回都配置为:服务器地址 ;服务器有404跳转或者DNS url跳转 , 跳转到的界面即为自动弹出登录界面;

  2 :   http跳转,对所有的http请求返回302或者301或者404跳转, 跳转到的界面即为自动弹出登录界面;

  3 :   ip跳转,既把所有的ip包里的目标地址改为认证服务器,然后在认证服务器上做404跳转

  第三种实现起来比较麻烦, 我们要实现第一种和第二种 Captive portal;

  配置本地服务器

  使用NodeJS搭建一个本地WEB服务器, 服务器端口为默认的80, 无论访问服务器的任意地址 都有正常200返回,app.use函数是精华.., 然后使用node app.js启动服务器:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require("http");
var index = require('./routes/index'); var app = express(); app.set('views', path.join(__dirname, 'views')); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })); app.use('/', index);
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); //该函数是关键 , 如论用户访问任何页面 , 都会重定向到本地index.html文件
app.use(function(req, res, next) {
res.status = ;
res.redirect('/index.html');
}); http.createServer(app).listen();

  

  使用HTTP跳转

  本机环境是kali系统, 外加一个usb网卡, 使用以下代码实现热点:

#!/bin/bash
## quick and dirty AP with hostapd and dnsmasq
## exit properly with ctrl-c echo "Exit this script with Ctrl-C and it will attempt to clean up properly." if [ -z $ ]; then
echo -n "SSID: "
read ssid
else
ssid=$
fi # get wep key
function get_wep_key() {
echo -n "WEP Key [must be exactly 5 or 13 ascii characters]: "
read wep_key
if [[ $wep_key =~ ^[a-zA-Z0-]{}$ ]] ; then
echo "Key accepted"
elif [[ $wep_key =~ ^[a-zA-Z0-]{}$ ]] ; then
echo "Key accepted"
else
echo "WEP key must be exactly 5 or 13 characters"
get_wep_key
fi
} # get mac
function get_mac() {
echo -n "Enter MAC address in the following format AB:CD:EF:12:34:56: "
read new_mac
if [[ $new_mac =~ ^[a-fA-F0-]{}:[a-fA-F0-]{}:[a-fA-F0-]{}:[a-fA-F0-]{}:[a-fA-F0-]{}:[a-fA-F0-]{}$ ]] ; then
macchanger --mac=$new_mac wlan0
else
echo "MAC Address format not correct."
get_mac
fi
} # ask for WEP
echo -n "Do you want WEP enabled? [y/n]: "
read wep
case $wep in
y*)
get_wep_key
;;
*)
;;
esac # ask for MAC change
echo -n "Do you want to change your MAC? [y/n]: "
read changemac
case $changemac in
y*)
echo -n "Custom MAC? [y/n]: "
read random_mac
case $random_mac in
y*)
get_mac
;;
n*)
macchanger -r wlan0
;;
*)
echo "Invalid choice, keeping current MAC address."
;;
esac
;;
n*)
;;
esac # install packages if need be
if [ $(dpkg-query -W -f='${Status}' dnsmasq >/dev/null | grep -c "ok installed") -eq ];
then
apt-get install dnsmasq
fi
if [ $(dpkg-query -W -f='${Status}' hostapd >/dev/null | grep -c "ok installed") -eq ];
then
apt-get install hostapd
fi # trap control c
trap ctrl_c INT function ctrl_c() {
echo "wlan0 managed mode"
iwconfig wlan0 mode managed
echo "downing wlan0"
ifconfig wlan0 down
echo "flushing firewall"
iptables -F
iptables -F -t nat
echo "resetting wlan0 mac"
macchanger -p wlan0
kill - `cat /tmp/dnsmasq.run`
} ## script begins # stop and disable services
service hostapd stop
service dnsmasq stop
pkill - dnsmasq
pkill - hostapd # bring up wlan0
nmcli radio wifi off
rfkill unblock wlan
iwconfig wlan0 mode monitor
ifconfig wlan0 10.0.0.1/ up # forwarding and nat
sysctl -w net.ipv4.conf.all.route_localnet=
iptables -t nat -I PREROUTING -p tcp --dport -j DNAT --to 10.0.0.1: # dns masq conf
cat > /tmp/dnsmasq.conf <<!
bind-interfaces
interface=wlan0
dhcp-range=10.0.0.2,10.0.0.254
! # hostapd conf
cat > /tmp/hostapd.conf<<!
interface=wlan0
driver=nl80211
ssid=${ssid}
hw_mode=g
channel=
! # if WEP key, add to hostapd conf
if [[ -n $wep_key ]]; then echo -e "wep_default_key=0\nwep_key0=\"${wep_key}\"" >> /tmp/hostapd.conf; fi # run dnsmasq and hostapd
dnsmasq --pid-file=/tmp/dnsmasq.run -C /tmp/dnsmasq.conf
hostapd /tmp/hostapd.conf

  以上代码中:

    sysctl -w net.ipv4.conf.all.route_localnet=1
    iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 10.0.0.1:80

  是本例子的精华所在, 第一句的意思是指所有的ipv4请求全部重定向到本地, 第二句的意思为所有准备访问本地80端口的机器,全部指向到10.0.0.1的80端口, 而我们本地的80端口为我们部署的服务器, 这样即可实现 Captive portal

  使用DNS跳转

  在本地网关搭建DNS服务器, 局域网内部的所有DNS请求全部返回10.0.0.1

  sh文件代码如下:

#!/bin/bash
## quick and dirty AP with hostapd and dnsmasq
## exit properly with ctrl-c echo "Exit this script with Ctrl-C and it will attempt to clean up properly." if [ -z $ ]; then
echo -n "SSID: "
read ssid
else
ssid=$
fi # get wep key
function get_wep_key() {
echo -n "WEP Key [must be exactly 5 or 13 ascii characters]: "
read wep_key
if [[ $wep_key =~ ^[a-zA-Z0-]{}$ ]] ; then
echo "Key accepted"
elif [[ $wep_key =~ ^[a-zA-Z0-]{}$ ]] ; then
echo "Key accepted"
else
echo "WEP key must be exactly 5 or 13 characters"
get_wep_key
fi
} # get mac
function get_mac() {
echo -n "Enter MAC address in the following format AB:CD:EF:12:34:56: "
read new_mac
if [[ $new_mac =~ ^[a-fA-F0-]{}:[a-fA-F0-]{}:[a-fA-F0-]{}:[a-fA-F0-]{}:[a-fA-F0-]{}:[a-fA-F0-]{}$ ]] ; then
macchanger --mac=$new_mac wlan0
else
echo "MAC Address format not correct."
get_mac
fi
} # ask for WEP
echo -n "Do you want WEP enabled? [y/n]: "
read wep
case $wep in
y*)
get_wep_key
;;
*)
;;
esac # ask for MAC change
echo -n "Do you want to change your MAC? [y/n]: "
read changemac
case $changemac in
y*)
echo -n "Custom MAC? [y/n]: "
read random_mac
case $random_mac in
y*)
get_mac
;;
n*)
macchanger -r wlan0
;;
*)
echo "Invalid choice, keeping current MAC address."
;;
esac
;;
n*)
;;
esac # install packages if need be
if [ $(dpkg-query -W -f='${Status}' dnsmasq >/dev/null | grep -c "ok installed") -eq ];
then
apt-get install dnsmasq
fi
if [ $(dpkg-query -W -f='${Status}' hostapd >/dev/null | grep -c "ok installed") -eq ];
then
apt-get install hostapd
fi # trap control c
trap ctrl_c INT function ctrl_c() {
echo "wlan0 managed mode"
iwconfig wlan0 mode managed
echo "downing wlan0"
ifconfig wlan0 down
echo "flushing firewall"
iptables -F
iptables -F -t nat
echo "resetting wlan0 mac"
macchanger -p wlan0
kill - `cat /tmp/dnsmasq.run`
} ## script begins # stop and disable services
service hostapd stop
service dnsmasq stop
pkill - dnsmasq
pkill - hostapd # bring up wlan0
nmcli radio wifi off
rfkill unblock wlan
iwconfig wlan0 mode monitor
ifconfig wlan0 10.0.0.1/ up #dhcp
iptables --policy INPUT ACCEPT
iptables --policy FORWARD ACCEPT
iptables --policy OUTPUT ACCEPT
iptables -F
iptables -t nat -F
iptables -t nat -A PREROUTING -i wlan0 -p udp --dport -j DNAT --to 10.0.0.1 # dns masq conf
cat > /tmp/dnsmasq.conf <<!
bind-interfaces
interface=wlan0
address=/#/10.0.0.1
dhcp-range=10.0.0.2,10.0.0.254
dhcp-option=,10.0.0.1 #DNS
dhcp-option=,10.0.0.1 #Gateway
dhcp-option=,"http://wpad.example.com/wpad.dat\n" #WPAD
dhcp-authoritative
! # hostapd conf
cat > /tmp/hostapd.conf<<!
interface=wlan0
driver=nl80211
ssid=${ssid}
hw_mode=g
channel=
! # if WEP key, add to hostapd conf
if [[ -n $wep_key ]]; then echo -e "wep_default_key=0\nwep_key0=\"${wep_key}\"" >> /tmp/hostapd.conf; fi # run dnsmasq and hostapd
dnsmasq --pid-file=/tmp/dnsmasq.run -C /tmp/dnsmasq.conf
hostapd /tmp/hostapd.conf

  其中以下代码尤为重要:

  bind-interfaces
  interface=wlan0
  address=/#/10.0.0.1  《==这个配置说明为所有dns请求都返回10.0.0.1 
  dhcp-range=10.0.0.2,10.0.0.254  《==客户机器IP池
  dhcp-option=6,10.0.0.1 #DNS   《==本地DNS服务器
  dhcp-option=3,10.0.0.1 #Gateway 《==本地网关
  dhcp-option=252,"http://wpad.example.com/wpad.dat\n" #WPAD
  dhcp-authoritative

  参考

  dnsmasq参考API :https://wiki.archlinux.org/index.php/dnsmasq

  Captive portal是怎样强制弹出窗口的呢? : https://www.zhihu.com/question/38843766

作者: NONO
出处:http://www.cnblogs.com/diligenceday/

企业网站:http://www.idrwl.com/
开源博客:http://www.github.com/sqqihao
QQ:287101329

微信:18101055830

厦门点燃未来网络科技有限公司, 是厦门最好的微信应用, 小程序, 微信网站, 公众号开发公司

连上Wi-Fi 热点自动弹窗的实现方法的更多相关文章

  1. DedecmsV5.7本地上传缩略图无法自动添加水印的解决方法

    问题:dedecms后台 系统->图片水印设置 图片水印设置有开启了,但是本地上传缩略图无法自动添加水印 网上有很多资料,所以记录一下 1.打开dede(实际项目后台文件夹)/archives_ ...

  2. Dedecms本地上传缩略图无法自动添加水印的解决方法

    客户遇到一个问题,DEDECMS(V5.7)后台添加文档时,本地上传缩略图无法自动添加水印(系统设置里的图片水印设置没有问题),找了半天,终于找到了解决方法,留个记号: 打开dede/archives ...

  3. 手机连接wifi自动弹窗的原理及其实现方案

    一.手机连上wifi后会自动弹窗的原理 生活中,有很多需要认证的路由器,手机连接wifi热点后会自动弹出一个网页,让用户输入账号和密码,比如星巴克,肯地基,麦当劳,甚至是火车站和机场的候车室.其实这是 ...

  4. 手机连上wifi热点后自动弹窗的功能

    使用buildroot编译bind DNS服务器 用buildroot来制作文件系统很方便,编译出来的文件系统是直接可用的,不用添加脚本等麻烦的工作,很多的库和app都可以直接添加到文件系统里边,如常 ...

  5. 艺萌文件上传下载及自动更新系统(基于networkComms开源TCP通信框架)

    1.艺萌文件上传下载及自动更新系统,基于Winform技术,采用CS架构,开发工具为vs2010,.net2.0版本(可以很容易升级为3.5和4.0版本)开发语言c#. 本系统主要帮助客户学习基于TC ...

  6. 艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输)(一)

    艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输) 该系统基于开源的networkComms通讯框架,此通讯框架以前是收费的,目前已经免费并开元,作者是英国的,开发时间5年多,框架很稳定. 项 ...

  7. winrar在右键菜单上加上:打包自动加上日期时间标签【图文教程】 - imsoft.cnblogs

    说明:  注册表HKEY_CURRENT_USER\Software\WinRAR\Profiles\0找到GenerateArcName修改0为1,修改GenerateMask为你想要的日期式(默认 ...

  8. 在 uniGUI 中实现自动弹窗后延迟几秒关闭 — Toast 功能

    在 uniGUI 中实现自动弹窗后延迟几秒关闭 — Toast 功能. uniGUI 的客户端使用 EXTJS 6 ,本就有 Toast 功能. 但UniGui 官方没有相应的控件,我们如何使用 EX ...

  9. 在windows 上自动重启 tomcat 的方法

    在windows 上自动重启 tomcat 的方法 实现思路: Windows 上监控tomcat 进程并且自动重启的脚本 一类是 定时重启 tomcat 一类是 监控并重启 写一个守护tomcat进 ...

随机推荐

  1. 基于Json序列化和反序列化通用的封装

    1. 最近项目已经上线了 ,闲暇了几天 想将JSON的序列化以及反序列化进行重新的封装一下本人定义为JSONHelp,虽然Microsoft 已经做的很好了.但是我想封装一套为自己开发的项目使用.方便 ...

  2. js中嵌入jsp(html)代码的双引号转换问题--事件没反应

    下面是一段今天遇到问题的代码,select中写了onchange事件 ,在没有加转义的情况下,F12解析的代码是错乱的,双引号与内容中写的不一致,还会有空格出现,经过一段时间的摸索,发现在出错的地方加 ...

  3. dfs.datanode.max.transfer.threads

    An HDFS DataNode has an upper bound on the number of files that it will serve at any one time: <p ...

  4. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  5. SecureFX 乱码问题

    英文平时连终端的都是用SecureCRT, 今天试了一些SecureFX, 结果乱码了, 把redhat下的中文桌面标题显示乱码, 然后参考了一下别的前辈, 完美解决, 下面是解决办法: 1.找到配置 ...

  6. 移动端JS事件、移动端框架

    一.移动端的操作方式和PC端是不同的,移动端主要是用手指操作,所以有特殊的touch事件,touch事件包括如下几个事件: 1.手指放到屏幕上时触发   touchstart 2.手指放在屏幕上滑动式 ...

  7. Java 9 揭秘(15. 增强的弃用注解)

    Tips 做一个终身学习的人. 主要介绍以下内容: 如何弃用API @deprecate Javadoc标签和@Deprecation注解在弃用的API中的角色 用于生成弃用警告的详细规则 在JDK ...

  8. Kibana5 数据探索使用(Discover功能)

    认识Kibana Kibana 是一个为 Logstash 和 ElasticSearch 提供的日志分析的 Web 接口.可使用它对日志进行高效的搜索.可视化.分析等各种操作.Kibana的使用场景 ...

  9. Maven “Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:create...”问题总结

    今天学习Maven的过程中,一直遇到一个问题:用maven指令构建新项目时,一直报错,用的 Maven 3.2 , JDK 6. 构建的命令: 错误信息: 解决方案: 在StackOverFlow上找 ...

  10. phpMyAdmin安装部署

    phpMyAdmin 是一个用PHP编写的软件工具,可以通过web方式控制和操作MySQL数据库.通过phpMyAdmin 可以完全对数据库进行操作,例如建立.复制和删除数据等等.如果使用合适的工具, ...