惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Engineering at Meta
Engineering at Meta
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
博客园_首页
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
博客园 - Franky
Cyberwarzone
Cyberwarzone
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
T
Tailwind CSS Blog
Project Zero
Project Zero
博客园 - 叶小钗
S
SegmentFault 最新的问题
K
Kaspersky official blog
B
Blog
T
The Exploit Database - CXSecurity.com
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
G
Google Developers Blog
Security Latest
Security Latest
T
Tenable Blog
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
H
Help Net Security
T
Threat Research - Cisco Blogs
美团技术团队
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
F
Fortinet All Blogs
NISL@THU
NISL@THU
大猫的无限游戏
大猫的无限游戏
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
D
DataBreaches.Net

日常系列 on 打工人日志

Proxmox VE(PVE) 更新到最新版本 iStoreOS(旁路由)使用openclash实现dns劫持 异常流量分析:图片库服务黑客入侵 openwrt 硬盘扩容 通知:《打工人日报》迁移到独立板块 黑群晖最新安装教程 推荐一下 容器云资源 2010年的天涯神贴聊房价 如何礼貌回绝不合理的需求 github 国内代理访问下载 逆境和成长-2022年终总结 优雅的使用Conda管理python环境 shell功能脚本集合 Cloudflare Zero Trust 内网穿透 headscale 部署使用 羊了个羊小程序 破解通关 RocketMQ k8s部署 4主4从集群 linux服务器 删除空间却未释放 VSCode插件推荐=> Code Runner ant build.xml 编写 记录一次上门打散工 Ant中如何添加第三方jar包依赖 linux 网络测速 网心云挂机教程 | 轻松实现睡后收入~ Proxmox VE 在线扩容磁盘分区 centos7.9 网络配置 RocketMQ 安装和启动 安装 minIO Azure S3网关 logrotate 日志滚动的使用 安装配置 Terraform rsync 文件同步 获取用户浏览器默认语言设置,自动判断跳转不同网站 linux系统开启root权限 163企业邮箱设置教程 2021年第50周记 自建服务器内网穿透 树莓派搭建k3s 优秀英语教材的选择
搭建ip地址检索服务
2024-10-11 · via 日常系列 on 打工人日志

很多时候,我们需要查询一个IP地址,都得通过百度,谷歌,或者其他搜索引擎,非常麻烦。教大家一个使用cloudflare worker搭建一个只属于我们自己的ip地址检索服务。

  1addEventListener('fetch', event => {
  2  event.respondWith(handleRequest(event.request));
  3});
  4
  5/**
  6 * Handle the incoming request and return formatted IP information
  7 * @param {Request} request
  8 */
  9async function handleRequest(request) {
 10  const url = new URL(request.url);
 11  const queryIp = url.searchParams.get('ip');
 12  const clientIp = queryIp || request.headers.get('cf-connecting-ip');
 13  const path = url.pathname;
 14
 15  // 获取 IP 信息
 16  const ipInfo = await getIpInfo(clientIp);
 17
 18  // 根据路径选择返回格式
 19  if (path === '/table') {
 20    const tableFormat = formatAsTable(ipInfo);
 21    return new Response(tableFormat, {
 22      headers: { 'Content-Type': 'text/plain; charset=utf-8' },
 23      status: 200,
 24    });
 25  } else {
 26    return new Response(JSON.stringify(ipInfo), {
 27      headers: { 'Content-Type': 'application/json' },
 28      status: 200,
 29    });
 30  }
 31}
 32
 33/**
 34 * Get IP information and format it according to the required structure
 35 * @param {string} ip
 36 */
 37async function getIpInfo(ip) {
 38  try {
 39    const response = await fetch(`http://ip-api.com/json/${ip}`);
 40    const data = await response.json();
 41
 42    // Format the returned data
 43    return {
 44      ip: data.query || ip,
 45      city: data.city || "None",
 46      province: data.regionName || "None",
 47      country: data.country || "None",
 48      continent: data.continent || "None",
 49      isp: data.isp || "None",
 50      time_zone: data.timezone || "None",
 51      latitude: data.lat || 0,
 52      longitude: data.lon || 0,
 53      postal_code: data.zip || "None",
 54      iso_code: data.countryCode || "None",
 55      notice: "Let it be",
 56      provider: "Powered by Jobcher",
 57      blog: "https://www.jobcher.com",
 58      data_updatetime: new Date().toISOString().slice(0, 10).replace(/-/g, '')
 59    };
 60  } catch (error) {
 61    return {
 62      ip: ip,
 63      city: "None",
 64      province: "None",
 65      country: "None",
 66      continent: "None",
 67      isp: "None",
 68      time_zone: "None",
 69      latitude: 0,
 70      longitude: 0,
 71      postal_code: "None",
 72      iso_code: "None",
 73      notice: "Let it be",
 74      provider: "Powered by Jobcher",
 75      blog: "https://www.jobcher.com",
 76      data_updatetime: new Date().toISOString().slice(0, 10).replace(/-/g, '')
 77    };
 78  }
 79}
 80
 81/**
 82 * Format the IP information as a table-like structure
 83 * @param {Object} info
 84 */
 85function formatAsTable(info) {
 86  const pad = (str, length) => str.toString().padEnd(length, ' ');
 87  const keyWidth = 20;  // Adjusted width for keys
 88  const valueWidth = 60; // Adjusted width for values
 89
 90  const lineBorder = (keyLength, valueLength) =>
 91    `┏${'━'.repeat(keyLength)}${'━'.repeat(valueLength)}┓`;
 92  const lineMiddle = (keyLength, valueLength) =>
 93    `┡${'━'.repeat(keyLength)}${'━'.repeat(valueLength)}┩`;
 94  const lineEnd = (keyLength, valueLength) =>
 95    `└${'─'.repeat(keyLength)}${'─'.repeat(valueLength)}┘`;
 96
 97  const borderTop = lineBorder(keyWidth+2, valueWidth+2);
 98  const borderMiddle = lineMiddle(keyWidth+2, valueWidth+2);
 99  const borderBottom = lineEnd(keyWidth+2, valueWidth+2);
100
101  const lines = [
102    '                                  ip.jobcher.com                               ',
103    borderTop,
104    `┃ ${pad('key', keyWidth)}${pad('value', valueWidth)} ┃`,
105    borderMiddle,
106    `│ ${pad('ip', keyWidth)}${pad(info.ip, valueWidth)} │`,
107    `│ ${pad('city', keyWidth)}${pad(info.city, valueWidth)} │`,
108    `│ ${pad('province', keyWidth)}${pad(info.province, valueWidth)} │`,
109    `│ ${pad('country', keyWidth)}${pad(info.country, valueWidth)} │`,
110    `│ ${pad('continent', keyWidth)}${pad(info.continent, valueWidth)} │`,
111    `│ ${pad('isp', keyWidth)}${pad(info.isp, valueWidth)} │`,
112    `│ ${pad('time_zone', keyWidth)}${pad(info.time_zone, valueWidth)} │`,
113    `│ ${pad('latitude', keyWidth)}${pad(info.latitude, valueWidth)} │`,
114    `│ ${pad('longitude', keyWidth)}${pad(info.longitude, valueWidth)} │`,
115    `│ ${pad('postal_code', keyWidth)}${pad(info.postal_code, valueWidth)} │`,
116    `│ ${pad('iso_code', keyWidth)}${pad(info.iso_code, valueWidth)} │`,
117    `│ ${pad('notice', keyWidth)}${pad(info.notice, valueWidth)} │`,
118    `│ ${pad('', keyWidth)}${pad('©2020-01-01->now', valueWidth)} │`,
119    `│ ${pad('provider', keyWidth)}${pad(info.provider, valueWidth)} │`,
120    `│ ${pad('blog', keyWidth)}${pad(info.blog, valueWidth)} │`,
121    `│ ${pad('data_updatetime', keyWidth)}${pad(info.data_updatetime, valueWidth)} │`,
122    borderBottom,
123    ` `,
124  ];
125
126  return lines.join('\n');
127}
1# 查询本地ip
2curl ip.jobcher.com
1# 查询指定ip
2curl ip.jobcher.com?ip=1.1.1.1
1curl ip.jobcher.com/table
1curl ip.jobcher.com/table?ip=1.1.1.1