diff --git a/cmd/root.go b/cmd/root.go index 52db91a..8349996 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) } } diff --git a/constant/path.go b/constant/path.go new file mode 100644 index 0000000..6204f87 --- /dev/null +++ b/constant/path.go @@ -0,0 +1,5 @@ +package constant + +var ( + HomePath string +) diff --git a/go.mod b/go.mod index eb95aef..3423717 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/internal/app/parse.go b/internal/app/parse.go index 2effb47..50903d6 100644 --- a/internal/app/parse.go +++ b/internal/app/parse.go @@ -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 } diff --git a/main.go b/main.go index bc656a0..d692626 100644 --- a/main.go +++ b/main.go @@ -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") + } + } +} diff --git a/pkg/qqwry/update.go b/pkg/qqwry/update.go index c39a20e..6d77812 100644 --- a/pkg/qqwry/update.go +++ b/pkg/qqwry/update.go @@ -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 }