mirror of
https://github.com/xiaoqidun/qqwry.git
synced 2023-06-27 14:38:22 +08:00
feat(添加功能): client作为本地查询工具,server作为服务给其他服务调用
This commit is contained in:
parent
47ef8058d0
commit
88e9c989f5
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,4 @@
|
|||||||
.idea/
|
.idea/
|
||||||
.vscode/
|
.vscode/
|
||||||
.devcontainer/
|
.devcontainer/
|
||||||
qqwry.dat
|
assets/qqwry.dat
|
||||||
|
12
README.md
12
README.md
@ -33,6 +33,18 @@ func main() {
|
|||||||
|
|
||||||
- [https://aite.xyz/share-file/qqwry/qqwry.dat](https://aite.xyz/share-file/qqwry/qqwry.dat)
|
- [https://aite.xyz/share-file/qqwry/qqwry.dat](https://aite.xyz/share-file/qqwry/qqwry.dat)
|
||||||
|
|
||||||
|
# 编译说明
|
||||||
|
|
||||||
|
1. 下载IP数据库并放置于assets目录中。
|
||||||
|
2. client和server需要go1.16的内嵌资源特性。
|
||||||
|
3. 作为库使用,请直接引包,并不需要go1.16+才能编译。
|
||||||
|
|
||||||
|
# 服务接口
|
||||||
|
|
||||||
|
1. 自行根据需要调整server下源码。
|
||||||
|
2. 可以通过-listen参数指定http服务地址。
|
||||||
|
3. curl http://127.0.0.1/ip/1.1.1.1
|
||||||
|
|
||||||
# 特别感谢
|
# 特别感谢
|
||||||
|
|
||||||
- 感谢[纯真IP库](https://www.cz88.net/)一直坚持为大家提供免费IP数据库。
|
- 感谢[纯真IP库](https://www.cz88.net/)一直坚持为大家提供免费IP数据库。
|
||||||
|
6
assets/assets.go
Normal file
6
assets/assets.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package assets
|
||||||
|
|
||||||
|
import _ "embed"
|
||||||
|
|
||||||
|
//go:embed qqwry.dat
|
||||||
|
var QQWryDat []byte
|
25
client/client.go
Normal file
25
client/client.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/xiaoqidun/qqwry"
|
||||||
|
"github.com/xiaoqidun/qqwry/assets"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
qqwry.LoadData(assets.QQWryDat)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
queryIp := os.Args[1]
|
||||||
|
city, area, err := qqwry.QueryIP(queryIp)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("错误:%v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("城市:%s,区域:%s\n", city, area)
|
||||||
|
}
|
@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if err := LoadFile("qqwry.dat"); err != nil {
|
if err := LoadFile("assets/qqwry.dat"); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
47
server/server.go
Normal file
47
server/server.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"github.com/xiaoqidun/qqwry"
|
||||||
|
"github.com/xiaoqidun/qqwry/assets"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resp struct {
|
||||||
|
IP string `json:"ip"`
|
||||||
|
Err string `json:"err"`
|
||||||
|
City string `json:"city"`
|
||||||
|
Area string `json:"area"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
qqwry.LoadData(assets.QQWryDat)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
listen := flag.String("listen", "127.0.0.1:80", "http server listen addr")
|
||||||
|
flag.Parse()
|
||||||
|
http.HandleFunc("/ip/", IpAPI)
|
||||||
|
if err := http.ListenAndServe(*listen, nil); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func IpAPI(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
ip := request.URL.Path[4:]
|
||||||
|
if ip == "" {
|
||||||
|
ip, _, _ = net.SplitHostPort(request.RemoteAddr)
|
||||||
|
}
|
||||||
|
rw := &resp{IP: ip}
|
||||||
|
city, area, err := qqwry.QueryIP(ip)
|
||||||
|
if err != nil {
|
||||||
|
rw.Err = err.Error()
|
||||||
|
} else {
|
||||||
|
rw.City = city
|
||||||
|
rw.Area = area
|
||||||
|
}
|
||||||
|
b, _ := json.MarshalIndent(rw, "", " ")
|
||||||
|
_, _ = writer.Write(b)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user