From b6d44f2c96b58ef59d4d415f2f26cdc984ee7e82 Mon Sep 17 00:00:00 2001
From: zu1k
Date: Fri, 17 Jul 2020 09:05:25 +0800
Subject: [PATCH] init
---
.github/workflows/go.yml | 44 ++++++++
.gitignore | 27 +++++
Dockerfile | 14 +++
LICENSE | 21 ++++
Makefile | 45 ++++++++
README.md | 33 ++++++
cmd/parse.go | 32 ++++++
cmd/root.go | 42 +++++++
constant/version.go | 8 ++
go.mod | 12 ++
go.sum | 163 +++++++++++++++++++++++++++
internal/app/parse.go | 49 +++++++++
internal/ipdb/db.go | 5 +
internal/ipdb/ipdb.go | 8 ++
main.go | 28 +++++
pkg/geoip/geoip.go | 37 +++++++
pkg/qqwry/qqwry.go | 231 +++++++++++++++++++++++++++++++++++++++
pkg/qqwry/qqwry_test.go | 13 +++
pkg/qqwry/update.go | 57 ++++++++++
19 files changed, 869 insertions(+)
create mode 100644 .github/workflows/go.yml
create mode 100644 .gitignore
create mode 100644 Dockerfile
create mode 100644 LICENSE
create mode 100644 Makefile
create mode 100644 README.md
create mode 100644 cmd/parse.go
create mode 100644 cmd/root.go
create mode 100644 constant/version.go
create mode 100644 go.mod
create mode 100644 go.sum
create mode 100644 internal/app/parse.go
create mode 100644 internal/ipdb/db.go
create mode 100644 internal/ipdb/ipdb.go
create mode 100644 main.go
create mode 100644 pkg/geoip/geoip.go
create mode 100644 pkg/qqwry/qqwry.go
create mode 100644 pkg/qqwry/qqwry_test.go
create mode 100644 pkg/qqwry/update.go
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
new file mode 100644
index 0000000..18ae7e9
--- /dev/null
+++ b/.github/workflows/go.yml
@@ -0,0 +1,44 @@
+name: Go
+on: [push, pull_request]
+jobs:
+
+ build:
+ name: Build
+ runs-on: ubuntu-latest
+ steps:
+ - name: Setup Go
+ uses: actions/setup-go@v1
+ with:
+ go-version: 1.14
+
+ - name: Check out code into the Go module directory
+ uses: actions/checkout@v1
+
+ - name: Cache go module
+ uses: actions/cache@v1
+ with:
+ path: ~/go/pkg/mod
+ key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
+ restore-keys: |
+ ${{ runner.os }}-go-
+
+ - name: Build
+ env:
+ NAME: nali
+ BINDIR: bin
+ run: make -j releases
+
+ - uses: actions/upload-artifact@v1
+ with:
+ name: build
+ path: bin
+
+ - name: Upload Release
+ uses: softprops/action-gh-release@v1
+ if: startsWith(github.ref, 'refs/tags/')
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ with:
+ files: bin/*
+ draft: true
+ prerelease: true
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..614b9af
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,27 @@
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+bin/*
+
+# Test binary, build with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+# dep
+vendor
+
+# GoLand
+.idea/*
+
+# macOS file
+.DS_Store
+
+*.exe
+
+# ku
+db/*
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..d79de0d
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,14 @@
+FROM golang:alpine as builder
+
+RUN apk add --no-cache make git
+WORKDIR /nali-src
+COPY . /nali-src
+RUN go mod download && \
+ make linux-amd64 && \
+ mv ./bin/nali-linux-amd64 /nali
+
+FROM alpine:latest
+
+RUN apk add --no-cache ca-certificates
+COPY --from=builder /nali /
+ENTRYPOINT ["/nali"]
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d4b72e6
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright © 2020 zu1k
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..e1127a0
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,45 @@
+NAME=nali
+BINDIR=bin
+VERSION=$(hell git describe --tags || echo "unknown version")
+BUILDTIME=$(hell date -u)
+GOBUILD=CGO_ENABLED=0 go build -ldflags '-X "github.com/zu1k/nali/constant.Version=$(VERSION)" \
+ -X "github.com/zu1k/nali/constant.BuildTime=$(BUILDTIME)" \
+ -w -s' -trimpath
+
+PLATFORM_LIST = \
+ darwin-amd64 \
+ linux-amd64 \
+ freebsd-amd64
+
+WINDOWS_ARCH_LIST = \
+ windows-amd64
+
+all: linux-amd64 darwin-amd64 windows-amd64 # Most used
+
+darwin-amd64:
+ GOARCH=amd64 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
+
+linux-amd64:
+ GOARCH=amd64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
+
+freebsd-amd64:
+ GOARCH=amd64 GOOS=freebsd $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
+
+windows-amd64:
+ GOARCH=amd64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
+
+gz_releases=$(addsuffix .gz, $(PLATFORM_LIST))
+zip_releases=$(addsuffix .zip, $(WINDOWS_ARCH_LIST))
+
+$(gz_releases): %.gz : %
+ chmod +x $(BINDIR)/$(NAME)-$(basename $@)
+ gzip -f -S -$(VERSION).gz $(BINDIR)/$(NAME)-$(basename $@)
+
+$(zip_releases): %.zip : %
+ zip -m -j $(BINDIR)/$(NAME)-$(basename $@)-$(VERSION).zip $(BINDIR)/$(NAME)-$(basename $@).exe
+
+all-arch: $(PLATFORM_LIST) $(WINDOWS_ARCH_LIST)
+
+releases: $(gz_releases) $(zip_releases)
+clean:
+ rm $(BINDIR)/*
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c4793b6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,33 @@
+
+
nali
+
+
+Something
+
+
+
+
+
+
+
+
+
+
+
+
+
+## Install
+
+he Requires Go >= 1.14. You can build it from source:
+
+```sh
+$ go get -u -v github.com/zu1k/nali
+```
+
+Pre-built binaries are available here: [release](https://github.com/zu1k/nali/releases)
+
+## Usage
+
+## License
+
+[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fzu1k%2Fnali.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fzu1k%2Fhe?ref=badge_large)
diff --git a/cmd/parse.go b/cmd/parse.go
new file mode 100644
index 0000000..70df3cb
--- /dev/null
+++ b/cmd/parse.go
@@ -0,0 +1,32 @@
+package cmd
+
+import (
+ "github.com/zu1k/nali/internal/app"
+
+ "github.com/spf13/cobra"
+)
+
+// parseCmd represents the parse command
+var parseCmd = &cobra.Command{
+ Use: "parse",
+ Short: "Query IP information",
+ Long: `Query IP information.`,
+ Args: cobra.MinimumNArgs(1),
+ Run: func(cmd *cobra.Command, args []string) {
+ app.ParseIPs(args)
+ },
+}
+
+func init() {
+ rootCmd.AddCommand(parseCmd)
+
+ // Here you will define your flags and configuration settings.
+
+ // Cobra supports Persistent Flags which will work for this command
+ // and all subcommands, e.g.:
+ // parseCmd.PersistentFlags().String("foo", "", "A help for foo")
+
+ // Cobra supports local flags which will only run when this command
+ // is called directly, e.g.:
+ // parseCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+}
diff --git a/cmd/root.go b/cmd/root.go
new file mode 100644
index 0000000..52db91a
--- /dev/null
+++ b/cmd/root.go
@@ -0,0 +1,42 @@
+package cmd
+
+import (
+ "fmt"
+
+ "github.com/zu1k/nali/internal/app"
+
+ "github.com/spf13/cobra"
+)
+
+// rootCmd represents the base command when called without any subcommands
+var rootCmd = &cobra.Command{
+ Use: "nali",
+ Short: "",
+ Long: ``,
+ Args: cobra.MinimumNArgs(1),
+ Run: func(cmd *cobra.Command, args []string) {
+ if len(args) == 0 {
+ fmt.Println("Usage: balabala")
+ return
+ }
+ app.ParseIPs(args)
+ },
+}
+
+// Execute adds all child commands to the root command and sets flags appropriately.
+// This is called by main.main(). It only needs to happen once to the rootCmd.
+func Execute() {
+ if err := rootCmd.Execute(); err != nil {
+ panic(err)
+ }
+}
+
+func init() {
+ // Here you will define your flags and configuration settings.
+ // Cobra supports persistent flags, which, if defined here,
+ // will be global for your application.
+
+ // Cobra also supports local flags, which will only run
+ // when this action is called directly.
+ rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+}
diff --git a/constant/version.go b/constant/version.go
new file mode 100644
index 0000000..bfc944e
--- /dev/null
+++ b/constant/version.go
@@ -0,0 +1,8 @@
+package constant
+
+const Name = "Nali"
+
+var (
+ Version = "unknown version"
+ BuildTime = "unknown time"
+)
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..eb95aef
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,12 @@
+module github.com/zu1k/nali
+
+go 1.14
+
+require (
+ github.com/oschwald/geoip2-golang v1.4.0
+ github.com/spf13/cobra v1.0.0
+ github.com/spf13/pflag v1.0.5 // indirect
+ golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
+ golang.org/x/text v0.3.3
+ gopkg.in/yaml.v2 v2.3.0 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..3012cc5
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,163 @@
+cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
+github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
+github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
+github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
+github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
+github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
+github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
+github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
+github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
+github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
+github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
+github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
+github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
+github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
+github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
+github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
+github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
+github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
+github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
+github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
+github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
+github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
+github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
+github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
+github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
+github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
+github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
+github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
+github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
+github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
+github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
+github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
+github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
+github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
+github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
+github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
+github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
+github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
+github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
+github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
+github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
+github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
+github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
+github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
+github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
+github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
+github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
+github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
+github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
+github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
+github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
+github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
+github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
+github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
+github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
+github.com/oschwald/geoip2-golang v1.4.0 h1:5RlrjCgRyIGDz/mBmPfnAF4h8k0IAcRv9PvrpOfz+Ug=
+github.com/oschwald/geoip2-golang v1.4.0/go.mod h1:8QwxJvRImBH+Zl6Aa6MaIcs5YdlZSTKtzmPGzQqi9ng=
+github.com/oschwald/maxminddb-golang v1.6.0 h1:KAJSjdHQ8Kv45nFIbtoLGrGWqHFajOIm7skTyz/+Dls=
+github.com/oschwald/maxminddb-golang v1.6.0/go.mod h1:DUJFucBg2cvqx42YmDa/+xHvb0elJtOm3o4aFQ/nb/w=
+github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
+github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
+github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
+github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
+github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
+github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
+github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
+github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
+github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
+github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
+github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
+github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
+github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
+github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
+github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
+github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
+github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
+github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
+github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
+github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
+github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
+github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
+github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
+github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
+github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
+github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
+github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
+github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
+github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
+github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
+github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
+github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
+github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
+go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
+go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
+go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
+go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
+golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
+golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
+golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
+golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
+golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
+golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
+golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
+google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
+google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
+google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
+google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
+gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
+gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
+gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
+gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
+gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
diff --git a/internal/app/parse.go b/internal/app/parse.go
new file mode 100644
index 0000000..2effb47
--- /dev/null
+++ b/internal/app/parse.go
@@ -0,0 +1,49 @@
+package app
+
+import (
+ "fmt"
+
+ "github.com/zu1k/nali/internal/ipdb"
+
+ geoip2 "github.com/zu1k/nali/pkg/geoip"
+ "github.com/zu1k/nali/pkg/qqwry"
+)
+
+var (
+ db ipdb.IPDB
+ qqip qqwry.QQwry
+ geoip geoip2.GeoIP
+)
+
+func init() {
+ qqip = qqwry.NewQQwry("db/qqwry.dat")
+ geoip = geoip2.NewGeoIP("db/GeoLite2-City.mmdb")
+ db = qqip
+}
+
+func SetDB(dbName ipdb.IPDBType) {
+ switch dbName {
+ case ipdb.GEOIP2:
+ db = geoip
+ case ipdb.QQIP:
+ db = qqip
+ }
+}
+
+func ParseIPs(ips []string) {
+ for _, ip := range ips {
+ ParseIP(ip)
+ }
+}
+
+func ParseIP(ip string) {
+ result := db.Find(ip)
+ fmt.Println(formatResult(ip, result))
+}
+
+func formatResult(ip string, result string) string {
+ if result == "" {
+ result = "未找到"
+ }
+ return fmt.Sprintf("%s [%s]", ip, result)
+}
diff --git a/internal/ipdb/db.go b/internal/ipdb/db.go
new file mode 100644
index 0000000..c34f9d9
--- /dev/null
+++ b/internal/ipdb/db.go
@@ -0,0 +1,5 @@
+package ipdb
+
+type IPDB interface {
+ Find(ip string) string
+}
diff --git a/internal/ipdb/ipdb.go b/internal/ipdb/ipdb.go
new file mode 100644
index 0000000..14caba5
--- /dev/null
+++ b/internal/ipdb/ipdb.go
@@ -0,0 +1,8 @@
+package ipdb
+
+type IPDBType int
+
+const (
+ GEOIP2 = iota
+ QQIP
+)
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..bc656a0
--- /dev/null
+++ b/main.go
@@ -0,0 +1,28 @@
+/*
+Copyright © 2020 zu1k
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+package main
+
+import "github.com/zu1k/nali/cmd"
+
+func main() {
+ cmd.Execute()
+}
diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go
new file mode 100644
index 0000000..baabc57
--- /dev/null
+++ b/pkg/geoip/geoip.go
@@ -0,0 +1,37 @@
+package geoip
+
+import (
+ "fmt"
+ "log"
+ "net"
+
+ "github.com/oschwald/geoip2-golang"
+)
+
+type GeoIP struct {
+ db *geoip2.Reader
+}
+
+func NewGeoIP(filePath string) GeoIP {
+ db, err := geoip2.Open(filePath)
+ if err != nil {
+ log.Fatal(err)
+ }
+ return GeoIP{db: db}
+}
+
+func (g GeoIP) Find(ip string) string {
+ // If you are using strings that may be invalid, check that ip is not nil
+ ipData := net.ParseIP(ip)
+ record, err := g.db.City(ipData)
+ if err != nil {
+ log.Fatal(err)
+ }
+ country := record.Country.Names["zh-CN"]
+ city := record.City.Names["zh-CN"]
+ if city == "" {
+ return country
+ } else {
+ return fmt.Sprintf("%s %s", country, city)
+ }
+}
diff --git a/pkg/qqwry/qqwry.go b/pkg/qqwry/qqwry.go
new file mode 100644
index 0000000..6bb04c2
--- /dev/null
+++ b/pkg/qqwry/qqwry.go
@@ -0,0 +1,231 @@
+package qqwry
+
+import (
+ "encoding/binary"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "net"
+ "os"
+ "strings"
+
+ "golang.org/x/text/encoding/simplifiedchinese"
+)
+
+type FileInfo struct {
+ Data []byte
+ FilePath string
+ FileBase *os.File
+ IPNum int64
+}
+
+type QQwry struct {
+ data *FileInfo
+ offset int64
+}
+
+const (
+ IndexLen = 7
+ RedirectMode1 = 0x01
+ RedirectMode2 = 0x02
+)
+
+func NewQQwry(filePath string) QQwry {
+ var tmpData []byte
+ var fileInfo FileInfo
+
+ // 判断文件是否存在
+ _, err := os.Stat(filePath)
+ if err != nil && os.IsNotExist(err) {
+ log.Println("文件不存在,尝试从网络获取最新纯真 IP 库")
+ tmpData, err = GetOnline()
+ if err != nil {
+ panic(err)
+ } else {
+ if err := ioutil.WriteFile(filePath, tmpData, 0644); err == nil {
+ log.Printf("已将最新的纯真 IP 库保存到本地 %s ", filePath)
+ }
+ }
+ } else {
+ // 打开文件句柄
+ fileInfo.FileBase, err = os.OpenFile(filePath, os.O_RDONLY, 0400)
+ if err != nil {
+ panic(err)
+ }
+ defer fileInfo.FileBase.Close()
+
+ tmpData, err = ioutil.ReadAll(fileInfo.FileBase)
+ if err != nil {
+ panic(err)
+ }
+ }
+
+ fileInfo.Data = tmpData
+
+ buf := fileInfo.Data[0:8]
+ start := binary.LittleEndian.Uint32(buf[:4])
+ end := binary.LittleEndian.Uint32(buf[4:])
+
+ fileInfo.IPNum = int64((end-start)/IndexLen + 1)
+
+ return QQwry{
+ data: &fileInfo,
+ }
+}
+
+// setOffset 设置偏移量
+func (q *QQwry) setOffset(offset int64) {
+ q.offset = offset
+}
+
+// Find ip地址查询对应归属地信息
+func (q QQwry) Find(ip string) (res string) {
+ if strings.Count(ip, ".") != 3 {
+ return
+ }
+ offset := q.searchIndex(binary.BigEndian.Uint32(net.ParseIP(ip).To4()))
+ if offset <= 0 {
+ return
+ }
+
+ var gbkCountry []byte
+ var gbkArea []byte
+
+ mode := q.readMode(offset + 4)
+ if mode == RedirectMode1 {
+ countryOffset := q.readUInt24()
+ mode = q.readMode(countryOffset)
+ if mode == RedirectMode2 {
+ c := q.readUInt24()
+ gbkCountry = q.readString(c)
+ countryOffset += 4
+ } else {
+ gbkCountry = q.readString(countryOffset)
+ countryOffset += uint32(len(gbkCountry) + 1)
+ }
+ gbkArea = q.readArea(countryOffset)
+ } else if mode == RedirectMode2 {
+ countryOffset := q.readUInt24()
+ gbkCountry = q.readString(countryOffset)
+ gbkArea = q.readArea(offset + 8)
+ } else {
+ gbkCountry = q.readString(offset + 4)
+ gbkArea = q.readArea(offset + uint32(5+len(gbkCountry)))
+ }
+
+ enc := simplifiedchinese.GBK.NewDecoder()
+ country, _ := enc.String(string(gbkCountry))
+ area, _ := enc.String(string(gbkArea))
+
+ return fmt.Sprintf("%s %s", country, area)
+}
+
+// readMode 获取偏移值类型
+func (q *QQwry) readMode(offset uint32) byte {
+ mode := q.readData(1, int64(offset))
+ return mode[0]
+}
+
+// readArea 读取区域
+func (q *QQwry) readArea(offset uint32) []byte {
+ mode := q.readMode(offset)
+ if mode == RedirectMode1 || mode == RedirectMode2 {
+ areaOffset := q.readUInt24()
+ if areaOffset == 0 {
+ return []byte("")
+ }
+ return q.readString(areaOffset)
+ }
+ return q.readString(offset)
+}
+
+// readString 获取字符串
+func (q *QQwry) readString(offset uint32) []byte {
+ q.setOffset(int64(offset))
+ data := make([]byte, 0, 30)
+ buf := make([]byte, 1)
+ for {
+ buf = q.readData(1)
+ if buf[0] == 0 {
+ break
+ }
+ data = append(data, buf[0])
+ }
+ return data
+}
+
+// searchIndex 查找索引位置
+func (q *QQwry) searchIndex(ip uint32) uint32 {
+ header := q.readData(8, 0)
+
+ start := binary.LittleEndian.Uint32(header[:4])
+ end := binary.LittleEndian.Uint32(header[4:])
+
+ buf := make([]byte, IndexLen)
+ mid := uint32(0)
+ _ip := uint32(0)
+
+ for {
+ mid = q.getMiddleOffset(start, end)
+ buf = q.readData(IndexLen, int64(mid))
+ _ip = binary.LittleEndian.Uint32(buf[:4])
+
+ if end-start == IndexLen {
+ offset := byteToUInt32(buf[4:])
+ buf = q.readData(IndexLen)
+ if ip < binary.LittleEndian.Uint32(buf[:4]) {
+ return offset
+ }
+ return 0
+ }
+
+ // 找到的比较大,向前移
+ if _ip > ip {
+ end = mid
+ } else if _ip < ip { // 找到的比较小,向后移
+ start = mid
+ } else if _ip == ip {
+ return byteToUInt32(buf[4:])
+ }
+ }
+}
+
+// readUInt24
+func (q *QQwry) readUInt24() uint32 {
+ buf := q.readData(3)
+ return byteToUInt32(buf)
+}
+
+// getMiddleOffset
+func (q *QQwry) getMiddleOffset(start uint32, end uint32) uint32 {
+ records := ((end - start) / IndexLen) >> 1
+ return start + records*IndexLen
+}
+
+// byteToUInt32 将 byte 转换为uint32
+func byteToUInt32(data []byte) uint32 {
+ i := uint32(data[0]) & 0xff
+ i |= (uint32(data[1]) << 8) & 0xff00
+ i |= (uint32(data[2]) << 16) & 0xff0000
+ return i
+}
+
+// readData 从文件中读取数据
+func (q *QQwry) readData(num int, offset ...int64) (rs []byte) {
+ if len(offset) > 0 {
+ q.setOffset(offset[0])
+ }
+ nums := int64(num)
+ end := q.offset + nums
+ dataNum := int64(len(q.data.Data))
+ if q.offset > dataNum {
+ return nil
+ }
+
+ if end > dataNum {
+ end = dataNum
+ }
+ rs = q.data.Data[q.offset:end]
+ q.offset = end
+ return
+}
diff --git a/pkg/qqwry/qqwry_test.go b/pkg/qqwry/qqwry_test.go
new file mode 100644
index 0000000..beea95a
--- /dev/null
+++ b/pkg/qqwry/qqwry_test.go
@@ -0,0 +1,13 @@
+package qqwry
+
+import (
+ "fmt"
+ "testing"
+)
+
+func TestNewQQwry(t *testing.T) {
+ fmt.Println("看看中文")
+ qqwry := NewQQwry("../../db/qqwry.dat")
+ fmt.Println(qqwry.Find("8.8.8.8"))
+ fmt.Println("我是中文")
+}
diff --git a/pkg/qqwry/update.go b/pkg/qqwry/update.go
new file mode 100644
index 0000000..c39a20e
--- /dev/null
+++ b/pkg/qqwry/update.go
@@ -0,0 +1,57 @@
+package qqwry
+
+import (
+ "bytes"
+"compress/zlib"
+"encoding/binary"
+"io/ioutil"
+"net/http"
+)
+
+// @ref https://zhangzifan.com/update-qqwry-dat.html
+
+func getKey() (uint32, error) {
+ resp, err := http.Get("http://update.cz88.net/ip/copywrite.rar")
+ if err != nil {
+ return 0, err
+ }
+ defer resp.Body.Close()
+
+ if body, err := ioutil.ReadAll(resp.Body); err != nil {
+ return 0, err
+ } else {
+ // @see https://stackoverflow.com/questions/34078427/how-to-read-packed-binary-data-in-go
+ return binary.LittleEndian.Uint32(body[5*4:]), nil
+ }
+}
+
+func GetOnline() ([]byte, error) {
+ resp, err := http.Get("http://update.cz88.net/ip/qqwry.rar")
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ if body, err := ioutil.ReadAll(resp.Body); err != nil {
+ return nil, err
+ } else {
+ if key, err := getKey(); err != nil {
+ return nil, err
+ } else {
+ for i := 0; i < 0x200; i++ {
+ key = key * 0x805
+ key++
+ key = key & 0xff
+
+ body[i] = byte(uint32(body[i]) ^ key)
+ }
+
+ reader, err := zlib.NewReader(bytes.NewReader(body))
+ if err != nil {
+ return nil, err
+ }
+
+ return ioutil.ReadAll(reader)
+ }
+ }
+}