158 lines
3.1 KiB
Go
158 lines
3.1 KiB
Go
package geo
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/textproto"
|
|
"proxy-server/client/env"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
Ip string
|
|
Prov string
|
|
City string
|
|
Isp string
|
|
)
|
|
|
|
func Query() (err error) {
|
|
|
|
switch env.Mode {
|
|
case "dev":
|
|
err = dev()
|
|
default:
|
|
err = ipapi()
|
|
}
|
|
return err
|
|
}
|
|
|
|
func dev() (err error) {
|
|
Prov = "河南省"
|
|
City = "郑州市"
|
|
Isp = "电信"
|
|
return nil
|
|
}
|
|
|
|
func cip() (err error) {
|
|
const endpoint = "http://cip.cc"
|
|
|
|
req, err := http.NewRequest("GET", endpoint, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("创建请求失败: %w", err)
|
|
}
|
|
req.Header.Set("User-Agent", "curl/8.9.1")
|
|
req.Header.Set("Accept", "*/*")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("执行请求失败: %w", err)
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
_ = Body.Close()
|
|
}(resp.Body)
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("状态码: %s", resp.Status)
|
|
}
|
|
|
|
reader := textproto.NewReader(bufio.NewReader(resp.Body))
|
|
ipLine, err := reader.ReadLine()
|
|
if err != nil {
|
|
return fmt.Errorf("读取响应失败: %w", err)
|
|
}
|
|
Ip = strings.TrimSpace(strings.Split(ipLine, ":")[1])
|
|
|
|
addrLine, err := reader.ReadLine()
|
|
if err != nil {
|
|
return fmt.Errorf("读取响应失败: %w", err)
|
|
}
|
|
addr := strings.Split(strings.Split(addrLine, ":")[1], " ")
|
|
Prov = strings.TrimSpace(addr[1])
|
|
City = strings.TrimSpace(addr[2])
|
|
|
|
ispLine, err := reader.ReadLine()
|
|
if err != nil {
|
|
return fmt.Errorf("读取响应失败: %w", err)
|
|
}
|
|
Isp = strings.TrimSpace(strings.Split(ispLine, ":")[1])
|
|
|
|
return nil
|
|
}
|
|
|
|
func ipapi() (err error) {
|
|
const endpoint = "http://ip-api.com/json/?fields=regionName,city,as,query&lang=zh-CN"
|
|
|
|
resp, err := http.Get(endpoint)
|
|
if err != nil {
|
|
return fmt.Errorf("执行请求失败: %w", err)
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
_ = Body.Close()
|
|
}(resp.Body)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("状态码: %s", resp.Status)
|
|
}
|
|
|
|
var data struct {
|
|
RegionName string `json:"regionName"`
|
|
City string `json:"city"`
|
|
As string `json:"as"`
|
|
Query string `json:"query"`
|
|
}
|
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
|
if err != nil {
|
|
return fmt.Errorf("解析响应失败: %w", err)
|
|
}
|
|
|
|
Ip = data.Query
|
|
Prov = data.RegionName
|
|
City = data.City
|
|
|
|
var telecom = []string{"AS4134", "AS4812", "AS134419", "AS140292"}
|
|
var unicom = []string{"AS4837", "AS17621", "AS17816"}
|
|
var mobile = []string{
|
|
"AS9808", "AS24444", "AS24445", "AS24547", "AS38019",
|
|
"AS56040", "AS56041", "AS56042", "AS56044", "AS56046", "AS56047",
|
|
"AS132525", "AS134810",
|
|
}
|
|
var foreign = []string{
|
|
"AS9299",
|
|
}
|
|
|
|
for _, telecomAsn := range telecom {
|
|
if strings.HasPrefix(data.As, telecomAsn) {
|
|
Isp = "电信"
|
|
break
|
|
}
|
|
}
|
|
if Isp == "" {
|
|
for _, unicomAsn := range unicom {
|
|
if strings.HasPrefix(data.As, unicomAsn) {
|
|
Isp = "联通"
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if Isp == "" {
|
|
for _, mobileAsn := range mobile {
|
|
if strings.HasPrefix(data.As, mobileAsn) {
|
|
Isp = "移动"
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if Isp == "" {
|
|
for _, foreignAsn := range foreign {
|
|
if strings.HasPrefix(data.As, foreignAsn) {
|
|
Isp = "国外"
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|