move db to ~/.nali

This commit is contained in:
zu1k 2020-07-17 09:41:09 +08:00
parent de8496a281
commit b71b39f1f0
6 changed files with 48 additions and 35 deletions

View File

@ -2,6 +2,8 @@ package cmd
import (
"fmt"
"log"
"os"
"github.com/zu1k/nali/internal/app"
@ -27,7 +29,8 @@ var rootCmd = &cobra.Command{
// 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)
log.Fatal(err.Error())
os.Exit(1)
}
}

5
constant/path.go Normal file
View File

@ -0,0 +1,5 @@
package constant
var (
HomePath string
)

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/zu1k/nali
go 1.14
require (
github.com/mitchellh/go-homedir v1.1.0
github.com/oschwald/geoip2-golang v1.4.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 // indirect

View File

@ -2,6 +2,9 @@ package app
import (
"fmt"
"path/filepath"
"github.com/zu1k/nali/constant"
"github.com/zu1k/nali/internal/ipdb"
@ -15,9 +18,9 @@ var (
geoip geoip2.GeoIP
)
func init() {
qqip = qqwry.NewQQwry("db/qqwry.dat")
geoip = geoip2.NewGeoIP("db/GeoLite2-City.mmdb")
func InitIPDB() {
qqip = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat"))
//geoip = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb"))
db = qqip
}

48
main.go
View File

@ -1,28 +1,32 @@
/*
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"
import (
"log"
"os"
"path/filepath"
"github.com/zu1k/nali/internal/app"
"github.com/zu1k/nali/cmd"
"github.com/zu1k/nali/constant"
)
func main() {
setHomePath()
app.InitIPDB()
cmd.Execute()
}
func setHomePath() {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
homePath := filepath.Join(homeDir, ".nali")
constant.HomePath = homePath
if _, err := os.Stat(homePath); os.IsNotExist(err) {
if err := os.MkdirAll(homePath, 0777); err != nil {
log.Fatal("can not create", homePath, ", use bin dir instead")
}
}
}

View File

@ -2,16 +2,14 @@ package qqwry
import (
"bytes"
"compress/zlib"
"encoding/binary"
"io/ioutil"
"net/http"
"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")
resp, err := http.Get("https://qqwry.mirror.noc.one/copywrite.rar")
if err != nil {
return 0, err
}
@ -20,13 +18,12 @@ func getKey() (uint32, error) {
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")
resp, err := http.Get("https://qqwry.mirror.noc.one/qqwry.rar")
if err != nil {
return nil, err
}