Skip to content

Commit

Permalink
Feat: add merge command
Browse files Browse the repository at this point in the history
merge command can merge plaintext IP & CIDR from standard input, then print the merged result to standard output

usage example:
curl https://example.com/path/to/your/plaintext/cidr.txt | geoip merge

cat ./path/to/your/plaintext/cidr.txt | geoip merge
  • Loading branch information
Loyalsoldier committed Jul 8, 2024
1 parent 18e9f1a commit 130d27a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Available Commands:
convert Convert geoip data from one format to another by using config file
help Help about any command
list List all available input and output formats
merge Merge plaintext IP & CIDR from standard input, then print to standard output
Flags:
-h, --help help for geoip
Expand Down Expand Up @@ -271,6 +272,17 @@ All available output formats:
- v2rayGeoIPDat (Convert data to V2Ray GeoIP dat format)
```

```bash
$ curl -s https://core.telegram.org/resources/cidr.txt | ./geoip merge -t ipv4
91.105.192.0/23
91.108.4.0/22
91.108.8.0/21
91.108.16.0/21
91.108.56.0/22
149.154.160.0/20
185.76.151.0/24
```

```bash
$ ./geoip convert -c config.json
2021/08/29 12:11:35 ✅ [v2rayGeoIPDat] geoip.dat --> output/dat
Expand Down
16 changes: 16 additions & 0 deletions lib/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ func (i *Instance) Init(configFile string) error {
return nil
}

func (i *Instance) InitFromBytes(content []byte) error {
if err := json.Unmarshal(content, &i.config); err != nil {
return err
}

for _, input := range i.config.Input {
i.input = append(i.input, input.converter)
}

for _, output := range i.config.Output {
i.output = append(i.output, output.converter)
}

return nil
}

func (i *Instance) Run() error {
if len(i.input) == 0 || len(i.output) == 0 {
return errors.New("input type and output type must be specified")
Expand Down
117 changes: 117 additions & 0 deletions merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"log"
"strings"

"github.com/Loyalsoldier/geoip/lib"
"github.com/spf13/cobra"
)

const tempConfig = `
{
"input": [
{
"type": "stdin",
"action": "add",
"args": {
"name": "temp"
}
}
],
"output": [
{
"type": "stdout",
"action": "output"
}
]
}
`

const tempConfigWithIPv4 = `
{
"input": [
{
"type": "stdin",
"action": "add",
"args": {
"name": "temp"
}
}
],
"output": [
{
"type": "stdout",
"action": "output",
"args": {
"onlyIPType": "ipv4"
}
}
]
}
`

const tempConfigWithIPv6 = `
{
"input": [
{
"type": "stdin",
"action": "add",
"args": {
"name": "temp"
}
}
],
"output": [
{
"type": "stdout",
"action": "output",
"args": {
"onlyIPType": "ipv6"
}
}
]
}
`

func init() {
rootCmd.AddCommand(mergeCmd)
mergeCmd.PersistentFlags().StringP("onlyiptype", "t", "", "The only IP type to output, available options: \"ipv4\", \"ipv6\"")
}

var mergeCmd = &cobra.Command{
Use: "merge",
Aliases: []string{"m"},
Short: "Merge plaintext IP & CIDR from standard input, then print to standard output",
Run: func(cmd *cobra.Command, args []string) {
otype, _ := cmd.Flags().GetString("onlyiptype")
otype = strings.ToLower(strings.TrimSpace(otype))

if otype != "" && otype != "ipv4" && otype != "ipv6" {
log.Fatal("invalid argument onlyiptype: ", otype)
}

var configBytes []byte
switch lib.IPType(otype) {
case lib.IPv4:
configBytes = []byte(tempConfigWithIPv4)
case lib.IPv6:
configBytes = []byte(tempConfigWithIPv6)
default:
configBytes = []byte(tempConfig)
}

instance, err := lib.NewInstance()
if err != nil {
log.Fatal(err)
}

if err := instance.InitFromBytes(configBytes); err != nil {
log.Fatal(err)
}

if err := instance.Run(); err != nil {
log.Fatal(err)
}
},
}

0 comments on commit 130d27a

Please sign in to comment.